Skip to content

perf(decoding): set sequence table counters in one pass - #682

Merged
polaz merged 1 commit into
mainfrom
perf/#678-fse-setup
Nov 17, 2026
Merged

polaz merged 1 commit into
mainfrom
perf/#678-fse-setup

Conversation

@polaz

@polaz polaz commented Nov 17, 2026 •

Copy link
Copy Markdown
Member

Summary

  • The sequence FSE table build walked the alphabet three times before its per-state pass and branched on each probability while laying symbols down. It now does the setup in one pass and lays down without a branch, as upstream's ZSTD_buildFSETable_body does.

Changes

  • One pass over the alphabet sets every counter to its probability and places a -1 symbol at the top of the table with a counter of 1.
  • The fast lay-down writes the word of a zero-probability symbol too and leaves the position in place, so it takes no branch on the probability; the symbol word steps with the symbol instead of being multiplied out.
  • Output is unchanged.

Testing

  • Formatting and clippy pass; the FSE, sequence, decoder and round-trip tests pass.

  • Interleaved prebuilt-binary run on a Broadwell host, z000033 decode, --profile bench with the dashboard's x86-64-v3; main, this branch and upstream through the C bindings in the same run, ms per frame:

    level main this branch upstream
    3 1.921-2.010 1.896-1.913 1.723-1.752
    13 1.847-1.852 1.804-1.880 1.645-1.684
    19 3.244-3.452 3.268-3.328 2.915-2.945
  • Against upstream the decode still takes 1.08-1.11x its time at level 3 (main: 1.11-1.16x) and about 1.1x at levels 13 and 19; the remaining gap is in the sequence loop and is not addressed here.

  • The level 3 wall-clock gain over main (about 1.2%) is under the layout band of two separate builds, so it is not claimed. The attributable figure is the table build itself at level 19: 2.88G to 2.59G cycles over 3000 frames (-10%), while the sequence loop, which the change cannot execute, moves +0.7% in the same run. Levels 13 and 19 wall-clock are within noise.

Part of #678

The table build walked the alphabet three times before the per-state
pass (zero the counters, place the -1 symbols, lay down the rest) and
the lay-down branched on each probability. Upstream's
ZSTD_buildFSETable_body walks it once and lays down without a branch.

- One pass sets every counter to its probability and places a -1
  symbol at the top of the table with a counter of 1.
- The lay-down writes the word of a zero-probability symbol too and
  leaves the position in place, so it takes no branch on the
  probability; the word steps with the symbol instead of being
  multiplied out.

Output is unchanged.

Part of #678
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 17, 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-17T19:13:55.220582Z e17d089 PR opened
ℹ️ 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 17, 2026 •

Copy link
Copy Markdown

Explain Change →

No actionable comments were generated in the recent review.

Walkthrough

The FSE decoding-table builder now initializes symbol counters during probability scanning. Its no--1 fast-spread path uses a rolling repeated-byte word, and its fallback spread path enumerates probabilities.

Changes: FSE decoding table spread

Layer / File(s) Summary
Counter initialization and symbol spreading
zstd/src/fse/fse_decoder.rs
The probability scan sets each counter to 1 for -1 symbols and to the probability value otherwise. The fast-spread path advances a repeated-byte word for each symbol, including zero-probability symbols without advancing pos. The fallback path enumerates probabilities and no longer initializes counters there.

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

Change: Refactor

Possibly related PRs

Merge Risk: Minimal · up to e17d0

This change speeds up FSE table construction in the decoder without changing its output. No actionable merge-blocking risk remains.

Pre-merge checks: Passed 4

Check name Status Explanation
Title check Passed The title "perf(decoding): set sequence table counters in one pass" matches the main change. The PR changes the FSE decoding table construction so that symbol counters are set in a single pass instead…
Linked Issues check Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check Passed Check skipped because no linked issues were found for this pull request.
Description Check Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Full details: Title check

Explanation

The title "perf(decoding): set sequence table counters in one pass" matches the main change. The PR changes the FSE decoding table construction so that symbol counters are set in a single pass instead of three passes. The raw summary confirms the counter initialization was moved into the probability scan in build_decoding_table_inner, and the objectives describe the single setup pass and branchless symbol lay-down. The title is specific and uses a conventional commit prefix.

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 219ff03 and e17d089.

Files selected for processing: 1

  • zstd/src/fse/fse_decoder.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: e17d089a9f

ℹ️ 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".

Comment thread zstd/src/fse/fse_decoder.rs Resolved
@codecov

codecov Bot commented Nov 17, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

@polaz
polaz merged commit 640a655 into main Nov 17, 2026
36 checks passed
@polaz
polaz deleted the perf/#678-fse-setup branch November 17, 2026 21:32
@sw-release-bot sw-release-bot Bot mentioned this pull request Nov 17, 2026
polaz pushed a commit that referenced this pull request Nov 21, 2026
## 🤖 New release

* `structured-zstd`: 0.4.2 -> 0.4.3

<details><summary><i><b>Changelog</b></i></summary><p>

<blockquote>

##
[0.4.3](v0.4.2...v0.4.3)
- 2026-11-21

### Performance

- *(encoding)* fewer memcpy calls on small dictionary frames
([#691](#691))
- *(encoding)* cheaper dictionary frames at negative levels
([#687](#687))
- *(encoding)* optimal-parser workspace in upstream's shape
([#686](#686))
- *(decoding)* sequence loop common path in assembly on x86_64
([#683](#683))
- *(decoding)* set sequence table counters in one pass
([#682](#682))
- *(decoding)* four-stream literals in a machine word, asm loop on
32-bit x86
([#681](#681))
- *(decoding)* four-stream literal loop in assembly
([#679](#679))
</blockquote>


</p></details>

---
This PR was generated with
[release-plz](https://github.com/release-plz/release-plz/).

Co-authored-by: sw-release-bot[bot] <255865126+sw-release-bot[bot]@users.noreply.github.com>
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.

1 participant