perf(hle): reuse Opus PCM scratch buffer across packets #508

Manually merged
github merged 1 commit from github-bot/gh-508 into main 2026-12-05 17:30:55 +00:00 AGit
Owner

@⁠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 UTC

What this does

Stores a reusable pcm: Vec<f32> scratch buffer on victory_hle::modules::apu::State instead of allocating and zero-initializing a fresh vec![0f32; MAX_FRAMES * channels] (~46 KiB for stereo 5760-frame max) on every XApuEnqueueCommand Opus decode call.

Checks

  • N/A: pure allocation-elimination optimization preserving identical decoded PCM output.
  • N/A: no switches added, changed, or removed.
  • No unrelated cleanup: diff is limited to State and process in crates/victory-hle/src/modules/apu.rs.
  • N/A: existing modules::apu::tests verify decoded Opus PCM output matches; no behavioural bug existed before to fail a test.
  • Checked open PR queue: open PR #455 restructures apu.rs to decode Opus on dedicated worker threads. This PR is independent on main; if #455 merges first, the same scratch-buffer reuse would belong per worker thread instead of on State.
  • cargo xtask policy passed (359 files ok); no fork-only files, configs, or build outputs.

Verified

  • cargo xtask policy — 359 files ok
  • PKG=victory-hle ./build.sh test --lib -- apu:: (Proton Wine on Linux) — 2 passed; 0 failed, 0 warnings
  • Also exercised in gameplay on GTA 6 Series S (Ryzen 5 7600X, RX 9070 XT, CachyOS + Proton); not benchmarked in isolation.

Cost

Retains one MAX_FRAMES * channels * 4 byte (~46 KiB for stereo) buffer per active APU connection instead of allocating/freeing it on every packet; 0 new warnings.

AI-assisted: yes

**@⁠Not4Eric** on GitHub · 2026-12-04 20:24 UTC · [original](https://github.com/lobomfz/VIctory-xmem/pull/508) `Not4Eric:perf/apu-reuse-pcm-buffer` → `main` · +17 −5 · 1 files · 1 commits · merged 2026-12-05 17:29 UTC ## What this does Stores a reusable `pcm: Vec<f32>` scratch buffer on `victory_hle::modules::apu::State` instead of allocating and zero-initializing a fresh `vec![0f32; MAX_FRAMES * channels]` (~46 KiB for stereo 5760-frame max) on every `XApuEnqueueCommand` Opus decode call. ## Checks - N/A: pure allocation-elimination optimization preserving identical decoded PCM output. - N/A: no switches added, changed, or removed. - [x] No unrelated cleanup: diff is limited to `State` and `process` in `crates/victory-hle/src/modules/apu.rs`. - N/A: existing `modules::apu::tests` verify decoded Opus PCM output matches; no behavioural bug existed before to fail a test. - [x] Checked open PR queue: open PR #455 restructures `apu.rs` to decode Opus on dedicated worker threads. This PR is independent on `main`; if #455 merges first, the same scratch-buffer reuse would belong per worker thread instead of on `State`. - [x] `cargo xtask policy` passed (`359 files ok`); no fork-only files, configs, or build outputs. ## Verified - `cargo xtask policy` — `359 files ok` - `PKG=victory-hle ./build.sh test --lib -- apu::` (Proton Wine on Linux) — `2 passed; 0 failed`, 0 warnings - Also exercised in gameplay on GTA 6 Series S (Ryzen 5 7600X, RX 9070 XT, CachyOS + Proton); not benchmarked in isolation. ## Cost Retains one `MAX_FRAMES * channels * 4` byte (~46 KiB for stereo) buffer per active APU connection instead of allocating/freeing it on every packet; 0 new warnings. AI-assisted: yes
github-bot locked as GitHub sync and limited conversation to collaborators 2026-12-04 20:27:29 +00:00
Author
Owner

@⁠Kal0s-design on GitHub · 2026-12-04 22:34 UTC · original

Checked 9d37ee1 on 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

**@⁠Kal0s-design** on GitHub · 2026-12-04 22:34 UTC · [original](https://github.com/lobomfz/VIctory-xmem/pull/508#issuecomment-7426089056) Checked 9d37ee1 on 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
Author
Owner

@⁠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

**@⁠Not4Eric** on GitHub · 2026-12-05 00:00 UTC · [original](https://github.com/lobomfz/VIctory-xmem/pull/508#issuecomment-7427748818) Thank you so much for helping, I'm not the best at this and I'm heavily relying on AI to even do this
github manually merged commit 8631492486 into main 2026-12-05 17:30:55 +00:00
This discussion has been locked. Commenting is limited to contributors.
No description provided.