Skip to content

Commit

Permalink
Reduce byte[] allocations by reusing buffers
Browse files Browse the repository at this point in the history
Reduces byte[] allocations from 280MB to <4MB when reading a 133MB base64 stream. Messured with JFR.
  • Loading branch information
apinske committed May 10, 2021
1 parent a7c5af9 commit 6bd8d86
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 2 deletions.
Expand Up @@ -75,6 +75,7 @@ static class Context {
* Buffer for streaming.
*/
byte[] buffer;
byte[] bufferCache;

/**
* Position where next character should be written in the buffer.
Expand Down Expand Up @@ -569,7 +570,11 @@ public String encodeToString(final byte[] pArray) {
*/
protected byte[] ensureBufferSize(final int size, final Context context){
if (context.buffer == null) {
context.buffer = new byte[Math.max(size, getDefaultBufferSize())];
int bufferSize = Math.max(size, getDefaultBufferSize());
if (context.bufferCache == null || context.bufferCache.length != bufferSize) {
context.bufferCache = new byte[bufferSize];
}
context.buffer = context.bufferCache;
context.pos = 0;
context.readPos = 0;

Expand Down
Expand Up @@ -39,12 +39,15 @@ public class BaseNCodecInputStream extends FilterInputStream {

private final byte[] singleByte = new byte[1];

private final byte[] buf;

private final Context context = new Context();

protected BaseNCodecInputStream(final InputStream input, final BaseNCodec baseNCodec, final boolean doEncode) {
super(input);
this.doEncode = doEncode;
this.baseNCodec = baseNCodec;
this.buf = new byte[doEncode ? 4096 : 8192];
}

/**
Expand Down Expand Up @@ -171,7 +174,6 @@ public int read(final byte array[], final int offset, final int len) throws IOEx
*/
while (readLen == 0) {
if (!baseNCodec.hasData(context)) {
final byte[] buf = new byte[doEncode ? 4096 : 8192];
final int c = in.read(buf);
if (doEncode) {
baseNCodec.encode(buf, 0, c, context);
Expand Down

0 comments on commit 6bd8d86

Please sign in to comment.