Repository navigation
Fix stale useRowMatchFinder/postBlockSplitter in ZSTD_CCtxParams_init_internal - #74
Merged
oleg-st merged 1 commit intoSep 25, 2026
Conversation
…_internal The object initializer read cctxParams->useRowMatchFinder and cctxParams->postBlockSplitter before the struct was overwritten, so the values came from the previous call (or from uninitialized stack memory for callers passing a fresh local, since the module uses SkipLocalsInit). Upstream zstd memsets the struct first, so both always resolve from ZSTD_ps_auto. Effect: after a ZSTD_compressCCtx call at a level that disables the row match finder (levels 1-4, negative levels), later calls on the same context at levels 5-12 used the hash chain finder. Output differed from native zstd and compression was 2-5x slower.
Owner
|
This file is generated by https://github.com/oleg-st/ZstdSharp.Generator from |
Contributor
Author
|
Thanks, fixed in the generator: oleg-st/ZstdSharp.Generator#1. I regenerated from zstd v1.5.7 before and after that change, and the only output difference is |
Owner
|
I can merge this once it’s marked as ready for review. |
kibertoad
marked this pull request as ready for review
September 25, 2026 13:15
Owner
|
@kibertoad Thank you |
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.
Problem
ZSTD_CCtxParams_init_internalbuilds the new params with an object initializer that readscctxParams->useRowMatchFinderandcctxParams->postBlockSplitter. C# evaluates those reads before the struct is overwritten, so they return the previous call's resolved values. Upstream zstd memsets the struct first, so both always resolve fromZSTD_ps_auto.Callers that pass a fresh stack local (
ZSTD_compressBegin_advanced,ZSTD_compressBegin_usingDict_deprecated,ZSTD_compressBegin_usingCDict_internal) read uninitialized memory here, because the module usesSkipLocalsInit.On a reused context, a
ZSTD_compressCCtxcall at a level whose strategy disables the row match finder (levels 1-4 and negative levels) leavessimpleApiParams.useRowMatchFinder = ZSTD_ps_disable. Every later call on that context at levels 5-12 then uses the hash chain match finder. The output differs from native zstd and compression runs 2-5x slower.Fix
Assign
useRowMatchFinderandpostBlockSplitterafter the struct is reset, matching upstream.Benchmark
Same
ZSTD_CCtxcompresses dickens at levels 1, 3, 5, 7, 12 (first 2 MB) in sequence. Min of 5 runs, Ryzen 7700X, .NET 10:After the fix the compressed sizes match native zstd.
Test
CompressCCtxReuseAcrossLevelsMatchesFreshContextcompresses at level 1 and then level 12 on one context, and checks that the output matches a fresh context and that the row match finder is enabled. It fails on master and passes with the fix. The full suite passes on net9.0.