Repository navigation
compress: respect ZSTD_c_forceMaxWindow with a copied CDict - #5068
Open
pylaterreur wants to merge 1 commit into
Open
pylaterreur wants to merge 1 commit into
pylaterreur wants to merge 1 commit into
Conversation
ZSTD_c_forceMaxWindow is documented to keep back-reference distances within the window, even into dictionary content. For a dictionary loaded into the context, ZSTD_loadDictionaryContent() sets loadedDictEnd to 0 for that purpose, so the window also bounds matches into the dictionary. A referenced CDict is copied into the context in that case (attaching is disabled with forceWindow), but ZSTD_resetCCtx_byCopyingCDict() copied the CDict's loadedDictEnd, computed without forceWindow. The whole dictionary then stayed reachable for the first window of input: with a 1 KB window and a 64 KB CDict, ZSTD_generateSequences() reports offsets of 65536. Clear loadedDictEnd when forceWindow is set, as for a loaded dictionary. Add a test that checks the offsets for a source that repeats the start of a 64 KB CDict, with a 1 KB window. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01GwUvhmzgfkiZ6vo4uGyesN
This branch has not been deployed
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.
What and why
ZSTD_c_forceMaxWindowis documented to "Force back-reference distances to remain < windowSize, even when referencing into Dictionary content".ZSTD_CCtx_loadDictionary()builds its CDict with the context's parameters, andZSTD_CCtx_refPrefix()loads the prefix with them. In both cases,ZSTD_loadDictionaryContent()setsloadedDictEndto 0 whenforceWindowis set, so the window also bounds matches into the dictionary. A CDict referenced withZSTD_CCtx_refCDict()is copied into the context whenforceWindowis set, since attaching is then disabled (ZSTD_shouldAttachDict()), unless the CDict was built withZSTD_c_enableDedicatedDictSearch, which is always attached. ButZSTD_resetCCtx_byCopyingCDict()copies the CDict'sloadedDictEnd, which was computed with the CDict's own parameters, usually withoutforceWindow. The whole dictionary then stays reachable for the first window of input.Example: 1 KB window (
windowLog = 10),ZSTD_c_forceMaxWindow = 1, a 64 KB dictionary of random bytes, and an 8 KB source that repeats the start of the dictionary. The largest offset thatZSTD_generateSequences()reports is 0 withZSTD_CCtx_loadDictionary()(871 at level 19), and 65536 withZSTD_CCtx_refCDict(), at levels 1, 3, 6, 12 and 19. The format allows such references within the firstWindow_Sizebytes of a frame, so the zstd decoder accepts these frames; it is the documented guarantee of the parameter that is not kept.This change clears
loadedDictEndwhenforceWindowis set, asZSTD_loadDictionaryContent()does. The new test intests/fuzzer.cchecks the offsets for that source with a referenced CDict. It fails ondevand passes with this change.Not covered: a CDict built with
ZSTD_c_enableDedicatedDictSearchis attached even withforceWindow, and still reaches 64 KB back at levels 5 and 6 in the example above. Attached dictionaries need a different fix, which I left out of this change.Checklist
A gain in one configuration is not a general gain: see the Performance section of CONTRIBUTING.md.
Tools used
Claude Code (Anthropic's AI coding agent) found the problem while comparing the documentation of the dictionary parameters with their behaviour, and wrote the fix, the test and this description.
🤖 Generated with Claude Code
https://claude.ai/code/session_01GwUvhmzgfkiZ6vo4uGyesN