Skip to content

Commit

Permalink
1. reduce memory copy in zstd compression
Browse files Browse the repository at this point in the history
  • Loading branch information
luyuncheng committed May 23, 2023
1 parent 63834d9 commit a1b7871
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ private void doCompress(byte[] bytes, int offset, int length, ZstdCompressCtx cc
return;
}
final int maxCompressedLength = (int) Zstd.compressBound(length);
compressedBuffer = ArrayUtil.grow(compressedBuffer, maxCompressedLength);
compressedBuffer = ArrayUtil.growNoCopy(compressedBuffer, maxCompressedLength);

int compressedSize = cctx.compressByteArray(compressedBuffer, 0, compressedBuffer.length, bytes, offset, length);

Expand Down Expand Up @@ -139,7 +139,7 @@ private void doDecompress(DataInput in, ZstdDecompressCtx dctx, BytesRef bytes,
return;
}

compressedBuffer = ArrayUtil.grow(compressedBuffer, compressedLength);
compressedBuffer = ArrayUtil.growNoCopy(compressedBuffer, compressedLength);
in.readBytes(compressedBuffer, 0, compressedLength);

bytes.bytes = ArrayUtil.grow(bytes.bytes, bytes.length + decompressedLen);
Expand All @@ -161,7 +161,7 @@ public void decompress(DataInput in, int originalLength, int offset, int length,
}
final int dictLength = in.readVInt();
final int blockLength = in.readVInt();
bytes.bytes = ArrayUtil.grow(bytes.bytes, dictLength);
bytes.bytes = ArrayUtil.growNoCopy(bytes.bytes, dictLength);
bytes.offset = bytes.length = 0;

try (ZstdDecompressCtx dctx = new ZstdDecompressCtx()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ private void compress(byte[] bytes, int offset, int length, DataOutput out) thro
}

final int maxCompressedLength = (int) Zstd.compressBound(l);
compressedBuffer = ArrayUtil.grow(compressedBuffer, maxCompressedLength);
compressedBuffer = ArrayUtil.growNoCopy(compressedBuffer, maxCompressedLength);

int compressedSize = (int) Zstd.compressByteArray(
compressedBuffer,
Expand Down Expand Up @@ -151,7 +151,7 @@ public void decompress(DataInput in, int originalLength, int offset, int length,
if (compressedLength == 0) {
return;
}
compressed = ArrayUtil.grow(compressed, compressedLength);
compressed = ArrayUtil.growNoCopy(compressed, compressedLength);
in.readBytes(compressed, 0, compressedLength);

int l = Math.min(blockLength, originalLength - offsetInBlock);
Expand Down

0 comments on commit a1b7871

Please sign in to comment.