Skip to content

Commit 031ead3

Browse files
committed
Rename to PackStringWithGetBytes and add comments
1 parent f7d57e3 commit 031ead3

File tree

1 file changed

+6
-4
lines changed

1 file changed

+6
-4
lines changed

msgpack-core/src/main/java/org/msgpack/core/MessagePacker.java

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -428,10 +428,12 @@ public MessagePacker packDouble(double v)
428428
return this;
429429
}
430430

431-
private void packStringByGetBytes(String s)
431+
private void packStringWithGetBytes(String s)
432432
throws IOException
433433
{
434+
// JVM performs various optimizations (memory allocation, reusing encoder etc.) when String.getBytes is used
434435
byte[] bytes = s.getBytes(MessagePack.UTF8);
436+
// Write the length and payload of small string to the buffer so that it avoids an extra flush of buffer
435437
packRawStringHeader(bytes.length);
436438
addPayload(bytes);
437439
}
@@ -481,8 +483,8 @@ public MessagePacker packString(String s)
481483
return this;
482484
}
483485
else if (s.length() < smallStringOptimizationThreshold) {
484-
// Write the length and payload of small string to the buffer so that it avoids an extra flush of buffer
485-
packStringByGetBytes(s);
486+
// Using String.getBytes is generally faster for small strings
487+
packStringWithGetBytes(s);
486488
return this;
487489
}
488490
else if (s.length() < (1 << 8)) {
@@ -549,7 +551,7 @@ else if (s.length() < (1 << 16)) {
549551
// 384KB, which is OK size to keep in memory.
550552

551553
// fallback
552-
packStringByGetBytes(s);
554+
packStringWithGetBytes(s);
553555
return this;
554556
}
555557

0 commit comments

Comments
 (0)