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

Open
StExpeditus wants to merge 2 commits from upstream-pr/2034 into main
StExpeditus commented 2026-11-25 03:49:19 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by StExpeditus on 2026-11-25T03:49:19Z.


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.

<!-- greyfinch-sync:Marvisak/VIctory:issue:6596779599 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/2034). Originally posted by [StExpeditus](https://github.com/StExpeditus) on 2026-11-25T03:49:19Z. --- 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.
jeremygold02 commented 2026-11-25 04:07:11 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by jeremygold02 on 2026-11-25T03:55:14Z.


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.

<!-- greyfinch-sync:Marvisak/VIctory:comment:7152362583 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/2034#issuecomment-7152362583). Originally posted by [jeremygold02](https://github.com/jeremygold02) on 2026-11-25T03:55:14Z. --- **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://github.com/Marvisak/VIctory/pull/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.
DenRakEiw commented 2026-11-25 05:05:39 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by DenRakEiw on 2026-11-25T04:06:56Z.

Review: APPROVED


Verified locally merged onto origin/main (940d32f, Windows 11 x64, Rust 1.98.1 MSVC):

  • cargo test -p victory-formats --lib bcpack:: — 9 passed, 0 failed (2 ignored)
  • cargo clippy -p victory-formats --all-targets — 0 warnings
  • cargo xtask policy — ok (183 files ok)

Clean optimization avoiding per-call allocation and zeroing of BCPack block histories. Looks good.

AI-assisted (Gemini 4 Neon).

<!-- greyfinch-sync:Marvisak/VIctory:review:6051715898 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/2034#pullrequestreview-6051715898). Originally posted by [DenRakEiw](https://github.com/DenRakEiw) on 2026-11-25T04:06:56Z. **Review:** APPROVED --- Verified locally merged onto `origin/main` (`940d32f`, Windows 11 x64, Rust 1.98.1 MSVC): - `cargo test -p victory-formats --lib bcpack::` — 9 passed, 0 failed (2 ignored) - `cargo clippy -p victory-formats --all-targets` — 0 warnings - `cargo xtask policy` — ok (`183 files ok`) Clean optimization avoiding per-call allocation and zeroing of BCPack block histories. Looks good. AI-assisted (Gemini 4 Neon).
catminou4 commented 2026-11-25 05:05:39 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by catminou4 on 2026-11-25T04:17:02Z.

Review: APPROVED


Tested locally: macOS arm64, rustc 1.98.1, PR head merged into main 940d32f69b, cargo test -p victory-formats --locked.

38 passed / 0 failed. BCPack block histories without per-call allocation or zeroing — allocation-free history tracking; 38 passed / 0 failed / 5 ignored (current formats baseline).

<!-- greyfinch-sync:Marvisak/VIctory:review:6051785972 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/2034#pullrequestreview-6051785972). Originally posted by [catminou4](https://github.com/catminou4) on 2026-11-25T04:17:02Z. **Review:** APPROVED --- Tested locally: macOS arm64, rustc 1.98.1, PR head merged into main 940d32f69b, `cargo test -p victory-formats --locked`. 38 passed / 0 failed. BCPack block histories without per-call allocation or zeroing — allocation-free history tracking; 38 passed / 0 failed / 5 ignored (current formats baseline).
StExpeditus commented 2026-11-25 09:06:26 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by StExpeditus on 2026-11-25T09:05:11Z.


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.

<!-- greyfinch-sync:Marvisak/VIctory:comment:7157855032 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/2034#issuecomment-7157855032). Originally posted by [StExpeditus](https://github.com/StExpeditus) on 2026-11-25T09:05:11Z. --- 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.
Marvisak commented 2026-11-25 10:07:37 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by Marvisak on 2026-11-25T09:18:51Z.


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

<!-- greyfinch-sync:Marvisak/VIctory:comment:7158163303 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/2034#issuecomment-7158163303). Originally posted by [Marvisak](https://github.com/Marvisak) on 2026-11-25T09:18:51Z. --- <!-- 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)_
fuzeofficlal commented 2026-11-26 02:05:57 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by fuzeofficlal on 2026-11-26T01:51:49Z.

Review: COMMENTED


Reviewed and verified against main (63bcca43) on Windows 11 — memory-safety invariants are airtight and ready to merge:

  • Soundness of [MaybeUninit<...>; 256]: references() computes indices via u8::wrapping_add (0..=255); HISTORY = 256 and DICTIONARY = 255. In pick(), r == 0 yields zero, r > i indexes dictionary[r - i - 1] (<= 254), and 1 <= r <= i implies (i - r) % 256 != i % 256 with no intervening overwrite, so history[(i - r) % HISTORY].assume_init_ref() is guaranteed initialized.
  • All stored element types (Plain, (Bc7Ep, Bc7Ix), (Bc6Ep, Bc6Ix)) are Copy with no Drop, so early error returns in bc6/bc7 (bcpack.rs:781, 1093) cannot drop uninitialized slots; stack-local arrays are naturally thread-safe.

Local verification:

  • cargo test --locked -p victory-formats: 47/47 passed (including new references_survive_the_dictionary_transition_and_the_history_wrap across 600 blocks for BC4/BC6/BC7)
  • cargo clippy + cargo xtask policy (191 files ok): 0 warnings
<!-- greyfinch-sync:Marvisak/VIctory:review:6067493763 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/2034#pullrequestreview-6067493763). Originally posted by [fuzeofficlal](https://github.com/fuzeofficlal) on 2026-11-26T01:51:49Z. **Review:** COMMENTED --- Reviewed and verified against `main` (`63bcca43`) on Windows 11 — memory-safety invariants are airtight and ready to merge: - **Soundness of `[MaybeUninit<...>; 256]`**: `references()` computes indices via `u8::wrapping_add` (`0..=255`); `HISTORY = 256` and `DICTIONARY = 255`. In `pick()`, `r == 0` yields zero, `r > i` indexes `dictionary[r - i - 1]` (`<= 254`), and `1 <= r <= i` implies `(i - r) % 256 != i % 256` with no intervening overwrite, so `history[(i - r) % HISTORY].assume_init_ref()` is guaranteed initialized. - All stored element types (`Plain`, `(Bc7Ep, Bc7Ix)`, `(Bc6Ep, Bc6Ix)`) are `Copy` with no `Drop`, so early error returns in `bc6`/`bc7` (`bcpack.rs:781, 1093`) cannot drop uninitialized slots; stack-local arrays are naturally thread-safe. **Local verification**: - `cargo test --locked -p victory-formats`: **47/47 passed** (including new `references_survive_the_dictionary_transition_and_the_history_wrap` across 600 blocks for BC4/BC6/BC7) - `cargo clippy` + `cargo xtask policy` (`191 files ok`): 0 warnings
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/2034:upstream-pr/2034'
git switch 'upstream-pr/2034'

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/2034'
git switch 'upstream-pr/2034'
git rebase 'main'
git switch 'main'
git merge --ff-only 'upstream-pr/2034'
git switch 'upstream-pr/2034'
git rebase 'main'
git switch 'main'
git merge --no-ff 'upstream-pr/2034'
git switch 'main'
git merge --squash 'upstream-pr/2034'
git switch 'main'
git merge --ff-only 'upstream-pr/2034'
git switch 'main'
git merge 'upstream-pr/2034'
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!2032
No description provided.