From 2a681ef8e6f01c1c38c144461fd56f3180c209f8 Mon Sep 17 00:00:00 2001 From: James Seibel Date: Thu, 5 Feb 2026 07:40:11 -0600 Subject: [PATCH] Pool ZStd decompress contexts --- .../dataStreams/PooledZstdDecompressor.java | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/core/src/main/java/com/seibel/distanthorizons/core/util/objects/dataStreams/PooledZstdDecompressor.java b/core/src/main/java/com/seibel/distanthorizons/core/util/objects/dataStreams/PooledZstdDecompressor.java index 2a8e5a791..c0c42797f 100644 --- a/core/src/main/java/com/seibel/distanthorizons/core/util/objects/dataStreams/PooledZstdDecompressor.java +++ b/core/src/main/java/com/seibel/distanthorizons/core/util/objects/dataStreams/PooledZstdDecompressor.java @@ -6,8 +6,15 @@ import com.github.luben.zstd.ZstdException; import com.seibel.distanthorizons.core.util.objects.pooling.PhantomArrayListCheckout; import it.unimi.dsi.fastutil.bytes.ByteArrayList; +import java.util.concurrent.ConcurrentLinkedQueue; + public class PooledZstdDecompressor { + /** reuse the ZStd native context for a minor speed improvement */ + private static final ConcurrentLinkedQueue ZSTD_CONTEXT_CACHE = new ConcurrentLinkedQueue<>(); + + + /** * Replaces {@link Zstd#decompress} so we can use a pooled byte array * which significantly reduces GC pressure. @@ -37,18 +44,22 @@ public class PooledZstdDecompressor throw new ZstdException(Zstd.errGeneric(), "Original size should not be negative"); } - int size; - try(ZstdDecompressCtx ctx = new ZstdDecompressCtx()) + // reuse the ZStd native context for a minor speed improvement + ZstdDecompressCtx ctx = ZSTD_CONTEXT_CACHE.poll(); + if (ctx == null) { - size = ctx.decompressByteArray(destination.elements(), 0, destination.size(), src, 0, srcSize); + ctx = new ZstdDecompressCtx(); } + int size = ctx.decompressByteArray(destination.elements(), 0, destination.size(), src, 0, srcSize); if (size != originalSize) { //return Arrays.copyOfRange(destination.elements(), 0, size); destination.size(size); // this assumes the size will only be smaller than the expected } + ZSTD_CONTEXT_CACHE.add(ctx); + return destination; }