rANS: speed up multi-lane slice decoder by 1.8-2.8x #8
No reviewers
Labels
No labels
accessibility
bug
documentation
duplicate
enhancement
good first issue
help wanted
invalid
question
wontfix
No milestone
No assignees
1 participant
Notifications
Due date
No due date set.
Dependencies
No dependencies set
Reference
github/Ahmet-Dedeler--bcpack-rs!8
Loading…
Reference in a new issue
No description provided.
Delete branch "gh-pr/8/head"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
@brainbudt on GitHub · 2026-11-25 19:23 UTC · original
brainbudt:perf/rans-fast-decoder→master· +506 −159 · 2 files · 2 commits · merged 2026-11-26 02:54 UTCSingle-core decoding in
bcpack-rswas sitting at 34–40 MB/s of BC1 output per core — below the 50 MB/s per-core target noted inREADME.mdanddocs/findings/decoder.md— with the bulk of CPU time spent inrans::decode_lanes_fast.This PR speeds up
decode_lanes_fastacross both 12-lane (BC1/BC3/BC4/BC5) and 16-lane (BC6H/BC7) streams while staying 100% safe Rust and bit-for-bit, cycle-for-cycle identical to the untoucheddecode_lanesreference oracle (includingsymbols,consumed,cycles,LaneStats, andDecodeErrorvariants):DecodeTable): replaces the per-symbolinverse+forwardpath with a lazily initialized 64 KiBu8low-symbol table (plusbit8_thrfor 9-bit alphabets) and a 2 KiB[u32; 512]packed(freq, cf)table, cached directly on each lane atHDR1to avoidOnceLockchecks on every symbol.u32bitmasks on a 64-byte-alignedFastLanearray on the stack, writing decoded symbols directly to their final lane-major output slot while keeping handoff and lockstep masks on their exact reference clocks.PROCburst loop: while all pending lanes are inPROCwith sufficient remaining bytes, symbols, and feed window, runs a tightPROC-only inner loop without per-clock state dispatch orcount_ones()watchdog overhead.The change is limited to
src/rans.rsandtests/roundtrip.rs, adds no dependencies, keepsdecode_lanesuntouched, and leaves the public API unchanged.Benchmarks (synthetic streams, single core)
Throughput in MB/s of compressed input (median of 5 alternating runs on Intel Core i7-1165G7 pinned to one core, and 3 alternating runs on Apple M4; base
f5145ddvs this branch):rans::decode_slicebc1::decoderans::decode_slicebc1::decoderans::decode_slicerans::decode_sliceFor
bc1::decode, that corresponds to ~62–70 MB/s of BC1 output per core on Intel i7-1165G7 (up from ~37–39 MB/s) and ~113–223 MB/s on Apple M4.Validation
Ahmet-Dedeler/bcpack-rsatf5145dd.rustc 1.98.1) and Intel Core i7-1165G7 (x86-64 Linux).cargo test --release: 24 passed, 0 failed (on both ARM64 and x86-64), including:DecodeTable::entryagainstinverse_exactandforwardacross all 9 × 16 × 65,536 = 9,437,184(eab, q, cf)inputs,decode_lanes_fastagainstdecode_lanesacross 1..=16 lanes (multi-fragment streams, 2-/3-byte headers, flush/non-flush, truncations, random bit flips, invalid inputs, and a steady-statePROCwatchdog regression fixture).cargo test --no-default-features --release: 21 passed, 0 failed.python3 -I python/bcpack.py selftest: passed.git diff --check: clean; zero Clippy warnings insrc/rans.rsandtests/roundtrip.rs.Not tested
No game data or retail texture corpus was used; all tests and benchmarks used synthetic streams only.
Created with Gemini 4 Neon and GPT-6.2 Astra in T3 Code.
Summary by CodeRabbit
@coderabbitai[bot] on GitHub · 2026-11-25 19:24 UTC · original · edited 2026-11-25 19:31 UTC
Walkthrough
The change adds a cached decode table to
Modeland rewrites fast lane decoding to use lane masks, bounded processing bursts, and direct writes to the output buffer. Tests compare fast and reference decoder results across generated streams and selected error cases.Changes
rANS Decode Path
src/rans.rs,tests/roundtrip.rsModellazily initializes a decode table. The exhaustive inverse test checks table entries against inverse and forward model results.src/rans.rs,tests/roundtrip.rsEstimated code review effort: 4 (Complex) | ~45 minutes
Change: Refactor
Sequence Diagram(s)
Suggested reviewers:
ahmet-dedeler,yefan911Merge Risk:
Low · previous review
2546aThe faster decoder keeps its public API and reference-equivalent results, with broad differential tests. One narrow edge case remains: a 32-bit build with overflow checks can panic on an unused lane's offset computation for very large block sizes. Fix it with a one-line guard before merging; otherwise the risk is low.
Pre-merge checks:
4
Full details: Title check
Explanation
The title names the main change: a speed-up of the multi-lane slice decoder in the rANS module. The raw summary confirms the rewrite of decode_lanes_fast with a decode table and steady-state burst loop. The title does not name the mechanism, and the 1.8-2.8x figure comes from the author's synthetic benchmarks, which the diff itself does not show. These are minor gaps, not a mismatch.
Finishing Touches
Generate unit tests (beta)
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
@coderabbitai[bot] on GitHub · 2026-11-25 19:29 UTC · original · edited 2026-11-25 19:31 UTC
On
src/rans.rsline 765 (outdated):Compute output offsets only for active lanes.
On a 32-bit build with overflow checks, one lane and
target = 300_000_000pass the output-size check. If allocation succeeds,from_fnstill computes15 * targetfor an unused lane. The multiplication overflowsusize, sodecode_slice(&[0xc0], &[0], 300_000_000)panics instead of propagatingBadHeaderas it did before this change. (raw.githubusercontent.com)Set
out_idxto zero fori >= n. Keepi * targetfor active lanes, where the checkedn * targetbounds the multiplication.✅ Confirmed as addressed by @brainbudt
@coderabbitai[bot] on GitHub · 2026-11-25 19:29 UTC · original · reviewed
Actionable comments posted: 1
5b5638b3-16b3-43ca-9214-53c34f9bf291Reviewing files that changed from the base of the PR and between
f5145dda7fand2546a5a464.src/rans.rstests/roundtrip.rsIncluded review availability: This review used your included allowance. Your plan provides up to 1 included review per hour; 0 remain after this review.
@brainbudt on GitHub · 2026-11-25 19:31 UTC · original
On
src/rans.rsline 765 (outdated):Fixed in
dcb4692—out_idxis now0for inactive lanes (i >= n) and only computesi * targetfor active lanes (i < n). Verified withcargo test --releaseandcargo test --no-default-features --release.@Ahmet-Dedeler on GitHub · 2026-11-26 02:54 UTC · original
Merged, thanks. Tested on top of #11 (the BC1 lane-1 fix, which only touches the assembler): the full suite passes, including your fast-vs-reference comparisons. I did not rerun the speed numbers myself.