diff --git a/CHANGELOG.md b/CHANGELOG.md index 3dff210f43976..ee9027914fbd7 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -12,6 +12,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), ### Changed - Replace jboss-annotations-api_1.2_spec with jakarta.annotation-api ([#7836](https://github.com/opensearch-project/OpenSearch/pull/7836)) +- Reduce memory copy in zstd compression ([#7681](https://github.com/opensearch-project/OpenSearch/pull/7681)) ### Deprecated diff --git a/sandbox/plugins/custom-codecs/src/main/java/org/opensearch/index/codec/customcodecs/ZstdCompressionMode.java b/sandbox/plugins/custom-codecs/src/main/java/org/opensearch/index/codec/customcodecs/ZstdCompressionMode.java index ceba6ec731e5a..5b8f1ffcc9569 100644 --- a/sandbox/plugins/custom-codecs/src/main/java/org/opensearch/index/codec/customcodecs/ZstdCompressionMode.java +++ b/sandbox/plugins/custom-codecs/src/main/java/org/opensearch/index/codec/customcodecs/ZstdCompressionMode.java @@ -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); @@ -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); @@ -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()) { diff --git a/sandbox/plugins/custom-codecs/src/main/java/org/opensearch/index/codec/customcodecs/ZstdNoDictCompressionMode.java b/sandbox/plugins/custom-codecs/src/main/java/org/opensearch/index/codec/customcodecs/ZstdNoDictCompressionMode.java index f5d5019092cc9..6cfd85b053190 100644 --- a/sandbox/plugins/custom-codecs/src/main/java/org/opensearch/index/codec/customcodecs/ZstdNoDictCompressionMode.java +++ b/sandbox/plugins/custom-codecs/src/main/java/org/opensearch/index/codec/customcodecs/ZstdNoDictCompressionMode.java @@ -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, @@ -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);