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

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

@⁠wbkepler on GitHub · 2026-11-24 07:07 UTC · original
wbkepler:formats/bcpack-fast-decode → main · +764 −384 · 1 files · 1 commits · open

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)
**@⁠wbkepler** on GitHub · 2026-11-24 07:07 UTC · [original](https://github.com/Marvisak/VIctory/pull/703) `wbkepler:formats/bcpack-fast-decode` → `main` · +764 −384 · 1 files · 1 commits · open ## 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)
Rebased onto upstream/main (ffe310e), preserving #93 window-table bucket lookup (Tables::bucket + bucket_of) and combining it with zero-allocation Workspace reuse, stack-streamed lanes_stream, and precomputed bit lookup tables. Documented that dst contents are unspecified on Err.
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 04:13:34 +00:00
Author
Owner

@⁠molodayamama on GitHub · 2026-11-24 14:04 UTC · original

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.

**@⁠molodayamama** on GitHub · 2026-11-24 14:04 UTC · [original](https://github.com/Marvisak/VIctory/pull/703#issuecomment-7133794821) 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.
Author
Owner

@⁠Marvisak on GitHub · 2026-11-25 01:25 UTC · original

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

**@⁠Marvisak** on GitHub · 2026-11-25 01:25 UTC · [original](https://github.com/Marvisak/VIctory/pull/703#issuecomment-7149527204) <!-- 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)_
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/703/head:github-bot/gh-703'
git switch 'github-bot/gh-703'

Merge

Merge the changes and update on Forgejo.
git switch 'main'
git merge --no-ff 'github-bot/gh-703'
git switch 'github-bot/gh-703'
git rebase 'main'
git switch 'main'
git merge --ff-only 'github-bot/gh-703'
git switch 'github-bot/gh-703'
git rebase 'main'
git switch 'main'
git merge --no-ff 'github-bot/gh-703'
git switch 'main'
git merge --squash 'github-bot/gh-703'
git switch 'main'
git merge --ff-only 'github-bot/gh-703'
git switch 'main'
git merge 'github-bot/gh-703'
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/Marvisak--VIctory!703
No description provided.