Formats: speed up XMA2 inverse MDCT by 24-29% #2795

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

@⁠brainbudt on GitHub · 2026-11-25 20:46 UTC · original
brainbudt:perf/xma-imdct → main · +112 −58 · 1 files · 1 commits · open

Every XMA2 subframe runs a half-length inverse MDCT across three fixed sizes (128, 256, 512), but the FFT processed its short stages in separate passes with runtime-sized scratch slices.

What changed

  • Use fixed-size stack buffers for the 128-, 256-, and 512-coefficient transforms.
  • Fuse the first three FFT stages within each 8-point block, preserving floating-point operation order.
  • Add direct f64 cosine-sum accuracy tests and a full mono frame test verifying every PCM sample through flush.
  • Pure safe Rust, no new dependencies, confined to crates/victory-formats/src/xma.rs (does not overlap with VLC changes in #2663).

Benchmark (imdct_throughput, median of 5 release runs per host, ns/transform)

Coefficients macOS ARM64 before → after Linux x86-64 before → after Max abs error vs f64
128 298 → 211 ns (-29.2%) 577 → 409 ns (-29.1%) 2.178e-7
256 602 → 440 ns (-26.9%) 1,159 → 876 ns (-24.4%) 4.974e-7
512 1,264 → 909 ns (-28.1%) 2,389 → 1,688 ns (-29.3%) 1.893e-6

Verification

  • cargo test --release -p victory-formats passed (39 passed, 5 ignored) on both macOS ARM64 and Linux x86-64, including the existing bitwise regression test.
  • cargo clippy -p victory-formats --all-targets -- -D warnings passed; Windows MSVC cargo check -p victory-formats --target x86_64-pc-windows-msvc --tests passed.

Reviewed with mistral-loop: 1 round with Mistral Large 4 and DeepSeek V4.1 Flash on high. Verdict: approved.

Created with gpt-6.2-astra in Codex (orchestrated with Gemini 4 Neon in T3 Code).

**@⁠brainbudt** on GitHub · 2026-11-25 20:46 UTC · [original](https://github.com/Marvisak/VIctory/pull/2795) `brainbudt:perf/xma-imdct` → `main` · +112 −58 · 1 files · 1 commits · open Every XMA2 subframe runs a half-length inverse MDCT across three fixed sizes (128, 256, 512), but the FFT processed its short stages in separate passes with runtime-sized scratch slices. ### What changed - Use fixed-size stack buffers for the 128-, 256-, and 512-coefficient transforms. - Fuse the first three FFT stages within each 8-point block, preserving floating-point operation order. - Add direct `f64` cosine-sum accuracy tests and a full mono frame test verifying every PCM sample through `flush`. - Pure safe Rust, no new dependencies, confined to `crates/victory-formats/src/xma.rs` (does not overlap with VLC changes in #2663). ### Benchmark (`imdct_throughput`, median of 5 release runs per host, ns/transform) | Coefficients | macOS ARM64 before → after | Linux x86-64 before → after | Max abs error vs `f64` | |---:|---:|---:|---:| | 128 | 298 → 211 ns (-29.2%) | 577 → 409 ns (-29.1%) | `2.178e-7` | | 256 | 602 → 440 ns (-26.9%) | 1,159 → 876 ns (-24.4%) | `4.974e-7` | | 512 | 1,264 → 909 ns (-28.1%) | 2,389 → 1,688 ns (-29.3%) | `1.893e-6` | ### Verification - `cargo test --release -p victory-formats` passed (**39 passed, 5 ignored**) on both macOS ARM64 and Linux x86-64, including the existing bitwise regression test. - `cargo clippy -p victory-formats --all-targets -- -D warnings` passed; Windows MSVC `cargo check -p victory-formats --target x86_64-pc-windows-msvc --tests` passed. Reviewed with mistral-loop: 1 round with Mistral Large 4 and DeepSeek V4.1 Flash on high. Verdict: approved. Created with gpt-6.2-astra in Codex (orchestrated with Gemini 4 Neon in T3 Code).
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 05:14:25 +00:00
Author
Owner

@⁠fuzeofficlal on GitHub · 2026-11-25 21:10 UTC · original

Reviewed and benchmarked 74529ee9 on Windows 11 against main (13f91ca).

Code review

  • Zero unsafe, clean stack specialization for the 3 XMA2 subframe sizes (128/256/512), and fusing stages 2 -> 4 -> 8 inside each 8-element chunk (as_chunks_mut::<8>()) stays strictly self-contained per chunk, producing bit-identical floating-point results to the multi-pass loop.

Verification & same-machine --release benchmark (rustc 1.98.1 MSVC)

  • cargo test --locked -p victory-formats: 39 passed, 0 failed, 5 ignored (tabled_imdct_gives_the_same_bits bit-exact across n ∈ {0, 2..=2048} × 16 seeds; fast_imdct_matches_the_direct_sum max abs error 2.18e-7 / 4.97e-7 / 1.89e-6).
  • cargo clippy --locked -p victory-formats --all-targets -- -D warnings: 0 warnings; cargo xtask policy: 184 files ok; clean git merge-tree vs 13f91ca.
  • imdct_throughput (--release, same machine vs 13f91ca):
    • n = 128: 337 ns -> 224 ns (-33.5% / 1.50x)
    • n = 256: 688 ns -> 485 ns (-29.5% / 1.42x)
    • n = 512: 1,501 ns -> 1,063 ns (-29.2% / 1.41x)
**@⁠fuzeofficlal** on GitHub · 2026-11-25 21:10 UTC · [original](https://github.com/Marvisak/VIctory/pull/2795#issuecomment-7174717092) Reviewed and benchmarked `74529ee9` on Windows 11 against `main` (`13f91ca`). ### Code review - Zero `unsafe`, clean stack specialization for the 3 XMA2 subframe sizes (`128`/`256`/`512`), and fusing stages `2 -> 4 -> 8` inside each 8-element chunk (`as_chunks_mut::<8>()`) stays strictly self-contained per chunk, producing bit-identical floating-point results to the multi-pass loop. ### Verification & same-machine `--release` benchmark (`rustc 1.98.1` MSVC) - `cargo test --locked -p victory-formats`: **39 passed, 0 failed, 5 ignored** (`tabled_imdct_gives_the_same_bits` bit-exact across `n ∈ {0, 2..=2048}` × 16 seeds; `fast_imdct_matches_the_direct_sum` max abs error `2.18e-7` / `4.97e-7` / `1.89e-6`). - `cargo clippy --locked -p victory-formats --all-targets -- -D warnings`: **0 warnings**; `cargo xtask policy`: **184 files ok**; clean `git merge-tree` vs `13f91ca`. - `imdct_throughput` (`--release`, same machine vs `13f91ca`): - `n = 128`: `337 ns` -> **`224 ns`** (**-33.5% / 1.50x**) - `n = 256`: `688 ns` -> **`485 ns`** (**-29.5% / 1.42x**) - `n = 512`: `1,501 ns` -> **`1,063 ns`** (**-29.2% / 1.41x**)
Author
Owner

@⁠Marvisak on GitHub · 2026-11-25 23:22 UTC · original

Automated triage for the maintainers.

This PR splits imdct_half (crates/victory-formats/src/xma.rs) into Rotations::transform, using fixed stack buffers for the three XMA2 subframe sizes (128/256/512, the only sizes the decoder produces with SUBFRAME_SAMPLES = 128 and FRAME_SAMPLES = 512). It also fuses the first three butterfly stages within each 8-point block in butterflies. Those stages never cross an 8-point block, so the arithmetic and its order are unchanged, and the existing tabled_imdct_gives_the_same_bits test still holds. The accuracy test is tightened, and a full-frame decode test is added. CI passes, and an independent Windows benchmark in the thread shows a similar speedup.

This is a recommendation for a maintainer to look, not an approval.


Generated by Claude Code

**@⁠Marvisak** on GitHub · 2026-11-25 23:22 UTC · [original](https://github.com/Marvisak/VIctory/pull/2795#issuecomment-7177475624) <!-- victory-triage v1 head=74529ee920d1c4c2ff51c34c5a2a27d787fde16f verdict=merge-candidate at=2026-11-25T23:22:28Z --> Automated triage for the maintainers. This PR splits `imdct_half` (`crates/victory-formats/src/xma.rs`) into `Rotations::transform`, using fixed stack buffers for the three XMA2 subframe sizes (128/256/512, the only sizes the decoder produces with `SUBFRAME_SAMPLES` = 128 and `FRAME_SAMPLES` = 512). It also fuses the first three butterfly stages within each 8-point block in `butterflies`. Those stages never cross an 8-point block, so the arithmetic and its order are unchanged, and the existing `tabled_imdct_gives_the_same_bits` test still holds. The accuracy test is tightened, and a full-frame decode test is added. CI passes, and an independent Windows benchmark in the thread shows a similar speedup. This is a recommendation for a maintainer to look, not an approval. --- _Generated by [Claude Code](https://claude.ai/code)_
Author
Owner

@⁠kotekompotek on GitHub · 2026-11-25 21:17 UTC · original · reviewed

Bit-exactness verified by construction: butterflies at len ≤ 8 are block-local (each chunk only touches its own data), so running stages 2/4/8 per 8-point block instead of per-stage across all blocks is the same f64 operations in the same intra-block order. Stages ≥ 16 go through the unchanged stage path, and the fixed-size dispatch preserves the log2(n) - 7 indexing (128→0, 256→1, 512→2). The non-multiple-of-8 fallback keeps the old generic loop. Reference test upgraded to f64 direct-sum — good. LGTM.

**@⁠kotekompotek** on GitHub · 2026-11-25 21:17 UTC · [original](https://github.com/Marvisak/VIctory/pull/2795#pullrequestreview-6065108243) · **reviewed** Bit-exactness verified by construction: butterflies at len ≤ 8 are block-local (each chunk only touches its own data), so running stages 2/4/8 per 8-point block instead of per-stage across all blocks is the same f64 operations in the same intra-block order. Stages ≥ 16 go through the unchanged `stage` path, and the fixed-size dispatch preserves the `log2(n) - 7` indexing (128→0, 256→1, 512→2). The non-multiple-of-8 fallback keeps the old generic loop. Reference test upgraded to f64 direct-sum — good. LGTM.
Author
Owner

@⁠Dukestar69 on GitHub · 2026-11-26 03:12 UTC · original · approved these changes

Tested merged onto origin/main (63bcca4) on Windows 11 x64:

  • cargo test -p victory-formats --lib xma::: 21 passed, 0 failed, 3 ignored (including fast_imdct_matches_the_direct_sum and decode_single_bin_xma_frame).
  • cargo clippy -p victory-formats --all-targets -- -D warnings: clean.
  • cargo xtask policy: 191 files ok.

Synthetic tests only (no game files used). AI-assisted (Gemini 4 Neon).

**@⁠Dukestar69** on GitHub · 2026-11-26 03:12 UTC · [original](https://github.com/Marvisak/VIctory/pull/2795#pullrequestreview-6068047780) · **approved these changes** Tested merged onto `origin/main` (`63bcca4`) on Windows 11 x64: - `cargo test -p victory-formats --lib xma::`: **21 passed, 0 failed, 3 ignored** (including `fast_imdct_matches_the_direct_sum` and `decode_single_bin_xma_frame`). - `cargo clippy -p victory-formats --all-targets -- -D warnings`: clean. - `cargo xtask policy`: 191 files ok. Synthetic tests only (no game files used). AI-assisted (Gemini 4 Neon).
Author
Owner

@⁠futurepro62 on GitHub · 2026-11-26 13:49 UTC · original

NVIDIA full-suite run, to go with the reports above by Dukestar69, fuzeofficlal, since none of them names a GPU run. Tested exact head 74529ee (on its base 13f91ca).

Tested

  • NVIDIA GeForce RTX 5070 Ti 16 GB (Blackwell, 32-lane waves only, mesh shaders), driver 621.40, Ryzen 7 9800X3D, Windows 11 Home 26300, Rust 1.98.1 MSVC (pinned), dxil.dll from Windows SDK 10.0.26100 (the loader's default), debug.
  • cargo build --workspace --locked, then cargo test --workspace --locked --no-fail-fast -- --test-threads=4 --skip emit::memory_tests::heap_direct_texture_reads_mark_their_heap_index --skip descriptor::host::tests::dynamic_slots_are_held_until_released --skip memory::tests::lists_share_a_table_and_copies_keep_their_views --skip emit::stride_tests::a_heap_buffer_read_by_element_takes_the_views_stride --skip emit::stride_tests::a_heap_buffer_read_where_no_buffer_view_is_reads_zeros --skip emit::synthetic_tests::gathers_at_a_level_match_the_hardware: 774 passed, 0 failed, 9 ignored.
  • The new tests ran and passed: decode_single_bin_xma_frame.
  • --skip leaves out tests that fail on main 63bcca4 on this card too, not because of this PR: heap_direct_texture_reads_mark_their_heap_index ends the victory-shader test process with 0xc0000005 (#2571), so without the skip the rest of that crate never reports; dynamic_slots_are_held_until_released, lists_share_a_table_and_copies_keep_their_views and the two stride_tests hit the 1,000,000-view heap cap of NVIDIA drivers (#1835, #2317); gathers_at_a_level_match_the_hardware fails on lane 0 (#1744).

Not tested

  • AMD (the tests take the default adapter), a release build, the title (test run only).

AI use

  • Claude Code (Claude Opus 5.5) ran the tests and wrote this report. I checked it before posting.
**@⁠futurepro62** on GitHub · 2026-11-26 13:49 UTC · [original](https://github.com/Marvisak/VIctory/pull/2795#issuecomment-7193898903) NVIDIA full-suite run, to go with the reports above by Dukestar69, fuzeofficlal, since none of them names a GPU run. Tested exact head `74529ee` (on its base `13f91ca`). **Tested** - NVIDIA GeForce RTX 5070 Ti 16 GB (Blackwell, 32-lane waves only, mesh shaders), driver 621.40, Ryzen 7 9800X3D, Windows 11 Home 26300, Rust 1.98.1 MSVC (pinned), `dxil.dll` from Windows SDK 10.0.26100 (the loader's default), debug. - `cargo build --workspace --locked`, then `cargo test --workspace --locked --no-fail-fast -- --test-threads=4 --skip emit::memory_tests::heap_direct_texture_reads_mark_their_heap_index --skip descriptor::host::tests::dynamic_slots_are_held_until_released --skip memory::tests::lists_share_a_table_and_copies_keep_their_views --skip emit::stride_tests::a_heap_buffer_read_by_element_takes_the_views_stride --skip emit::stride_tests::a_heap_buffer_read_where_no_buffer_view_is_reads_zeros --skip emit::synthetic_tests::gathers_at_a_level_match_the_hardware`: **774 passed, 0 failed, 9 ignored**. - The new tests ran and passed: `decode_single_bin_xma_frame`. - `--skip` leaves out tests that fail on main `63bcca4` on this card too, not because of this PR: `heap_direct_texture_reads_mark_their_heap_index` ends the `victory-shader` test process with `0xc0000005` (#2571), so without the skip the rest of that crate never reports; `dynamic_slots_are_held_until_released`, `lists_share_a_table_and_copies_keep_their_views` and the two `stride_tests` hit the 1,000,000-view heap cap of NVIDIA drivers (#1835, #2317); `gathers_at_a_level_match_the_hardware` fails on lane 0 (#1744). **Not tested** - AMD (the tests take the default adapter), a release build, the title (test run only). **AI use** - Claude Code (Claude Opus 5.5) ran the tests and wrote this report. I checked it before posting.
Author
Owner

@⁠fuzeofficlal on GitHub · 2026-11-26 14:15 UTC · original · approved these changes

The math, buffer sizing, and floating-point evaluation order look sound. Stages 2, 4, and 8 only couple elements within aligned 8-point blocks (xma.rs:440-493), so fusing them per 8-point chunk preserves every butterfly input, twiddle offset, and rounding order before continuing at length 16..=m. The cached 128/256/512 stack-buffer paths match the decoder's subframe sizes without heap allocations, and tabled_imdct_gives_the_same_bits plus the new decode_single_bin_xma_frame fixture cover both bitwise transform equivalence and the decoder boundary.

Note: the checked-in imdct_throughput benchmark compares against imdct_half_untabled (4.8x/5.2x/5.2x here), so I also measured --release against origin/main's previous tabled imdct_half directly below (also verified clean 3-way merge with origin/main and with #2663).

Local verification at 74529ee9: xtask policy (184 files ok), cargo clippy -p victory-formats --all-targets (default and --all-features, 0 warnings), and cargo test -p victory-formats (debug and --release: 39 passed, 0 failed, 5 ignored) all passed. Release imdct_throughput on Ryzen 9 9950X improved tabled imdct_half over origin/main (63bcca43) by 24.6% at n=128 (329 ns -> 248 ns), 32.8% at n=256 (692 ns -> 465 ns), and 34.8% at n=512 (1470 ns -> 958 ns). Approving.

**@⁠fuzeofficlal** on GitHub · 2026-11-26 14:15 UTC · [original](https://github.com/Marvisak/VIctory/pull/2795#pullrequestreview-6075629464) · **approved these changes** The math, buffer sizing, and floating-point evaluation order look sound. Stages 2, 4, and 8 only couple elements within aligned 8-point blocks (`xma.rs:440-493`), so fusing them per 8-point chunk preserves every butterfly input, twiddle offset, and rounding order before continuing at length `16..=m`. The cached 128/256/512 stack-buffer paths match the decoder's subframe sizes without heap allocations, and `tabled_imdct_gives_the_same_bits` plus the new `decode_single_bin_xma_frame` fixture cover both bitwise transform equivalence and the decoder boundary. Note: the checked-in `imdct_throughput` benchmark compares against `imdct_half_untabled` (4.8x/5.2x/5.2x here), so I also measured `--release` against `origin/main`'s previous tabled `imdct_half` directly below (also verified clean 3-way merge with `origin/main` and with #2663). Local verification at `74529ee9`: `xtask policy` (`184 files ok`), `cargo clippy -p victory-formats --all-targets` (`default` and `--all-features`, 0 warnings), and `cargo test -p victory-formats` (debug and `--release`: `39 passed, 0 failed, 5 ignored`) all passed. Release `imdct_throughput` on Ryzen 9 9950X improved tabled `imdct_half` over `origin/main` (`63bcca43`) by **24.6%** at `n=128` (`329 ns -> 248 ns`), **32.8%** at `n=256` (`692 ns -> 465 ns`), and **34.8%** at `n=512` (`1470 ns -> 958 ns`). Approving.
Author
Owner

@⁠rezioliver-collab on GitHub · 2026-11-26 18:02 UTC · original

I measured how much of the IMDCT gain reaches PCM output on Apple M1. A 64-frame mono stream at 48 kHz went from 3.845 to 3.229 µs per frame when every frame carried one nonzero coefficient. The all-silent control stayed within 0.003 µs/frame.

Frames carrying a coefficient Base, µs/frame PR, µs/frame
0% 0.903 0.906
25% 1.625 1.487
50% 2.352 2.071
100% 3.845 3.229

I interleaved the PR’s synthetic single-bin frame with coefficient-free frames, kept decoder state across all 64 frames, and flushed once at the end. Timings include overlap and PCM writes into a preallocated buffer. All 33,024 signed 16-bit samples per stream, including the flush tail, matched byte for byte on 13f91cae and 74529ee9. Medians from 12 alternating release pairs, Rust 1.98.1 on macOS 14.7.

**@⁠rezioliver-collab** on GitHub · 2026-11-26 18:02 UTC · [original](https://github.com/Marvisak/VIctory/pull/2795#issuecomment-7199977174) I measured how much of the IMDCT gain reaches PCM output on Apple M1. A 64-frame mono stream at 48 kHz went from **3.845 to 3.229 µs per frame** when every frame carried one nonzero coefficient. The all-silent control stayed within 0.003 µs/frame. | Frames carrying a coefficient | Base, µs/frame | PR, µs/frame | |---:|---:|---:| | 0% | 0.903 | 0.906 | | 25% | 1.625 | 1.487 | | 50% | 2.352 | 2.071 | | 100% | 3.845 | 3.229 | I interleaved the PR’s synthetic single-bin frame with coefficient-free frames, kept decoder state across all 64 frames, and flushed once at the end. Timings include overlap and PCM writes into a preallocated buffer. All **33,024 signed 16-bit samples per stream**, including the flush tail, matched byte for byte on `13f91cae` and `74529ee9`. Medians from 12 alternating release pairs, Rust 1.98.1 on macOS 14.7.
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/2795/head:github-bot/gh-2795'
git switch 'github-bot/gh-2795'

Merge

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