File tree Expand file tree Collapse file tree 1 file changed +6
-4
lines changed
msgpack-core/src/main/java/org/msgpack/core Expand file tree Collapse file tree 1 file changed +6
-4
lines changed Original file line number Diff line number Diff 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
You can’t perform that action at this time.
0 commit comments