Audio/XMA: box voices, skip inactive slots in pump, and reuse per-frame scratch #3383

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

@⁠Desamodeth on GitHub · 2026-11-26 21:12 UTC · original
Desamodeth:perf/xma-voice-storage-and-scratch → main · +309 −66 · 2 files · 1 commits · open

Claimed in #1633: #1633

Problem

On main (63bcca43c6a00a758510398fa43096ce7e4f7df0):

  1. Voice embeds Option<xma::Decoder> (14,304 B) inline (size_of::<Voice>() = 14,448 B), and Xma::voices stores Vec<Mutex<Option<Voice>>> contiguously. Allocating 512 XMA contexts (as in startup_as_fmod_does_it) immediately allocates and permanently retains 7,405,568 B (~7.06 MiB) even when idle or after a burst of voices is disabled.
  2. Xma::pump() scans all 512 voice slots on every wakeup and every 2 ms idle poll, locking/unlocking 512 mutexes spaced ~14.5 KiB apart across 7.06 MiB even when 0 or 16 voices are active.
  3. Every decoded XMA frame performs two heap allocations and frees: xma::read_frame allocates a fresh Vec<u8> and audio::decode allocates a fresh Vec<i16> (plus a third Vec<i16> on flush).
  4. Every xma::Decoder::new recomputes and stores 2,832 B of constant scale-factor band tables (num_sfb, sfb_offsets, sf_offsets) inline per voice.

Changes

  • crates/victory-formats/src/xma.rs:
    • Add FrameMeta and read_frame_into(data, start, out) so callers can reuse encoded-frame byte capacity across frames; implement existing read_frame on top of it (next_frame untouched).
    • Share constant BandTables across the four clamped sample-rate buckets (24 / 32 / 44.1 / 48 kHz) via OnceLock, shrinking Decoder from 14,304 B to 11,480 B and avoiding per-voice table construction.
    • Pre-reserve exact sample counts in Decoder::decode and Decoder::flush so fresh scratch Vec<i16> buffers grow in one allocation instead of geometric reallocations.
  • crates/victory-hle/src/modules/audio.rs:
    • Box Voice and xma::Decoder (Vec<Mutex<Option<Box<Voice>>>>; size_of::<Voice>() = 208 B), freeing decoder and scratch memory when a voice is disabled.
    • Track enabled voice indices in Xma::active: Vec<AtomicU64> (updated under the matching voice mutex with Release ordering, read with Acquire in Xma::pump) so pump only locks active slots.
    • Reuse per-voice frame: Vec<u8> and scratch: Vec<i16> across decode and flush.

Separate from #2788 (lock split / MMCSS), #1880 (reserve refill), #2663 (VLC / quant LUT), #2795 (IMDCT), and #2423 (Opus APU).

Measurements (Windows 11 Pro 10.0.26300, Ryzen 7 9800X3D, rustc 1.98.1, --release, CPU-only, synthetic streams)

Compared standalone --release binaries of main (63bcca43) vs this branch (23e62b7c) using identical synthetic 48 kHz non-silent XMA2 streams (512 allocated contexts, 4-block rings) and a counting GlobalAlloc wrapper, run interleaved in 6x A/B forward + 6x B/A reverse (12 runs each).

Memory & allocations

Metric main (63bcca43) Patched (23e62b7c) Delta
Idle XMA heap (512 contexts, excl. 32 KiB APU array) 7,405,568 B (7.06 MiB) 8,256 B (8.06 KiB) -99.9% (897.0x less)
Burst 64 active stereo voices — peak total XMA heap 7,538,727 B (7.19 MiB) 1,020,928 B (0.97 MiB) -86.5% (7.38x less)
Post-burst retained XMA heap (after disabling 64) 7,405,568 B (7.06 MiB) 8,256 B (8.06 KiB) -99.9% (897.0x less)
Steady-state 1,000 stereo frames (after warmup) 2,000 allocs (2,087,031 B) 1 alloc (78 B) (0 once max frame size seen) -99.95% allocs
Regression check: 0-frame enable+disable churn (32,000 pairs) 0 allocs 32,000 allocs (+1 Box<Voice> / pair) +1 alloc / pair
Regression check: 1-ring one-shot SFX (960 voices) 2,880 allocs (3.0 / voice) 4,800 allocs (5.0 / voice) +2 allocs / voice

CPU timings (median [min..max] over 12 runs; forward A/B vs reverse B/A medians)

Scenario main Patched Delta Fwd A/B vs Rev A/B
Idle Xma::pump() (0/512 active) 4,115.35 ns [4,108..4,223] 59.00 ns [58.7..59.6] -98.6% (69.8x) 4,114.7/58.9 vs 4,117.2/59.0
Sparse Xma::pump() (16/512 active, rings full) 4,116.70 ns [4,114..4,156] 189.15 ns [188.7..191.8] -95.4% (21.8x) 4,115.8/189.2 vs 4,116.8/189.2
Streaming 16 stereo voices (decode + refill / round) 89.97 us [87.8..94.9] 83.06 us [82.9..86.3] -7.7% (1.08x) 90.40/83.04 vs 89.50/83.15
Dense Xma::pump() (512/512 active, rings full) 4,342.95 ns [4,331..4,376] 4,126.85 ns [4,122..4,571] -5.0% (1.05x) 4,338.5/4,128.8 vs 4,347.2/4,126.9
0-frame enable+disable churn (ns / pair) 170.05 ns [155.8..174.4] 141.00 ns [140.5..147.6] -17.1% (1.21x) 170.4/141.2 vs 170.1/141.0
1-ring one-shot SFX enable+decode+disable (us / voice) 10.11 us [9.78..10.19] 9.28 us [9.21..9.81] -8.2% (1.09x) 10.06/9.28 vs 10.11/9.28

Trade-off

Boxing Voice and Decoder trades 0-frame/1-frame voice churn allocator traffic (+1 Box<Voice> on enable, +1 Box<Decoder> on first frame) for ~7.05 MiB lower idle/post-burst heap retention, 0 steady-state per-frame allocations, and skipping inactive slots in Xma::pump(). Even in the 0-frame and 1-ring churn tests, wall time stayed faster on this host because enable no longer initializes a 14.4 KiB inline struct and Decoder::new no longer rebuilds band tables, though heavy multi-threaded allocator contention on very short-lived voices is the main potential regression case.

Correctness & Checks (CPU-only, no GPU/WARP, no game files)

  • Bit-exact PCM output verified across all 24 benchmark runs:
    • 64-frame non-silent stereo stream + flush: 66,048 samples, FNV-1a 0x29b87c8ab9c1311d, peak 2384, 0 bad frames (identical).
    • Looping non-silent mono stream (loops=2, start_skip=1, loop_skip=2) + flush: 19,584 samples, FNV-1a 0x6deafbf1c8a3834d, peak 1302, 0 bad frames (identical).
  • cargo test --locked -p victory-formats --lib: 48 passed, 0 failed, 5 ignored (incl. read_frame_into_matches_read_frame_and_reuses_capacity and shared_band_tables_cover_all_sample_rate_buckets).
  • cargo test --release --locked -p victory-formats --lib xma::: 22 passed, 0 failed, 3 ignored.
  • cargo test --locked -p victory-hle --lib modules::audio:: -- --test-threads=1 (debug & release): 9 passed, 0 failed (incl. xma_active_bitset_tracks_words_and_reuses_voice_scratch).
  • cargo test --locked -p victory-hle --lib modules::apu:: (2 passed) and modules::dsp:: (1 passed).
  • cargo clippy --locked -p victory-formats -p victory-hle --all-targets -- -D warnings: clean.
  • cargo check --workspace --all-targets --locked: clean.
  • cargo xtask policy: 191 files ok.

AI-assisted with Gemini 4 Neon.

**@⁠Desamodeth** on GitHub · 2026-11-26 21:12 UTC · [original](https://github.com/Marvisak/VIctory/pull/3383) `Desamodeth:perf/xma-voice-storage-and-scratch` → `main` · +309 −66 · 2 files · 1 commits · open Claimed in #1633: https://git.gtahub.st/github/Marvisak--VIctory/issues/1633 ### Problem On `main` (`63bcca43c6a00a758510398fa43096ce7e4f7df0`): 1. `Voice` embeds `Option<xma::Decoder>` (`14,304 B`) inline (`size_of::<Voice>() = 14,448 B`), and `Xma::voices` stores `Vec<Mutex<Option<Voice>>>` contiguously. Allocating 512 XMA contexts (as in `startup_as_fmod_does_it`) immediately allocates and permanently retains **7,405,568 B (~7.06 MiB)** even when idle or after a burst of voices is disabled. 2. `Xma::pump()` scans all 512 voice slots on every wakeup and every 2 ms idle poll, locking/unlocking 512 mutexes spaced ~14.5 KiB apart across 7.06 MiB even when 0 or 16 voices are active. 3. Every decoded XMA frame performs two heap allocations and frees: `xma::read_frame` allocates a fresh `Vec<u8>` and `audio::decode` allocates a fresh `Vec<i16>` (plus a third `Vec<i16>` on `flush`). 4. Every `xma::Decoder::new` recomputes and stores 2,832 B of constant scale-factor band tables (`num_sfb`, `sfb_offsets`, `sf_offsets`) inline per voice. ### Changes - **`crates/victory-formats/src/xma.rs`**: - Add `FrameMeta` and `read_frame_into(data, start, out)` so callers can reuse encoded-frame byte capacity across frames; implement existing `read_frame` on top of it (`next_frame` untouched). - Share constant `BandTables` across the four clamped sample-rate buckets (24 / 32 / 44.1 / 48 kHz) via `OnceLock`, shrinking `Decoder` from 14,304 B to 11,480 B and avoiding per-voice table construction. - Pre-reserve exact sample counts in `Decoder::decode` and `Decoder::flush` so fresh scratch `Vec<i16>` buffers grow in one allocation instead of geometric reallocations. - **`crates/victory-hle/src/modules/audio.rs`**: - Box `Voice` and `xma::Decoder` (`Vec<Mutex<Option<Box<Voice>>>>`; `size_of::<Voice>() = 208 B`), freeing decoder and scratch memory when a voice is disabled. - Track enabled voice indices in `Xma::active: Vec<AtomicU64>` (updated under the matching voice mutex with `Release` ordering, read with `Acquire` in `Xma::pump`) so `pump` only locks active slots. - Reuse per-voice `frame: Vec<u8>` and `scratch: Vec<i16>` across `decode` and `flush`. Separate from #2788 (lock split / MMCSS), #1880 (reserve refill), #2663 (VLC / quant LUT), #2795 (IMDCT), and #2423 (Opus APU). ### Measurements (Windows 11 Pro 10.0.26300, Ryzen 7 9800X3D, rustc 1.98.1, `--release`, CPU-only, synthetic streams) Compared standalone `--release` binaries of `main` (`63bcca43`) vs this branch (`23e62b7c`) using identical synthetic 48 kHz non-silent XMA2 streams (512 allocated contexts, 4-block rings) and a counting `GlobalAlloc` wrapper, run interleaved in **6x A/B forward + 6x B/A reverse** (12 runs each). #### Memory & allocations | Metric | `main` (`63bcca43`) | Patched (`23e62b7c`) | Delta | |---|---:|---:|---:| | Idle XMA heap (512 contexts, excl. 32 KiB APU array) | 7,405,568 B (7.06 MiB) | **8,256 B (8.06 KiB)** | **-99.9% (897.0x less)** | | Burst 64 active stereo voices — peak total XMA heap | 7,538,727 B (7.19 MiB) | **1,020,928 B (0.97 MiB)** | **-86.5% (7.38x less)** | | Post-burst retained XMA heap (after disabling 64) | 7,405,568 B (7.06 MiB) | **8,256 B (8.06 KiB)** | **-99.9% (897.0x less)** | | Steady-state 1,000 stereo frames (after warmup) | 2,000 allocs (2,087,031 B) | **1 alloc (78 B)** *(0 once max frame size seen)* | **-99.95% allocs** | | Regression check: 0-frame `enable`+`disable` churn (32,000 pairs) | 0 allocs | 32,000 allocs (+1 `Box<Voice>` / pair) | +1 alloc / pair | | Regression check: 1-ring one-shot SFX (960 voices) | 2,880 allocs (3.0 / voice) | 4,800 allocs (5.0 / voice) | +2 allocs / voice | #### CPU timings (median [min..max] over 12 runs; forward A/B vs reverse B/A medians) | Scenario | `main` | Patched | Delta | Fwd A/B vs Rev A/B | |---|---:|---:|---:|---:| | Idle `Xma::pump()` (0/512 active) | 4,115.35 ns [4,108..4,223] | **59.00 ns** [58.7..59.6] | **-98.6% (69.8x)** | 4,114.7/58.9 vs 4,117.2/59.0 | | Sparse `Xma::pump()` (16/512 active, rings full) | 4,116.70 ns [4,114..4,156] | **189.15 ns** [188.7..191.8] | **-95.4% (21.8x)** | 4,115.8/189.2 vs 4,116.8/189.2 | | Streaming 16 stereo voices (decode + refill / round) | 89.97 us [87.8..94.9] | **83.06 us** [82.9..86.3] | **-7.7% (1.08x)** | 90.40/83.04 vs 89.50/83.15 | | Dense `Xma::pump()` (512/512 active, rings full) | 4,342.95 ns [4,331..4,376] | **4,126.85 ns** [4,122..4,571] | **-5.0% (1.05x)** | 4,338.5/4,128.8 vs 4,347.2/4,126.9 | | 0-frame `enable`+`disable` churn (ns / pair) | 170.05 ns [155.8..174.4] | **141.00 ns** [140.5..147.6] | **-17.1% (1.21x)** | 170.4/141.2 vs 170.1/141.0 | | 1-ring one-shot SFX `enable`+decode+`disable` (us / voice) | 10.11 us [9.78..10.19] | **9.28 us** [9.21..9.81] | **-8.2% (1.09x)** | 10.06/9.28 vs 10.11/9.28 | #### Trade-off Boxing `Voice` and `Decoder` trades 0-frame/1-frame voice churn allocator traffic (+1 `Box<Voice>` on `enable`, +1 `Box<Decoder>` on first frame) for ~7.05 MiB lower idle/post-burst heap retention, 0 steady-state per-frame allocations, and skipping inactive slots in `Xma::pump()`. Even in the 0-frame and 1-ring churn tests, wall time stayed faster on this host because `enable` no longer initializes a 14.4 KiB inline struct and `Decoder::new` no longer rebuilds band tables, though heavy multi-threaded allocator contention on very short-lived voices is the main potential regression case. ### Correctness & Checks (CPU-only, no GPU/WARP, no game files) - Bit-exact PCM output verified across all 24 benchmark runs: - 64-frame non-silent stereo stream + flush: `66,048` samples, FNV-1a `0x29b87c8ab9c1311d`, peak `2384`, 0 bad frames (identical). - Looping non-silent mono stream (`loops=2`, `start_skip=1`, `loop_skip=2`) + flush: `19,584` samples, FNV-1a `0x6deafbf1c8a3834d`, peak `1302`, 0 bad frames (identical). - `cargo test --locked -p victory-formats --lib`: 48 passed, 0 failed, 5 ignored (incl. `read_frame_into_matches_read_frame_and_reuses_capacity` and `shared_band_tables_cover_all_sample_rate_buckets`). - `cargo test --release --locked -p victory-formats --lib xma::`: 22 passed, 0 failed, 3 ignored. - `cargo test --locked -p victory-hle --lib modules::audio:: -- --test-threads=1` (debug & release): 9 passed, 0 failed (incl. `xma_active_bitset_tracks_words_and_reuses_voice_scratch`). - `cargo test --locked -p victory-hle --lib modules::apu::` (2 passed) and `modules::dsp::` (1 passed). - `cargo clippy --locked -p victory-formats -p victory-hle --all-targets -- -D warnings`: clean. - `cargo check --workspace --all-targets --locked`: clean. - `cargo xtask policy`: 191 files ok. AI-assisted with Gemini 4 Neon.
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 21:13:45 +00:00
This pull request can be merged automatically.
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/3383/head:github-bot/gh-3383'
git switch 'github-bot/gh-3383'

Merge

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