perf(hle): reuse Opus PCM scratch buffer across packets #508
No reviewers
Labels
No labels
accessibility
bug
documentation
duplicate
enhancement
good first issue
help wanted
invalid
question
wontfix
No milestone
No assignees
1 participant
Notifications
Due date
No due date set.
Dependencies
No dependencies set
Reference
github/lobomfz--VIctory-xmem!508
Loading…
Reference in a new issue
No description provided.
Delete branch "github-bot/gh-508"
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?
@Not4Eric on GitHub · 2026-12-04 20:24 UTC · original
Not4Eric:perf/apu-reuse-pcm-buffer→main· +17 −5 · 1 files · 1 commits · merged 2026-12-05 17:29 UTCWhat this does
Stores a reusable
pcm: Vec<f32>scratch buffer onvictory_hle::modules::apu::Stateinstead of allocating and zero-initializing a freshvec on everyXApuEnqueueCommandOpus decode call.Checks
Stateandprocessincrates/victory-hle/src/modules/apu.rs.modules::apu::testsverify decoded Opus PCM output matches; no behavioural bug existed before to fail a test.apu.rsto decode Opus on dedicated worker threads. This PR is independent onmain; if #455 merges first, the same scratch-buffer reuse would belong per worker thread instead of onState.cargo xtask policypassed (359 files ok); no fork-only files, configs, or build outputs.Verified
cargo xtask policy—359 files okPKG=victory-hle ./build.sh test --lib -- apu::(Proton Wine on Linux) —2 passed; 0 failed, 0 warningsCost
Retains one
MAX_FRAMES * channels * 4byte (~46 KiB for stereo) buffer per active APU connection instead of allocating/freeing it on every packet; 0 new warnings.AI-assisted: yes
@Kal0s-design on GitHub · 2026-12-04 22:34 UTC · original
Checked
9d37ee1on native Windows (didn't run the game). Merges clean on current main, victory-hle lib tests give 162 passed and 1 ignored like main, and clippy with and without diagnostics shows nothing new in apu.rs.The reuse looks safe. The buffer is sliced to MAX_FRAMES * channels for every packet (apu.rs:347-349), so a 120 ms packet still fits, and only pcm[first*ch..(first+frames)*ch] is read back with frames capped at what was decoded (apu.rs:362), so a longer earlier packet's tail never leaks out. I also tried a throwaway test (not pushed) with the scratch prefilled with NaN, then 60, 10, 20, 2.5, 60 and 5 ms stereo packets and a few first_frame offsets, compared byte for byte against decoding each into a fresh buffer. All identical and no NaN, so for these packets at least the decoder only writes the output. It lives in State under the connection mutex, one per connection, so nothing is shared between threads on main.
With #455 it conflicts in apu.rs, and since #455 decodes on its own threads and still allocates per call in its process, State.pcm wouldn't carry over. Whichever lands second needs reworking, probably a scratch per decoder thread or a thread_local, as you already noted.
AI-assisted: yes
@Not4Eric on GitHub · 2026-12-05 00:00 UTC · original
Thank you so much for helping, I'm not the best at this and I'm heavily relying on AI to even do this