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

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

@⁠pedrofort on GitHub · 2026-11-24 02:37 UTC · original
pedrofort:optimize-xma-vlc → main · +207 −2 · 1 files · 1 commits · open

<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.

**@⁠pedrofort** on GitHub · 2026-11-24 02:37 UTC · [original](https://github.com/Marvisak/VIctory/pull/364) `pedrofort:optimize-xma-vlc` → `main` · +207 −2 · 1 files · 1 commits · open <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>
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 04:01:01 +00:00
Author
Owner

@⁠jeremygold02 on GitHub · 2026-11-24 04:28 UTC · original

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

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

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

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

**@⁠Marvisak** on GitHub · 2026-11-25 00:27 UTC · [original](https://github.com/Marvisak/VIctory/pull/364#issuecomment-7148534727) <!-- 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)_
Author
Owner

@⁠passerbyyhx on GitHub · 2026-11-26 05:54 UTC · original

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.

**@⁠passerbyyhx** on GitHub · 2026-11-26 05:54 UTC · [original](https://github.com/Marvisak/VIctory/pull/364#issuecomment-7183908067) 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 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/364/head:github-bot/gh-364'
git switch 'github-bot/gh-364'

Merge

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