XMA: accelerate VLC decoding with an eight-bit prefix lookup #364

Open
pedrofort wants to merge 1 commit from upstream-pr/364 into main
pedrofort commented 2026-11-24 02:37:01 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by pedrofort on 2026-11-24T02:37:01Z.


<head></head>

Motivation

XMA variable-length code decoding currently performs a binary search over the entire VLC table for every decoded symbol.

Implementation

Precompute candidate ranges for all 256 possible eight-bit prefixes during VLC table initialization.

  • Directly resolve prefixes with a single candidate.

  • Restrict binary searches to the corresponding candidate range otherwise.

  • Preserve symbol selection and bit consumption.

  • No new dependencies or unsafe code.

The additional lookup tables consume approximately 28 KiB across the seven codebooks on 64-bit targets.

Performance

Synthetic release microbenchmarks on Apple M5, macOS arm64, Rust 1.98.1, base commit d2a4bae.

Session | All codes | Short codes -- | -- | -- 1 | 2.206x | 7.829x 2 | 2.058x | 7.877x 3 | 2.013x | 7.728x Median speedup | 2.058x | 7.829x

Each measurement processes 1,048,576 symbols. Each session includes warm-up and five measurements with alternating execution order.

All benchmark checksums matched.

The original implementation was also benchmarked against itself as a baseline control, producing speedups of 0.997x and 0.986x.

These are synthetic VLC microbenchmarks, not end-to-end audio decoding or game performance measurements.

Correctness and validation

  • Equivalence verified across all seven VLC codebooks, code boundaries, prefix boundaries, deterministic random inputs and all eight bit alignments.

  • Decoded symbols and consumed bit counts match the original implementation.

  • Full formats suite: 21 passed, 0 failed, 4 ignored.

  • Clippy with -D warnings: clean.

  • Asset policy: 151 files OK.

  • git diff --check: clean.

Reproduction

cargo test --locked --release -p victory-formats --lib vlc_prefix_benchmark -- --ignored --nocapture

All benchmark inputs are synthetic. No game assets were used.

Independent performance measurements on Windows x86-64 would be welcome.

AI-assisted implementation and test preparation (ChatGPT), with tests and benchmarks executed locally on Apple Silicon.

<!-- greyfinch-sync:Marvisak/VIctory:issue:6573354258 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/364). Originally posted by [pedrofort](https://github.com/pedrofort) on 2026-11-24T02:37:01Z. --- <head></head><h2 style="caret-color: rgb(255, 255, 255); color: rgb(255, 255, 255); font-style: normal; font-variant-caps: normal; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration: none;"><span>Motivation</span></h2><p style="caret-color: rgb(255, 255, 255); color: rgb(255, 255, 255); font-size: medium; font-style: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration: none;"><span>XMA variable-length code decoding currently performs a binary search over the entire VLC table for every decoded symbol.</span></p><h2 style="caret-color: rgb(255, 255, 255); color: rgb(255, 255, 255); font-style: normal; font-variant-caps: normal; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration: none;"><span>Implementation</span></h2><p style="caret-color: rgb(255, 255, 255); color: rgb(255, 255, 255); font-size: medium; font-style: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration: none;"><span>Precompute candidate ranges for all 256 possible eight-bit prefixes during VLC table initialization.</span></p><ul style="caret-color: rgb(255, 255, 255); color: rgb(255, 255, 255); font-size: medium; font-style: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration: none;"><li><p><span>Directly resolve prefixes with a single candidate.</span></p></li><li><p><span>Restrict binary searches to the corresponding candidate range otherwise.</span></p></li><li><p><span>Preserve symbol selection and bit consumption.</span></p></li><li><p><span>No new dependencies or unsafe code.</span></p></li></ul><p style="caret-color: rgb(255, 255, 255); color: rgb(255, 255, 255); font-size: medium; font-style: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration: none;"><span>The additional lookup tables consume approximately 28 KiB across the seven codebooks on 64-bit targets.</span></p><h2 style="caret-color: rgb(255, 255, 255); color: rgb(255, 255, 255); font-style: normal; font-variant-caps: normal; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration: none;"><span>Performance</span></h2><p style="caret-color: rgb(255, 255, 255); color: rgb(255, 255, 255); font-size: medium; font-style: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration: none;"><span>Synthetic release microbenchmarks on Apple M5, macOS arm64, Rust 1.98.1, base commit<span class="Apple-converted-space"> </span></span><code><span>d2a4bae</span></code><span>.</span></p> Session | All codes | Short codes -- | -- | -- 1 | 2.206x | 7.829x 2 | 2.058x | 7.877x 3 | 2.013x | 7.728x Median speedup | 2.058x | 7.829x <p style="caret-color: rgb(255, 255, 255); color: rgb(255, 255, 255); font-size: medium; font-style: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration: none;"><span>Each measurement processes 1,048,576 symbols. Each session includes warm-up and five measurements with alternating execution order.</span></p><p style="caret-color: rgb(255, 255, 255); color: rgb(255, 255, 255); font-size: medium; font-style: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration: none;"><span>All benchmark checksums matched.</span></p><p style="caret-color: rgb(255, 255, 255); color: rgb(255, 255, 255); font-size: medium; font-style: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration: none;"><span>The original implementation was also benchmarked against itself as a baseline control, producing speedups of 0.997x and 0.986x.</span></p><p style="caret-color: rgb(255, 255, 255); color: rgb(255, 255, 255); font-size: medium; font-style: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration: none;"><span>These are synthetic VLC microbenchmarks, not end-to-end audio decoding or game performance measurements.</span></p><h2 style="caret-color: rgb(255, 255, 255); color: rgb(255, 255, 255); font-style: normal; font-variant-caps: normal; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration: none;"><span>Correctness and validation</span></h2><ul style="caret-color: rgb(255, 255, 255); color: rgb(255, 255, 255); font-size: medium; font-style: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration: none;"><li><p><span>Equivalence verified across all seven VLC codebooks, code boundaries, prefix boundaries, deterministic random inputs and all eight bit alignments.</span></p></li><li><p><span>Decoded symbols and consumed bit counts match the original implementation.</span></p></li><li><p><span>Full formats suite: 21 passed, 0 failed, 4 ignored.</span></p></li><li><p><span>Clippy with<span class="Apple-converted-space"> </span></span><code><span>-D warnings</span></code><span>: clean.</span></p></li><li><p><span>Asset policy: 151 files OK.</span></p></li><li><p><code><span>git diff --check</span></code><span>: clean.</span></p></li></ul><h2 style="caret-color: rgb(255, 255, 255); color: rgb(255, 255, 255); font-style: normal; font-variant-caps: normal; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration: none;"><span>Reproduction</span></h2><p style="caret-color: rgb(255, 255, 255); color: rgb(255, 255, 255); font-size: medium; font-style: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration: none;"><code><span>cargo test --locked --release -p victory-formats --lib vlc_prefix_benchmark -- --ignored --nocapture</span></code></p><p style="caret-color: rgb(255, 255, 255); color: rgb(255, 255, 255); font-size: medium; font-style: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration: none;"><span>All benchmark inputs are synthetic. No game assets were used.</span></p><p style="caret-color: rgb(255, 255, 255); color: rgb(255, 255, 255); font-size: medium; font-style: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration: none;"><span>Independent performance measurements on Windows x86-64 would be welcome.</span></p><p style="caret-color: rgb(255, 255, 255); color: rgb(255, 255, 255); font-size: medium; font-style: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration: none;"><span>AI-assisted implementation and test preparation (ChatGPT), with tests and benchmarks executed locally on Apple Silicon.</span></p>
jeremygold02 commented 2026-11-24 06:55:45 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by jeremygold02 on 2026-11-24T04:28:13Z.


Tested PR #364 at head e503756597cafb3ac6c259ec82458fa5c61215aa (state at test time: OPEN). Pre-comment refresh: state OPEN, head unchanged at e503756597cafb3ac6c259ec82458fa5c61215aa.

  • cargo test --locked -p victory-formats vlc_prefix_matches_binary_search — passed (ok. 1 passed; 0 failed; 0 ignored; 0 measured; 24 filtered out; finished in 0.04s ); cases: xma::tests::vlc_prefix_matches_binary_search.

Behavior covered: Checks prefix-table decoding matches the binary-search reference across all seven VLC books, prefix/code boundaries, random probes and bit alignments.
Code: crates/victory-formats/src/xma.rs

<!-- greyfinch-sync:Marvisak/VIctory:comment:7122048599 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/364#issuecomment-7122048599). Originally posted by [jeremygold02](https://github.com/jeremygold02) on 2026-11-24T04:28:13Z. --- Tested PR #364 at head `e503756597cafb3ac6c259ec82458fa5c61215aa` (state at test time: OPEN). Pre-comment refresh: state OPEN, head unchanged at `e503756597cafb3ac6c259ec82458fa5c61215aa`. - `cargo test --locked -p victory-formats vlc_prefix_matches_binary_search` — passed (ok. 1 passed; 0 failed; 0 ignored; 0 measured; 24 filtered out; finished in 0.04s ); cases: xma::tests::vlc_prefix_matches_binary_search. Behavior covered: Checks prefix-table decoding matches the binary-search reference across all seven VLC books, prefix/code boundaries, random probes and bit alignments. Code: [crates/victory-formats/src/xma.rs](https://github.com/Marvisak/VIctory/blob/e503756597cafb3ac6c259ec82458fa5c61215aa/crates/victory-formats/src/xma.rs)
mercuryVM commented 2026-11-24 13:01:12 +00:00 (Migrated from github.com)

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

Review: APPROVED


Tested e503756 on Windows 11, RTX 5070, MSVC: cargo test -p victory-formats 21 passed, and cargo clippy -p victory-formats --all-targets is clean.

  • Differential check: I ran 20,000 seeded random inputs (mono and stereo Decoders, 8 to 608 bytes, random bit lengths) through Decoder::decode on mercuryVM/VIctory main and on this head. Both decoders give the same Ok and Err split (2,102 succeed, 17,898 fail), the same 15,391,232 output samples, and an identical hash over every sample. So the eight-bit prefix table is a pure speedup, with no change on the accept or error paths, malformed streams included.

AI-assisted (Claude Code).

<!-- greyfinch-sync:Marvisak/VIctory:review:6039492144 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/364#pullrequestreview-6039492144). Originally posted by [mercuryVM](https://github.com/mercuryVM) on 2026-11-24T12:50:44Z. **Review:** APPROVED --- Tested e503756 on Windows 11, RTX 5070, MSVC: `cargo test -p victory-formats` 21 passed, and `cargo clippy -p victory-formats --all-targets` is clean. - Differential check: I ran 20,000 seeded random inputs (mono and stereo `Decoder`s, 8 to 608 bytes, random bit lengths) through `Decoder::decode` on mercuryVM/VIctory main and on this head. Both decoders give the same Ok and Err split (2,102 succeed, 17,898 fail), the same 15,391,232 output samples, and an identical hash over every sample. So the eight-bit prefix table is a pure speedup, with no change on the accept or error paths, malformed streams included. AI-assisted (Claude Code).
Marvisak commented 2026-11-25 01:07:50 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by Marvisak on 2026-11-25T00:27:46Z.


Automated triage for the maintainers.

Needs work (small). The change in Vlc::new/Vlc::read (crates/victory-formats/src/xma.rs) looks correct: each 8-bit prefix maps to the range [last code <= prefix<<24, first code > prefix<<24|0xffffff), and since codes[0].0 == 0 the - 1 cannot underflow; vlc_prefix_matches_binary_search is a good equivalence test. To merge: run cargo fmt on the file (stray blank line after use super::*;, wrapping rustfmt would change); drop the ~120-line ignored vlc_prefix_benchmark or cut it to a few lines (figures belong in the description); and ideally give one end-to-end number, e.g. decoding a synthetic XMA stream, to show the lookup is worth 28 KiB of tables.

CI: no check runs on the head commit.


Generated by Claude Code

<!-- greyfinch-sync:Marvisak/VIctory:comment:7148534727 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/364#issuecomment-7148534727). Originally posted by [Marvisak](https://github.com/Marvisak) on 2026-11-25T00:27:46Z. --- <!-- victory-triage v1 head=e503756597cafb3ac6c259ec82458fa5c61215aa verdict=needs-work at=2026-11-25T00:22:20Z --> Automated triage for the maintainers. **Needs work (small).** The change in `Vlc::new`/`Vlc::read` (`crates/victory-formats/src/xma.rs`) looks correct: each 8-bit prefix maps to the range [last code <= prefix<<24, first code > prefix<<24|0xffffff), and since `codes[0].0 == 0` the `- 1` cannot underflow; `vlc_prefix_matches_binary_search` is a good equivalence test. To merge: run `cargo fmt` on the file (stray blank line after `use super::*;`, wrapping rustfmt would change); drop the ~120-line ignored `vlc_prefix_benchmark` or cut it to a few lines (figures belong in the description); and ideally give one end-to-end number, e.g. decoding a synthetic XMA stream, to show the lookup is worth 28 KiB of tables. CI: no check runs on the head commit. --- _Generated by [Claude Code](https://claude.ai/code)_
passerbyyhx commented 2026-11-26 06:03:50 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by passerbyyhx on 2026-11-26T05:54:08Z.


On Windows x86-64, the full decoder ran 1.244× faster (median of 12 paired parent/PR timings) on a synthetic non-silent stream made entirely of short VLC codes. Both builds produced the same 16,896 PCM bytes, including 8,446 nonzero samples out of 8,448.

I compared parent d2a4bae1 and PR e5037565 on an i9-14900HX with release Rust 1.98.1/MSVC. Each stream constructs a decoder, reads 16 mono 48 kHz XMA2 frames via the public packet/frame and decode APIs, and flushes it. A separate untimed count verified 368 one-bit scale codes and 2,048 six-bit vector codes per stream.

Each arm warmed up for 16 streams and then decoded 32,768 frames on the same logical CPU. I alternated the running order every pair. Median elapsed times were 470.526 ms (parent) and 380.160 ms (PR). The PR was faster in all 12 pairs, with individual ratios from 1.061× to 1.523×. CPU frequency and background load were unmeasured, and other code-length distributions may produce a different result.

The PR's original symbol benchmark, over three processes, gave median speed ratios of 6.966× for short codes and 1.033× for the all-code corpus. These are isolated lookup timings. Reproduction scripts, full measurements and PCM checksum.

<!-- greyfinch-sync:Marvisak/VIctory:comment:7183908067 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/364#issuecomment-7183908067). Originally posted by [passerbyyhx](https://github.com/passerbyyhx) on 2026-11-26T05:54:08Z. --- On Windows x86-64, the full decoder ran 1.244× faster (median of 12 paired parent/PR timings) on a synthetic non-silent stream made entirely of short VLC codes. Both builds produced the same 16,896 PCM bytes, including 8,446 nonzero samples out of 8,448. I compared parent `d2a4bae1` and PR `e5037565` on an i9-14900HX with release Rust 1.98.1/MSVC. Each stream constructs a decoder, reads 16 mono 48 kHz XMA2 frames via the public packet/frame and decode APIs, and flushes it. A separate untimed count verified 368 one-bit scale codes and 2,048 six-bit vector codes per stream. Each arm warmed up for 16 streams and then decoded 32,768 frames on the same logical CPU. I alternated the running order every pair. Median elapsed times were 470.526 ms (parent) and 380.160 ms (PR). The PR was faster in all 12 pairs, with individual ratios from 1.061× to 1.523×. CPU frequency and background load were unmeasured, and other code-length distributions may produce a different result. The PR's original symbol benchmark, over three processes, gave median speed ratios of 6.966× for short codes and 1.033× for the all-code corpus. These are isolated lookup timings. [Reproduction scripts, full measurements and PCM checksum](https://github.com/passerbyyhx/VIctory/tree/b1a924674140d0998e69fa3a6572eb881c795b48/tests/benchmarks/xma-vlc-windows).
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/364:upstream-pr/364'
git switch 'upstream-pr/364'

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