feat: context recycling (reset) + dictionaries on contexts (load/ref)#26
Merged
Conversation
Bind ZSTD_CCtx_reset and ZSTD_DCtx_reset behind a new ZstdResetDirective enum (SESSION_ONLY, PARAMETERS, SESSION_AND_PARAMETERS), exposed as ZstdCompressCtx.reset(...) / ZstdDecompressCtx.reset(...). Lets a pooled or long-lived context recycle its native state between frames without freeing and recreating it. A parameter reset clears the context back to defaults, so the compress context drops its cached level back to Zstd.defaultCompressionLevel() to stay in sync with the native state. Document the recipe in docs/how-to.md, flip both symbols to bound in docs/supported.md (advanced-parameter count 8 -> 10, total 55 -> 57), and add the 0.5 changelog entry. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Bind ZSTD_CCtx/DCtx_loadDictionary on the one-shot contexts (previously only on streams) and the new ZSTD_CCtx_refCDict / ZSTD_DCtx_refDDict. A sticky dictionary on a context lets compression combine a dictionary with the advanced parameters (checksum, window log, long-distance matching) via the compress2 path — impossible through the per-call compress(src, dict) overloads, which route the legacy dictionary path. refDictionary attaches a pre-digested CDict/DDict by reference (no copy, no per-call digest), the pooled-context hot path that pairs with reset. A parameter reset clears either. loadDictionary takes a ZstdDictionary or a native MemorySegment (zero-copy); refDictionary borrows the digest, so the caller keeps it alive. Tests cover dict+checksum combine, ref-across-session-reset, reset/null clearing, segment load, and zstd-jni interop on both frames. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
dfa1
added a commit
that referenced
this pull request
Jun 27, 2026
…elog Relocate docs/adr -> adr/ (top-level), rewriting the relative links for the new depth. Link the ADR index from the README Documentation section. Add the missing commit hash (3dfd5b8, #26) to the 0.5 "Added" changelog entries so every line cites its commit. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Two related steps toward closing the dictionary story on the one-shot contexts.
1. Context reset —
ZSTD_CCtx_reset/ZSTD_DCtx_resetZstdCompressCtx.reset(ZstdResetDirective)/ZstdDecompressCtx.reset(...)recycle a context's native state between frames without freeing and recreating it.SESSION_ONLY— abort the current frame, drop unflushed data; keep level, parameters, dictionary.PARAMETERS/SESSION_AND_PARAMETERS— also restore defaults and clear the dictionary; valid only between frames.A parameter reset returns native state to defaults, so the compress context drops its cached level back to
Zstd.defaultCompressionLevel()to stay in sync.2. Dictionaries on contexts — load / ref
The per-call
compress(src, dict)overloads take the legacy dictionary path, which ignores the advanced parameters (checksum, window log, long-distance matching). To combine the two you need a sticky dictionary on the context:loadDictionary(ZstdDictionary)/loadDictionary(MemorySegment)on both contexts — bindsZSTD_CCtx/DCtx_loadDictionary(previously wired only to the streams). The nativeMemorySegmentoverload is the zero-copy path.refDictionary(ZstdCompressDict)/refDictionary(ZstdDecompressDict)— newZSTD_CCtx_refCDict/ZSTD_DCtx_refDDict. Attaches a pre-digested dictionary by reference: no copy, no per-call digest. Borrowed — the caller keeps the digest alive. The pooled-context hot path that pairs withreset.A sticky dictionary now lets the normal
compresspath honour dictionary + advanced parameters together. A parameterreset, or passingnull, clears it.Tests
ZstdParameterTest$Reset(6): session-only keeps level, parameter reset restores default level (bothPARAMETERSandSESSION_AND_PARAMETERS), dictionary round-trips after reset, decompress ctx decodes after reset, null directive → NPE.ZstdDictionaryTest$StickyDictionary(6): dict+checksum combine, referenced digest survives session reset, parameter reset clears the loaded dict, null clears it, native-segment load round-trips, heap segment rejected.ZstdJniInteropTest$Dictionary(+2): loaded-dict-with-checksum frame and referenced-digest frame both decode in zstd-jni.verifygreen: 81 integration tests, 0 Checkstyle violations, javadoc clean.Docs
docs/supported.md—C/DCtx_reset,CCtx_refCDict,DCtx_refDDictflipped to bound; advanced-parameter count 8 → 12, total 55 → 59;loadDictionarymapping now lists the contexts.docs/how-to.md— "Reset a context to recycle it" and "Compress with a dictionary and advanced parameters" recipes.CHANGELOG.md—[0.5]Added entries.🤖 Generated with Claude Code