HLE: offload async saves, reuse audio buffers, pin APU heap, and decouple input rumble/dialogs #237

Open
wbkepler wants to merge 1 commit from upstream-pr/237 into main
wbkepler commented 2026-11-24 01:42:29 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by wbkepler on 2026-11-24T01:42:29Z.


Summary

Eliminates save, audio, and input frame hitches across victory-hle:

  • xasync & xgamesave:
    • xasync::pending_with runs on the shared thread pool with Arc-owned state and a cancel guard (preventing UAF on XAsyncCancel and per-call OS thread creation).
    • All xgamesave *Async methods (initialize_async, quota_async, delete_async, read_async, submit_async) snapshot caller handles/buffers synchronously and offload blocking disk I/O + sync_all() with a FIFO SaveTicket order guard preserving crash-safe atomic saves.
  • apu.rs:
    • Moves Opus decoders and lazily-grown reusable per-stream PCM scratch buffers out of Connection::state into per-stream mutexes (zero 46 KiB per-packet allocation and no dequeue/count lock contention while keeping synchronous completion semantics).
  • audio.rs:
    • Switches HEAP to RwLock, replaces unbounded-lifetime heap_bytes with Heap::slice/Heap::ptr held under a single read guard per pump() pass (blocking concurrent ApuFree UAF and removing per-256-byte relocking), and reuses Voice::scratch across XMA frame decodes/flushes.
  • gameinput.rs & xgameui.rs:
    • Decouples set_rumble from Hub::source via lock-free per-pad AtomicU64 slots flushed on Hub::poll.
    • Tracks active modal dialogs in xgameui.rs with a RAII DialogGuard checked by gameinput::host::focused() to suppress key leakage during modal text entry.
  • Adds 5 unit tests covering all changes.

Verification

  • cargo clippy --workspace --all-targets — passed (zero new warnings)
  • cargo clippy -p victory --features diagnostics --all-targets — passed
  • cargo test --workspace — passed (including crash-safe save test and all 5 new tests)
  • cargo xtask policy — passed (151 files ok)
<!-- greyfinch-sync:Marvisak/VIctory:issue:6572676896 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/237). Originally posted by [wbkepler](https://github.com/wbkepler) on 2026-11-24T01:42:29Z. --- ## Summary Eliminates save, audio, and input frame hitches across `victory-hle`: - **`xasync` & `xgamesave`**: - `xasync::pending_with` runs on the shared thread pool with `Arc`-owned state and a cancel guard (preventing UAF on `XAsyncCancel` and per-call OS thread creation). - All `xgamesave` `*Async` methods (`initialize_async`, `quota_async`, `delete_async`, `read_async`, `submit_async`) snapshot caller handles/buffers synchronously and offload blocking disk I/O + `sync_all()` with a FIFO `SaveTicket` order guard preserving crash-safe atomic saves. - **`apu.rs`**: - Moves Opus decoders and lazily-grown reusable per-stream PCM scratch buffers out of `Connection::state` into per-stream mutexes (zero 46 KiB per-packet allocation and no dequeue/count lock contention while keeping synchronous completion semantics). - **`audio.rs`**: - Switches `HEAP` to `RwLock`, replaces unbounded-lifetime `heap_bytes` with `Heap::slice`/`Heap::ptr` held under a single read guard per `pump()` pass (blocking concurrent `ApuFree` UAF and removing per-256-byte relocking), and reuses `Voice::scratch` across XMA frame decodes/flushes. - **`gameinput.rs` & `xgameui.rs`**: - Decouples `set_rumble` from `Hub::source` via lock-free per-pad `AtomicU64` slots flushed on `Hub::poll`. - Tracks active modal dialogs in `xgameui.rs` with a RAII `DialogGuard` checked by `gameinput::host::focused()` to suppress key leakage during modal text entry. - Adds 5 unit tests covering all changes. ## Verification - `cargo clippy --workspace --all-targets` — passed (zero new warnings) - `cargo clippy -p victory --features diagnostics --all-targets` — passed - `cargo test --workspace` — passed (including crash-safe save test and all 5 new tests) - `cargo xtask policy` — passed (151 files ok)
jeremygold02 commented 2026-11-24 06:55:55 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by jeremygold02 on 2026-11-24T05:01:00Z.


Thanks @wbkepler for the combined async-save, audio-buffer, APU, and input work. I tested the open PR at head 203d9f38407edeb74f37ae03f7a44b323cf402fb with:

cargo test --locked -p victory-hle --lib -- --test-threads=1

Result: 91 passed, 0 failed, 0 ignored. Representative coverage includes APU scratch reuse without holding the state lock, audio heap pinning and voice-buffer reuse, host rumble routing, cancellation of queued XAsync work, FIFO ordering for offloaded async saves, and typed dialog completion. The suite is HLE unit coverage; it does not launch a title or exercise a real host audio device/dialog.

Relevant tests: apu.rs, audio.rs, gameinput.rs, xasync.rs, xgamesave.rs, xgameui.rs.

<!-- greyfinch-sync:Marvisak/VIctory:comment:7122544925 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/237#issuecomment-7122544925). Originally posted by [jeremygold02](https://github.com/jeremygold02) on 2026-11-24T05:01:00Z. --- Thanks @wbkepler for the combined async-save, audio-buffer, APU, and input work. I tested the open PR at head `203d9f38407edeb74f37ae03f7a44b323cf402fb` with: ``` cargo test --locked -p victory-hle --lib -- --test-threads=1 ``` Result: 91 passed, 0 failed, 0 ignored. Representative coverage includes APU scratch reuse without holding the state lock, audio heap pinning and voice-buffer reuse, host rumble routing, cancellation of queued XAsync work, FIFO ordering for offloaded async saves, and typed dialog completion. The suite is HLE unit coverage; it does not launch a title or exercise a real host audio device/dialog. Relevant tests: [apu.rs](https://github.com/Marvisak/VIctory/blob/203d9f38407edeb74f37ae03f7a44b323cf402fb/crates/victory-hle/src/modules/apu.rs), [audio.rs](https://github.com/Marvisak/VIctory/blob/203d9f38407edeb74f37ae03f7a44b323cf402fb/crates/victory-hle/src/modules/audio.rs), [gameinput.rs](https://github.com/Marvisak/VIctory/blob/203d9f38407edeb74f37ae03f7a44b323cf402fb/crates/victory-hle/src/modules/gameinput.rs), [xasync.rs](https://github.com/Marvisak/VIctory/blob/203d9f38407edeb74f37ae03f7a44b323cf402fb/crates/victory-hle/src/modules/xasync.rs), [xgamesave.rs](https://github.com/Marvisak/VIctory/blob/203d9f38407edeb74f37ae03f7a44b323cf402fb/crates/victory-hle/src/modules/xgamesave.rs), [xgameui.rs](https://github.com/Marvisak/VIctory/blob/203d9f38407edeb74f37ae03f7a44b323cf402fb/crates/victory-hle/src/modules/xgameui.rs).
Marvisak commented 2026-11-25 01:07:43 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by Marvisak on 2026-11-25T00:23:02Z.


Automated triage for the maintainers.

Needs rebase. This PR does not merge cleanly into current main (ac2525d). Conflicting files: crates/victory-hle/src/modules/xasync.rs, crates/victory-hle/src/modules/xgamesave.rs, crates/victory-hle/src/modules/xgameui.rs.

The content has not been reviewed yet; it will be once the branch is rebased onto main.


Generated by Claude Code

<!-- greyfinch-sync:Marvisak/VIctory:comment:7148452674 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/237#issuecomment-7148452674). Originally posted by [Marvisak](https://github.com/Marvisak) on 2026-11-25T00:23:02Z. --- <!-- victory-triage v1 head=203d9f38407edeb74f37ae03f7a44b323cf402fb verdict=needs-rebase at=2026-11-25T00:13:20Z --> Automated triage for the maintainers. **Needs rebase.** This PR does not merge cleanly into current `main` (ac2525d). Conflicting files: `crates/victory-hle/src/modules/xasync.rs`, `crates/victory-hle/src/modules/xgamesave.rs`, `crates/victory-hle/src/modules/xgameui.rs`. The content has not been reviewed yet; it will be once the branch is rebased onto `main`. --- _Generated by [Claude Code](https://claude.ai/code)_
andrewpozdnakov7-jpg commented 2026-11-26 01:04:11 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by andrewpozdnakov7-jpg on 2026-11-26T00:42:57Z.


I checked the Opus allocation change on exact 203d9f38407edeb74f37ae03f7a44b323cf402fb against current main 63bcca43. The same synthetic Windows test measured allocations on the calling thread after eight warm-up calls, then ran 128 commands alternating mono and stereo with a trimmed deinterleaved stereo output.

  • 63bcca43: 128 allocations, 4,423,680 bytes requested.
  • 203d9f38: 0 allocations and 0 bytes requested during the measured commands.

The outputs reported the same frame and byte counts, with PCM checksum c4ab76792457d6ab on both revisions. This confirms the per-stream scratch change against an allocation count as well as the existing scratch-capacity test.

git merge-tree against 63bcca43 also finds an apu.rs conflict. In that file, the activation hunk can keep main's upfront 1–2 channel guard and assign the decoder to slot.decoder on success. The test hunk can keep both reuses_pcm_scratch_and_decouples_state_lock and main's decodes_into_unaligned_output_interleaved_and_deinterleaved.

<!-- greyfinch-sync:Marvisak/VIctory:comment:7179041781 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/237#issuecomment-7179041781). Originally posted by [andrewpozdnakov7-jpg](https://github.com/andrewpozdnakov7-jpg) on 2026-11-26T00:42:57Z. --- I checked the Opus allocation change on exact `203d9f38407edeb74f37ae03f7a44b323cf402fb` against current main `63bcca43`. The same synthetic Windows test measured allocations on the calling thread after eight warm-up calls, then ran 128 commands alternating mono and stereo with a trimmed deinterleaved stereo output. - `63bcca43`: 128 allocations, 4,423,680 bytes requested. - `203d9f38`: 0 allocations and 0 bytes requested during the measured commands. The outputs reported the same frame and byte counts, with PCM checksum `c4ab76792457d6ab` on both revisions. This confirms the per-stream scratch change against an allocation count as well as the existing scratch-capacity test. `git merge-tree` against `63bcca43` also finds an `apu.rs` conflict. In that file, the activation hunk can keep main's upfront 1–2 channel guard and assign the decoder to `slot.decoder` on success. The test hunk can keep both `reuses_pcm_scratch_and_decouples_state_lock` and main's `decodes_into_unaligned_output_interleaved_and_deinterleaved`.
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/237:upstream-pr/237'
git switch 'upstream-pr/237'

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