Skip to content

perf(decoding): four-stream literal loop in assembly - #679

Merged
polaz merged 2 commits into
mainfrom
perf/#678-z33-decode
Nov 17, 2026
Merged

polaz merged 2 commits into
mainfrom
perf/#678-z33-decode

Conversation

@polaz

@polaz polaz commented Nov 17, 2026 •

Copy link
Copy Markdown
Member

Summary

  • The four-stream Huffman literal loop runs in an assembly loop of upstream's shape on x86_64 with BMI2 and 64-bit pointers, for the 10- and 11-bit tables that carry most literals; elsewhere (the x32 ABI included) the Rust loop runs, now with pointer cursors and a per-batch bound.
  • Decoding z000033 at level 3 runs 711K fewer instructions a frame (8.20G against 8.63G over 600 frames, libzstd 8.00G), and the literal stage takes about 3% fewer cycles. On Broadwell the wall-clock A/B is within noise; the dashboard's hosts decide the effect on the decompress / decodecorpus-z000033 cells.

Changes

  • run_4stream_burst_loop carries each stream as an input and an output pointer, and computes once per batch how many iterations pass both the output and the input budget (upstream's olimit), so the loop body tests one pointer. The batch admits exactly the iterations the per-iteration gates did.
  • burst_batch_asm: five symbols per stream an iteration, four instructions a symbol. The symbol store writes a high byte register, so the output pointers sit in registers without a REX prefix; streams 1-3 keep their input pointers in memory, read into a register before each reload.
  • CpuKernel::HAS_BMI2 (x86_64 with 64-bit pointers) marks the tiers that guarantee BMI2.

Testing

Tests, clippy and formatting pass on x86_64 (AVX2 host) and aarch64; output is unchanged. A test decodes 10- and 11-bit literal sections on every tier the running CPU supports against the scalar tier.

Part of #678

Summary by CodeRabbit

  • Performance
    • Improved decompression speed on supported 64-bit x86 processors for certain Huffman-encoded data.
    • Other processor types and decoding paths continue to use their existing behavior.

The four-stream Huffman burst could not keep its streams in
registers: the symbol store writes a high byte register, which pins
the table entry to rax-rdx, so the allocator put the output pointers
on the stack and reloaded them on every symbol, and three Rust layouts
of the loop left its time unchanged.

- The burst carries each stream as an input and an output pointer and
  works out once per batch how many iterations pass both the output
  and the input budget, upstream's olimit shape, so the loop tests one
  pointer.
- On x86_64 with BMI2 and a 10- or 11-bit table each batch runs in an
  assembly loop of upstream's shape (huf_decompress_amd64.S): outputs
  in registers without a REX prefix, streams 1-3's input pointers in
  memory and stepped in a register, four instructions a symbol.
- CpuKernel gains HAS_BMI2 for the tiers that guarantee it.

Output is unchanged. Decoding z000033 at level 3 runs 711K fewer
instructions a frame, close to libzstd's count; the literal stage's
cycles fall by about 3%, below the wall-clock noise of an A/B on
Broadwell.

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-17T07:54:00.247942Z 583d110 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 change adds an x86_64 BMI2 capability flag to CpuKernel and updates four-stream Huffman decoding to use bounded assembly batches for eligible kernels and table sizes. A new test compares decoding output and consumed lengths across applicable CPU tiers.

Changes: Four-stream Huffman decoding

Layer / File(s) Summary
BMI2 kernel capability
zstd/src/cpu_kernel.rs
CpuKernel adds an x86_64, 64-bit-pointer HAS_BMI2 constant that defaults to false. The BMI2, AVX2, and VBMI2 kernels set it to true.
Bounded Huffman batch decoding
zstd/src/decoding/literals_section_decoder.rs, zstd/src/decoding/literals_section_decoder/burst_gate_tests.rs
The decoder tracks stream and output cursors with pointers, bounds batches by output capacity and safe input-reload budgets, and uses assembly for eligible x86_64 BMI2 kernels with 10- or 11-bit tables. Other cases retain scalar bursts. Tests compare applicable CPU tiers against scalar decoding.

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

Change: Refactor

Sequence Diagram(s)

sequenceDiagram
  participant Decoder as Huffman decoder
  participant CpuKernel
  participant Batch as burst_batch_asm
  Decoder->>CpuKernel: Check HAS_BMI2 and table width
  CpuKernel-->>Decoder: Return kernel capability
  Decoder->>Batch: Decode bounded batch when eligible
  Batch-->>Decoder: Update stream state and pointers
Loading

Suggested reviewers: rowang077

Merge Risk: Minimal · up to 583d1

This change speeds up four-stream Huffman literal decoding on x86_64 CPUs with BMI2 and leaves other configurations on the existing path. No merge-blocking risk was identified.

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 four-stream Huffman literal decoding loop moves to hand-written assembly. The changeset matches this. The summary shows a new assembly decoder for x86_64 with BMI2…
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 main change: the four-stream Huffman literal decoding loop moves to hand-written assembly. The changeset matches this. The summary shows a new assembly decoder for x86_64 with BMI2 and 10- or 11-bit tables, a pointer-based burst loop, a HAS_BMI2 constant, and a tier comparison test. The conventional commit prefix 'perf(decoding)' also fits a performance change in the decoding module. The title does not mention the x86_64/BMI2 scope, but it is specific enough for a teammate scanning history.

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 84e7511 and 583d110.

Files selected for processing: 3

  • zstd/src/cpu_kernel.rs
  • zstd/src/decoding/literals_section_decoder.rs
  • zstd/src/decoding/literals_section_decoder/burst_gate_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: 84e75118c1

ℹ️ 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 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 assembly loop addresses its slots as eight-byte words, which the
  x32 ABI (x86_64 with four-byte pointers) does not have: there the
  slot array is 20 bytes and the loop would read past it and combine
  two pointers into one. The loop and HAS_BMI2 are now built only for
  64-bit pointers; x32 takes the Rust loop. No x32 target is built or
  run anywhere, so no test can go red for it; the gate is checked by
  compilation.
- A test decodes 4-stream literal sections with 10- and 11-bit tables,
  the widths the assembly loop takes, on every tier the running CPU
  supports and holds each to the scalar tier byte for byte.

Part of #678
@polaz
polaz enabled auto-merge (squash) November 17, 2026 08:09
@polaz
polaz merged commit 93997bd into main Nov 17, 2026
36 checks passed
@polaz
polaz deleted the perf/#678-z33-decode branch November 17, 2026 08:16
@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