perf(bcpack): reuse thread-local BC6H and BC7 history rings across decode calls #4148
No reviewers
Labels
No labels
AMD
audio
bug
confirmed
crash
cutscene
dlss
documentation
duplicate
enhancement
fixed-on-main
frame-generation
good first issue
help wanted
input
INTEL
invalid
lighting
linux-proton
memory
needs-info
needs-logs
needs-rebase
NVIDIA
performance
pr-diagnostics
pr-docs
pr-feature
pr-fix
pr-performance
pr-refactor
pr-tests
question
RAYTRACING
rendering
report
series-s
shaders
spam
startup
upstream
wontfix
No milestone
No assignees
1 participant
Notifications
Due date
No due date set.
Dependencies
No dependencies set
Reference
github/trustno1-coder--VIctory-NVIDIA!4148
Loading…
Reference in a new issue
No description provided.
Delete branch "github-bot/gh-4148"
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?
@Andreww2009 on GitHub · 2026-11-30 16:48 UTC · original
Andreww2009:perf/bcpack-thread-local-history-ring→main· +45 −6 · 1 files · 1 commits · openSummary
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.rswith reusablethread_local!RefCell<Box<[..., HISTORY]>>buffers.Problem
decode_bc7anddecode_bc6calledalloc_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_bc7anddecode_bc6even thoughpick()only ever reads slots(i - r) & (HISTORY - 1)where1 <= r <= i(slots already written earlier in the same decode call;r == 0returnszero, andr > ireadsdictionary).Implementation
Box<[(Bc7Ep, Bc7Ix); HISTORY]>inBC7_HISTORYand oneBox<[(Bc6Ep, Bc6Ix); HISTORY]>inBC6_HISTORYper thread (thread_local! RefCell), allocated lazily once per worker thread viaalloc_boxed_array().decode_bc7/decode_bc6todecode_bc7_with/decode_bc6_withborrowing the thread-local ring (&mut [_; HISTORY]).unsafecode; 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
cargo test --release --locked -p victory-formats bcpack_throughput -- --ignored --nocaptureon Intel Core Ultra 9 275HX:BC1 210.7 MB/s,BC7 123.6 MB/s,builtin BC6 34.2 MB/s(vs33.7 MB/s),builtin BC7 34.9 MB/s(vs34.8 MB/s), with the largest savings on small DirectStorage streaming chunks where allocation/zeroing dominates.Hardware Configuration
Compatibility
Zero-dependency
victory-formatscrate; 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
main(84bd61e2). Cleanly builds on top of merged #2092 without overlapping any open BCPack PR.@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 warningsis clean andcargo test -p victory-formatsgives 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, andix[0]/ix[1]only whenbits[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 whencommon >= 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_setignoresref_ixwhenref_bits == 0, andbits,ctrlandrows[0]are written for every block;?andreturn Errexits 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.mainref_oracleSpeed, release build, best of 5, two alternating runs each (ns per
decodecall, builtin tables):mainmainSo 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.
d7fa7833a8tof10cb395f3View command line instructions
Checkout
From your project repository, check out a new branch and test the changes.Merge
Merge the changes and update on Forgejo.