Skip to content

Commit a839b88

Browse files
committed
Merge v07-develop
2 parents 4f5b594 + 5e989d9 commit a839b88

File tree

3 files changed

+55
-0
lines changed

3 files changed

+55
-0
lines changed

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

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,11 @@ public static class Config
6666
public final int stringDecoderBufferSize;
6767
public final int packerBufferSize;
6868
public final int packerRawDataCopyingThreshold;
69+
/**
70+
* Use String.getBytes() for strings smaller than this threshold.
71+
* Note that this parameter is subject to change.
72+
*/
73+
public final int packerSmallStringOptimizationThreshold;
6974

7075
public Config(
7176
boolean readStringAsBinary,
@@ -76,6 +81,7 @@ public Config(
7681
int stringEncoderBufferSize,
7782
int stringDecoderBufferSize,
7883
int packerBufferSize,
84+
int packerSmallStringOptimizationThreshold,
7985
int packerRawDataCopyingThreshold)
8086
{
8187
checkArgument(packerBufferSize > 0, "packer buffer size must be larger than 0: " + packerBufferSize);
@@ -90,6 +96,7 @@ public Config(
9096
this.stringEncoderBufferSize = stringEncoderBufferSize;
9197
this.stringDecoderBufferSize = stringDecoderBufferSize;
9298
this.packerBufferSize = packerBufferSize;
99+
this.packerSmallStringOptimizationThreshold = packerSmallStringOptimizationThreshold;
93100
this.packerRawDataCopyingThreshold = packerRawDataCopyingThreshold;
94101
}
95102
}
@@ -109,6 +116,7 @@ public static class ConfigBuilder
109116
private int stringEncoderBufferSize = 8192;
110117
private int stringDecoderBufferSize = 8192;
111118
private int packerBufferSize = 8192;
119+
private int packerSmallStringOptimizationThreshold = 512; // This parameter is subject to change
112120
private int packerRawDataCopyingThreshold = 512;
113121

114122
public Config build()
@@ -122,6 +130,7 @@ public Config build()
122130
stringEncoderBufferSize,
123131
stringDecoderBufferSize,
124132
packerBufferSize,
133+
packerSmallStringOptimizationThreshold,
125134
packerRawDataCopyingThreshold
126135
);
127136
}
@@ -174,6 +183,12 @@ public ConfigBuilder packerBufferSize(int size)
174183
return this;
175184
}
176185

186+
public ConfigBuilder packerSmallStringOptimizationThreshold(int threshold)
187+
{
188+
this.packerSmallStringOptimizationThreshold = threshold;
189+
return this;
190+
}
191+
177192
public ConfigBuilder packerRawDataCopyingThreshold(int threshold)
178193
{
179194
this.packerRawDataCopyingThreshold = threshold;

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

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -442,6 +442,14 @@ public MessagePacker packDouble(double v)
442442
return this;
443443
}
444444

445+
private void packSmallString(String s)
446+
throws IOException
447+
{
448+
byte[] bytes = s.getBytes(MessagePack.UTF8);
449+
packRawStringHeader(bytes.length);
450+
writePayload(bytes);
451+
}
452+
445453
/**
446454
* Pack the input String in UTF-8 encoding
447455
*
@@ -457,6 +465,12 @@ public MessagePacker packString(String s)
457465
return this;
458466
}
459467

468+
if (s.length() < config.packerSmallStringOptimizationThreshold) {
469+
// Write the length and payload of small string to the buffer so that it avoids an extra flush of buffer
470+
packSmallString(s);
471+
return this;
472+
}
473+
460474
CharBuffer in = CharBuffer.wrap(s);
461475
prepareEncoder();
462476

msgpack-core/src/test/scala/org/msgpack/core/MessagePackerTest.scala

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -235,6 +235,32 @@ class MessagePackerTest
235235
up1.hasNext shouldBe false
236236
up1.close
237237
}
238+
239+
"pack a lot of String within expected time" in {
240+
val count = 20000
241+
242+
def measureDuration(outputStream: java.io.OutputStream) = {
243+
val packer = MessagePack.newDefaultPacker(outputStream)
244+
var i = 0
245+
while (i < count) {
246+
packer.packString("0123456789ABCDEF")
247+
i += 1
248+
}
249+
packer.close
250+
}
251+
252+
val t = time("packString into OutputStream", repeat = 10) {
253+
block("byte-array-output-stream") {
254+
measureDuration(new ByteArrayOutputStream())
255+
}
256+
257+
block("file-output-stream") {
258+
val (_, fileOutput) = createTempFileWithOutputStream
259+
measureDuration(fileOutput)
260+
}
261+
}
262+
t("file-output-stream").averageWithoutMinMax shouldBe < (t("byte-array-output-stream").averageWithoutMinMax * 4)
263+
}
238264
}
239265

240266
"compute totalWrittenBytes" in {

0 commit comments

Comments
 (0)