Skip to content

perf(encoding): attached dictionary tree at the CDict's hashLog - #704

Merged
polaz merged 16 commits into
mainfrom
perf/#703-bt-dict-table
Nov 25, 2026
Merged

polaz merged 16 commits into
mainfrom
perf/#703-bt-dict-table

Conversation

@polaz

@polaz polaz commented Nov 24, 2026 •

Copy link
Copy Markdown
Member

Summary

  • The binary-tree backend (btopt and up) hashes an attached dictionary at the CDict's hashLog, as upstream walks dms->hashTable at the dictionary's own cParams and as the Fast and Dfast backends already do. It used a width sized from the dictionary's length, which ran a bit under the CDict's on small dictionaries.
  • On a 1280-byte dictionary at level 11 (a btopt CDict of hashLog 12) the old width was 11 bits: the buckets were half full on random input against a fifth for upstream, the search's empty-bucket test mispredicted about twice as often, and more colliding dictionary nodes were walked.
  • A reused compressor whose CDict width changes between frames rebuilds the tree at the new width instead of re-borrowing the primed one.

Effect, 10 KiB of random input with its trained dictionary, cycles against main (Broadwell, interleaved rounds):

target level change against libzstd, before -> after
i686 11 -8.4% 0.91 -> 1.00
i686 12 -8.0% 0.92 -> 1.00
x86_64 11 -9.9% 1.05 -> 1.16
x86_64 12 -8.1% 0.99 -> 1.08

Levels 13, 16 and 19, and 4 KiB log frames with their dictionary at every level, are unchanged in time.

Output

Frames compared against the parent at levels 9-22 over text, datagen and random input, 1280-byte, 16 KiB and 110 KiB dictionaries, 1 KiB and 10 KiB frames: identical except four level-13 cells on text, each the same size or smaller and closer to libzstd 1.6.0 (dictionary / frame: 16 KiB / 1 KiB 515 -> 510 bytes, libzstd 507; 110 KiB / 1 KiB 468 -> 457, libzstd 451; 110 KiB / 10 KiB 3454 -> 3452, libzstd 3452; 16 KiB / 10 KiB unchanged size). Hash width changes which colliding nodes the bounded walk visits, so the output is not byte-identical.

Changes

  • MatchTable::dict_table_hash_log: the CDict's hashLog, set by the frame's reset from the resolved dictionary tables; prime_dms_bt builds the dictionary tree at it, and a primed tree of another width is dropped before the reset re-borrows it.
  • The length-based dms_hash_log and its tests are removed.
  • Regression tests driver_bt_dictionary_table_takes_the_cdict_hash_log and driver_bt_dictionary_tree_follows_a_changed_cdict_hash_log.

Testing

Library tests, formatting, clippy and rustdoc pass locally; CI runs the full matrix.

Closes #703

Summary by CodeRabbit

  • Bug Fixes
    • Improved dictionary-based compression reliability when dictionary table settings change, helping ensure the match tree uses the configured table width.
    • Added regression coverage for dictionary table sizing with different compression settings.

The binary-tree backend hashed an attached dictionary into a table sized
from the dictionary's length (ceil_log2 of it, clamped to the frame's
hashLog), while upstream walks dms->hashTable at the dictionary's own
cParams, and the Fast and Dfast backends already take the CDict's
geometry. On a small dictionary the length rule ran a bit under the
CDict's: a 1280-byte dictionary at level 11 (a btopt CDict of hashLog
12) got 11 bits, its buckets were half full on random input against a
fifth for upstream, and the search's empty-bucket test mispredicted
twice as often (923 against 470 mispredicted taken branches per profile
on i686), with more colliding dictionary nodes walked after it.

The frame's reset now hands the HashChain backend the resolved CDict
hashLog, which the dictionary tree is built at; the length-based sizing
and its tests are gone.

Regression test: driver_bt_dictionary_table_takes_the_cdict_hash_log.

Closes #703
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 24, 2026 •

Copy link
Copy Markdown
Codex Review Summary: ✅ Completed

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-25T11:41:16.704967Z f51c53a New commits
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@coderabbitai

coderabbitai Bot commented Nov 24, 2026 •

Copy link
Copy Markdown

Explain Change →

Warning

Review limit reached

You've used all free OSS reviews for now. Wait for the free limit to reset to keep reviewing this public repository.

Next included review available in 22 minutes.

Check out review usage here.

View limit details

Limit details: You’ve used the included review currently available.

Learn how review limits work.

Review configuration:

Run configuration

Commits

Reviewing files that changed from the base of the PR and between 3821fff and f51c53a.

Files selected for processing: 5

  • zstd/src/encoding/frame_compressor/tests.rs
  • zstd/src/encoding/match_generator/mod.rs
  • zstd/src/encoding/match_generator/tests.rs
  • zstd/src/encoding/match_table/storage.rs
  • zstd/src/encoding/match_table/storage/dms_hash_log_tests.rs

No actionable comments were generated in the recent review.

Walkthrough

The change carries the resolved dictionary table hash-log width into MatchTable and uses it to build dictionary match state. It invalidates a primed dictionary tree when its width differs. New regression tests cover binary-tree dictionaries and reused drivers. The former sizing helper, tests, and related comments are removed.

Changes: Dictionary Match Table Hash Width

Layer / File(s) Summary
Resolve and carry dictionary hash width
zstd/src/encoding/match_table/storage.rs, zstd/src/encoding/match_generator/mod.rs, zstd/src/encoding/match_generator/tests.rs
MatchTable stores, initializes, and copies the dictionary table hash width. HashChain reset sets it from resolved dictionary parameters and invalidates a primed tree when its width differs. Regression tests check the width for binary-tree dictionaries, including after driver reuse.
Build dictionary match state from stored width
zstd/src/encoding/match_table/storage.rs, zstd/src/encoding/match_table/storage/dms_hash_log_tests.rs, zstd/src/encoding/frame_compressor/tests.rs
build_dms! reads the stored width and asserts that it is nonzero. The former sizing helper, its tests, and related comments are removed.

Estimated code review effort: 2 (Simple) | ~10 minutes

Change: Bug fix · Severity of issue fixed: Medium

Possibly related PRs

Merge Risk: Minimal · previous review 3821f

The change hashes the attached dictionary tree at the compressor's hash width, which should speed up small-frame dictionary compression at levels 11 and 12. Regression tests cover the width and the reused-driver case. No merge-blocking risk was found.

Pre-merge checks: Passed 4

Check name Status Explanation
Description Check Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check Passed The title names the main change: the attached dictionary's binary-tree (btopt and up) hash table now uses the CDict's hashLog instead of a width derived from dictionary length. The title follows the c…
Linked Issues check Passed Issue #703 requires closing the libzstd gap at lazy levels 11-12 with a dictionary on small-10k-random. The whole-PR diff of mod.rs sets dict_table_hash_log from the CDict hash_log during the HashChai…
Out of Scope Changes check Passed All changed files relate to the binary-tree dictionary width fix: mod.rs reset logic, storage.rs field and removal of dms_hash_log, the deleted dms_hash_log tests, the two regression tests, and a remo…
Full details: Title check

Explanation

The title names the main change: the attached dictionary's binary-tree (btopt and up) hash table now uses the CDict's hashLog instead of a width derived from dictionary length. The title follows the conventional type(scope) prefix and is specific enough for a reviewer scanning history. Its length (63 characters) is within a reasonable range.

Full details: Linked Issues check

Explanation

Issue #703 requires closing the libzstd gap at lazy levels 11-12 with a dictionary on small-10k-random. The whole-PR diff of mod.rs sets dict_table_hash_log from the CDict hash_log during the HashChain reset and drops a primed tree of another width. Regression tests cover the width. Benchmark acceptance criteria are measurements and are not verified here.

Full details: Out of Scope Changes check

Explanation

All changed files relate to the binary-tree dictionary width fix: mod.rs reset logic, storage.rs field and removal of dms_hash_log, the deleted dms_hash_log tests, the two regression tests, and a removed stale comment in frame_compressor tests. No public API change and no unrelated functional change appears in the evidence.

Finishing Touches

Generate unit tests (beta)

  • Commit to this branch
  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

Share

Recent review info

Run configuration

Commits

Reviewing files that changed from the base of the PR and between 3675866 and 3821fff.

Files selected for processing: 2

  • zstd/src/encoding/match_generator/mod.rs
  • zstd/src/encoding/match_generator/tests.rs

Included review availability: This review used your included allowance. Your plan provides up to 1 included review per hour; 0 remain after this review.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 36758667f5

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

@codecov

codecov Bot commented Nov 24, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

zstd 1.6.0 takes ZDICT_tryMerge's included-entry length difference
(MAX(elt.length - table[u].length, 1)) in unsigned 32-bit arithmetic
where 1.5.7 cast both to int, so an entry longer than the new element
adds a wrapped length to its savings, and the product is size_t. The
trainer now does the same at pointer width; against 1.5.7's rule it
chose different content on the unit-file fixture (4091 bytes against the
reference's 3941), which failed legacy_trainer_ffi on every test job.

ffi-bench now requires zstd-sys 2.2 (zstd 1.6.0): the benchmarks and
interop tests resolve the newest release anyway, the floor makes the
reference they run against a requirement rather than an accident of
resolution.

Part of #705
The reference moves to zstd 1.6.0 everywhere it was pinned to 1.5.7:
the vendored zstd.h (zdict.h and zstd_errors.h are unchanged), the
version the C ABI and pkg-config report (10600, "1.6.0"), the release
the conformance scripts and the c-abi job build, and the command line
the CLI follows.

Following 1.6.0's API changes:

- ZSTD_isDeterministicBuild() is exported. It returns 0: this encoder's
  frames are its own, so it does not belong to the builds whose output
  matches libzstd 1.6.0 byte for byte.
- ZSTD_copyCCtx() and ZSTD_copyDCtx(), removed upstream, are removed
  here with the state kept only for them.
- v0.5-v0.7 frames are no longer read by default, as libzstd stopped
  building that support by default (ZSTD_LEGACY_SUPPORT=0). The codec's
  `legacy` feature leaves `default`; the C ABI gains a `legacy` feature
  forwarding it, and without it such a frame is not a frame
  (ZSTD_isFrame 0, prefix_unknown), which a test pins. CI runs the
  decoder and command-line tests, the C ABI tests and the decode_legacy
  fuzz target with the feature on; the decoder step now builds the
  command line too, which its filter named but never compiled.

BREAKING CHANGE: the default build no longer decodes v0.5-v0.7 frames;
enable the `legacy` feature to keep reading them. The C ABI no longer
exports ZSTD_copyCCtx and ZSTD_copyDCtx.

Part of #705
@polaz
polaz changed the base branch from main to chore/#705-zstd-1.6.0 November 24, 2026 23:14
polaz added 11 commits November 25, 2026 01:38
Upstream 1.6.0's own suites (tests/playTests.sh and every case of
tests/cli-tests the conformance script runs) pass against the command
line. What they required:

- --filelist keeps a CR before the newline as part of the name except on
  native Windows, where CRLF ends a line.
- -f replaces a symbolic link standing at the output name with a regular
  file, never writing through it; without -f such an output is refused.
- -l says "reached end of file with incomplete (skippable) frame" for an
  empty file or a header cut short, and "seeked to position N, which is
  beyond file size of M" for a skippable frame whose payload runs past
  the end.
- --output-dir-flat refuses any two inputs that would share an output,
  with or without --rm, before anything is written: each shared name,
  then "no files were processed".
- -M bounds the decode window at any value, as ZSTD_DCtx_setMaxWindowSize
  does upstream: a lower limit refuses the frames that pass it when they
  are decoded instead of refusing the limit itself. A plain decode's
  -D dictionary is weighed alone against it; benchmarks and training keep
  their accounting. The value is a 32-bit field, so 4 GiB overflows, and
  the numeric error lists the G suffixes.
- Training refuses more samples than an int count and a size_t length
  table hold, with status 14, before any sample is read.
- Help text, the too-few-samples advice and the refusal of an output that
  is an input take 1.6.0's wording; -vvV says the build is
  non-deterministic (its frames are not libzstd's byte for byte) and
  -vV lists legacy support when it is built in.

Part of #705
`frame_header_size`'s doc linked `LegacyVersion::header_size`, which
exists only with `legacy`; with the feature out of `default` the docs
build (RUSTDOCFLAGS=-D warnings) failed on the broken link. The method is
named without a link, and `legacy` joins the docs.rs feature set and the
CI rustdoc run that mirrors it, so the public docs keep the type; the
private-items run stays without it and covers the other configuration.

Part of #705
cltools/zstdless.sh exercises the reference's two-line zstdless script,
which this project does not ship, through the host's less, and expects
the exit status of an older less: less 710 exits 1 on a missing file, so
the case fails on such hosts with the reference binary as well. It
checks nothing this command decides.

Part of #705
The zstdless case runs again. Its bad-path step ran under set -e and so
asserted the exit status of less, which zstdless execs: less 668 exits 0
on a missing file, less 710 exits 1, with the reference binary too. The
conformance script lets that one step fail; the case still compares
stdout and stderr, including this command's "can't stat" message, and
the good-path steps keep set -e. The script stops if the step changes
shape upstream.

Part of #705
Bash 5.2 expands an unquoted & in a ${var/pat/rep} replacement to the
matched text, so the adjusted step's >&2 became a bad descriptor. Both
sides of the replacement are quoted now.

Part of #705
The ${var/pat/rep} replacement kept its inner quotes literally under the
bash 3.2 that macOS runs, turning the adjusted step into one quoted word
and failing with "command not found". The test is rewritten line by
line now, which behaves the same under bash 3.2 and 5.2.

Part of #705
A reused compressor whose hash_log changed between frames kept the
attached dictionary tree at the previous CDict width: on small frames the
live widths are capped by the source and stay the same, so the reset
re-borrowed the primed tree without the build's shape check. A primed
tree whose width differs from the frame's CDict is now dropped before the
reset, as the Fast backend's set_dict_table_hash_log does.

Regression test driver_bt_dictionary_tree_follows_a_changed_cdict_hash_log
(1280-byte dictionary, level 11, 256-byte frames, hash_log 11 then 12).

Part of #703
- A set of dictionaries (`reset_with_dict_handles`,
  `decode_all_with_dict_handles`, the C ABI's ZSTD_d_refMultipleDDicts)
  gives a frame naming a nonzero ID that entry and fails with
  DictNotProvided without one; a frame naming none or ID 0 takes the
  entry of ID 0, or no dictionary. It took the last entry instead, so a
  frame naming no dictionary decoded with an unrelated one (upstream
  1.6.0 `ZSTD_DCtx_getFrameDDict`). One dictionary handed in alone
  (`*_with_dict_handle`, the C ABI's loaded, single referenced and prefix
  dictionaries) still applies to a frame naming none.
- The C ABI's raw-content decode dictionary has ID 0, as upstream's
  raw-content DDict has, so it is the set entry such a frame takes.
- Turning ZSTD_d_refMultipleDDicts off drops the set, as upstream clears
  its DDict set; its entries were still used.
- A skippable frame read through ZSTD_decompressStream spends a
  referenced prefix, as upstream resolves the frame's dictionary before
  telling a skippable header apart; the prefix outlived it.
- `-l`'s skippable-frame end is computed with the bound it relies on
  asserted in debug builds; the `-D` check under `-M` and `--version`
  get tests; the README feature table shows `legacy` off by default.

Regression tests: dict_set_takes_the_id_zero_entry_for_a_frame_naming_none,
single_ddict_decodes_a_frame_naming_no_dictionary,
disabling_multiple_ddicts_releases_the_set,
a_skippable_frame_spends_the_prefix,
a_decode_dictionary_fits_a_limit_covering_both_copies.

BREAKING CHANGE: reset_with_dict_handles and decode_all_with_dict_handles
no longer apply the last dictionary to a frame naming none; they take the
entry of ID 0, or decode without a dictionary, and a frame naming an ID the
set lacks fails with DictNotProvided instead of DictIdMismatch.

Part of #705
Base automatically changed from chore/#705-zstd-1.6.0 to main November 25, 2026 09:31
@polaz
polaz enabled auto-merge (squash) November 25, 2026 11:19
@polaz
polaz disabled auto-merge November 25, 2026 11:20
@polaz
polaz enabled auto-merge (squash) November 25, 2026 11:40
@polaz
polaz merged commit ac491f3 into main Nov 25, 2026
36 checks passed
@polaz
polaz deleted the perf/#703-bt-dict-table branch November 25, 2026 12:07
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.

perf: lazy levels 11-12 with a dictionary on 10 KB frames

1 participant