perf(formats): BCPack decodes a payload symbol without branching on bucket 0 #566

Open
github-bot wants to merge 1 commit from github-bot/gh-566 into a-publier AGit
Owner

@⁠IonCojucari on GitHub · 2026-11-27 11:41 UTC · original
IonCojucari:perf/bcpack-branchless-symbol → a-publier · +46 −22 · 1 files · 1 commits · open

What

Each rANS payload step in bcpack::Lane::step read the model's frequencies, starts and reciprocals from three separate tables. It then branched on whether the slot fell in bucket 0 (symbol 0), which is decided by the data and so mispredicts often.

Bucket 0 is now one more entry in a per-bucket decode table: start 1, frequency f[0] - 1, reciprocal 0. Every bucket goes through the same branch-free formula with a single lookup:

let d = &t.decode[model][b];
let offset = slot.wrapping_sub(d.start);          // bucket 0: slot - 1, wrapping at slot 0 as before
let q = ((u64::from(offset) * d.recip) >> 32) as u32; // bucket 0: q = 0
let rem = offset.wrapping_sub(q * d.freq);
self.x = (x >> 16).wrapping_mul(d.freq).wrapping_add(rem);
self.out = Some((d.first + q) as u8);

The freq and recip fields of Tables are gone; start and the window table stay because bucket_of uses them. This decoder runs on the DirectStorage workers for every BCPack request, so the speed-up applies to texture streaming and loading.

Measurements

cargo test --release -p victory-formats bcpack_throughput -- --ignored --nocapture, synthetic streams (no game data), three alternating old/new runs on one Linux machine:

before (ns/block) after (ns/block)
BC1 185–200 166–172
BC4 185–189 173–181 (one 216 outlier)
BC7 626–653 580–616

Timed alone, the lane loop is 15–20% faster (BC1 165 → 134 ns/block, BC7 433 → 373). These figures come from synthetic streams. Real title data has different symbol statistics, so the gain on real data may differ.

Verification

  • cargo test -p victory-formats, debug and release: 41 passed. This includes the exhaustive payload_step_matches_the_reference_decode (every model, every slot, both table sets). Its reference decode now rebuilds the frequencies from the bucket starts, so it does not depend on the new table.
  • Differential check (run locally, not committed): the old and new decoders gave byte-identical output and identical errors on 6,006 random, noisy and valid streams covering BC1/3/4/5/6/7 with the shipped and synthetic tables.
  • cargo clippy -p victory-formats --all-targets --all-features -- -D warnings is clean, both on Linux and with --target x86_64-pc-windows-msvc. rustfmt is clean on the file.
  • Not done: a run on Windows with the game. Only victory-formats changes, and it has no platform-specific code.

Also tried, not included

  • Looking up the bucket with a branch-free count over the starts instead of the window table: about 35% slower without AVX.
  • Walking texture::Layout::detile/tile one block column at a time: faster for 64 KiB modes but slower for R8 and 4 KiB modes once the offset fix it needed was in, so no clear win.

🤖 Generated with Claude Code

**@⁠IonCojucari** on GitHub · 2026-11-27 11:41 UTC · [original](https://github.com/Belobug/VIctory-Belobug-AMD/pull/566) `IonCojucari:perf/bcpack-branchless-symbol` → `a-publier` · +46 −22 · 1 files · 1 commits · open ## What Each rANS payload step in `bcpack::Lane::step` read the model's frequencies, starts and reciprocals from three separate tables. It then branched on whether the slot fell in bucket 0 (symbol 0), which is decided by the data and so mispredicts often. Bucket 0 is now one more entry in a per-bucket decode table: start 1, frequency `f[0] - 1`, reciprocal 0. Every bucket goes through the same branch-free formula with a single lookup: ```rust let d = &t.decode[model][b]; let offset = slot.wrapping_sub(d.start); // bucket 0: slot - 1, wrapping at slot 0 as before let q = ((u64::from(offset) * d.recip) >> 32) as u32; // bucket 0: q = 0 let rem = offset.wrapping_sub(q * d.freq); self.x = (x >> 16).wrapping_mul(d.freq).wrapping_add(rem); self.out = Some((d.first + q) as u8); ``` The `freq` and `recip` fields of `Tables` are gone; `start` and the window table stay because `bucket_of` uses them. This decoder runs on the DirectStorage workers for every BCPack request, so the speed-up applies to texture streaming and loading. ## Measurements `cargo test --release -p victory-formats bcpack_throughput -- --ignored --nocapture`, synthetic streams (no game data), three alternating old/new runs on one Linux machine: | | before (ns/block) | after (ns/block) | |---|---|---| | BC1 | 185–200 | 166–172 | | BC4 | 185–189 | 173–181 (one 216 outlier) | | BC7 | 626–653 | 580–616 | Timed alone, the lane loop is 15–20% faster (BC1 165 → 134 ns/block, BC7 433 → 373). These figures come from synthetic streams. Real title data has different symbol statistics, so the gain on real data may differ. ## Verification - `cargo test -p victory-formats`, debug and release: 41 passed. This includes the exhaustive `payload_step_matches_the_reference_decode` (every model, every slot, both table sets). Its reference decode now rebuilds the frequencies from the bucket starts, so it does not depend on the new table. - Differential check (run locally, not committed): the old and new decoders gave byte-identical output and identical errors on 6,006 random, noisy and valid streams covering BC1/3/4/5/6/7 with the shipped and synthetic tables. - `cargo clippy -p victory-formats --all-targets --all-features -- -D warnings` is clean, both on Linux and with `--target x86_64-pc-windows-msvc`. rustfmt is clean on the file. - Not done: a run on Windows with the game. Only victory-formats changes, and it has no platform-specific code. ## Also tried, not included - Looking up the bucket with a branch-free count over the starts instead of the window table: about 35% slower without AVX. - Walking `texture::Layout::detile`/`tile` one block column at a time: faster for 64 KiB modes but slower for R8 and 4 KiB modes once the offset fix it needed was in, so no clear win. 🤖 Generated with [Claude Code](https://claude.com/claude-code)
Each rANS step looked up the model's frequencies, starts and reciprocals
in three tables and branched on whether the slot fell in bucket 0
(symbol 0), a branch the data decides. Bucket 0 is now one more entry of
a per-bucket decode table (start 1, frequency f[0] - 1, reciprocal 0),
so every bucket takes the same branch-free formula and one lookup.

Synthetic streams (bcpack_throughput, three alternating A/B runs on one
Linux machine): BC1 186-200 -> 170-172 ns/block, BC4 185-189 ->
173-177, BC7 626-653 -> 587-616. The lane loop alone is 15-20% faster.
Output and errors are byte-identical to the previous decoder on 6,006
random, noisy and valid streams of every format and both table sets.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-27 11:54:48 +00:00
Author
Owner

@⁠WellyngtonSantos on GitHub · 2026-11-27 13:08 UTC · original

Measured on an older CPU: Intel i7-2600 (Sandy Bridge, no AVX2), Debian 13, Rust 1.98.1, head 9b9cee9 against a-publier 4eb6cfd.

  • cargo test --release -p victory-formats: 41 passed, 0 failed, 5 ignored on both.
  • bcpack_throughput (--ignored --nocapture): both test binaries built once, then run alternately 15 times each. The machine had other load, so the minimum is the figure to read:
ns/block base min / median this PR min / median min ratio
BC1 206 / 215 191 / 204 1.08×
BC4 208 / 216 191 / 200 1.09×
BC7 782 / 830 734 / 753 1.07×

So the branch-free bucket lookup is also faster on a 2011 core, by about 7–9% end to end, which is in line with your Linux numbers. Same caveat as yours: these are synthetic streams.

**@⁠WellyngtonSantos** on GitHub · 2026-11-27 13:08 UTC · [original](https://github.com/Belobug/VIctory-Belobug-AMD/pull/566#issuecomment-7219102373) Measured on an older CPU: **Intel i7-2600 (Sandy Bridge, no AVX2), Debian 13, Rust 1.98.1**, head `9b9cee9` against `a-publier` `4eb6cfd`. - `cargo test --release -p victory-formats`: **41 passed, 0 failed, 5 ignored** on both. - `bcpack_throughput` (`--ignored --nocapture`): both test binaries built once, then run alternately 15 times each. The machine had other load, so the minimum is the figure to read: | ns/block | base min / median | this PR min / median | min ratio | |---|---|---|---| | BC1 | 206 / 215 | **191** / 204 | 1.08× | | BC4 | 208 / 216 | **191** / 200 | 1.09× | | BC7 | 782 / 830 | **734** / 753 | 1.07× | So the branch-free bucket lookup is also faster on a 2011 core, by about 7–9% end to end, which is in line with your Linux numbers. Same caveat as yours: these are synthetic streams.
Author
Owner

@⁠gw1337x on GitHub · 2026-11-27 13:10 UTC · original

In-game check on RDNA 3. Memory Integrity was off.

PC: Ryzen 7 9800X3D, RX 7800 XT, 32 GB. Test: 270 s hands-off boat scene, RT off, Belobug launcher settings plus the 5 port switches, warm caches. #566 (9b9cee9) on 4eb6cfd vs 4eb6cfd, interleaved with base.

#566 run 1 #566 run 2 base run 1 base run 2
World on screen (first second with 1,000+ draws, from log start) 44.2 s 43.9 s 43.8 s 44.5 s
Steady fps (log, last minute) 28.6 28.7 28.6 28.6
Device loss 0 0 0 0

No change in load time or fps in this scene. Decode time itself isn't logged, so this doesn't measure the per-block gain. It only shows that the load in this scene doesn't get shorter. Pictures weren't compared.

AI-assisted (Claude Code); numbers from the run logs.

**@⁠gw1337x** on GitHub · 2026-11-27 13:10 UTC · [original](https://github.com/Belobug/VIctory-Belobug-AMD/pull/566#issuecomment-7219141417) In-game check on RDNA 3. Memory Integrity was off. PC: Ryzen 7 9800X3D, RX 7800 XT, 32 GB. Test: 270 s hands-off boat scene, RT off, Belobug launcher settings plus the 5 port switches, warm caches. #566 (`9b9cee9`) on `4eb6cfd` vs `4eb6cfd`, interleaved with base. | | #566 run 1 | #566 run 2 | base run 1 | base run 2 | |---|---|---|---|---| | World on screen (first second with 1,000+ draws, from log start) | 44.2 s | 43.9 s | 43.8 s | 44.5 s | | Steady fps (log, last minute) | 28.6 | 28.7 | 28.6 | 28.6 | | Device loss | 0 | 0 | 0 | 0 | No change in load time or fps in this scene. Decode time itself isn't logged, so this doesn't measure the per-block gain. It only shows that the load in this scene doesn't get shorter. Pictures weren't compared. AI-assisted (Claude Code); numbers from the run logs.
Author
Owner

@⁠MamaLeVrai on GitHub · 2026-11-27 15:56 UTC · original

Tested on an RX 6950 XT (RDNA 2), driver 32.0.21045.29003, Windows 11 build 26300, Rust 1.98.1, release. At 9b9cee9: victory-formats 41 passed, 0 failed, 5 ignored.

AI-assisted (Claude Code); no game data.

**@⁠MamaLeVrai** on GitHub · 2026-11-27 15:56 UTC · [original](https://github.com/Belobug/VIctory-Belobug-AMD/pull/566#issuecomment-7222662613) Tested on an RX 6950 XT (RDNA 2), driver 32.0.21045.29003, Windows 11 build 26300, Rust 1.98.1, release. At `9b9cee9`: `victory-formats` 41 passed, 0 failed, 5 ignored. AI-assisted (Claude Code); no game data.
This pull request can be merged automatically.
You are not authorized to merge this pull request.
View command line instructions

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u origin '+refs/pull/566/head:github-bot/gh-566'
git switch 'github-bot/gh-566'

Merge

Merge the changes and update on Forgejo.
git switch 'a-publier'
git merge --no-ff 'github-bot/gh-566'
git switch 'github-bot/gh-566'
git rebase 'a-publier'
git switch 'a-publier'
git merge --ff-only 'github-bot/gh-566'
git switch 'github-bot/gh-566'
git rebase 'a-publier'
git switch 'a-publier'
git merge --no-ff 'github-bot/gh-566'
git switch 'a-publier'
git merge --squash 'github-bot/gh-566'
git switch 'a-publier'
git merge --ff-only 'github-bot/gh-566'
git switch 'a-publier'
git merge 'github-bot/gh-566'
git push origin 'a-publier'
This discussion has been locked. Commenting is limited to contributors.
No description provided.