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

Open
github-bot wants to merge 1 commit from github-bot/gh-237 into main AGit
Owner

@⁠wbkepler on GitHub · 2026-11-24 01:42 UTC · original
wbkepler:hle/async-saves-and-audio-hitches → main · +850 −156 · 6 files · 1 commits · open

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)
**@⁠wbkepler** on GitHub · 2026-11-24 01:42 UTC · [original](https://github.com/Marvisak/VIctory/pull/237) `wbkepler:hle/async-saves-and-audio-hitches` → `main` · +850 −156 · 6 files · 1 commits · open ## 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)
- xasync.rs & xgamesave.rs: run `pending_with` work on the shared `xasync`
  thread pool with `Arc`-owned state and a cancel guard (preventing UAF on
  cancellation and avoiding per-call OS thread creation). Offload all
  `xgamesave` `*Async` methods (`initialize_async`, `quota_async`,
  `delete_async`, `read_async`, `submit_async`) onto `xasync::pending_with`,
  snapshotting caller handles/buffers synchronously before returning `S_OK`
  and preserving strict FIFO ordering and crash-safe atomic commits.
- apu.rs: move Opus decoders and a lazily grown per-stream reusable PCM
  scratch `Vec<f32>` out of `Connection::state` into per-stream mutexes so
  decoding neither allocates 46 KiB per packet nor blocks `XApuDequeueResult`,
  `XApuGetResultCount`, or other streams.
- audio.rs: replace `HEAP` `Mutex` with `RwLock`, remove the unbounded-lifetime
  `heap_bytes` slice in favor of `Heap::slice`/`Heap::ptr` tied to a single
  read guard per `pump()` pass (preventing concurrent `ApuFree` UAF and
  eliminating per-256-byte relocking), and reuse `Voice::scratch` across XMA
  frame decodes and flushes.
- gameinput.rs & xgameui.rs: decouple `set_rumble` from `Hub::source` using
  lock-free per-pad `AtomicU64` pending rumble slots flushed by `Hub::poll`;
  track open modal `xgameui` dialogs with a RAII `DialogGuard` counter so
  `gameinput::host::focused()` suppresses keyboard/fallback-pad forwarding
  while a modal text-entry dialog is active.
- Add unit tests covering `pending_with` cancel safety, offloaded FIFO
  `xgamesave` calls, `apu` scratch reuse & lock decoupling, `audio` `RwLock`
  free-pinning & XMA scratch reuse, and `gameinput` non-blocking rumble &
  modal dialog focus suppression.
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 03:55:56 +00:00
Author
Owner

@⁠jeremygold02 on GitHub · 2026-11-24 05:01 UTC · original

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.

**@⁠jeremygold02** on GitHub · 2026-11-24 05:01 UTC · [original](https://github.com/Marvisak/VIctory/pull/237#issuecomment-7122544925) 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).
Author
Owner

@⁠Marvisak on GitHub · 2026-11-25 00:23 UTC · original

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

**@⁠Marvisak** on GitHub · 2026-11-25 00:23 UTC · [original](https://github.com/Marvisak/VIctory/pull/237#issuecomment-7148452674) <!-- 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)_
Author
Owner

@⁠andrewpozdnakov7-jpg on GitHub · 2026-11-26 00:42 UTC · original

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.

**@⁠andrewpozdnakov7-jpg** on GitHub · 2026-11-26 00:42 UTC · [original](https://github.com/Marvisak/VIctory/pull/237#issuecomment-7179041781) 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 has changes conflicting with the target branch.
  • crates/victory-hle/src/modules/apu.rs
  • crates/victory-hle/src/modules/audio.rs
  • crates/victory-hle/src/modules/gameinput.rs
  • crates/victory-hle/src/modules/xasync.rs
  • crates/victory-hle/src/modules/xgamesave.rs
  • crates/victory-hle/src/modules/xgameui.rs
View command line instructions

Manual merge helper

Use this merge commit message when completing the merge manually.

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u origin '+refs/pull/237/head:github-bot/gh-237'
git switch 'github-bot/gh-237'

Merge

Merge the changes and update on Forgejo.
git switch 'main'
git merge --no-ff 'github-bot/gh-237'
git switch 'github-bot/gh-237'
git rebase 'main'
git switch 'main'
git merge --ff-only 'github-bot/gh-237'
git switch 'github-bot/gh-237'
git rebase 'main'
git switch 'main'
git merge --no-ff 'github-bot/gh-237'
git switch 'main'
git merge --squash 'github-bot/gh-237'
git switch 'main'
git merge --ff-only 'github-bot/gh-237'
git switch 'main'
git merge 'github-bot/gh-237'
git push origin 'main'
This discussion has been locked. Commenting is limited to contributors.
No reviewers
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.

Dependencies

No dependencies set

Reference
github/Marvisak--VIctory!237
No description provided.