Skip to content

Fix stale useRowMatchFinder/postBlockSplitter in ZSTD_CCtxParams_init_internal - #74

Merged
oleg-st merged 1 commit into
oleg-st:masterfrom
kibertoad:fix/cctx-params-row-matchfinder-reuse
Sep 25, 2026
Merged

oleg-st merged 1 commit into
oleg-st:masterfrom
kibertoad:fix/cctx-params-row-matchfinder-reuse

Conversation

@kibertoad

Copy link
Copy Markdown
Contributor

Problem

ZSTD_CCtxParams_init_internal builds the new params with an object initializer that reads cctxParams->useRowMatchFinder and cctxParams->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 from ZSTD_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 uses SkipLocalsInit.

On a reused context, a ZSTD_compressCCtx call at a level whose strategy disables the row match finder (levels 1-4 and negative levels) leaves simpleApiParams.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 useRowMatchFinder and postBlockSplitter after the struct is reset, matching upstream.

Benchmark

Same ZSTD_CCtx compresses dickens at levels 1, 3, 5, 7, 12 (first 2 MB) in sequence. Min of 5 runs, Ryzen 7700X, .NET 10:

case before after native 1.5.7
level 5 102 ms, 3513081 B 76 ms, 3498935 B 67 ms, 3498935 B
level 7 258 ms, 3312364 B 128 ms, 3311075 B 116 ms, 3311075 B
level 12, 2 MB 194-340 ms, 675287 B 67-72 ms, 676303 B 58 ms, 676303 B

After the fix the compressed sizes match native zstd.

Test

CompressCCtxReuseAcrossLevelsMatchesFreshContext compresses 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.

…_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.
@oleg-st

oleg-st commented Sep 25, 2026 •

Copy link
Copy Markdown
Owner

@kibertoad

Copy link
Copy Markdown
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 ZSTD_CCtxParams_init_internal. It's the same code this PR has in ZstdCompress.cs. I can keep this PR for the regenerated file and the regression test, or close it if you'd rather regenerate yourself.

@oleg-st

oleg-st commented Sep 25, 2026

Copy link
Copy Markdown
Owner

I can merge this once it’s marked as ready for review.

@kibertoad
kibertoad marked this pull request as ready for review September 25, 2026 13:15
@oleg-st
oleg-st merged commit a8d4b0e into oleg-st:master Sep 25, 2026
@oleg-st

oleg-st commented Sep 25, 2026

Copy link
Copy Markdown
Owner

@kibertoad Thank you

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants