Repository navigation
XAudio2: add lock-free hook trampoline lookup, atomic silent-voice fast path, and zero-copy decoded PCM - #2683
Conversation
…st path, and zero-copy decoded PCM Mirror patched vtable originals in a lock-free FastOriginals table so original() avoids locking ORIGINALS on every XAudio2 call, guard XMA lookups in submit_source_buffer and destroy_voice with an atomic SILENT_LIVE_COUNT updated under the XMA lock, hand decoded Vec<i16> PCM16 buffers directly to XAudio2 without allocating/copying a second byte buffer (clamping to whole frames within XAUDIO2_MAX_BUFFER_BYTES and falling back to shared silence on < 1 frame), and back decode-error silence fallbacks with the shared read-only silence() mapping instead of allocating up to 32 MiB on the heap. Assisted-by: Google Antigravity (Gemini 4 Neon)
luckyteagirl
left a comment
There was a problem hiding this comment.
At the new decoded-error path in submit_decoded_buffer, prepare_fallback_silence() now requires the process-wide 32 MiB silence() mapping. If that VirtualAlloc fails once, OnceLock<usize> permanently stores the null result, so every later decoded-error or short-frame fallback returns E_FAIL even if memory becomes available. Before this PR, the decoded path allocated only the selected buffer's sb.audio_bytes of silence (the PR's 480-stereo-frame case needs 1,920 bytes). Could the shared mapping stay the fast path while allocation failure falls back to a buffer-sized owned silence allocation or a retryable mapping? A test with an injectable failing mapping would cover the recovery case; the new test currently exercises only successful mapping.
This is an exact-head source review of c970f2257e0935d8e1c17f14e62d9aade3838ee4; I did not force an allocation failure or run a retail title. AI-assisted review with Codex.
luckyteagirl
left a comment
There was a problem hiding this comment.
The new FAST_ORIG_CAP test fills a local FastOriginals to 32 entries and checks that an unknown key requests fallback. The later original() assertion uses a separate process-wide registry with just one patched entry, so the production lookup's overflow path (Err(()) to ORIGINALS) is never exercised. This test would still pass if original() accidentally returned None for every entry beyond the fast table. Could a fresh-process fixture register 33 distinct originals and assert that original() resolves both a mirrored key and the 33rd key (while an absent key stays absent), or could an injectable local fast/slow pair test the same path without polluting process-wide tables with short-lived stack vtables?
This is an exact-head source review of c970f2257e0935d8e1c17f14e62d9aade3838ee4; no runtime build was run. AI-assisted review with Codex.
|
Tested on the Linux/Wine lane, full standard env (AMD rig: Ryzen 9 9900X, 96 GB DDR5, RX 7900 XT 20 GB, Arch kernel 7.2.15, Mesa 26.2.5 RADV, wine-11.0 Proton, rustc 1.94.0; |
|
Test report: passes on NVIDIA. Exact head Tested
Not tested
Claude Code (Claude Opus 5.5) ran the tests and wrote this report. |
…th, and zero-copy decoded PCM (port upstream xcl-dev#2683)
What is in it
Removes per-call global mutex traffic, eliminates decoded PCM double-allocation/byte-copying, and replaces up-to-32 MiB decode-error heap allocations with the shared silence mapping in
crates/xcl/src/hle/xaudio2.rs:FastOriginals,FAST_ORIG_CAP = 32): Mirrors the first 32(vtable, slot, orig)entries published inpatch()intoFAST_ORIGINALS(Release/Acquirelen, while still pushing toORIGINALSso existing checks remain intact).original()now resolves patched slots lock-free without acquiringORIGINALS, falling back toORIGINALS.lock()only if the 32-slot cache is full.SILENT_LIVE_COUNT): Tracks live silent voices viaSILENT_LIVE_COUNT: AtomicUsizeupdated strictly while holding theXMAlock (create_source_voiceon.is_none(),destroy_voiceon.is_some()), allowingsubmit_source_bufferanddestroy_voiceto bypassXMA.lock()when zero silent voices exist.DecodedBuffer._pcm: Vec<i16>+prepare_decoded_pcm): Assertscfg!(target_endian = "little")at compile time and hands decodedVec<i16>buffers directly to XAudio2 without allocating a secondVec<u8>or copying 2 bytes per sample.prepare_decoded_pcmclamps whole frames withinXAUDIO2_MAX_BUFFER_BYTES, returnsNoneif fewer than 1 complete frame was decoded, and constructsBox::new(DecodedBuffer { ... })before readingboxed._pcm.as_ptr().silence()fallback (prepare_fallback_silence): On decode error or< 1decoded frame,submit_decoded_bufferpointsnb.audio_dataat the process-wide read-onlysilence()mapping with_pcm: Vec::new()(0 bytes heap allocated, never overwritingnb.audio_data/nb.audio_byteswhen_pcm.is_empty()), instead of allocating up toSILENCE_BYTES(32 MiB) on the heap.pcm16_for_rate,submit_decoded_bufferentry,get_state,DECODED_VOICESteardown, and existing tests untouched.Tested
origin/main(b49be183,cargo test -p xcl --lib "hle::xaudio2::tests::failing_first" -- --test-threads=1 --nocapture): confirmed pre-fixsubmit_decoded_bufferbyte-by-byte conversion copiedVec<i16>into a separate heap allocation (0x445320 != 0x445410) and did not reject< 1complete frame.XCL_ADAPTER=warp,XCL_WARP=1,cargo test -p xcl --lib "hle::xaudio2::tests::" -- --test-threads=1 --nocapture):12 passed; 0 failed; 0 ignored; 0 skipped(0.25s, including real Windows XAudio2 2.9 Opus & XMA2 decode/fallback andengine_from_the_system_dll; zero compiler warnings inxaudio2.rs).XCL_ADAPTER=warp,XCL_WARP=1,cargo test --release -p xcl --lib "hle::xaudio2::tests::" -- --test-threads=1 --nocapture):12 passed; 0 failed; 0 ignored; 0 skipped(1.28s).XAUDIO2_BENCH_NS):originallookup over 6 entries (200,000 iters):21.21 ns->3.65 ns(5.8xfaster, WARP run) /17.98 ns->3.74 ns(4.8xfaster, RTX run).4,365.6 ns->131.5 ns(33.2xfaster, WARP run) /2,203.0 ns->121.9 ns(18.1xfaster, RTX run).cargo clippy --all-targets):0new warnings incrates/xcl/src/hle/xaudio2.rs(only the 3 pre-existing lints on untouchedXAudio2Create,XAudio2CreateWithVersionInfo, andcreate_decoded_voice).cargo test --release -p xcl --lib "hle::xaudio2::tests::" -- --test-threads=1 --nocapture):12 passed; 0 failed; 0 ignored; 0 skipped(0.41s).Regression run
Full release workspace on RTX 4060 (
cargo test --release --workspace --no-fail-fast -- --test-threads=4 --nocapture):37test binaries,1438 passed; 0 failed; 47 ignored,29pre-existing environment-gatedSKIPPEDlines:21skipped:no wave64(RTX 4060 is wave32)3skipped: wave32per-lane form: refusedby driver (f16 arithmetic,f16 packed,f32 conversions)3skipped: no D3D12 debug layer (d3d12SDKLayers.dllunavailable on host: debug-layerdred: false/trueanduploads_start_from_each_subresources_state)1skipped:pso_measurement_compiles_an_exact_synthetic_record(cannot disable driver cache on Windows)1skipped:heap_window_out_of_memory_falls_back_to_the_switch_form(no heap windows)Paired 60 s RTX 4060 smoke capture (
scripts/smoke.ps1):Base (
origin/mainb49be1836844fe0b0cdb3211dce3cb7fb4e21ac7):PR (
c970f2257e0935d8e1c17f14e62d9aade3838ee4):Not tested
29host-gated workspace tests skipped on this RTX 4060 machine (wave64 hardware, wave32 per-lane driver support, D3D12 SDK debug layer, driver cache disable, and heap windows).AI use
Google Antigravity (Gemini 4 Neon) wrote the patch, unit tests, and PR description; all commands, test runs, micro-benchmarks, and paired 60 s GPU smoke captures were executed directly in this workspace.