apu: Opus packets decoded by a thread of the connection, not in the title's call #175

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

@⁠Ekabusto on GitHub · 2026-12-02 09:44 UTC · original
Ekabusto:perf/apu-off-title-thread → main · +205 −52 · 1 files · 1 commits · open

What

XApuEnqueueCommand decoded each Opus packet inside the title's call. On the console the audio decoder is hardware beside the CPU: the title queues a command and later polls XApuDequeueResult (PENDING_RESULTS) or waits on the connection's event. This moves the decoding to one thread per connection, the way the console works.

  • Commands run in order on the connection's thread, so results keep the order of commands.
  • Queue room (QUEUE_FULL) and the total out parameter are still counted at the call; error codes are checked in the same order as before.
  • The thread reuses one PCM buffer instead of allocating and zeroing 46 KB per packet.
  • XApuDisconnect stops the thread, after the command it is running, before freeing the memory and closing the event.
  • Same decoder, same output: audio is unchanged.

Why

A perf profile (30 s driving in a busy scene, diag build) had 26 % of the busiest title thread's samples in opus_pure, called from XApuEnqueueCommand.

Measured

Release builds, RX 9070 XT + Ryzen 9 5900X, 1440p, Series X profile, ~30 s driving the same route, before/after:

before after
busiest title thread 74 % of a core 48 % of a core
decoder thread n/a 0.22 cores
fps no change beyond run-to-run noise

To be clear: this does not raise fps here. The thread that was decoding is not on this title's frame path (the timeline's critical path is the title's own code on other threads). The change frees about a quarter of a title thread and matches the console's behavior; it may matter more on CPUs with fewer cores.

Tests

  • cargo test -p victory-hle --lib -- apu: both APU tests pass. They now wait for the connection's thread (settle) before reading results, since results are no longer ready at the call.
  • cargo clippy -p victory-hle --all-targets: clean.
  • Played the patched build: audio normal (music, voices, radio).

🤖 Generated with Claude Code

**@⁠Ekabusto** on GitHub · 2026-12-02 09:44 UTC · [original](https://github.com/lobomfz/VIctory-xmem/pull/175) `Ekabusto:perf/apu-off-title-thread` → `main` · +205 −52 · 1 files · 1 commits · open ## What `XApuEnqueueCommand` decoded each Opus packet inside the title's call. On the console the audio decoder is hardware beside the CPU: the title queues a command and later polls `XApuDequeueResult` (`PENDING_RESULTS`) or waits on the connection's event. This moves the decoding to one thread per connection, the way the console works. - Commands run in order on the connection's thread, so results keep the order of commands. - Queue room (`QUEUE_FULL`) and the `total` out parameter are still counted at the call; error codes are checked in the same order as before. - The thread reuses one PCM buffer instead of allocating and zeroing 46 KB per packet. - `XApuDisconnect` stops the thread, after the command it is running, before freeing the memory and closing the event. - Same decoder, same output: audio is unchanged. ## Why A `perf` profile (30 s driving in a busy scene, diag build) had **26 % of the busiest title thread's samples in `opus_pure`**, called from `XApuEnqueueCommand`. ## Measured Release builds, RX 9070 XT + Ryzen 9 5900X, 1440p, Series X profile, ~30 s driving the same route, before/after: | | before | after | |---|---|---| | busiest title thread | 74 % of a core | 48 % of a core | | decoder thread | n/a | 0.22 cores | | fps | no change beyond run-to-run noise | | To be clear: **this does not raise fps here.** The thread that was decoding is not on this title's frame path (the timeline's critical path is the title's own code on other threads). The change frees about a quarter of a title thread and matches the console's behavior; it may matter more on CPUs with fewer cores. ## Tests - `cargo test -p victory-hle --lib -- apu`: both APU tests pass. They now wait for the connection's thread (`settle`) before reading results, since results are no longer ready at the call. - `cargo clippy -p victory-hle --all-targets`: clean. - Played the patched build: audio normal (music, voices, radio). 🤖 Generated with [Claude Code](https://claude.com/claude-code)
XApuEnqueueCommand decoded each packet before returning. On the console
the decoder is hardware beside the CPU: the title queues a command and
polls XApuDequeueResult (PENDING_RESULTS) or waits on the connection's
event. A profile while driving in a busy scene had 26 % of the busiest
title thread's samples in the Opus decoder.

Now each connection has a thread that runs the queued commands in order,
so results keep the order of commands; queue room and the total are still
counted at the call. The thread reuses one PCM buffer instead of zeroing
46 KB per packet. Disconnect stops the thread before the memory and the
event go.

Measured (release, RX 9070 XT + 5900X, ~30 s driving the same route):
the busiest title thread went from 74 % to 48 % of a core, the decoder
thread takes 0.22 cores. Frame rate did not change beyond run-to-run
noise: that thread is not on this title's frame path.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
github-bot locked as GitHub sync and limited conversation to collaborators 2026-12-02 09:47:29 +00:00
Author
Owner

@⁠Kal0s-design on GitHub · 2026-12-03 19:19 UTC · original

Tested this on Windows 11 native (MSVC, toolchain 1.98.1), CPU only, no game run. Checked both the PR head and a local merge with current main (only apu.rs differs from main, it merges clean).

What I looked at in apu.rs:

  • The thread writes the PCM outside the lock, then takes the lock and pushes the result (serve, ~374-376). Dequeue pops under the same mutex, so by the time the title sees a result, the output bytes are already written.
  • SetEvent runs after push_back, under the lock (~376-382), so the event can't fire before the result is there.
  • Disconnect sets closing and notifies, the guard is a temporary so it isn't held across join(), then joins and only after that closes the event and frees the memory (~297-307). The thread never runs title code, so it can't call Disconnect itself. Commands still queued at disconnect are dropped without results, which seems fine since the handle is gone after that.
  • No lost wakeup that I can see: the thread checks closing and commands_left under the lock before waiting, and Enqueue pushes under the lock before notify.
  • queued[stream] still goes up at enqueue and down at dequeue, same as before, so QUEUE_FULL can't drift. INVALID_STREAM / QUEUE_FULL / INVALID_COMMAND are still returned at the call in the same order, and total is the same value as before.
  • Only visible behaviour change: an enqueue followed right away by a dequeue can now get PENDING_RESULTS where main always returned the result. That's the point of the PR and titles poll or wait on the event anyway.

Tests:

  • cargo test -p victory-hle --lib: main 157 passed / 3 failed / 1 ignored, PR merged with main the same 157 / 3 / 1, PR head alone 154 / 3 / 1 (it's behind main). The 3 failures are the same gameinput tests on all three and fail on main too, so they're unrelated to this PR.
  • The two apu tests, 20 runs in a row on the PR head: all passed.
  • I also wrote a few local tests (not pushed) and ran them 20x on the PR head and 10x on the merge, all passed:
    • 20k process commands over 8 streams, no settle(), dequeuing while enqueuing: results come back in command order and the output buffer is already filled when each result shows up.
    • RESULT_EVENT + SIGNAL, 2000 rounds of enqueue, wait on the event, dequeue: the result was always there after the event.
    • 200 connect / activate / fill every queue / disconnect cycles: disconnect always returned (about 50 ms for all 200), and the shared state's Arc was gone right after each disconnect, so the thread had ended.
    • 20k random garbage packets on one stream: every command got a result (about 40% BAD_PACKET), the thread kept running.
  • cargo clippy -p victory-hle --all-targets: no warnings in apu.rs. The 3 in victory-hle are in other files the PR doesn't touch.

One small thing I only found by reading, not by a test. If the decoder thread ever panicked, the stream would just stop producing results and the title would keep polling, and Disconnect drops the join error with let _ = worker.join(). On main the same panic would have happened in the title's call. I couldn't make it panic, even with the garbage packets, so maybe not worth more than a log line on the join error.

Otherwise I didn't find anything to change. Looks good to me.

AI-assisted: yes

**@⁠Kal0s-design** on GitHub · 2026-12-03 19:19 UTC · [original](https://github.com/lobomfz/VIctory-xmem/pull/175#issuecomment-7389502500) Tested this on Windows 11 native (MSVC, toolchain 1.98.1), CPU only, no game run. Checked both the PR head and a local merge with current main (only apu.rs differs from main, it merges clean). What I looked at in apu.rs: - The thread writes the PCM outside the lock, then takes the lock and pushes the result (serve, ~374-376). Dequeue pops under the same mutex, so by the time the title sees a result, the output bytes are already written. - SetEvent runs after push_back, under the lock (~376-382), so the event can't fire before the result is there. - Disconnect sets `closing` and notifies, the guard is a temporary so it isn't held across join(), then joins and only after that closes the event and frees the memory (~297-307). The thread never runs title code, so it can't call Disconnect itself. Commands still queued at disconnect are dropped without results, which seems fine since the handle is gone after that. - No lost wakeup that I can see: the thread checks `closing` and `commands_left` under the lock before waiting, and Enqueue pushes under the lock before notify. - `queued[stream]` still goes up at enqueue and down at dequeue, same as before, so QUEUE_FULL can't drift. INVALID_STREAM / QUEUE_FULL / INVALID_COMMAND are still returned at the call in the same order, and `total` is the same value as before. - Only visible behaviour change: an enqueue followed right away by a dequeue can now get PENDING_RESULTS where main always returned the result. That's the point of the PR and titles poll or wait on the event anyway. Tests: - `cargo test -p victory-hle --lib`: main 157 passed / 3 failed / 1 ignored, PR merged with main the same 157 / 3 / 1, PR head alone 154 / 3 / 1 (it's behind main). The 3 failures are the same gameinput tests on all three and fail on main too, so they're unrelated to this PR. - The two apu tests, 20 runs in a row on the PR head: all passed. - I also wrote a few local tests (not pushed) and ran them 20x on the PR head and 10x on the merge, all passed: - 20k process commands over 8 streams, no settle(), dequeuing while enqueuing: results come back in command order and the output buffer is already filled when each result shows up. - RESULT_EVENT + SIGNAL, 2000 rounds of enqueue, wait on the event, dequeue: the result was always there after the event. - 200 connect / activate / fill every queue / disconnect cycles: disconnect always returned (about 50 ms for all 200), and the shared state's Arc was gone right after each disconnect, so the thread had ended. - 20k random garbage packets on one stream: every command got a result (about 40% BAD_PACKET), the thread kept running. - `cargo clippy -p victory-hle --all-targets`: no warnings in apu.rs. The 3 in victory-hle are in other files the PR doesn't touch. One small thing I only found by reading, not by a test. If the decoder thread ever panicked, the stream would just stop producing results and the title would keep polling, and Disconnect drops the join error with `let _ = worker.join()`. On main the same panic would have happened in the title's call. I couldn't make it panic, even with the garbage packets, so maybe not worth more than a log line on the join error. Otherwise I didn't find anything to change. Looks good to me. AI-assisted: yes
Author
Owner

@⁠Luxuride on GitHub · 2026-12-04 12:01 UTC · original

Land this one. It is the smallest correct version of the APU-async fix, it merges cleanly today, and it is the only one of the three PRs on this issue whose tests actually run the async path (settle(handle) at apu.rs:561 waits for real quiescence instead of timing).

Two things before you do:

  1. Lifetime, not threading, is the risk: apu.rs:340 copies only the command descriptor, whose input/output are still title addresses, and the worker dereferences them later at apu.rs:439. That is contract-legal on the console (the packet buffer is the title's until the result is dequeued), but a title that reuses its buffer on the next submit now gets its bytes read after it overwrote them. Copying the packet bytes at submit removes the question; at minimum say in the body that you checked the title's submit loop.
  2. Give it an escape hatch. The async path is unconditional today, so a misbehaving title can only be fixed by a revert; a five-line VICTORY_APU_SYNC-style switch is the difference between "ship it" and "hold it".

apu.rs:146's #[cfg(test)] idle: Condvar living in the shared state struct is harmless, just note it.

**@⁠Luxuride** on GitHub · 2026-12-04 12:01 UTC · [original](https://github.com/lobomfz/VIctory-xmem/pull/175#issuecomment-7410869461) Land this one. It is the smallest correct version of the APU-async fix, it merges cleanly today, and it is the only one of the three PRs on this issue whose tests actually run the async path (`settle(handle)` at `apu.rs:561` waits for real quiescence instead of timing). Two things before you do: 1. Lifetime, not threading, is the risk: `apu.rs:340` copies only the command descriptor, whose `input`/`output` are still title addresses, and the worker dereferences them later at `apu.rs:439`. That is contract-legal on the console (the packet buffer is the title's until the result is dequeued), but a title that reuses its buffer on the next submit now gets its bytes read after it overwrote them. Copying the packet bytes at submit removes the question; at minimum say in the body that you checked the title's submit loop. 2. Give it an escape hatch. The async path is unconditional today, so a misbehaving title can only be fixed by a revert; a five-line `VICTORY_APU_SYNC`-style switch is the difference between "ship it" and "hold it". `apu.rs:146`'s `#[cfg(test)] idle: Condvar` living in the shared state struct is harmless, just note it.
This pull request has changes conflicting with the target branch.
  • crates/victory-hle/src/modules/apu.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/175/head:github-bot/gh-175'
git switch 'github-bot/gh-175'

Merge

Merge the changes and update on Forgejo.
git switch 'main'
git merge --no-ff 'github-bot/gh-175'
git switch 'github-bot/gh-175'
git rebase 'main'
git switch 'main'
git merge --ff-only 'github-bot/gh-175'
git switch 'github-bot/gh-175'
git rebase 'main'
git switch 'main'
git merge --no-ff 'github-bot/gh-175'
git switch 'main'
git merge --squash 'github-bot/gh-175'
git switch 'main'
git merge --ff-only 'github-bot/gh-175'
git switch 'main'
git merge 'github-bot/gh-175'
git push origin 'main'
This discussion has been locked. Commenting is limited to contributors.
No description provided.