diff --git a/benchmarks/src/main/java/com/esotericsoftware/kryo/benchmarks/io/HugeStringBenchmark.java b/benchmarks/src/main/java/com/esotericsoftware/kryo/benchmarks/io/HugeStringBenchmark.java new file mode 100644 index 000000000..bcf7aaf17 --- /dev/null +++ b/benchmarks/src/main/java/com/esotericsoftware/kryo/benchmarks/io/HugeStringBenchmark.java @@ -0,0 +1,50 @@ +/* Copyright (c) 2008-2020, Nathan Sweet + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following + * conditions are met: + * + * - Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. + * - Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following + * disclaimer in the documentation and/or other materials provided with the distribution. + * - Neither the name of Esoteric Software nor the names of its contributors may be used to endorse or promote products derived + * from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, + * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT + * SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ + +package com.esotericsoftware.kryo.benchmarks.io; + +import com.esotericsoftware.kryo.io.Output; +import org.openjdk.jmh.annotations.*; + + + + +/* + * this benchmark is used to test serialize huge string when not reuse the buffer + * */ +@BenchmarkMode(Mode.SingleShotTime) +@Measurement(batchSize = 120000) +public class HugeStringBenchmark { + static String hugeString = ""; + static { + for(int i = 0; i < 256; i++){ + hugeString += "abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz0123456789"; + } + } + + /** + * 1. not reuse the output + * 2. the initial bufferSize will be smaller than hugeString size + * ***/ + @Benchmark + public void writeAsciiHuge (InputOutputState state) { + Output output = new Output(1024, 1024 * 512); + output.writeAscii(hugeString); + } +} \ No newline at end of file diff --git a/src/com/esotericsoftware/kryo/io/ByteBufferOutput.java b/src/com/esotericsoftware/kryo/io/ByteBufferOutput.java index c3cb14769..d2efc4ba5 100644 --- a/src/com/esotericsoftware/kryo/io/ByteBufferOutput.java +++ b/src/com/esotericsoftware/kryo/io/ByteBufferOutput.java @@ -649,7 +649,7 @@ private void writeAscii_slow (String value, int charCount) throws KryoException buffer.put(tmp, 0, charsToWrite); charIndex += charsToWrite; position += charsToWrite; - charsToWrite = Math.min(charCount - charIndex, capacity); + charsToWrite = Math.min(charCount - charIndex, maxAvailableRequired()); if (require(charsToWrite)) buffer = this.byteBuffer; } } diff --git a/src/com/esotericsoftware/kryo/io/Output.java b/src/com/esotericsoftware/kryo/io/Output.java index 6a8aa1289..fc78f5cd8 100644 --- a/src/com/esotericsoftware/kryo/io/Output.java +++ b/src/com/esotericsoftware/kryo/io/Output.java @@ -173,7 +173,13 @@ public void reset () { position = 0; total = 0; } - + + /**how much space can be allocated when call require **/ + public int maxAvailableRequired(){ + if(outputStream != null){return maxCapacity;} + else{return maxCapacity - position;} + } + /** Ensures the buffer is large enough to read the specified number of bytes. * @return true if the buffer has been resized. */ protected boolean require (int required) throws KryoException { @@ -746,7 +752,7 @@ private void writeAscii_slow (String value, int charCount) throws KryoException value.getBytes(charIndex, charIndex + charsToWrite, buffer, position); charIndex += charsToWrite; position += charsToWrite; - charsToWrite = Math.min(charCount - charIndex, capacity); + charsToWrite = Math.min(charCount - charIndex, maxAvailableRequired()); if (require(charsToWrite)) buffer = this.buffer; } }