Skip to content

perf(decoding): four-stream literals in a machine word, asm loop on 32-bit x86 - #681

Merged
polaz merged 4 commits into
mainfrom
perf/#678-i686-literals
Nov 17, 2026
Merged

polaz merged 4 commits into
mainfrom
perf/#678-i686-literals

Conversation

@polaz

@polaz polaz commented Nov 17, 2026 •

Copy link
Copy Markdown
Member

Summary

  • On 32-bit x86 the four-stream Huffman literal decoder was the whole decompress gap against libzstd on z000033: the literal stage cost about 25% more cycles than upstream's 32-bit loop, while sequences were already faster.
  • Each stream now keeps its bits in one machine word, and on 32-bit x86 the batch loop runs in assembly with all four words in registers.

Changes

  • Burst word is the machine word: u64 on 64-bit targets, u32 on 32-bit, with the safe burst length and the input window derived from its width. Two or three symbols per stream between reloads on 32-bit.
  • The reload counts bits on a nonzero word (bare bsf, no zero guard), since the sentinel bit keeps it nonzero.
  • On 32-bit x86, with a 10- or 11-bit table, each batch runs in an assembly loop: words in ebx, edx, esi and edi, the table in ebp; input and output pointers and the bound live on the stack and are read off the dependency chains. The Rust loop stays for other table widths and targets.
  • Output is unchanged.

Testing

  • Formatting and clippy pass; the literal, Huffman and round-trip tests pass on i686-unknown-linux-gnu.
  • Interleaved prebuilt-binary A/B on a Broadwell host, z000033 built for i686 (--profile bench), against the Rust loop on a 32-bit word: L1 -12%, L3 -6%, L14 -7%, L16 -4% in two of three rounds. Against libzstd on the same host: 1.07x faster at L1, 1.02x at L3, 1.03x at L14, parity at L16.

Part of #678

Summary by CodeRabbit

  • Performance
    • Optimized Huffman decompression on supported 32-bit systems, which may improve decoding speed. Certain decoding cases now use a dedicated path to process multiple symbols per iteration.
  • Compatibility
    • Updated decoding to support narrower processor word sizes while preserving existing behavior on 64-bit systems. On narrower targets, streams with less input available are handled through an alternate decoding path.

On a 32-bit target the four-stream Huffman loop carried each stream in
a u64: every symbol shifted a register pair and every reload read eight
bytes into two registers, on a machine with seven of them.

- Each stream's state, pending bits and sentinel now live in a word of
  the pointer's width, as upstream zstd's four-stream loops keep a
  stream in a size_t. A 32-bit word takes two symbols per stream
  between reloads at table widths 8 to 11, as upstream's 32-bit loop
  does, and reloads four bytes.
- The word's window ends where the reader's eight-byte window ends, so
  the bits consumed carry over unchanged and the input budget and the
  writeback for the drain are the same on both widths.
- 64-bit targets keep a u64 and the same loop.

Part of #678
The drain keeps the decoder state in a u64; a 32-bit burst word is
widened when the burst hands it back.

Part of #678
With a 32-bit word per stream the Rust loop still did not fit seven
registers: the compiler kept some streams' words on the stack, so every
symbol of those streams loaded the word, shifted it and stored it back,
a store-to-load wait on the chain each symbol depends on. The reload's
bit count also guarded the zero case with a conditional move although
the sentinel keeps the word nonzero.

- On 32-bit x86 with a 10- or 11-bit table each batch runs in an
  assembly loop: the four words in ebx, edx, esi and edi, the table in
  ebp, the entry in ecx; input and output pointers and the bound live
  on the stack and are read off the chains. Two symbols per stream an
  iteration, as upstream zstd's 32-bit loop decodes.
- The Rust loop counts the reload's bits on a nonzero word, a bare bsf
  on x86.

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-17T15:06:15.766944Z 09050d7 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 17, 2026 •

Copy link
Copy Markdown

Explain Change →

No actionable comments were generated in the recent review.

Walkthrough

The four-stream Huffman burst decoder now uses a burst word sized for the target pointer width. On 32-bit x86, 10- and 11-bit tables use a dedicated assembly loop; other cases retain the Rust burst loop.

Changes: Huffman burst decoding

Layer / File(s) Summary
Width-aware burst state and reader windows
zstd/src/decoding/literals_section_decoder.rs
Burst bounds, state extraction, input windows, reloads, and reader writeback use the selected burst-word width. Narrow-word targets skip bursting when any stream lacks an eight-byte window.
32-bit x86 assembly burst loop
zstd/src/decoding/literals_section_decoder.rs
The decoder dispatches 10- and 11-bit tables to an assembly loop that decodes two symbols per stream per iteration. Other cases use the Rust loop.

Estimated code review effort: 4 (Complex) | ~45 minutes

Change: Refactor

Possibly related PRs

Suggested reviewers: rowang077

Merge Risk: Minimal · up to 09050

This change adds a 32-bit-word burst path and a 32-bit x86 assembly loop for 10- and 11-bit Huffman tables, and output is described as unchanged. No concrete merge-blocking defect was identified. The author reports passing tests on i686.

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 two main changes: a machine-word-sized (u64/u32) four-stream Huffman literal burst decoder and an inline-assembly batch loop on 32-bit x86. The raw summary and PR objectives both d…
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.
Full details: Title check

Explanation

The title names the two main changes: a machine-word-sized (u64/u32) four-stream Huffman literal burst decoder and an inline-assembly batch loop on 32-bit x86. The raw summary and PR objectives both describe these as the core changes, so the title matches the changeset. It does not mention the drain-path handling for narrow targets, but it does not need to cover every detail.

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 133762f and 09050d7.

Files selected for processing: 1

  • zstd/src/decoding/literals_section_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.

@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!

The short-stream bail-out and the two- and three-symbol burst arms can
only run with a 32-bit word: a 64-bit word gives five or more symbols
per burst and needs no window skip. Gate them on the pointer width so
the 64-bit loop keeps exactly its earlier arms instead of carrying
unreachable ones.

Part of #678
@polaz

polaz commented Nov 17, 2026

Copy link
Copy Markdown
Member Author

@coderabbitai review

@coderabbitai

coderabbitai Bot commented Nov 17, 2026 •

Copy link
Copy Markdown
Action performed

Review finished.

Note: CodeRabbit is an incremental review system and does not re-review already reviewed commits. This command is applicable only when automatic reviews are paused.

@polaz
polaz merged commit 219ff03 into main Nov 17, 2026
59 of 61 checks passed
@polaz
polaz deleted the perf/#678-i686-literals branch November 17, 2026 18:09
@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