Skip to content

Commit 45f510d

Browse files
Override writeChar and readChar methods to use unsafe: writeChar now performs about 125% faster and readChar 10% faster than overriden safe versions.
1 parent 2aaeb94 commit 45f510d

File tree

4 files changed

+28
-8
lines changed

4 files changed

+28
-8
lines changed

src/com/esotericsoftware/kryo/io/UnsafeInput.java

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
* <p>
1717
* Important notes:<br/>
1818
* <li>Bulk operations, e.g. on arrays of primitive types, are always using native byte order.</li>
19-
* <li>Fixed-size int, long, short, float and double elements are always read using native byte order.</li>
19+
* <li>Fixed-size char, int, long, short, float and double elements are always read using native byte order.</li>
2020
* <li>Best performance is achieved if no variable length encoding for integers is used.</li>
2121
* <li>Serialized representation used as input for this class should always be produced using @link{UnsafeOutput}</li>
2222
* </p>
@@ -106,6 +106,16 @@ public double readDouble () throws KryoException {
106106
position += 8;
107107
return result;
108108
}
109+
110+
// char
111+
112+
public char readChar () throws KryoException {
113+
require(2);
114+
char result = unsafe().getChar(buffer, byteArrayBaseOffset + position);
115+
position += 2;
116+
return result;
117+
}
118+
109119

110120
public int readInt (boolean optimizePositive) throws KryoException {
111121
if (!varIntsEnabled)

src/com/esotericsoftware/kryo/io/UnsafeMemoryInput.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
* <p>
1818
* Important notes:<br/>
1919
* <li>Bulk operations, e.g. on arrays of primitive types, are always using native byte order.</li>
20-
* <li>Fixed-size int, long, short, float and double elements are always read using native byte order.</li>
20+
* <li>Fixed-size char, int, long, short, float and double elements are always read using native byte order.</li>
2121
* <li>Best performance is achieved if no variable length encoding for integers is used.</li>
2222
* <li>Serialized representation used as input for this class should always be produced using @link{UnsafeMemoryOutput}</li>
2323
* </p>
@@ -127,8 +127,10 @@ public byte readByte () throws KryoException {
127127

128128
/** Reads a 2 byte char. */
129129
public char readChar () throws KryoException {
130-
super.niobuffer.position(position);
131-
return super.readChar();
130+
require(2);
131+
char result = unsafe().getChar(bufaddress + position);
132+
position += 2;
133+
return result;
132134
}
133135

134136
/** Reads an 8 byte double. */

src/com/esotericsoftware/kryo/io/UnsafeMemoryOutput.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
* Important notes:<br/>
2222
* <li>This class increases performance, but may result in bigger size of serialized representation.</li>
2323
* <li>Bulk operations, e.g. on arrays of primitive types, are always using native byte order.</li>
24-
* <li>Fixed-size int, long, short, float and double elements are always written using native byte order.</li>
24+
* <li>Fixed-size char, int, long, short, float and double elements are always written using native byte order.</li>
2525
* <li>Best performance is achieved if no variable length encoding for integers is used.</li>
2626
* <li>Output serialized using this class should always be deserilized using @link{UnsafeMemoryInput}</li>
2727
*
@@ -132,8 +132,9 @@ final public void writeBoolean (boolean value) throws KryoException {
132132

133133
/** Writes a 2 byte char. */
134134
final public void writeChar (char value) throws KryoException {
135-
super.niobuffer.position(position);
136-
super.writeChar(value);
135+
require(2);
136+
unsafe().putChar(bufaddress + position, value);
137+
position += 2;
137138
}
138139

139140
/** Writes an 8 byte double. */

src/com/esotericsoftware/kryo/io/UnsafeOutput.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
* Important notes:<br/>
2020
* <li>This class increases performance, but may result in bigger size of serialized representation.</li>
2121
* <li>Bulk operations, e.g. on arrays of primitive types, are always using native byte order.</li>
22-
* <li>Fixed-size int, long, short, float and double elements are always written using native byte order.</li>
22+
* <li>Fixed-size char, int, long, short, float and double elements are always written using native byte order.</li>
2323
* <li>Best performance is achieved if no variable length encoding for integers is used.</li>
2424
* <li>Output serialized using this class should always be deserilized using @link{UnsafeInput}</li>
2525
*
@@ -124,6 +124,13 @@ final public void writeDouble (double value) throws KryoException {
124124
position += 8;
125125
}
126126

127+
/** Writes a 2 byte char. */
128+
final public void writeChar (char value) throws KryoException {
129+
require(2);
130+
unsafe().putChar(buffer, byteArrayBaseOffset + position, value);
131+
position += 2;
132+
}
133+
127134
final public int writeInt (int value, boolean optimizePositive) throws KryoException {
128135
if (!supportVarInts) {
129136
writeInt(value);

0 commit comments

Comments
 (0)