Skip to content

Commit

Permalink
[msgpack#691] Improve memory usage via reuse InputStreamBufferInput
Browse files Browse the repository at this point in the history
  • Loading branch information
koo-taejin committed Nov 16, 2022
1 parent f1ac3dd commit 7e59e49
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,6 @@ public InputStreamBufferInput(InputStream in, int bufferSize)
* @return the old resource
*/
public InputStream reset(InputStream in)
throws IOException
{
InputStream old = this.in;
this.in = in;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ public class MessagePackFactory
private final MessagePack.PackerConfig packerConfig;
private boolean reuseResourceInGenerator = true;
private boolean reuseResourceInParser = true;
private boolean reuseInputStreamBufferInput = true;

private ExtensionTypeCustomDeserializers extTypeCustomDesers;

public MessagePackFactory()
Expand All @@ -58,6 +60,7 @@ public MessagePackFactory(MessagePackFactory src)
this.packerConfig = src.packerConfig.clone();
this.reuseResourceInGenerator = src.reuseResourceInGenerator;
this.reuseResourceInParser = src.reuseResourceInParser;
this.reuseInputStreamBufferInput = src.reuseInputStreamBufferInput;
if (src.extTypeCustomDesers != null) {
this.extTypeCustomDesers = new ExtensionTypeCustomDeserializers(src.extTypeCustomDesers);
}
Expand All @@ -75,6 +78,12 @@ public MessagePackFactory setReuseResourceInParser(boolean reuseResourceInParser
return this;
}

public MessagePackFactory setReuseInputStreamBufferInput(boolean reuseInputStreamBufferInput)
{
this.reuseInputStreamBufferInput = reuseInputStreamBufferInput;
return this;
}

public MessagePackFactory setExtTypeCustomDesers(ExtensionTypeCustomDeserializers extTypeCustomDesers)
{
this.extTypeCustomDesers = extTypeCustomDesers;
Expand Down Expand Up @@ -167,6 +176,12 @@ boolean isReuseResourceInParser()
return reuseResourceInParser;
}

@VisibleForTesting
boolean isReuseInputStreamBufferInput()
{
return reuseInputStreamBufferInput;
}

@VisibleForTesting
ExtensionTypeCustomDeserializers getExtTypeCustomDesers()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,10 @@ public class MessagePackParser
{
private static final ThreadLocal<Tuple<Object, MessageUnpacker>> messageUnpackerHolder =
new ThreadLocal<Tuple<Object, MessageUnpacker>>();

private static final ThreadLocal<InputStreamBufferInput> inputStreamBufferInputHolder =
new ThreadLocal<>();

private final MessageUnpacker messageUnpacker;

private static final BigInteger LONG_MIN = BigInteger.valueOf((long) Long.MIN_VALUE);
Expand Down Expand Up @@ -130,7 +134,36 @@ public MessagePackParser(
boolean reuseResourceInParser)
throws IOException
{
this(ctxt, features, new InputStreamBufferInput(in), objectCodec, in, reuseResourceInParser);
this(ctxt, features, objectCodec, in, reuseResourceInParser, true);
}

public MessagePackParser(
IOContext ctxt,
int features,
ObjectCodec objectCodec,
InputStream in,
boolean reuseResourceInParser,
boolean reuseInputStreamBufferInput)
throws IOException
{
this(ctxt, features, getInputStreamBufferInput(in, reuseInputStreamBufferInput), objectCodec, in, reuseResourceInParser);
}

private static InputStreamBufferInput getInputStreamBufferInput(InputStream in, boolean reuseInputStreamBufferInput)
{
if (reuseInputStreamBufferInput) {
final InputStreamBufferInput inputStreamBufferInput = inputStreamBufferInputHolder.get();
if (inputStreamBufferInput != null) {
inputStreamBufferInput.reset(in);
return inputStreamBufferInput;
}
final InputStreamBufferInput newInputStreamBufferInput = new InputStreamBufferInput(in);
inputStreamBufferInputHolder.set(newInputStreamBufferInput);
return newInputStreamBufferInput;
}
else {
return new InputStreamBufferInput(in);
}
}

public MessagePackParser(IOContext ctxt, int features, ObjectCodec objectCodec, byte[] bytes)
Expand Down

0 comments on commit 7e59e49

Please sign in to comment.