Audio/XMA: box voices, skip inactive slots in pump, and reuse per-frame scratch #3383
No reviewers
Labels
No labels
accessibility
area: build
area: formats
area: gpu
area: loader
area: runtime
area: shader
bug
documentation
duplicate
enhancement
good first issue
gpu: amd
gpu: intel
gpu: nvidia
help wanted
host: wine
invalid
out of scope
question
request
run report
triage: confirmed
triage: has patch
triage: merge candidate
triage: needs info
triage: needs rebase
triage: needs work
triage: proposed close
wontfix
No milestone
No assignees
1 participant
Notifications
Due date
No due date set.
Dependencies
No dependencies set
Reference
github/Marvisak--VIctory!3383
Loading…
Reference in a new issue
No description provided.
Delete branch "github-bot/gh-3383"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
@Desamodeth on GitHub · 2026-11-26 21:12 UTC · original
Desamodeth:perf/xma-voice-storage-and-scratch→main· +309 −66 · 2 files · 1 commits · openClaimed in #1633: #1633
Problem
On
main(63bcca43c6a00a758510398fa43096ce7e4f7df0):VoiceembedsOption<xma::Decoder>(14,304 B) inline (size_of::<Voice>() = 14,448 B), andXma::voicesstoresVec<Mutex<Option<Voice>>>contiguously. Allocating 512 XMA contexts (as instartup_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.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.xma::read_frameallocates a freshVec<u8>andaudio::decodeallocates a freshVec<i16>(plus a thirdVec<i16>onflush).xma::Decoder::newrecomputes 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:FrameMetaandread_frame_into(data, start, out)so callers can reuse encoded-frame byte capacity across frames; implement existingread_frameon top of it (next_frameuntouched).BandTablesacross the four clamped sample-rate buckets (24 / 32 / 44.1 / 48 kHz) viaOnceLock, shrinkingDecoderfrom 14,304 B to 11,480 B and avoiding per-voice table construction.Decoder::decodeandDecoder::flushso fresh scratchVec<i16>buffers grow in one allocation instead of geometric reallocations.crates/victory-hle/src/modules/audio.rs:Voiceandxma::Decoder(Vec<Mutex<Option<Box<Voice>>>>;size_of::<Voice>() = 208 B), freeing decoder and scratch memory when a voice is disabled.Xma::active: Vec<AtomicU64>(updated under the matching voice mutex withReleaseordering, read withAcquireinXma::pump) sopumponly locks active slots.frame: Vec<u8>andscratch: Vec<i16>acrossdecodeandflush.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
--releasebinaries ofmain(63bcca43) vs this branch (23e62b7c) using identical synthetic 48 kHz non-silent XMA2 streams (512 allocated contexts, 4-block rings) and a countingGlobalAllocwrapper, run interleaved in 6x A/B forward + 6x B/A reverse (12 runs each).Memory & allocations
main(63bcca43)23e62b7c)enable+disablechurn (32,000 pairs)Box<Voice>/ pair)CPU timings (median [min..max] over 12 runs; forward A/B vs reverse B/A medians)
mainXma::pump()(0/512 active)Xma::pump()(16/512 active, rings full)Xma::pump()(512/512 active, rings full)enable+disablechurn (ns / pair)enable+decode+disable(us / voice)Trade-off
Boxing
VoiceandDecodertrades 0-frame/1-frame voice churn allocator traffic (+1Box<Voice>onenable, +1Box<Decoder>on first frame) for ~7.05 MiB lower idle/post-burst heap retention, 0 steady-state per-frame allocations, and skipping inactive slots inXma::pump(). Even in the 0-frame and 1-ring churn tests, wall time stayed faster on this host becauseenableno longer initializes a 14.4 KiB inline struct andDecoder::newno 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)
66,048samples, FNV-1a0x29b87c8ab9c1311d, peak2384, 0 bad frames (identical).loops=2,start_skip=1,loop_skip=2) + flush:19,584samples, FNV-1a0x6deafbf1c8a3834d, peak1302, 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_capacityandshared_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) andmodules::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.
View command line instructions
Checkout
From your project repository, check out a new branch and test the changes.Merge
Merge the changes and update on Forgejo.