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

Closed
github-bot wants to merge 1 commit from github-bot/gh-535 into main AGit
Owner

@⁠cerealfordinner on GitHub · 2026-12-04 23:29 UTC · original
cerealfordinner:upstream-pr-opus-thread → main · +205 −52 · 1 files · 1 commits · closed without merging

Decodes Opus packets on a thread of the connection instead of in the title's call, so decoding doesn't run on the title's thread.

cargo check -p victory-hle --lib --target x86_64-pc-windows-gnu: clean.

Note: overlaps the Opus work in open PRs #508 and #497 — flagging in case they'd rather land first; happy to rebase or close if it duplicates.

Signed-off-by: cerealfordinner [email protected]

**@⁠cerealfordinner** on GitHub · 2026-12-04 23:29 UTC · [original](https://github.com/lobomfz/VIctory-xmem/pull/535) `cerealfordinner:upstream-pr-opus-thread` → `main` · +205 −52 · 1 files · 1 commits · closed without merging Decodes Opus packets on a thread of the connection instead of in the title's call, so decoding doesn't run on the title's thread. `cargo check -p victory-hle --lib --target x86_64-pc-windows-gnu`: clean. Note: overlaps the Opus work in open PRs #508 and #497 — flagging in case they'd rather land first; happy to rebase or close if it duplicates. Signed-off-by: cerealfordinner <[email protected]>
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-04 23:33:29 +00:00
Author
Owner

@⁠kromka45 on GitHub · 2026-12-05 00:08 UTC · original

good pr i feel like the sound is finally there...there huge upgrade in ms

**@⁠kromka45** on GitHub · 2026-12-05 00:08 UTC · [original](https://github.com/lobomfz/VIctory-xmem/pull/535#issuecomment-7427910305) good pr i feel like the sound is finally there...there huge upgrade in ms
Author
Owner

@⁠Kal0s-design on GitHub · 2026-12-05 00:18 UTC · original

Checked a1f7369 on native Windows with MSVC (didn't run the game).

victory-hle lib tests pass 162/162 (1 ignored) on the PR, five runs with --test-threads=2, same as main. Clippy with and without --features diagnostics gives the same warnings as main, nothing new in apu.rs.

I also ran a throwaway test against both. It used 480 real Opus packets on two streams, queued in bursts up to QUEUE_FULL. Results came back in order, both counters went up by one each time, the PCM matched main's synchronous decode byte for byte and every output was already filled when its result appeared. A bad packet still came back as BAD_PACKET with its neighbours fine. With SIGNAL only on the last of 15 commands, all 15 results were there once the event fired. Disconnecting with 60 commands queued, before or partway through decoding, never wrote anything after XApuDisconnect returned.

From the code, the result is pushed only after the PCM is written, under the mutex the title's dequeue takes (apu.rs:373-376). SetEvent comes after that, still only for SIGNAL (377-381). It doesn't need #455's is_front signal, because with one thread and one FIFO a signalled result can never sit behind an unfinished one. Disconnect joins the thread before freeing anything and drops queued commands (297-300, 362). Deactivate and reset cancel nothing, earlier queued packets still decode and write, so a title would only get a late write if it freed an output buffer before dequeuing its result. One decoder set and one queue per connection keeps order and Opus state. The thread waits on a condvar when idle and there is only one mutex.

Things to consider

  • There's no switch, where #455 has VICTORY_APU_ASYNC=0. In my test the result count was 0 right before each of the 480 dequeues, while on main a result is always ready. If a title dequeues in the same tick it queues, a way back to sync would help.
  • The packet is read and the output written after the call returns (467, 489). Fine for a title that waits as on the console, but on main a packet slot could be reused at once.
  • If the thread ever died the connection would stay PENDING with nothing logged.

This and #455 do the same job and conflict in apu.rs, so only one should land. This one is smaller, strict FIFO, joins on disconnect instead of a 5 s wait and leak, and adds no extra signals. #455 has the switch, a thread pool and higher priority. The reused pcm buffer (471) covers #508, which also conflicts. #497 is in dsp and merges cleanly with this.

AI-assisted: yes

**@⁠Kal0s-design** on GitHub · 2026-12-05 00:18 UTC · [original](https://github.com/lobomfz/VIctory-xmem/pull/535#issuecomment-7428109055) Checked a1f7369 on native Windows with MSVC (didn't run the game). victory-hle lib tests pass 162/162 (1 ignored) on the PR, five runs with --test-threads=2, same as main. Clippy with and without `--features diagnostics` gives the same warnings as main, nothing new in apu.rs. I also ran a throwaway test against both. It used 480 real Opus packets on two streams, queued in bursts up to QUEUE_FULL. Results came back in order, both counters went up by one each time, the PCM matched main's synchronous decode byte for byte and every output was already filled when its result appeared. A bad packet still came back as BAD_PACKET with its neighbours fine. With SIGNAL only on the last of 15 commands, all 15 results were there once the event fired. Disconnecting with 60 commands queued, before or partway through decoding, never wrote anything after XApuDisconnect returned. From the code, the result is pushed only after the PCM is written, under the mutex the title's dequeue takes (apu.rs:373-376). SetEvent comes after that, still only for SIGNAL (377-381). It doesn't need #455's `is_front` signal, because with one thread and one FIFO a signalled result can never sit behind an unfinished one. Disconnect joins the thread before freeing anything and drops queued commands (297-300, 362). Deactivate and reset cancel nothing, earlier queued packets still decode and write, so a title would only get a late write if it freed an output buffer before dequeuing its result. One decoder set and one queue per connection keeps order and Opus state. The thread waits on a condvar when idle and there is only one mutex. Things to consider - There's no switch, where #455 has VICTORY_APU_ASYNC=0. In my test the result count was 0 right before each of the 480 dequeues, while on main a result is always ready. If a title dequeues in the same tick it queues, a way back to sync would help. - The packet is read and the output written after the call returns (467, 489). Fine for a title that waits as on the console, but on main a packet slot could be reused at once. - If the thread ever died the connection would stay PENDING with nothing logged. This and #455 do the same job and conflict in apu.rs, so only one should land. This one is smaller, strict FIFO, joins on disconnect instead of a 5 s wait and leak, and adds no extra signals. #455 has the switch, a thread pool and higher priority. The reused `pcm` buffer (471) covers #508, which also conflicts. #497 is in dsp and merges cleanly with this. AI-assisted: yes
github-bot closed this pull request 2026-12-05 17:32:02 +00:00

Pull request closed

This discussion has been locked. Commenting is limited to contributors.
No description provided.