Apply ZSTD_c_deterministicRefPrefix to ZSTD_createCDict_byReference , ensure deterministic output. - #4739
Open
cdcxd wants to merge 2 commits into
Open
Apply ZSTD_c_deterministicRefPrefix to ZSTD_createCDict_byReference , ensure deterministic output.#4739cdcxd wants to merge 2 commits into
cdcxd wants to merge 2 commits into
Conversation
…CDict_byReference The ZSTD_createCDict_byReference may produce different result if the input is adjacent to dict in memory. Which is exactly the opt-in option was trying to solve, make CDict load in non-contiguous mode if the flag is set
cdcxd
force-pushed
the
cdict_ref_deterministic
branch
from
August 20, 2026 22:04
2c388dd to
8e6d1d2
Compare
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.
Solves #4738
TL;DR
The PR ensures deterministic compression output by making the opt-in param ZSTD_c_deterministicRefPrefix apply to ZSTD_createCDict_byReference . It also added a unit test.
Context
My use case was one RAII wrapper loading the dict content from disk, then multiple threads create the cdict by ref. I noticed this non deterministic output may happen once per 3-5 millions of compression calls. I constructed a UT that can reproduce this discrepancy.
By default the output would be slightly different if input data is adjacent to the dict buffer. There's no param to control it. With this change, we can set ZSTD_c_deterministicRefPrefix and opt-in the deterministic mode.
It is a no-op on the attach copy cdict path. Its window never refers to the caller's dictionary buffer.
Why reuse the existing flag
I saw the comment
zstd/lib/zstd.h
Line 2275 in 82d322c
If you really care about determinism when using a dictionary or prefix, like when doing delta compression, you should select this option.So the param is reused here.The behavior at
zstd/lib/compress/zstd_compress.c
Line 5280 in 82d322c
It calls ZSTD_resetCCtx_usingCDict , that doesn't honor the flag
ZSTD_c_deterministicRefPrefix. Otherwise it would honor the flag. The PR makes both paths honor the flag.Tests
All unit tests passed.
Benchmark result
Corpus is zstd's own regression data (
tests/regression/data.c):github.tar(757,760 B) compressed against the
github.dictdictionary (112,640 B), both fetched from the
regression-datarelease the CI was using. x86-64, gcc 13, pinned16 core.
Measuring the overhead with adjacent input + flag on , otherwise it's a no-op.
adjacent
separate
adjacent
separate
dev-adjacent
dev-adj → branch-flag
(above size gate)
The adjacent input possibility is really low, so in the benchmark adjacent input was created deliberately. The performance loss with happened-to-be-adjacent-input is actually higher than the comment said (2.5%, link:
zstd/lib/zstd.h
Line 2277 in 82d322c
I tried the original refPrefix + ZSTD_c_deterministicRefPrefix (not the cdict one) with both HEAD@dev and the PR commit 172b4b6 in 2021, with many types of data, all combinations have 5%-20% speed loss (adjacent, flag on vs off). I wasn't able to reproduce the pre-existing 2.5% speed loss result. It might make sense to update the comment too (not included).