Formats: BCPack block histories without per-call allocation or zeroing #2034

Open
github-bot wants to merge 2 commits from github-bot/gh-2034 into main AGit
Owner

@⁠StExpeditus on GitHub · 2026-11-25 03:49 UTC · original
StExpeditus:formats/bcpack-history-no-alloc → main · +109 −8 · 1 files · 2 commits · open

Part of #703.

The per-call vec![Default::default(); HISTORY] in plain/bc6/bc7 cost one heap allocation plus a 10-30 KiB memset on every decode call. Ring entries are always written before they are read (block i writes slot i % HISTORY; a reference r of block i reads the slot block i - r wrote, and r <= 255 < HISTORY), so the slots never need initialising. [MaybeUninit::uninit(); HISTORY] skips both the allocation and the zeroing; the one unsafe (in pick) carries the invariant as its SAFETY comment.

Measured with bcpack_throughput's synthetic streams (cargo test --release -p victory-formats bcpack_throughput -- --ignored --nocapture, extended locally to small block counts):

  • BC7, 1 block: 1.61 -> 1.02 us/decode (-37%)
  • BC7, 16 blocks: 5.94 -> 5.14 us/decode (-13%)
  • BC4, 1 block: 0.79 -> 0.67 us/decode (-15%)
  • 16384 blocks (existing bench): unchanged within noise (BC7 55.3 -> 57.1 MB/s, BC1 96.8 -> 100.0 MB/s)

Small decodes are the mip-tail case that texture streaming does most, where the zeroing was proportionally heaviest.

cargo test -p victory-formats: 38 passed, 0 failed.

The other per-call allocation (records in decode, proportional to block count) and a fused streaming decode are left to follow-ups.

**@⁠StExpeditus** on GitHub · 2026-11-25 03:49 UTC · [original](https://github.com/Marvisak/VIctory/pull/2034) `StExpeditus:formats/bcpack-history-no-alloc` → `main` · +109 −8 · 1 files · 2 commits · open Part of #703. The per-call `vec![Default::default(); HISTORY]` in `plain`/`bc6`/`bc7` cost one heap allocation plus a 10-30 KiB memset on every `decode` call. Ring entries are always written before they are read (block i writes slot `i % HISTORY`; a reference `r` of block i reads the slot block `i - r` wrote, and `r <= 255 < HISTORY`), so the slots never need initialising. `[MaybeUninit::uninit(); HISTORY]` skips both the allocation and the zeroing; the one `unsafe` (in `pick`) carries the invariant as its SAFETY comment. Measured with `bcpack_throughput`'s synthetic streams (`cargo test --release -p victory-formats bcpack_throughput -- --ignored --nocapture`, extended locally to small block counts): - BC7, 1 block: 1.61 -> 1.02 us/decode (-37%) - BC7, 16 blocks: 5.94 -> 5.14 us/decode (-13%) - BC4, 1 block: 0.79 -> 0.67 us/decode (-15%) - 16384 blocks (existing bench): unchanged within noise (BC7 55.3 -> 57.1 MB/s, BC1 96.8 -> 100.0 MB/s) Small decodes are the mip-tail case that texture streaming does most, where the zeroing was proportionally heaviest. `cargo test -p victory-formats`: 38 passed, 0 failed. The other per-call allocation (`records` in `decode`, proportional to block count) and a fused streaming decode are left to follow-ups.
The per-call `vec![Default::default(); HISTORY]` in plain/bc6/bc7 cost a
heap allocation plus a 10-30 KiB memset on every decode. Ring entries are
written (block i writes slot i % HISTORY) before they are read (a
reference r of block i reads the slot block i - r wrote, and
r <= 255 < HISTORY), so the slots never need initialising. An uninit
array skips both the allocation and the zeroing.

Measured with bcpack_throughput's synthetic streams (release): small
decodes drop by the memset they no longer pay - BC7 1 block 1.61 to
1.02 us (-37%), BC7 16 blocks 5.94 to 5.14 us (-13%), BC4 1 block 0.79
to 0.67 us (-15%); 16384-block decodes are unchanged (BC7 55.3 to
57.1 MB/s, BC1 96.8 to 100.0 MB/s).

Part of #703.
Per the triage note: a default test around the dictionary-to-history
transition, distance 255, and ring wrap. 600 blocks for BC4, BC6 and
BC7; the first 255 carry per-block residuals so the base values differ,
and from block 255 every block must come back exactly as the block 255
before it. Covers the BC6 and BC7 loops the earlier coverage did not.
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 04:52:17 +00:00
Author
Owner

@⁠jeremygold02 on GitHub · 2026-11-25 03:55 UTC · original

Triage verdict — Keep the narrow history change, preserve a wraparound regression (de45bec)

Evidence

  • @⁠StExpeditus removes allocation and zeroing from the three history rings. Each in-range reference should target a slot written by an earlier block. #703 also removes per-call history allocation through reusable initialized storage, while adding separate streaming and packing work. If this lands first, @⁠wbkepler's broader work still has changes and tests worth retaining. Ring lookup · #703

Checks and limits

  • Applied the test-only #2036 on top of this exact head. The combined formats suite passed 39 tests with five ignored.
  • A separate synthetic BC4 decode exercised 513 blocks. Distance 255 selected the dictionary just before it could reach decoded history, then selected block zero at the first eligible position. It also returned the expected earlier blocks after wrapping, and distance one returned the latest entry across both wraps. That checks the shared plain-format path. The disposable boundary fixture did not cover the BC6 and BC7 loops.

Next step

  • Retain a default test around the dictionary-to-history transition, distance 255, and ring wrap. #2036 is useful independent coverage of BC1, and its two-block fixture can land independently. If both performance proposals proceed, use one history implementation and preserve #703's independent evidence and credit.

This verification was AI-assisted.

**@⁠jeremygold02** on GitHub · 2026-11-25 03:55 UTC · [original](https://github.com/Marvisak/VIctory/pull/2034#issuecomment-7152362583) **Triage verdict — Keep the narrow history change, preserve a wraparound regression** (`de45bec`) **Evidence** - @⁠StExpeditus removes allocation and zeroing from the three history rings. Each in-range reference should target a slot written by an earlier block. #703 also removes per-call history allocation through reusable initialized storage, while adding separate streaming and packing work. If this lands first, @⁠wbkepler's broader work still has changes and tests worth retaining. [Ring lookup](https://github.com/Marvisak/VIctory/blob/de45bec9945a7022ee2056c137908524dedf99ba/crates/victory-formats/src/bcpack.rs#L488-L510) · [#703](https://git.gtahub.st/github/Marvisak--VIctory/pulls/703) **Checks and limits** - Applied the test-only #2036 on top of this exact head. The combined formats suite passed 39 tests with five ignored. - A separate synthetic BC4 decode exercised 513 blocks. Distance 255 selected the dictionary just before it could reach decoded history, then selected block zero at the first eligible position. It also returned the expected earlier blocks after wrapping, and distance one returned the latest entry across both wraps. That checks the shared plain-format path. The disposable boundary fixture did not cover the BC6 and BC7 loops. **Next step** - Retain a default test around the dictionary-to-history transition, distance 255, and ring wrap. #2036 is useful independent coverage of BC1, and its two-block fixture can land independently. If both performance proposals proceed, use one history implementation and preserve #703's independent evidence and credit. This verification was AI-assisted.
Author
Owner

@⁠StExpeditus on GitHub · 2026-11-25 09:05 UTC · original

Addressed the triage note: added references_survive_the_dictionary_transition_and_the_history_wrap.

  • 600 blocks for BC4, BC6 and BC7 (the shared plain-format path and the two loops the earlier coverage missed).
  • Every block refers to the block 255 before it: blocks 0-254 hit the dictionary, block 255 reads block 0 (the dictionary-to-history transition), and later blocks read slots the ring has wrapped past.
  • The first 255 blocks carry per-block residuals so the base values differ; from block 255 the residual is zero, so each block must come back exactly as the one it refers to. An unwritten slot, a stale entry or an off-by-one in the distance fails the assert_eq.
  • Mutation-checked: an artificial +1 in the slot index makes the test fail.

cargo test -p victory-formats: 39 passed, 0 failed.

**@⁠StExpeditus** on GitHub · 2026-11-25 09:05 UTC · [original](https://github.com/Marvisak/VIctory/pull/2034#issuecomment-7157855032) Addressed the triage note: added `references_survive_the_dictionary_transition_and_the_history_wrap`. - 600 blocks for BC4, BC6 **and** BC7 (the shared plain-format path and the two loops the earlier coverage missed). - Every block refers to the block 255 before it: blocks 0-254 hit the dictionary, block 255 reads block 0 (the dictionary-to-history transition), and later blocks read slots the ring has wrapped past. - The first 255 blocks carry per-block residuals so the base values differ; from block 255 the residual is zero, so each block must come back exactly as the one it refers to. An unwritten slot, a stale entry or an off-by-one in the distance fails the `assert_eq`. - Mutation-checked: an artificial +1 in the slot index makes the test fail. `cargo test -p victory-formats`: 39 passed, 0 failed.
Author
Owner

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

Automated triage for the maintainers.

Merge candidate. In crates/victory-formats/src/bcpack.rs, the per-call vec[..; HISTORY] history rings in plain, bc6 and bc7 become stack arrays of MaybeUninit, and pick reads them with one unsafe assume_init_ref. Recommending a look, not approving.

What I checked:

  • The safety argument holds. references returns usize::from(u8), so r <= 255 < HISTORY (256). Slot (i - r) % HISTORY was written by block i - r and not overwritten since. Each block writes its slot only after both pick calls.
  • All element types are Copy, so the array repeat is valid.
  • The new test references_survive_the_dictionary_transition_and_the_history_wrap covers the dictionary-to-history transition and the ring wrap for BC4, BC6 and BC7.

Notes for the maintainer:

  • This adds unsafe for a micro-optimization. A safe stack array ([T::default(); HISTORY]) would also remove the allocation but keep the memset, if you prefer no unsafe here.
  • It overlaps #703, a much larger rewrite of the same file that keeps reusable initialized storage. Landing this one first would make #703 need a rebase.

CI: not run on this head (no check runs reported).


Generated by Claude Code

**@⁠Marvisak** on GitHub · 2026-11-25 09:18 UTC · [original](https://github.com/Marvisak/VIctory/pull/2034#issuecomment-7158163303) <!-- victory-triage v1 head=e23fa10b22cbbf22d7d0cb527a909a3f50b16127 verdict=merge-candidate at=2026-11-25T09:18:42Z --> Automated triage for the maintainers. **Merge candidate.** In `crates/victory-formats/src/bcpack.rs`, the per-call `vec[..; HISTORY]` history rings in `plain`, `bc6` and `bc7` become stack arrays of `MaybeUninit`, and `pick` reads them with one `unsafe` `assume_init_ref`. Recommending a look, not approving. What I checked: - The safety argument holds. `references` returns `usize::from(u8)`, so `r <= 255 < HISTORY` (256). Slot `(i - r) % HISTORY` was written by block `i - r` and not overwritten since. Each block writes its slot only after both `pick` calls. - All element types are `Copy`, so the array repeat is valid. - The new test `references_survive_the_dictionary_transition_and_the_history_wrap` covers the dictionary-to-history transition and the ring wrap for BC4, BC6 and BC7. Notes for the maintainer: - This adds `unsafe` for a micro-optimization. A safe stack array (`[T::default(); HISTORY]`) would also remove the allocation but keep the memset, if you prefer no `unsafe` here. - It overlaps #703, a much larger rewrite of the same file that keeps reusable initialized storage. Landing this one first would make #703 need a rebase. CI: not run on this head (no check runs reported). --- _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/2034/head:github-bot/gh-2034'
git switch 'github-bot/gh-2034'

Merge

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