Formats: eliminate BCPack per-call allocations and optimize streaming decode #703

Open
wbkepler wants to merge 1 commit from upstream-pr/703 into main
wbkepler commented 2026-11-24 07:07:03 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by wbkepler on 2026-11-24T07:07:03Z.


Summary

Eliminates per-call heap allocations and optimizes streaming BCPack decompression in crates/victory-formats/src/bcpack.rs (0 unsafe, Tables untouched):

  • Adds reusable bcpack::Workspace backed by a re-entrancy-safe thread-local Workspace in bcpack::decode.
  • Replaces the multi-MiB intermediate records buffer and two-pass decode with const-generic lanes_stream<N, PER, STRIDE> streaming stack records straight into block decoders.
  • Optimizes Lane::step with cold control-phase split, branchless 8-way bucket lookup, and direct remainder math.
  • Replaces bit-by-bit interleave/nibble/BC6H wide loops with compile-time LUTs (SPREAD4, SPREAD8, NIBBLES_EVEN_FIRST), precomputed plain-format masks, and 16-bit anchor_mask in bc7_pack (+40–52% single-core release throughput across BC1/BC4/BC6H/BC7).

Verification

  • cargo clippy --workspace --all-targets — passed (zero new warnings)
  • cargo clippy -p victory --features diagnostics --all-targets — passed
  • cargo test --workspace — passed (victory-formats: 23/23 including exhaustive 128x65536 rANS equivalence, all-6-format workspace reuse, and LUT equivalence tests)
  • cargo xtask policy — passed (151 files ok)
<!-- greyfinch-sync:Marvisak/VIctory:issue:5467711705 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/703). Originally posted by [wbkepler](https://github.com/wbkepler) on 2026-11-24T07:07:03Z. --- ## Summary Eliminates per-call heap allocations and optimizes streaming BCPack decompression in `crates/victory-formats/src/bcpack.rs` (`0 unsafe`, `Tables` untouched): - Adds reusable `bcpack::Workspace` backed by a re-entrancy-safe thread-local `Workspace` in `bcpack::decode`. - Replaces the multi-MiB intermediate `records` buffer and two-pass decode with const-generic `lanes_stream<N, PER, STRIDE>` streaming stack records straight into block decoders. - Optimizes `Lane::step` with cold control-phase split, branchless 8-way bucket lookup, and direct remainder math. - Replaces bit-by-bit interleave/nibble/`BC6H` wide loops with compile-time LUTs (`SPREAD4`, `SPREAD8`, `NIBBLES_EVEN_FIRST`), precomputed plain-format masks, and 16-bit `anchor_mask` in `bc7_pack` (+40–52% single-core release throughput across BC1/BC4/BC6H/BC7). ## Verification - `cargo clippy --workspace --all-targets` — passed (zero new warnings) - `cargo clippy -p victory --features diagnostics --all-targets` — passed - `cargo test --workspace` — passed (`victory-formats`: 23/23 including exhaustive 128x65536 rANS equivalence, all-6-format workspace reuse, and LUT equivalence tests) - `cargo xtask policy` — passed (151 files ok)
LAKSHYA-cmd-tech commented 2026-11-24 07:09:39 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by LAKSHYA-cmd-tech on 2026-11-24T07:09:39Z.

Review: APPROVED


Tested on Windows 11 MSVC (rustc 1.98.1):

  • cargo test -p victory-formats: 23 passed, 0 failed, 3 ignored (all rans equivalence and workspace reuse tests pass)
  • cargo clippy -p victory-formats --all-targets -- -D warnings: clean (0 warnings)
  • cargo xtask policy: 151 files ok

Stack-streamed decode and thread-local workspace reuse eliminate per-texture heap allocations during asset decompression without modifying existing format tables.

<!-- greyfinch-sync:Marvisak/VIctory:review:6035069466 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/703#pullrequestreview-6035069466). Originally posted by [LAKSHYA-cmd-tech](https://github.com/LAKSHYA-cmd-tech) on 2026-11-24T07:09:39Z. **Review:** APPROVED --- Tested on Windows 11 MSVC (rustc 1.98.1): - cargo test -p victory-formats: 23 passed, 0 failed, 3 ignored (all rans equivalence and workspace reuse tests pass) - cargo clippy -p victory-formats --all-targets -- -D warnings: clean (0 warnings) - cargo xtask policy: 151 files ok Stack-streamed decode and thread-local workspace reuse eliminate per-texture heap allocations during asset decompression without modifying existing format tables.
catminou4 commented 2026-11-24 08:04:00 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by catminou4 on 2026-11-24T08:04:00Z.

Review: APPROVED


cargo test -p victory-formats --locked on macOS 26 arm64, rustc 1.98.1: head alone 23 passed, and after merging current main (7374bfb) still 23 passed / 0 failed (+3 vs main's 20 — the new BCPack coverage runs ungated). Approving.

<!-- greyfinch-sync:Marvisak/VIctory:review:6035632992 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/703#pullrequestreview-6035632992). Originally posted by [catminou4](https://github.com/catminou4) on 2026-11-24T08:04:00Z. **Review:** APPROVED --- `cargo test -p victory-formats --locked` on macOS 26 arm64, rustc 1.98.1: head alone 23 passed, and after merging current main (`7374bfb`) still **23 passed / 0 failed** (+3 vs main's 20 — the new BCPack coverage runs ungated). Approving.
catminou4 commented 2026-11-24 09:04:49 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by catminou4 on 2026-11-24T09:04:49Z.

Review: COMMENTED


Rebase note: this no longer merges cleanly with current main (8e79c80) — conflict in crates/victory-formats/src/bcpack.rs. #93 (perf/bcpack-bucket-lookup) just merged and rewrites the same bucket-lookup path this PR optimizes, so the rebase needs a real look at how the two interact (per-call allocation elimination vs. the new bucket lookup), not just a textual resolve.

For the record, on its own base the head passed on macOS arm64 / rustc 1.98.1: cargo test -p victory-formats --locked → 23 passed / 0 failed.

<!-- greyfinch-sync:Marvisak/VIctory:review:6036368099 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/703#pullrequestreview-6036368099). Originally posted by [catminou4](https://github.com/catminou4) on 2026-11-24T09:04:49Z. **Review:** COMMENTED --- Rebase note: this no longer merges cleanly with current main (`8e79c80`) — conflict in `crates/victory-formats/src/bcpack.rs`. #93 (`perf/bcpack-bucket-lookup`) just merged and rewrites the same bucket-lookup path this PR optimizes, so the rebase needs a real look at how the two interact (per-call allocation elimination vs. the new bucket lookup), not just a textual resolve. For the record, on its own base the head passed on macOS arm64 / rustc 1.98.1: `cargo test -p victory-formats --locked` → 23 passed / 0 failed.
mercuryVM commented 2026-11-24 10:20:44 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by mercuryVM on 2026-11-24T10:20:44Z.

Review: APPROVED


Tested d45b04b on Windows 11, RTX 5070, MSVC: cargo test -p victory-formats 23 passed, including the existing reference_chunks, bc7_mode6_block and noise_never_panics. cargo clippy -p victory-formats --all-targets is clean.

  • Differential check against the current decoder: I ran 60,000 seeded random inputs (BC1–BC7, sources of 1–3000 bytes, a third of them biased toward small symbols) through bcpack::decode before and after this PR. The two decoders agree on which inputs succeed and which fail (27,481 succeed, 32,519 fail), and every successful decode produces byte-identical output.
  • One observable difference: on inputs that fail, the destination buffer is left with different contents, because the streaming decoder writes blocks as it goes. The only caller, dstorage (bcpack::decode into into_destination), turns the error into a failed request and does not read the buffer back, so I don't see a functional problem. It's worth one line in the decode docs saying that dst is unspecified on Err.

AI-assisted (Claude Code).

<!-- greyfinch-sync:Marvisak/VIctory:review:6037442354 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/703#pullrequestreview-6037442354). Originally posted by [mercuryVM](https://github.com/mercuryVM) on 2026-11-24T10:20:44Z. **Review:** APPROVED --- Tested d45b04b on Windows 11, RTX 5070, MSVC: `cargo test -p victory-formats` 23 passed, including the existing `reference_chunks`, `bc7_mode6_block` and `noise_never_panics`. `cargo clippy -p victory-formats --all-targets` is clean. - Differential check against the current decoder: I ran 60,000 seeded random inputs (BC1–BC7, sources of 1–3000 bytes, a third of them biased toward small symbols) through `bcpack::decode` before and after this PR. The two decoders agree on which inputs succeed and which fail (27,481 succeed, 32,519 fail), and every successful decode produces byte-identical output. - One observable difference: on inputs that fail, the destination buffer is left with different contents, because the streaming decoder writes blocks as it goes. The only caller, `dstorage` (`bcpack::decode` into `into_destination`), turns the error into a failed request and does not read the buffer back, so I don't see a functional problem. It's worth one line in the `decode` docs saying that `dst` is unspecified on `Err`. AI-assisted (Claude Code).
molodayamama commented 2026-11-24 14:05:02 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by molodayamama on 2026-11-24T14:04:22Z.


Independent Windows check of d45b04b0ab07daf732a2cd9dc85df9d5c9471de8 (Windows 10 22H2 19045, Rust 1.98.1, MSVC x64).

After a fresh build of the PR's victory-formats, I ran the three new synthetic regressions with cargo test --locked -p victory-formats --lib -- workspace_reuse_matches_fresh_decode_across_all_formats rans_step_matches_reference_formula_for_all_models_and_slots bit_lookup_tables_match_reference_bit_ops --test-threads=1 --nocapture. All three passed. They check workspace reuse against fresh decodes, the rANS step against its reference formula, and the bit tables against direct bit operations. No throughput comparison or external reference chunks.

Only source-built synthetic fixtures were used. AI-assisted verification.

<!-- greyfinch-sync:Marvisak/VIctory:comment:7133794821 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/703#issuecomment-7133794821). Originally posted by [molodayamama](https://github.com/molodayamama) on 2026-11-24T14:04:22Z. --- Independent Windows check of `d45b04b0ab07daf732a2cd9dc85df9d5c9471de8` (Windows 10 22H2 19045, Rust 1.98.1, MSVC x64). After a fresh build of the PR's `victory-formats`, I ran the three new synthetic regressions with `cargo test --locked -p victory-formats --lib -- workspace_reuse_matches_fresh_decode_across_all_formats rans_step_matches_reference_formula_for_all_models_and_slots bit_lookup_tables_match_reference_bit_ops --test-threads=1 --nocapture`. All three passed. They check workspace reuse against fresh decodes, the rANS step against its reference formula, and the bit tables against direct bit operations. No throughput comparison or external reference chunks. Only source-built synthetic fixtures were used. AI-assisted verification.
Marvisak commented 2026-11-25 02:06:01 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by Marvisak on 2026-11-25T01:25:15Z.


Automated triage for the maintainers.

Needs work.

This rewrites most of victory-formats/src/bcpack.rs (+764/−384) for speed. That is worth having, but the change needs evidence that the output stays the same:

  • No check against the current decoder. The new workspace_reuse_matches_fresh_decode_across_all_formats compares the new code with itself (reused vs fresh Workspace), and reference_chunks stays ignored. Please add a differential test that checks the new decode against main's decode, byte for byte, on synthetic streams for BC1/3/4/5/6/7. It should include every BC6H mode and BC7 control codes. Alternatively, report a reference_chunks run on both versions.
  • Behaviour change on error. On main, decode decodes every record before writing any block, so a short stream leaves dst untouched. With streaming it writes blocks before the error. The only caller, dstorage.rs (bcpack::decode inside into_destination), passes title memory, so a failed request now leaves partial data where the title expects none. Please keep main's behaviour or explain why the change is fine.
  • Re-entrancy fallback. The try_borrow_mut fallback in bcpack::decode guards against re-entry, which cannot happen (decode does not call itself). Please drop it, along with the per-thread buffers kept for the thread's lifetime, unless measurements show a need.

CI: no check runs are recorded for this head.


Generated by Claude Code

<!-- greyfinch-sync:Marvisak/VIctory:comment:7149527204 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/703#issuecomment-7149527204). Originally posted by [Marvisak](https://github.com/Marvisak) on 2026-11-25T01:25:15Z. --- <!-- victory-triage v1 head=712f89096fd7ca712fa5f68a603e6b0bdf5f04c3 verdict=needs-work at=2026-11-25T01:25:14Z --> Automated triage for the maintainers. **Needs work.** This rewrites most of `victory-formats/src/bcpack.rs` (+764/−384) for speed. That is worth having, but the change needs evidence that the output stays the same: - **No check against the current decoder.** The new `workspace_reuse_matches_fresh_decode_across_all_formats` compares the new code with itself (reused vs fresh `Workspace`), and `reference_chunks` stays ignored. Please add a differential test that checks the new `decode` against `main`'s decode, byte for byte, on synthetic streams for BC1/3/4/5/6/7. It should include every BC6H mode and BC7 control codes. Alternatively, report a `reference_chunks` run on both versions. - **Behaviour change on error.** On `main`, `decode` decodes every record before writing any block, so a short stream leaves `dst` untouched. With streaming it writes blocks before the error. The only caller, `dstorage.rs` (`bcpack::decode` inside `into_destination`), passes title memory, so a failed request now leaves partial data where the title expects none. Please keep `main`'s behaviour or explain why the change is fine. - **Re-entrancy fallback.** The `try_borrow_mut` fallback in `bcpack::decode` guards against re-entry, which cannot happen (`decode` does not call itself). Please drop it, along with the per-thread buffers kept for the thread's lifetime, unless measurements show a need. CI: no check runs are recorded for this head. --- _Generated by [Claude Code](https://claude.ai/code)_
Xvania7 commented 2026-11-25 08:52:04 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by Xvania7 on 2026-11-25T08:52:04Z.

Review: APPROVED


Verified locally merged onto main (940d32f, Windows 10 x64, Rust MSVC):

  • cargo test -p victory-formats --lib — 40 passed, 0 failed, 5 ignored (benchmarks/external fixtures)
  • cargo clippy -p victory-formats --all-targets -- -D warnings — 0 warnings
  • cargo xtask policy — ok (182 files ok)

Synthetic tests only; no game data.

<!-- greyfinch-sync:Marvisak/VIctory:review:6054425906 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/703#pullrequestreview-6054425906). Originally posted by [Xvania7](https://github.com/Xvania7) on 2026-11-25T08:52:04Z. **Review:** APPROVED --- Verified locally merged onto `main` (`940d32f`, Windows 10 x64, Rust MSVC): - `cargo test -p victory-formats --lib` — 40 passed, 0 failed, 5 ignored (benchmarks/external fixtures) - `cargo clippy -p victory-formats --all-targets -- -D warnings` — 0 warnings - `cargo xtask policy` — ok (182 files ok) Synthetic tests only; no game data.
Desamodeth commented 2026-11-26 18:47:33 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by Desamodeth on 2026-11-26T18:47:33Z.

Review: COMMENTED


Measured the workspace trade-off requested in triage on 712f8909 vs main 63bcca43. Reusing the history helps small chunks. Most of the large-chunk heap saving survives even with a fresh workspace on every call.

For 16,384 BC7 blocks, live decoder heap on main peaked at 541 KiB and returned to zero. A fresh PR workspace peaked at 29 KiB and returned to zero. On the PR's public thread-local path the first BC7 call added 29 KiB of history; the next call made zero allocations. One worker that had decoded all six formats retained 53.5 KiB in total (10 KiB shared by the plain formats, 14.5 KiB for BC6H, 29 KiB for BC7). These are allocator-requested bytes, excluding caller buffers and prebuilt tables.

BC7 workload main PR, fresh workspace PR, thread-local PR, explicit reused workspace
1 block, µs/decode 1.031 0.944 0.631 0.629
16,384 blocks, ms/decode 8.908 8.476 8.494 8.477

On this workload an explicit worker-owned workspace delivered the small-chunk benefit too. Large-chunk speed was format-dependent: BC3 went from 5.44 to 5.70 ms on the thread-local path; a second process with the order reversed gave 5.44 to 5.68 ms. Windows x64, Ryzen 7 9800X3D, Rust 1.98.1 MSVC release, 12 timed batches after warm-up. Allocation counters were disabled for timings.

Valid-stream differential: 24 fixtures (BC1/3/4/5/6H/7 × 1/16/600/16,384 blocks) produced identical bytes on main and the PR, including all 14 BC6H modes, all 285 populated BC7 controls, fragment boundaries and history wrap. The 600-block fixtures also matched all six published golden hashes from the synthetic encoder and regression in #2735. This check covers successful decodes. The outstanding question about destination contents after an error remains open.

CPU only, synthetic inputs, no title run. Coordination. AI-assisted with Codex.

<!-- greyfinch-sync:Marvisak/VIctory:review:6079150945 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/703#pullrequestreview-6079150945). Originally posted by [Desamodeth](https://github.com/Desamodeth) on 2026-11-26T18:47:33Z. **Review:** COMMENTED --- Measured the workspace trade-off requested in triage on `712f8909` vs main `63bcca43`. **Reusing the history helps small chunks. Most of the large-chunk heap saving survives even with a fresh workspace on every call.** For 16,384 BC7 blocks, live decoder heap on main peaked at **541 KiB** and returned to zero. A fresh PR workspace peaked at **29 KiB** and returned to zero. On the PR's public thread-local path the first BC7 call added **29 KiB** of history; the next call made **zero allocations**. One worker that had decoded all six formats retained **53.5 KiB** in total (10 KiB shared by the plain formats, 14.5 KiB for BC6H, 29 KiB for BC7). These are allocator-requested bytes, excluding caller buffers and prebuilt tables. | BC7 workload | main | PR, fresh workspace | PR, thread-local | PR, explicit reused workspace | |---|---:|---:|---:|---:| | 1 block, µs/decode | 1.031 | 0.944 | 0.631 | 0.629 | | 16,384 blocks, ms/decode | 8.908 | 8.476 | 8.494 | 8.477 | On this workload an explicit worker-owned workspace delivered the small-chunk benefit too. Large-chunk speed was format-dependent: BC3 went from 5.44 to 5.70 ms on the thread-local path; a second process with the order reversed gave 5.44 to 5.68 ms. Windows x64, Ryzen 7 9800X3D, Rust 1.98.1 MSVC release, 12 timed batches after warm-up. Allocation counters were disabled for timings. **Valid-stream differential:** 24 fixtures (BC1/3/4/5/6H/7 × 1/16/600/16,384 blocks) produced identical bytes on main and the PR, including all 14 BC6H modes, all 285 populated BC7 controls, fragment boundaries and history wrap. The 600-block fixtures also matched all six published golden hashes from [the synthetic encoder and regression in #2735](https://github.com/brainbudt/VIctory/blob/62911d14bbeabf35ead3550f3ad2359abc65b9e6/crates/victory-formats/src/bcpack.rs). This check covers successful decodes. The outstanding question about destination contents after an error remains open. CPU only, synthetic inputs, no title run. [Coordination](https://github.com/Marvisak/VIctory/issues/1633#issuecomment-7200793433). AI-assisted with Codex.
This pull request cannot be merged automatically due to conflicts.
Merge manually to resolve the conflicts.
View command line instructions

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u origin 'upstream-pr/703:upstream-pr/703'
git switch 'upstream-pr/703'

Merge

Merge the changes and update on Forgejo.

Warning: The "Autodetect manual merge" setting is not enabled for this repository, you will have to mark this pull request as manually merged afterwards.

git switch 'main'
git merge --no-ff 'upstream-pr/703'
git switch 'upstream-pr/703'
git rebase 'main'
git switch 'main'
git merge --ff-only 'upstream-pr/703'
git switch 'upstream-pr/703'
git rebase 'main'
git switch 'main'
git merge --no-ff 'upstream-pr/703'
git switch 'main'
git merge --squash 'upstream-pr/703'
git switch 'main'
git merge --ff-only 'upstream-pr/703'
git switch 'main'
git merge 'upstream-pr/703'
git push origin 'main'
Sign in to join this conversation.
No reviewers
No labels
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.

Reference
Marvisak/VIctory!703
No description provided.