perf(bcpack): reuse thread-local BC6H and BC7 history rings across decode calls #4148

Open
github-bot wants to merge 1 commit from github-bot/gh-4148 into main AGit
Owner

@⁠Andreww2009 on GitHub · 2026-11-30 16:48 UTC · original
Andreww2009:perf/bcpack-thread-local-history-ring → main · +45 −6 · 1 files · 1 commits · open

Summary

Replace per-call heap allocations of the 256-entry BC7 (~21.5 KiB) and BC6H (~11 KiB) history ring buffers in crates/victory-formats/src/bcpack.rs with reusable thread_local! RefCell<Box<[..., HISTORY]>> buffers.

Problem

decode_bc7 and decode_bc6 called alloc_boxed_array::<..., HISTORY>() on every texture decode request. Because DirectStorage streaming dispatches thousands of small compressed BC6H/BC7 texture chunks on worker threads, each decode paid a heap allocation, ~11–21.5 KiB zero-initialization, and free on every call.

Root Cause

The history ring was freshly heap-allocated and zero-initialized inside decode_bc7 and decode_bc6 even though pick() only ever reads slots (i - r) & (HISTORY - 1) where 1 <= r <= i (slots already written earlier in the same decode call; r == 0 returns zero, and r > i reads dictionary).

Implementation

  • Store one Box<[(Bc7Ep, Bc7Ix); HISTORY]> in BC7_HISTORY and one Box<[(Bc6Ep, Bc6Ix); HISTORY]> in BC6_HISTORY per thread (thread_local! RefCell), allocated lazily once per worker thread via alloc_boxed_array().
  • Delegate decode_bc7 / decode_bc6 to decode_bc7_with / decode_bc6_with borrowing the thread-local ring (&mut [_; HISTORY]).
  • Zero unsafe code; zero cross-thread contention.

Testing

  • cargo clippy -p victory-formats --all-targets --locked -- -D warnings: PASS (0 warnings).
  • cargo test --locked -p victory-formats: 85 passed, 0 failed, 7 ignored (including all BC1–BC7 oracle parity tests and synthetic goldens).
  • cargo xtask policy --staged: 1 files ok.

Performance Before and After

  • Heap allocations & zero-fill per BC6H / BC7 decode call: 1 allocation (~11–21.5 KiB zero-fill) -> 0 allocations, 0 zero-fill after thread warmup.
  • cargo test --release --locked -p victory-formats bcpack_throughput -- --ignored --nocapture on Intel Core Ultra 9 275HX: BC1 210.7 MB/s, BC7 123.6 MB/s, builtin BC6 34.2 MB/s (vs 33.7 MB/s), builtin BC7 34.9 MB/s (vs 34.8 MB/s), with the largest savings on small DirectStorage streaming chunks where allocation/zeroing dominates.

Hardware Configuration

  • CPU: Intel Core Ultra 9 275HX (8P+16E, 24 logical)
  • RAM: 64 GB DDR5-4800
  • OS: Windows 11 x64 (build 26200)

Compatibility

Zero-dependency victory-formats crate; works identically across all platforms and CPU architectures.

Risks and Limitations

Retains ~32.5 KiB of thread-local ring storage per thread that decodes both BC6H and BC7, released automatically when the thread exits.

  • Searched open pull requests and main (84bd61e2). Cleanly builds on top of merged #2092 without overlapping any open BCPack PR.
**@⁠Andreww2009** on GitHub · 2026-11-30 16:48 UTC · [original](https://github.com/trustno1-coder/VIctory-NVIDIA/pull/4148) `Andreww2009:perf/bcpack-thread-local-history-ring` → `main` · +45 −6 · 1 files · 1 commits · open ## Summary Replace per-call heap allocations of the 256-entry BC7 (~21.5 KiB) and BC6H (~11 KiB) history ring buffers in `crates/victory-formats/src/bcpack.rs` with reusable `thread_local!` `RefCell<Box<[..., HISTORY]>>` buffers. ## Problem `decode_bc7` and `decode_bc6` called `alloc_boxed_array::<..., HISTORY>()` on every texture decode request. Because DirectStorage streaming dispatches thousands of small compressed BC6H/BC7 texture chunks on worker threads, each decode paid a heap allocation, ~11–21.5 KiB zero-initialization, and free on every call. ## Root Cause The history ring was freshly heap-allocated and zero-initialized inside `decode_bc7` and `decode_bc6` even though `pick()` only ever reads slots `(i - r) & (HISTORY - 1)` where `1 <= r <= i` (slots already written earlier in the same decode call; `r == 0` returns `zero`, and `r > i` reads `dictionary`). ## Implementation - Store one `Box<[(Bc7Ep, Bc7Ix); HISTORY]>` in `BC7_HISTORY` and one `Box<[(Bc6Ep, Bc6Ix); HISTORY]>` in `BC6_HISTORY` per thread (`thread_local! RefCell`), allocated lazily once per worker thread via `alloc_boxed_array()`. - Delegate `decode_bc7` / `decode_bc6` to `decode_bc7_with` / `decode_bc6_with` borrowing the thread-local ring (`&mut [_; HISTORY]`). - Zero `unsafe` code; zero cross-thread contention. ## Testing - `cargo clippy -p victory-formats --all-targets --locked -- -D warnings`: **PASS** (0 warnings). - `cargo test --locked -p victory-formats`: **85 passed, 0 failed, 7 ignored** (including all BC1–BC7 oracle parity tests and synthetic goldens). - `cargo xtask policy --staged`: **1 files ok**. ## Performance Before and After - Heap allocations & zero-fill per BC6H / BC7 decode call: **1 allocation (~11–21.5 KiB zero-fill) -> 0 allocations, 0 zero-fill** after thread warmup. - `cargo test --release --locked -p victory-formats bcpack_throughput -- --ignored --nocapture` on Intel Core Ultra 9 275HX: `BC1 210.7 MB/s`, `BC7 123.6 MB/s`, `builtin BC6 34.2 MB/s` (vs `33.7 MB/s`), `builtin BC7 34.9 MB/s` (vs `34.8 MB/s`), with the largest savings on small DirectStorage streaming chunks where allocation/zeroing dominates. ## Hardware Configuration - CPU: Intel Core Ultra 9 275HX (8P+16E, 24 logical) - RAM: 64 GB DDR5-4800 - OS: Windows 11 x64 (build 26200) ## Compatibility Zero-dependency `victory-formats` crate; works identically across all platforms and CPU architectures. ## Risks and Limitations Retains ~32.5 KiB of thread-local ring storage per thread that decodes both BC6H and BC7, released automatically when the thread exits. ## Related Issues / Overlap Check - [x] Searched open pull requests and `main` (`84bd61e2`). Cleanly builds on top of merged #2092 without overlapping any open BCPack PR.
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-30 16:51:53 +00:00
Author
Owner

@⁠babuakhil on GitHub · 2026-11-30 17:02 UTC · original

Checked on Windows 11, AMD Ryzen 7 5700X (Rust 1.98.1, pinned), applied on main (84bd61e2); cherry-picks cleanly. cargo clippy -p victory-formats --all-targets --locked -- -D warnings is clean and cargo test -p victory-formats gives 85 passed, 0 failed, 7 ignored.

The reuse can't change the output. I wanted to check the case the description doesn't cover: the BC7 ring slot is only partly overwritten. rows[1..] are written only for blocks with that many subsets, and ix[0]/ix[1] only when bits[k] != 0. So with a reused ring, those fields hold another texture's data rather than zeros. That's safe here, because:

  • rows[r1]/rows[r2] are read only when common >= 2/>= 3, which is capped by the referenced block's own subset count (r.ctrl >> 6), so they were written by that block;
  • decode_index_set ignores ref_ix when ref_bits == 0, and bits, ctrl and rows[0] are written for every block;
  • the ? and return Err exits only end the call, and every block that a later block of the same call can reference has already written its slot.

To test that rather than rely on reading it, a throwaway test (not part of this PR) built 48 valid BC6H and BC7 streams (builtin and synthetic tables; 1 to 2000 blocks; single and multi-fragment). It decoded them on 8 threads, 6 shuffled passes each, and compared every result with ref_oracle, which starts with fresh history. Before many of them, the same thread decoded random noise, or a valid stream of another case with a flipped byte or with more blocks asked for than it holds, so that the ring was left part-written by a decode that stopped partway.

main this PR
decodes checked against ref_oracle 2304, 0 mismatches 2304, 0 mismatches
decodes in between that left the ring part-written (failed) 2307 (1958) 2307 (1958)

Speed, release build, best of 5, two alternating runs each (ns per decode call, builtin tables):

blocks BC6H main BC6H this PR BC7 main BC7 this PR
4 2357 / 2343 1895 / 1945 3282 / 3709 3142 / 3124
16 5613 / 5215 4685 / 4972 8597 / 7377 7542 / 5813
64 and more within noise within noise within noise within noise

So it saves about 0.3 to 0.5 µs a call, which is what the allocation and zeroing cost. That's visible on small chunks and lost in the noise from a few hundred blocks up, consistent with the description.

**@⁠babuakhil** on GitHub · 2026-11-30 17:02 UTC · [original](https://github.com/trustno1-coder/VIctory-NVIDIA/pull/4148#issuecomment-7285636329) Checked on Windows 11, AMD Ryzen 7 5700X (Rust 1.98.1, pinned), applied on `main` (`84bd61e2`); cherry-picks cleanly. `cargo clippy -p victory-formats --all-targets --locked -- -D warnings` is clean and `cargo test -p victory-formats` gives 85 passed, 0 failed, 7 ignored. **The reuse can't change the output.** I wanted to check the case the description doesn't cover: the BC7 ring slot is only partly overwritten. `rows[1..]` are written only for blocks with that many subsets, and `ix[0]`/`ix[1]` only when `bits[k] != 0`. So with a reused ring, those fields hold another texture's data rather than zeros. That's safe here, because: - `rows[r1]`/`rows[r2]` are read only when `common >= 2`/`>= 3`, which is capped by the referenced block's own subset count (`r.ctrl >> 6`), so they were written by that block; - `decode_index_set` ignores `ref_ix` when `ref_bits == 0`, and `bits`, `ctrl` and `rows[0]` are written for every block; - the `?` and `return Err` exits only end the call, and every block that a later block of the same call can reference has already written its slot. To test that rather than rely on reading it, a throwaway test (not part of this PR) built 48 valid BC6H and BC7 streams (builtin and synthetic tables; 1 to 2000 blocks; single and multi-fragment). It decoded them on 8 threads, 6 shuffled passes each, and compared every result with `ref_oracle`, which starts with fresh history. Before many of them, the same thread decoded random noise, or a valid stream of another case with a flipped byte or with more blocks asked for than it holds, so that the ring was left part-written by a decode that stopped partway. | | `main` | this PR | |---|---|---| | decodes checked against `ref_oracle` | 2304, 0 mismatches | 2304, 0 mismatches | | decodes in between that left the ring part-written (failed) | 2307 (1958) | 2307 (1958) | **Speed**, release build, best of 5, two alternating runs each (ns per `decode` call, builtin tables): | blocks | BC6H `main` | BC6H this PR | BC7 `main` | BC7 this PR | |---|---|---|---|---| | 4 | 2357 / 2343 | 1895 / 1945 | 3282 / 3709 | 3142 / 3124 | | 16 | 5613 / 5215 | 4685 / 4972 | 8597 / 7377 | 7542 / 5813 | | 64 and more | within noise | within noise | within noise | within noise | So it saves about 0.3 to 0.5 µs a call, which is what the allocation and zeroing cost. That's visible on small chunks and lost in the noise from a few hundred blocks up, consistent with the description.
github-bot force-pushed github-bot/gh-4148 from d7fa7833a8 to f10cb395f3 2026-12-01 09:51:30 +00:00 Compare
This pull request can be merged automatically.
This branch is out-of-date with the base branch
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/4148/head:github-bot/gh-4148'
git switch 'github-bot/gh-4148'

Merge

Merge the changes and update on Forgejo.
git switch 'main'
git merge --no-ff 'github-bot/gh-4148'
git switch 'github-bot/gh-4148'
git rebase 'main'
git switch 'main'
git merge --ff-only 'github-bot/gh-4148'
git switch 'github-bot/gh-4148'
git rebase 'main'
git switch 'main'
git merge --no-ff 'github-bot/gh-4148'
git switch 'main'
git merge --squash 'github-bot/gh-4148'
git switch 'main'
git merge --ff-only 'github-bot/gh-4148'
git switch 'main'
git merge 'github-bot/gh-4148'
git push origin 'main'
This discussion has been locked. Commenting is limited to contributors.
No reviewers
No milestone
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set

Reference
github/trustno1-coder--VIctory-NVIDIA!4148
No description provided.