BCPack: find a slot's bucket from a window table #93

Merged
TG0000 merged 1 commit from upstream-pr/93 into main 2026-11-24 08:47:35 +00:00
TG0000 commented 2026-11-24 00:24:37 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by TG0000 on 2026-11-24T00:24:37Z.


The BCPack decoder (crates/victory-formats/src/bcpack.rs, the DirectStorage texture unpack path) spends almost all of its time in Lane::step, one rANS symbol at a time. This PR makes the per-symbol work cheaper. The output stays the same.

What changes

  • Bucket lookup: each symbol used to scan all of its model's bucket starts. Tables now keeps one byte per 256-slot window per model (32 KiB, boxed, built in Tables::parse): the bucket of the window's first slot. bucket_of starts there and steps over the few buckets that begin inside the window. start gets one more 65536 sentinel, so the step loop needs no length check.
  • Symbol arithmetic: the symbol, frequency and remainder come straight from the bucket and the existing exact reciprocal. The old code rebuilt the frequency and cumulative start from the symbol (leading_zeros, a multiply, a mask).
  • Lane loop: the record offset is carried along, instead of computing pos / per * stride + pos % per (a division by a runtime value) for every symbol.

Measured

Synthetic streams with the synthetic test tables. No game data. Release build, one core, 16384 blocks, median of 3 runs, before and after on the same machine:

format before after throughput
BC1 177 ns/block 128 ns/block +38%
BC4 184 ns/block 128 ns/block +44%
BC7 591 ns/block 452 ns/block +31%

To reproduce: cargo test --release -p victory-formats bcpack_throughput -- --ignored --nocapture

Tests

  • bucket_lookup_matches_a_scan_of_the_starts: the window lookup against the old scan, for every slot of every model, on the shipped tables, on the uniform synthetic tables and on tables with one-slot buckets.
  • payload_step_matches_the_reference_decode: a payload Lane::step against the first version's arithmetic (scan plus division), on the shipped tables and the synthetic ones, for every model and slot at three state heights.
  • every_layout_round_trips_through_the_lanes: all six format lane layouts at 1, 2, 17, 300 and 1000 blocks, with short and long fragments.
  • bcpack_throughput (ignored): the benchmark above.

What ran: cargo test --workspace and cargo xtask policy pass, and clippy --workspace --all-targets reports nothing new. I didn't run the ignored reference_chunks test, because it needs reference chunks from outside the repository. This is CPU-only code, so it wasn't run on a GPU or against the game.

It touches only bcpack.rs, which no open PR touches.

AI-assisted (Claude Code).

🤖 Generated with Claude Code

<!-- greyfinch-sync:Marvisak/VIctory:issue:6571735668 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/93). Originally posted by [TG0000](https://github.com/TG0000) on 2026-11-24T00:24:37Z. --- The BCPack decoder (`crates/victory-formats/src/bcpack.rs`, the DirectStorage texture unpack path) spends almost all of its time in `Lane::step`, one rANS symbol at a time. This PR makes the per-symbol work cheaper. The output stays the same. ## What changes - **Bucket lookup**: each symbol used to scan all of its model's bucket starts. `Tables` now keeps one byte per 256-slot window per model (32 KiB, boxed, built in `Tables::parse`): the bucket of the window's first slot. `bucket_of` starts there and steps over the few buckets that begin inside the window. `start` gets one more 65536 sentinel, so the step loop needs no length check. - **Symbol arithmetic**: the symbol, frequency and remainder come straight from the bucket and the existing exact reciprocal. The old code rebuilt the frequency and cumulative start from the symbol (`leading_zeros`, a multiply, a mask). - **Lane loop**: the record offset is carried along, instead of computing `pos / per * stride + pos % per` (a division by a runtime value) for every symbol. ## Measured Synthetic streams with the synthetic test tables. No game data. Release build, one core, 16384 blocks, median of 3 runs, before and after on the same machine: | format | before | after | throughput | |---|---|---|---| | BC1 | 177 ns/block | 128 ns/block | +38% | | BC4 | 184 ns/block | 128 ns/block | +44% | | BC7 | 591 ns/block | 452 ns/block | +31% | To reproduce: `cargo test --release -p victory-formats bcpack_throughput -- --ignored --nocapture` ## Tests - `bucket_lookup_matches_a_scan_of_the_starts`: the window lookup against the old scan, for every slot of every model, on the shipped tables, on the uniform synthetic tables and on tables with one-slot buckets. - `payload_step_matches_the_reference_decode`: a payload `Lane::step` against the first version's arithmetic (scan plus division), on the shipped tables and the synthetic ones, for every model and slot at three state heights. - `every_layout_round_trips_through_the_lanes`: all six format lane layouts at 1, 2, 17, 300 and 1000 blocks, with short and long fragments. - `bcpack_throughput` (ignored): the benchmark above. What ran: `cargo test --workspace` and `cargo xtask policy` pass, and `clippy --workspace --all-targets` reports nothing new. I didn't run the ignored `reference_chunks` test, because it needs reference chunks from outside the repository. This is CPU-only code, so it wasn't run on a GPU or against the game. It touches only `bcpack.rs`, which no open PR touches. AI-assisted (Claude Code). 🤖 Generated with [Claude Code](https://claude.com/claude-code)
DaviTostes commented 2026-11-24 06:54:52 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by DaviTostes on 2026-11-24T01:12:05Z.


Tested 3e982f7: pass.

  • Tested on Arch Linux x86_64, rustc 1.98.1, cargo test -p victory-formats: 20 passed, 0 failed, 4 ignored, including the 4 tests this PR adds.

No game data used.

<!-- greyfinch-sync:Marvisak/VIctory:comment:7118861040 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/93#issuecomment-7118861040). Originally posted by [DaviTostes](https://github.com/DaviTostes) on 2026-11-24T01:12:05Z. --- Tested `3e982f7`: **pass**. - Tested on **Arch Linux x86_64, rustc 1.98.1**, `cargo test -p victory-formats`: **20 passed, 0 failed, 4 ignored**, including the 4 tests this PR adds. No game data used.
upstream-sync closed this pull request 2026-11-24 09:04:58 +00:00
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!93
No description provided.