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

Open
Desamodeth wants to merge 1 commit from upstream-pr/3383 into main
Desamodeth commented 2026-11-26 21:12:51 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by Desamodeth on 2026-11-26T21:12:51Z.


Claimed in #1633: https://github.com/Marvisak/VIctory/issues/1633#issuecomment-7203278391

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.

<!-- greyfinch-sync:Marvisak/VIctory:issue:5517210607 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/3383). Originally posted by [Desamodeth](https://github.com/Desamodeth) on 2026-11-26T21:12:51Z. --- Claimed in #1633: https://github.com/Marvisak/VIctory/issues/1633#issuecomment-7203278391 ### 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.
Dukestar69 commented 2026-11-26 21:29:55 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by Dukestar69 on 2026-11-26T21:29:55Z.

Review: APPROVED


LGTM — verified on Windows against origin/main (63bcca4).

  • Boxing XMA voices, tracking active voice slots via bitset in pump, sharing band tables by sample-rate bucket, and reusing per-frame scratch buffers cleanly reduce memory footprint and per-frame allocations.
  • cargo test -p victory-formats --lib xma:: -- --test-threads=1: 22 passed, 0 failed (3 ignored; including read_frame_into_matches_read_frame_and_reuses_capacity and shared_band_tables_cover_all_sample_rate_buckets).
  • cargo test -p victory-hle --lib modules::audio:: -- --test-threads=1: 9 passed, 0 failed (including xma_active_bitset_tracks_words_and_reuses_voice_scratch).
  • cargo clippy -p victory-formats -p victory-hle --all-targets -- -D warnings: clean.
  • cargo xtask policy: 191 files ok.

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

<!-- greyfinch-sync:Marvisak/VIctory:review:6080655001 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/3383#pullrequestreview-6080655001). Originally posted by [Dukestar69](https://github.com/Dukestar69) on 2026-11-26T21:29:55Z. **Review:** APPROVED --- LGTM — verified on Windows against `origin/main` (`63bcca4`). - Boxing XMA voices, tracking active voice slots via bitset in `pump`, sharing band tables by sample-rate bucket, and reusing per-frame scratch buffers cleanly reduce memory footprint and per-frame allocations. - `cargo test -p victory-formats --lib xma:: -- --test-threads=1`: 22 passed, 0 failed (3 ignored; including `read_frame_into_matches_read_frame_and_reuses_capacity` and `shared_band_tables_cover_all_sample_rate_buckets`). - `cargo test -p victory-hle --lib modules::audio:: -- --test-threads=1`: 9 passed, 0 failed (including `xma_active_bitset_tracks_words_and_reuses_voice_scratch`). - `cargo clippy -p victory-formats -p victory-hle --all-targets -- -D warnings`: clean. - `cargo xtask policy`: 191 files ok. Synthetic tests only (no game files used). AI-assisted (Gemini 4 Neon).
passerbyyhx commented 2026-11-26 21:35:50 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by passerbyyhx on 2026-11-26T21:35:50Z.


I measured the short-lived-voice case at 23e62b7c against 63bcca43 on Windows x64 (Core i9-14900HX, Rust 1.98.1 release). There were 1, 2 or 4 concurrent command workers sharing the XMA instance and a single pump thread. Each worker owned one voice. Each configuration used 12 adjacent base/head pairs, six in each order, with 1,024 waves per process. Timing binaries had no allocator instrumentation.

Workers Paired throughput ratio, head/base (min–max) P95 voice lifetime, base → head Pairs favoring head
1 1.203× (0.731–1.811) 160.9 → 142.2 μs 9/12
2 0.980× (0.773–1.303) 236.9 → 246.0 μs 6/12
4 0.966× (0.875–1.485) 411.5 → 416.2 μs 5/12

These are production full-pump results. The selected-slot control, which bypassed the inactive-slot scan while retaining the codec and shared XMA path, gave ratios of 1.051×, 0.973× and 1.016×. The one-worker median favors the change with wide dispersion. This workload did not establish a throughput change at two or four workers.

Separate warmed binaries counted Rust global allocation/reallocation calls in the command and pump spans. The command span went from 0 to 1 per voice; the pump went from 13 to 4 (4.25 at four workers). Total calls were 13 → 5–5.25. Four-worker PCM and voice/channel ownership matched byte-for-byte across both revisions and both pump modes: 20,480 PCM bytes plus 40 metadata bytes, SHA-256 b538f6b1f8f5b90a4b06af4b216fc85e0e76a0d387359d092b214d394025561d. The one- and two-worker files matched as well. The counter measures allocation traffic; mutex wait time, allocator wait time, and asynchronous real-time playback were not measured.

Base benchmark fixture · PR benchmark fixture.

AI-assisted with Codex.

<!-- greyfinch-sync:Marvisak/VIctory:comment:7203878575 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/3383#issuecomment-7203878575). Originally posted by [passerbyyhx](https://github.com/passerbyyhx) on 2026-11-26T21:35:50Z. --- I measured the short-lived-voice case at `23e62b7c` against `63bcca43` on Windows x64 (Core i9-14900HX, Rust 1.98.1 release). There were 1, 2 or 4 concurrent command workers sharing the XMA instance and a single pump thread. Each worker owned one voice. Each configuration used 12 adjacent base/head pairs, six in each order, with 1,024 waves per process. Timing binaries had no allocator instrumentation. | Workers | Paired throughput ratio, head/base (min–max) | P95 voice lifetime, base → head | Pairs favoring head | |---:|---:|---:|---:| | 1 | 1.203× (0.731–1.811) | 160.9 → 142.2 μs | 9/12 | | 2 | 0.980× (0.773–1.303) | 236.9 → 246.0 μs | 6/12 | | 4 | 0.966× (0.875–1.485) | 411.5 → 416.2 μs | 5/12 | These are production full-pump results. The selected-slot control, which bypassed the inactive-slot scan while retaining the codec and shared XMA path, gave ratios of 1.051×, 0.973× and 1.016×. The one-worker median favors the change with wide dispersion. This workload did not establish a throughput change at two or four workers. Separate warmed binaries counted Rust global allocation/reallocation calls in the command and pump spans. The command span went from 0 to 1 per voice; the pump went from 13 to 4 (4.25 at four workers). Total calls were 13 → 5–5.25. Four-worker PCM and voice/channel ownership matched byte-for-byte across both revisions and both pump modes: 20,480 PCM bytes plus 40 metadata bytes, SHA-256 `b538f6b1f8f5b90a4b06af4b216fc85e0e76a0d387359d092b214d394025561d`. The one- and two-worker files matched as well. The counter measures allocation traffic; mutex wait time, allocator wait time, and asynchronous real-time playback were not measured. [Base benchmark fixture](https://github.com/passerbyyhx/VIctory/commit/badfc3f7c2a50ff5648e2f60a67318f65f5ea5db) · [PR benchmark fixture](https://github.com/passerbyyhx/VIctory/commit/ce2b00ec0225b546c61148bb1fa05107b0b42b57). AI-assisted with Codex.
fuzeofficlal commented 2026-11-26 21:41:02 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by fuzeofficlal on 2026-11-26T21:41:02Z.

Review: APPROVED


Clean, focused optimization with solid test coverage across both victory-formats and victory-hle.

Checked the key invariants:

  • xma::read_frame_into (crates/victory-formats/src/xma.rs:139) clears out on entry and on every early None path while preserving its allocation, and band_tables (xma.rs:630) matches the exact four sample-rate buckets in a shared OnceLock.
  • Xma::active (crates/victory-hle/src/modules/audio.rs:408) is allocated alongside voices in AcpHalAllocateShapeContexts (audio.rs:317) and cleared atomically via Xma::EMPTY under the XMA write lock in AcpHalReleaseShapeContexts (audio.rs:321), so pump (audio.rs:554) can never observe a stale set bit past voices.len().
  • enable (audio.rs:462) and disable (audio.rs:500) update active with Ordering::Release while holding the per-voice lock (with enable waking the decoder thread afterward), matching pump's Ordering::Acquire load and per-slot re-check.
  • voice.scratch is cleared before both decoder.flush (audio.rs:626) and decoder.decode (audio.rs:664), avoiding stale PCM leakage across frames or failed decodes.

Verification on origin/main (63bcca43):

  • cargo xtask policy: ok (191 files ok)
  • cargo clippy --locked -p victory-formats -p victory-hle --all-targets -- -D warnings: clean
  • cargo test --locked -p victory-formats -- xma::: 22/22 passed (3 ignored)
  • cargo test --locked -p victory-hle -- modules::audio::: 9/9 passed
  • Additional edge-case probe covering 0 XMA contexts, non-multiple-of-64 count (65 voices), out-of-range enable/disable, repeated enable/disable, corrupt frame decode, and AcpHalReleaseShapeContexts() while voices are active followed by pump(): passed.
<!-- greyfinch-sync:Marvisak/VIctory:review:6080767278 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/3383#pullrequestreview-6080767278). Originally posted by [fuzeofficlal](https://github.com/fuzeofficlal) on 2026-11-26T21:41:02Z. **Review:** APPROVED --- Clean, focused optimization with solid test coverage across both `victory-formats` and `victory-hle`. Checked the key invariants: - `xma::read_frame_into` (`crates/victory-formats/src/xma.rs:139`) clears `out` on entry and on every early `None` path while preserving its allocation, and `band_tables` (`xma.rs:630`) matches the exact four sample-rate buckets in a shared `OnceLock`. - `Xma::active` (`crates/victory-hle/src/modules/audio.rs:408`) is allocated alongside `voices` in `AcpHalAllocateShapeContexts` (`audio.rs:317`) and cleared atomically via `Xma::EMPTY` under the `XMA` write lock in `AcpHalReleaseShapeContexts` (`audio.rs:321`), so `pump` (`audio.rs:554`) can never observe a stale set bit past `voices.len()`. - `enable` (`audio.rs:462`) and `disable` (`audio.rs:500`) update `active` with `Ordering::Release` while holding the per-voice lock (with `enable` waking the decoder thread afterward), matching `pump`'s `Ordering::Acquire` load and per-slot re-check. - `voice.scratch` is cleared before both `decoder.flush` (`audio.rs:626`) and `decoder.decode` (`audio.rs:664`), avoiding stale PCM leakage across frames or failed decodes. Verification on `origin/main` (`63bcca43`): - `cargo xtask policy`: ok (`191 files ok`) - `cargo clippy --locked -p victory-formats -p victory-hle --all-targets -- -D warnings`: clean - `cargo test --locked -p victory-formats -- xma::`: 22/22 passed (3 ignored) - `cargo test --locked -p victory-hle -- modules::audio::`: 9/9 passed - Additional edge-case probe covering 0 XMA contexts, non-multiple-of-64 count (65 voices), out-of-range enable/disable, repeated enable/disable, corrupt frame decode, and `AcpHalReleaseShapeContexts()` while voices are active followed by `pump()`: passed.
passerbyyhx commented 2026-11-26 22:34:48 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by passerbyyhx on 2026-11-26T22:34:48Z.


I captured one Windows kernel scheduler trace per revision with the same four-worker, single-pump Release workload used in the earlier allocation and timing comparison. At 4,096 rounds per run (16,384 voices), the parent 63bcca43c6a00a758510398fa43096ce7e4f7df0 and head 23e62b7c04f253f4e2fee181b77c16ec093350af produced the same PCM checksum 926c28842c9fde16. Both ETLs reported zero lost buffers and zero lost events.

Completed waits in the timed worker window Parent count / blocked thread-ms Head count / blocked thread-ms
Four workers, fixture barrier 31,276 / 4,193.88 31,495 / 4,755.21
Four workers, no resolved workload frame 6 / 0.90 6 / 0.50
Pump, fixture barrier 4,137 / 230.88 4,130 / 277.01

For each wait, I used the old thread's switch-out stack, the first matching ReadyThread event, and the next switch-in. The table excludes time spent ready to run. All completed intervals had a corresponding wakeup. At the right window boundary, three parent intervals and four head intervals were still open. Every measured pump wakeup came from a worker.

The fixture barrier accounts for over 99.9% of observed worker blocked time in both runs. No resolved blocking site identified a heap wait, per-voice lock wait or APU queue wait. These traces leave spinning, allocation work on CPU and the six unresolved worker intervals per build unattributed. One trace per revision cannot establish a timing regression or its cause.

Synthetic workload scheduler study and analysis source

AI-assisted with Codex.

<!-- greyfinch-sync:Marvisak/VIctory:comment:7204804323 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/3383#issuecomment-7204804323). Originally posted by [passerbyyhx](https://github.com/passerbyyhx) on 2026-11-26T22:34:48Z. --- I captured one Windows kernel scheduler trace per revision with the same four-worker, single-pump Release workload used in the [earlier allocation and timing comparison](https://github.com/Marvisak/VIctory/pull/3383#issuecomment-7203878575). At 4,096 rounds per run (16,384 voices), the parent `63bcca43c6a00a758510398fa43096ce7e4f7df0` and head `23e62b7c04f253f4e2fee181b77c16ec093350af` produced the same PCM checksum `926c28842c9fde16`. Both ETLs reported zero lost buffers and zero lost events. | Completed waits in the timed worker window | Parent count / blocked thread-ms | Head count / blocked thread-ms | | --- | ---: | ---: | | Four workers, fixture barrier | 31,276 / 4,193.88 | 31,495 / 4,755.21 | | Four workers, no resolved workload frame | 6 / 0.90 | 6 / 0.50 | | Pump, fixture barrier | 4,137 / 230.88 | 4,130 / 277.01 | For each wait, I used the old thread's switch-out stack, the first matching ReadyThread event, and the next switch-in. The table excludes time spent ready to run. All completed intervals had a corresponding wakeup. At the right window boundary, three parent intervals and four head intervals were still open. Every measured pump wakeup came from a worker. The fixture barrier accounts for over 99.9% of observed worker blocked time in both runs. No resolved blocking site identified a heap wait, per-voice lock wait or APU queue wait. These traces leave spinning, allocation work on CPU and the six unresolved worker intervals per build unattributed. One trace per revision cannot establish a timing regression or its cause. [Synthetic workload scheduler study and analysis source](https://github.com/passerbyyhx/VIctory/commit/553fdde7486abb02c02b9d52dc3ca95ab3eff0d5) AI-assisted with Codex.
Damokles2 commented 2026-11-27 06:51:28 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by Damokles2 on 2026-11-27T06:51:28Z.


Nice work! I independently implemented a similar encoded XMA frame buffer reuse optimization yesterday, using a single 4 KiB scratch buffer owned by the audio worker.

While both ownership models have their merits, my implementation includes a synthetic regression fixture that may complement your existing tests.

It verifies:

  • Independently calculated MSB-first bytes and tail padding.
  • Maximum-length frames (32,766 bits).
  • Packet skipping and multiple continuation boundaries.
  • Buffer reuse across independent streams.
  • Exact recovery after a partially truncated read.

The fixture should be adaptable to your FrameMeta API without changing your buffer ownership model.

Implementation and regression test:
https://github.com/Damokles2/VIctory/pull/1/files

Native Windows CI (872 tests passed, strict Clippy passed):
https://github.com/Damokles2/VIctory/actions/runs/45874582469

Feel free to incorporate the regression fixture if you find it useful.

<!-- greyfinch-sync:Marvisak/VIctory:comment:7211911306 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/3383#issuecomment-7211911306). Originally posted by [Damokles2](https://github.com/Damokles2) on 2026-11-27T06:51:28Z. --- Nice work! I independently implemented a similar encoded XMA frame buffer reuse optimization yesterday, using a single 4 KiB scratch buffer owned by the audio worker. While both ownership models have their merits, my implementation includes a synthetic regression fixture that may complement your existing tests. It verifies: - Independently calculated MSB-first bytes and tail padding. - Maximum-length frames (32,766 bits). - Packet skipping and multiple continuation boundaries. - Buffer reuse across independent streams. - Exact recovery after a partially truncated read. The fixture should be adaptable to your `FrameMeta` API without changing your buffer ownership model. Implementation and regression test: https://github.com/Damokles2/VIctory/pull/1/files Native Windows CI (872 tests passed, strict Clippy passed): https://github.com/Damokles2/VIctory/actions/runs/45874582469 Feel free to incorporate the regression fixture if you find it useful.
andrewpozdnakov7-jpg commented 2026-11-28 00:27:45 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by andrewpozdnakov7-jpg on 2026-11-28T00:27:45Z.


I checked 23e62b7 together with the PCM output change from #3631. Both exact PR commits are retained in 6a92aae. The conflict resolution keeps your explicit output reservations in decode and flush, then uses the channel-specific PCM append.

On Windows the combined branch matched all 32,000 PCM samples from the 62 generated mono frames and flush. The stereo flush regression passed as well. All 49 active formats tests and all nine HLE audio tests passed, including the active voice bitset, scratch reuse and output ring tests. Strict formats Clippy and the asset policy check passed.

If you reuse the PCM append or its test, please retain the Git author on cherry-pick or rebase, or add Co-authored-by: andrewpozdnakov7-jpg [email protected] if folding it into another commit.

<!-- greyfinch-sync:Marvisak/VIctory:comment:7230930244 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/3383#issuecomment-7230930244). Originally posted by [andrewpozdnakov7-jpg](https://github.com/andrewpozdnakov7-jpg) on 2026-11-28T00:27:45Z. --- I checked 23e62b7 together with the PCM output change from #3631. Both exact PR commits are retained in [6a92aae](https://github.com/andrewpozdnakov7-jpg/VIctory/commit/6a92aae5a047bfce6552df06f02b8e2be613a60d). The conflict resolution keeps your explicit output reservations in decode and flush, then uses the channel-specific PCM append. On Windows the combined branch matched all 32,000 PCM samples from the 62 generated mono frames and flush. The stereo flush regression passed as well. All 49 active formats tests and all nine HLE audio tests passed, including the active voice bitset, scratch reuse and output ring tests. Strict formats Clippy and the asset policy check passed. If you reuse the PCM append or its test, please retain the Git author on cherry-pick or rebase, or add Co-authored-by: andrewpozdnakov7-jpg <[email protected]> if folding it into another commit.
andrewpozdnakov7-jpg commented 2026-11-28 02:20:02 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by andrewpozdnakov7-jpg on 2026-11-28T02:20:02Z.


I added #3649 to the previously tested #3383/#3629/#3631 integration at 7713c9a. 2172a38 merges cleanly and retains all four exact PR heads. The new delta is the stereo coefficient-band transform.

63bcca43, this PR's 23e62b7, and the integration decoded the same 288 generated stereo frames at 24, 32, 44.1, and 48 kHz. The input frame bytes matched across all three runs. All 301,056 PCM samples, including flush output, matched bit for bit. The test exercised all-band transform, alternating-band transform, and scale-only bands.

The integrated formats release suite passed 44 tests, with five ignored and five noise tests filtered. Strict formats Clippy and the asset policy check passed. This run covers decoder behavior on generated audio.

Please preserve the Git author if cherry-picking or rebasing, or keep this trailer if folding the change into another commit.

Co-authored-by: andrewpozdnakov7-jpg <[email protected]>

<!-- greyfinch-sync:Marvisak/VIctory:comment:7232291535 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/3383#issuecomment-7232291535). Originally posted by [andrewpozdnakov7-jpg](https://github.com/andrewpozdnakov7-jpg) on 2026-11-28T02:20:02Z. --- I added #3649 to the previously tested #3383/#3629/#3631 integration at `7713c9a`. [2172a38](https://github.com/andrewpozdnakov7-jpg/VIctory/commit/2172a38da915fc803b00d3b7806c590ecfba3448) merges cleanly and retains all four exact PR heads. The new delta is the stereo coefficient-band transform. `63bcca43`, this PR's `23e62b7`, and the integration decoded the same 288 generated stereo frames at 24, 32, 44.1, and 48 kHz. The input frame bytes matched across all three runs. All 301,056 PCM samples, including flush output, matched bit for bit. The test exercised all-band transform, alternating-band transform, and scale-only bands. The integrated formats release suite passed 44 tests, with five ignored and five noise tests filtered. Strict formats Clippy and the asset policy check passed. This run covers decoder behavior on generated audio. Please preserve the Git author if cherry-picking or rebasing, or keep this trailer if folding the change into another commit. `Co-authored-by: andrewpozdnakov7-jpg <[email protected]>`
andrewpozdnakov7-jpg commented 2026-11-28 04:45:15 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by andrewpozdnakov7-jpg on 2026-11-28T04:45:15Z.


I checked #3666 with this PR in ae59889. The branch keeps the exact #3383 head and the tested #3629, #3631 and #3649 stack. I resolved the overlap in the shared band tables so only the selected sample rate is initialized on first use. The existing rate-bucket test and the new sharing test both pass.

Compared with the earlier combined branch, every band-table entry matched at 12 rates and all 301,056 generated PCM samples matched across the four rate buckets. The combined formats suite passed 50 tests, with five ignored. Strict formats Clippy and the asset policy passed.

In 21 paired fresh-process 48 kHz probes, the constructor median was 61.4 µs with this PR's first-use initialization of all four tables and 31.2 µs with per-rate initialization. Desamodeth or a maintainer can pick up the integration if the per-rate option fits the PR.

Please retain the Git authors on cherry-pick or rebase, or add Co-authored-by: andrewpozdnakov7-jpg <[email protected]> if folding this resolution into another commit.

AI-assisted with OpenAI Codex.

<!-- greyfinch-sync:Marvisak/VIctory:comment:7234238127 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/3383#issuecomment-7234238127). Originally posted by [andrewpozdnakov7-jpg](https://github.com/andrewpozdnakov7-jpg) on 2026-11-28T04:45:15Z. --- I checked #3666 with this PR in [ae59889](https://github.com/andrewpozdnakov7-jpg/VIctory/commit/ae59889fccc51b1ca0954257ac7889bc0d6cad73). The branch keeps the exact #3383 head and the tested #3629, #3631 and #3649 stack. I resolved the overlap in the shared band tables so only the selected sample rate is initialized on first use. The existing rate-bucket test and the new sharing test both pass. Compared with the earlier combined branch, every band-table entry matched at 12 rates and all 301,056 generated PCM samples matched across the four rate buckets. The combined formats suite passed 50 tests, with five ignored. Strict formats Clippy and the asset policy passed. In 21 paired fresh-process 48 kHz probes, the constructor median was 61.4 µs with this PR's first-use initialization of all four tables and 31.2 µs with per-rate initialization. Desamodeth or a maintainer can pick up the integration if the per-rate option fits the PR. Please retain the Git authors on cherry-pick or rebase, or add `Co-authored-by: andrewpozdnakov7-jpg <[email protected]>` if folding this resolution into another commit. AI-assisted with OpenAI Codex.
Desamodeth commented 2026-11-30 06:32:59 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by Desamodeth on 2026-11-30T06:32:59Z.


Thanks @Damokles2 and @andrewpozdnakov7-jpg for the regression fixture offer and the integration testing across #3629/#3631/#3649/#3666 (ae59889)!

Handoff note: I'm stepping away from this project, so I'm leaving #3383 as-is (clean on main, green CI, 2 approvals). Maintainers or other contributors are welcome to merge it as-is or fold in the per-rate band_tables initialization / extra regression fixtures in a follow-up.

AI-assisted with Gemini 4 Neon

<!-- greyfinch-sync:Marvisak/VIctory:comment:7270762939 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/3383#issuecomment-7270762939). Originally posted by [Desamodeth](https://github.com/Desamodeth) on 2026-11-30T06:32:59Z. --- Thanks @Damokles2 and @andrewpozdnakov7-jpg for the regression fixture offer and the integration testing across `#3629`/`#3631`/`#3649`/`#3666` (`ae59889`)! **Handoff note:** I'm stepping away from this project, so I'm leaving `#3383` as-is (clean on `main`, green CI, 2 approvals). Maintainers or other contributors are welcome to merge it as-is or fold in the per-rate `band_tables` initialization / extra regression fixtures in a follow-up. *AI-assisted with Gemini 4 Neon*
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/3383:upstream-pr/3383'
git switch 'upstream-pr/3383'

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