Skip to content

Commit

Permalink
optimize unnecessary require operation in write_ascii_slow
Browse files Browse the repository at this point in the history
  • Loading branch information
pyb1993 committed Jun 14, 2021
1 parent 1d72a60 commit 836ab60
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 9 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/* 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);
}

}
￿
2 changes: 1 addition & 1 deletion src/com/esotericsoftware/kryo/io/ByteBufferOutput.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
Expand Down
22 changes: 14 additions & 8 deletions src/com/esotericsoftware/kryo/io/Output.java
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -637,7 +643,7 @@ public void writeBoolean (boolean value) throws KryoException {
}

// String:

/** Writes the length and string, or null. Short strings are checked and if ASCII they are written more efficiently, else they
* are written as UTF8. If a string is known to be ASCII, {@link #writeAscii(String)} may be used. The string can be read using
* {@link Input#readString()} or {@link Input#readStringBuilder()}.
Expand All @@ -657,6 +663,8 @@ public void writeString (String value) throws KryoException {
if (charCount > 1 && charCount <= 32) {
for (int i = 0; i < charCount; i++)
if (value.charAt(i) > 127) break outer;

// todo can combine
if (capacity - position < charCount)
writeAscii_slow(value, charCount);
else {
Expand Down Expand Up @@ -686,7 +694,7 @@ public void writeString (String value) throws KryoException {
}
if (charIndex < charCount) writeUtf8_slow(value, charCount, charIndex);
}

/** Writes a string that is known to contain only ASCII characters. Non-ASCII strings passed to this method will be corrupted.
* Each byte is a 7 bit character with the remaining byte denoting if another character is available. This is slightly more
* efficient than {@link #writeString(String)}. The string can be read using {@link Input#readString()} or
Expand Down Expand Up @@ -735,7 +743,7 @@ else if (c > 0x07FF) {
}
}
}

private void writeAscii_slow (String value, int charCount) throws KryoException {
if (charCount == 0) return;
if (position == capacity) require(1); // Must be able to write at least one character.
Expand All @@ -746,13 +754,11 @@ 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;
}
}

// Primitive arrays:


/** Writes an int array in bulk. This may be more efficient than writing them individually. */
public void writeInts (int[] array, int offset, int count) throws KryoException {
if (capacity >= count << 2) {
Expand Down

0 comments on commit 836ab60

Please sign in to comment.