Audio: reuse the PCM workspace between Opus commands #2421

Open
faycalbaklouti wants to merge 1 commit from upstream-pr/2423 into main
faycalbaklouti commented 2026-11-25 13:14:21 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by faycalbaklouti on 2026-11-25T13:14:21Z.


What

Keep one PCM scratch buffer per APU connection and reuse it across Opus process commands under the existing connection mutex. Grow it only when needed and pass each decoder a slice sized for its channel count. Keep VecDeque::new() so the guest-provided stream and command counts do not determine an upfront result-queue allocation.

How it was tested

The new regression alternates mono and stereo commands and compares result lengths and PCM bytes against independent decoders. It tests decode behavior, not the address or capacity of the backing buffer. The new regression and the existing Opus decode test both passed on the head.

Head (`349929c0cc`):
test modules::apu::tests::alternating_mono_and_stereo_match_independent_decoders ... ok
test modules::apu::tests::decodes_opus_as_the_title_drives_it ... ok
test result: FAILED. 151 passed; 2 failed; 0 ignored; 0 measured; 0 filtered out; finished in 5.71s

Base (`63bcca43c6`):
test modules::apu::tests::decodes_opus_as_the_title_drives_it ... ok
test result: FAILED. 150 passed; 2 failed; 0 ignored; 0 measured; 0 filtered out; finished in 5.62s

Both workspace Clippy modes (default and all features, with -D warnings) exited 0 on head and base. The Windows-target workspace test under Wine exited 101 on both, stopping in victory-hle: modules::net::tests::only_loopback_is_reachable hit a missing WSAConnectByList export on the isolated runner, and modules::xgamesave::tests::a_kept_blob_past_the_link_limit_is_copied did not observe the expected NTFS hard-link limit. Native Windows CI remains the final gate.

A/B/B/A release microbenchmark on the Windows MSVC target under Wine: identical synthetic Opus enqueue/dequeue workload, seven 1,024-packet samples per arm after warm-up. The arm medians were 27,094.2 / 26,569.9 / 26,505.9 / 26,995.0 ns/packet. Median of arm medians: 63bcca43c6 27,044.6 ns/packet; 349929c0cc 26,537.9 ns/packet (1.87% lower). This measures the synthetic packet path, not gameplay or frame time.


Radicle project hub (open-source porting and preservation history): https://radicle.network/nodes/rosa.radicle.network/rad:z3rqQw7796wMbgj8RszExtWR4wMSE
Live development stream: https://vdo.ninja/?view=gta6rtx4090live1080p60

<!-- greyfinch-sync:Marvisak/VIctory:issue:5492032969 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/2423). Originally posted by [faycalbaklouti](https://github.com/faycalbaklouti) on 2026-11-25T13:14:21Z. --- ## What Keep one PCM scratch buffer per APU connection and reuse it across Opus process commands under the existing connection mutex. Grow it only when needed and pass each decoder a slice sized for its channel count. Keep `VecDeque::new()` so the guest-provided stream and command counts do not determine an upfront result-queue allocation. ## How it was tested The new regression alternates mono and stereo commands and compares result lengths and PCM bytes against independent decoders. It tests decode behavior, not the address or capacity of the backing buffer. The new regression and the existing Opus decode test both passed on the head. ```text Head (`349929c0cc`): test modules::apu::tests::alternating_mono_and_stereo_match_independent_decoders ... ok test modules::apu::tests::decodes_opus_as_the_title_drives_it ... ok test result: FAILED. 151 passed; 2 failed; 0 ignored; 0 measured; 0 filtered out; finished in 5.71s Base (`63bcca43c6`): test modules::apu::tests::decodes_opus_as_the_title_drives_it ... ok test result: FAILED. 150 passed; 2 failed; 0 ignored; 0 measured; 0 filtered out; finished in 5.62s ``` Both workspace Clippy modes (default and all features, with `-D warnings`) exited 0 on head and base. The Windows-target workspace test under Wine exited 101 on both, stopping in `victory-hle`: `modules::net::tests::only_loopback_is_reachable` hit a missing `WSAConnectByList` export on the isolated runner, and `modules::xgamesave::tests::a_kept_blob_past_the_link_limit_is_copied` did not observe the expected NTFS hard-link limit. Native Windows CI remains the final gate. A/B/B/A release microbenchmark on the Windows MSVC target under Wine: identical synthetic Opus enqueue/dequeue workload, seven 1,024-packet samples per arm after warm-up. The arm medians were 27,094.2 / 26,569.9 / 26,505.9 / 26,995.0 ns/packet. Median of arm medians: `63bcca43c6` 27,044.6 ns/packet; `349929c0cc` 26,537.9 ns/packet (1.87% lower). This measures the synthetic packet path, not gameplay or frame time. --- **Radicle project hub (open-source porting and preservation history):** https://radicle.network/nodes/rosa.radicle.network/rad:z3rqQw7796wMbgj8RszExtWR4wMSE **Live development stream:** https://vdo.ninja/?view=gta6rtx4090live1080p60
DenRakEiw commented 2026-11-25 13:18:38 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by DenRakEiw on 2026-11-25T13:18:38Z.

Review: APPROVED


Verified against origin/main (940d32f):

  • Reusing the Opus decode scratch buffer and pre-reserving the result queue cleanly avoids per-process allocations without altering decode output.
  • cargo test -p victory-hle apu::: 3/3 passed (including process_reuses_decode_scratch_buffer_without_reallocating).
  • cargo clippy -p victory-hle --all-targets -- -D warnings: 0 warnings.
  • cargo xtask policy: 183 files ok.

AI-assisted (Gemini 4 Neon).

<!-- greyfinch-sync:Marvisak/VIctory:review:6058333939 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/2423#pullrequestreview-6058333939). Originally posted by [DenRakEiw](https://github.com/DenRakEiw) on 2026-11-25T13:18:38Z. **Review:** APPROVED --- Verified against `origin/main` (`940d32f`): - Reusing the Opus decode scratch buffer and pre-reserving the result queue cleanly avoids per-process allocations without altering decode output. - `cargo test -p victory-hle apu::`: 3/3 passed (including `process_reuses_decode_scratch_buffer_without_reallocating`). - `cargo clippy -p victory-hle --all-targets -- -D warnings`: 0 warnings. - `cargo xtask policy`: 183 files ok. AI-assisted (Gemini 4 Neon).
catminou4 commented 2026-11-25 13:19:32 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by catminou4 on 2026-11-25T13:19:32Z.

Review: COMMENTED


victory-hle(apu): reuse Opus decode scratch buffer, pre-reserve. Static review only: victory-gpu/victory-hle are Windows-only (D3D12/windows-sys); not compiled or tested on this macOS arm64 host. rustc 1.98.1.

<!-- greyfinch-sync:Marvisak/VIctory:review:6058346924 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/2423#pullrequestreview-6058346924). Originally posted by [catminou4](https://github.com/catminou4) on 2026-11-25T13:19:32Z. **Review:** COMMENTED --- victory-hle(apu): reuse Opus decode scratch buffer, pre-reserve. Static review only: victory-gpu/victory-hle are Windows-only (D3D12/windows-sys); not compiled or tested on this macOS arm64 host. rustc 1.98.1.
Marvisak commented 2026-11-25 16:09:15 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by Marvisak on 2026-11-25T15:32:24Z.


Automated triage for the maintainers.

Needs work. This keeps a reused decode_buf in the APU State instead of allocating MAX_FRAMES * channels floats per packet in process (crates/victory-hle/src/modules/apu.rs), and gives results a starting capacity of streams * commands_per_stream. The logic is fine and it resizes for streams with more channels. What would change the verdict:

  • A measurement. The PR says the allocation causes heap churn on the audio thread but gives no profile or timing; one ~46 KB allocation per packet is unlikely to matter next to the Opus decode itself.
  • A better test. process_reuses_decode_scratch_buffer_without_reallocating asserts the buffer's pointer and capacity stay the same, which is the implementation, not behaviour. The existing decode tests already cover correctness.
  • The starting capacity comes from the title (streams * commands_per_stream); please cap it or keep VecDeque::new().

CI: passing.


Generated by Claude Code

<!-- greyfinch-sync:Marvisak/VIctory:comment:7167000188 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/2423#issuecomment-7167000188). Originally posted by [Marvisak](https://github.com/Marvisak) on 2026-11-25T15:32:24Z. --- <!-- victory-triage v1 head=cf1607b1261ca78b656a74c3cacaa66d5646d665 verdict=needs-work at=2026-11-25T15:36:00Z --> Automated triage for the maintainers. **Needs work.** This keeps a reused `decode_buf` in the APU `State` instead of allocating `MAX_FRAMES * channels` floats per packet in `process` (`crates/victory-hle/src/modules/apu.rs`), and gives `results` a starting capacity of `streams * commands_per_stream`. The logic is fine and it resizes for streams with more channels. What would change the verdict: - **A measurement.** The PR says the allocation causes heap churn on the audio thread but gives no profile or timing; one ~46 KB allocation per packet is unlikely to matter next to the Opus decode itself. - **A better test.** `process_reuses_decode_scratch_buffer_without_reallocating` asserts the buffer's pointer and capacity stay the same, which is the implementation, not behaviour. The existing decode tests already cover correctness. - **The starting capacity** comes from the title (`streams * commands_per_stream`); please cap it or keep `VecDeque::new()`. CI: passing. --- _Generated by [Claude Code](https://claude.ai/code)_
kotekompotek commented 2026-11-25 17:05:19 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by kotekompotek on 2026-11-25T16:53:32Z.


Sound: decode_buf is behind the connection mutex (streams can't race on it), needed falls back to resize for >2-channel decoders (Opus supports up to 8 — MAX_PCM_SAMPLES only pre-sizes stereo, not a cap), and the borrow is disjoint (state.streams[x] decoder vs state.decode_buf). results pre-reserve matches the documented max queue depth. Test verifies capacity retention — good regression pin.

<!-- greyfinch-sync:Marvisak/VIctory:comment:7168688629 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/2423#issuecomment-7168688629). Originally posted by [kotekompotek](https://github.com/kotekompotek) on 2026-11-25T16:53:32Z. --- Sound: `decode_buf` is behind the connection mutex (streams can't race on it), `needed` falls back to `resize` for >2-channel decoders (Opus supports up to 8 — `MAX_PCM_SAMPLES` only pre-sizes stereo, not a cap), and the borrow is disjoint (`state.streams[x]` decoder vs `state.decode_buf`). `results` pre-reserve matches the documented max queue depth. Test verifies capacity retention — good regression pin.
faycalbaklouti commented 2026-11-30 23:01:50 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by faycalbaklouti on 2026-11-30T23:01:50Z.


Thanks for the review. The update at 349929c0cc addresses the three requests:

  • Queue reservation: XApuConnect keeps VecDeque::new() (crates/victory-hle/src/modules/apu.rs:211); the guest’s streams * commands_per_stream no longer controls an upfront allocation.
  • Behavioral test: alternating_mono_and_stereo_match_independent_decoders compares returned lengths and PCM bytes against independent decoders instead of asserting pointer or capacity stability. Both that test and decodes_opus_as_the_title_drives_it reported ok on this head in the Windows-target run under Wine.
  • Measurement: A/B/B/A release microbenchmark on the Windows MSVC target under Wine: identical synthetic Opus enqueue/dequeue workload, seven 1,024-packet samples per arm after warm-up. The arm medians were 27,094.2 / 26,569.9 / 26,505.9 / 26,995.0 ns/packet. Median of arm medians: 63bcca43c6 27,044.6 ns/packet; 349929c0cc 26,537.9 ns/packet (1.87% lower). This measures the synthetic packet path, not gameplay or frame time.

Both workspace Clippy modes (default and all features, with -D warnings) exited 0 on head and base. The Windows-target workspace test under Wine exited 101 on both, stopping in victory-hle: modules::net::tests::only_loopback_is_reachable hit a missing WSAConnectByList export on the isolated runner, and modules::xgamesave::tests::a_kept_blob_past_the_link_limit_is_copied did not observe the expected NTFS hard-link limit. Native Windows CI remains the final gate. The previously reported green CI was for cf1607b126, not this update.

<!-- greyfinch-sync:Marvisak/VIctory:comment:7294621828 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/2423#issuecomment-7294621828). Originally posted by [faycalbaklouti](https://github.com/faycalbaklouti) on 2026-11-30T23:01:50Z. --- Thanks for the review. The update at `349929c0cc` addresses the three requests: - **Queue reservation:** `XApuConnect` keeps `VecDeque::new()` (`crates/victory-hle/src/modules/apu.rs:211`); the guest’s `streams * commands_per_stream` no longer controls an upfront allocation. - **Behavioral test:** `alternating_mono_and_stereo_match_independent_decoders` compares returned lengths and PCM bytes against independent decoders instead of asserting pointer or capacity stability. Both that test and `decodes_opus_as_the_title_drives_it` reported `ok` on this head in the Windows-target run under Wine. - **Measurement:** A/B/B/A release microbenchmark on the Windows MSVC target under Wine: identical synthetic Opus enqueue/dequeue workload, seven 1,024-packet samples per arm after warm-up. The arm medians were 27,094.2 / 26,569.9 / 26,505.9 / 26,995.0 ns/packet. Median of arm medians: `63bcca43c6` 27,044.6 ns/packet; `349929c0cc` 26,537.9 ns/packet (1.87% lower). This measures the synthetic packet path, not gameplay or frame time. Both workspace Clippy modes (default and all features, with `-D warnings`) exited 0 on head and base. The Windows-target workspace test under Wine exited 101 on both, stopping in `victory-hle`: `modules::net::tests::only_loopback_is_reachable` hit a missing `WSAConnectByList` export on the isolated runner, and `modules::xgamesave::tests::a_kept_blob_past_the_link_limit_is_copied` did not observe the expected NTFS hard-link limit. Native Windows CI remains the final gate. The previously reported green CI was for `cf1607b126`, not this update.
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/2423:upstream-pr/2423'
git switch 'upstream-pr/2423'

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