apu: Opus packets decode on a worker of the connection, off the title's thread #155

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

@⁠Arvi23 on GitHub · 2026-12-02 02:34 UTC · original
Arvi23:apu-worker → main · +378 −84 · 2 files · 1 commits · closed without merging

What

Each XApu decoder connection gets a worker thread (victory XApu decoder, THREAD_PRIORITY_HIGHEST) that decodes the queued commands in the order they were queued, reusing one scratch buffer. Until a command's result is ready the title is told PENDING_RESULTS, which it already polls for, and the connection's event is signalled when a command asks for it, as before. Disconnecting lets the worker finish what is queued, then frees the connection's memory.

VICTORY_APU_ASYNC=0 goes back to decoding each command as it is queued (README row added).

Refs #137.

Why

#137: the title's thread decoded every Opus packet itself, as it queued the command: about 29% of a thread that ran at 70% (i7-10700K, main f7f0360, measured by @⁠weneedbucks).

What is known so far

  • On a Ryzen 7 5800X3D (RX 9070 XT) the worker used 28–33% of a core in busy open-world scenes (per-thread sampling of /proc/<pid>/task/*/schedstat): time the title's thread no longer spends.
  • Several play sessions with it had no sound faults of its own.
  • No frame-rate A/B yet. @⁠weneedbucks offered to post before/after numbers on main.

Tests

async_worker_decodes_in_order_and_disconnects_cleanly drives the worker path (activate + process, results in order, clean disconnect); the existing tests drive the decode-as-queued path.

Note

Written by an AI agent (Gemini) on a local integration branch, then reviewed, documented and rebased onto main with Claude Code. #149 (real-time priority for the audio threads) promotes this worker by name where both are in.

🤖 Generated with Claude Code

**@⁠Arvi23** on GitHub · 2026-12-02 02:34 UTC · [original](https://github.com/lobomfz/VIctory-xmem/pull/155) `Arvi23:apu-worker` → `main` · +378 −84 · 2 files · 1 commits · closed without merging ## What Each XApu decoder connection gets a worker thread (`victory XApu decoder`, `THREAD_PRIORITY_HIGHEST`) that decodes the queued commands in the order they were queued, reusing one scratch buffer. Until a command's result is ready the title is told `PENDING_RESULTS`, which it already polls for, and the connection's event is signalled when a command asks for it, as before. Disconnecting lets the worker finish what is queued, then frees the connection's memory. `VICTORY_APU_ASYNC=0` goes back to decoding each command as it is queued (README row added). Refs #137. ## Why #137: the title's thread decoded every Opus packet itself, as it queued the command: about 29% of a thread that ran at 70% (i7-10700K, main f7f0360, measured by @⁠weneedbucks). ## What is known so far - On a Ryzen 7 5800X3D (RX 9070 XT) the worker used 28–33% of a core in busy open-world scenes (per-thread sampling of `/proc/<pid>/task/*/schedstat`): time the title's thread no longer spends. - Several play sessions with it had no sound faults of its own. - No frame-rate A/B yet. @⁠weneedbucks offered to post before/after numbers on main. ## Tests `async_worker_decodes_in_order_and_disconnects_cleanly` drives the worker path (activate + process, results in order, clean disconnect); the existing tests drive the decode-as-queued path. ## Note Written by an AI agent (Gemini) on a local integration branch, then reviewed, documented and rebased onto main with Claude Code. #149 (real-time priority for the audio threads) promotes this worker by name where both are in. 🤖 Generated with [Claude Code](https://claude.com/claude-code)
Issue #137: the title's thread decoded every Opus packet itself, as it
queued the command, about 29% of a thread that ran at 70% (i7-10700K, main
f7f0360). Each decoder connection now has a worker ("victory XApu decoder",
THREAD_PRIORITY_HIGHEST) that decodes the commands in the order they were
queued, reusing one scratch buffer. The title is told PENDING_RESULTS until
a result is ready, which it polls for, and the connection's event is
signalled when a command asks for it, as before. Disconnecting lets the
worker finish what is queued, then frees the connection's memory.

On a 5800X3D the worker used 28-33% of a core in busy open-world scenes:
time the title's thread no longer spends. Several play sessions with it
had no sound faults of its own. No frame-rate A/B yet.
VICTORY_APU_ASYNC=0 decodes when the command is queued, as before.

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

@⁠weneedbucks on GitHub · 2026-12-02 03:17 UTC · original

thanks for posting!
A/B on i7-10700K / RX 9060 XT (f7f0360 + #109, #113, #126, #129, with #155 on top). RT on, FSR 4 host, Series S. Same save, auto-drive to the same waypoint, alternating runs off/on/off/on with VICTORY_APU_ASYNC=0 for off, ~5–6.5 min each, MangoHud per-frame:

median fps 1% low >100 ms/min
off 26.7 / 26.3 9.4 / 8.5 8.8 / 11.5
on 23.5 / 23.3 8.5 / 8.4 10.0 / 11.4

So about 12% lower median with the worker, consistent across both pairs. The work does move: the title thread that decoded Opus went from 56–60% to 39–45% of a core, and victory XApu decoder ran at 20–23%. But the busiest title thread stayed at ~70% either way. My guess is that the title waits on PENDING_RESULTS and that adds latency per packet, or that the worker at THREAD_PRIORITY_HIGHEST takes time from title threads, but I haven't verified either. Happy to rerun with a variant (e.g. worker at normal priority).

**@⁠weneedbucks** on GitHub · 2026-12-02 03:17 UTC · [original](https://github.com/lobomfz/VIctory-xmem/pull/155#issuecomment-7333726858) thanks for posting! A/B on i7-10700K / RX 9060 XT (f7f0360 + #109, #113, #126, #129, with #155 on top). RT on, FSR 4 host, Series S. Same save, auto-drive to the same waypoint, alternating runs off/on/off/on with VICTORY_APU_ASYNC=0 for off, ~5–6.5 min each, MangoHud per-frame: | | median fps | 1% low | >100 ms/min | |-----|-------------|-----------|-------------| | off | 26.7 / 26.3 | 9.4 / 8.5 | 8.8 / 11.5 | | on | 23.5 / 23.3 | 8.5 / 8.4 | 10.0 / 11.4 | So about 12% lower median with the worker, consistent across both pairs. The work does move: the title thread that decoded Opus went from 56–60% to 39–45% of a core, and victory XApu decoder ran at 20–23%. But the busiest title thread stayed at ~70% either way. My guess is that the title waits on PENDING_RESULTS and that adds latency per packet, or that the worker at THREAD_PRIORITY_HIGHEST takes time from title threads, but I haven't verified either. Happy to rerun with a variant (e.g. worker at normal priority).
Author
Owner

@⁠Arvi23 on GitHub · 2026-12-02 10:27 UTC · original

Thanks, that's a clear result: about 12% lower median, the same in both pairs, so as it stands #155 costs more than it saves. I'm converting it to draft.

Since the busiest title thread stays at ~70% and the thread that decoded only drops from 56–60% to 39–45%, my guess is that the title waits for PENDING_RESULTS within the same frame (a poll or a short wait per batch), so each batch's decode latency lands on the frame instead of running beside it. If you have time, a run with the worker at normal priority (remove the SetThreadPriority call at the top of worker_loop) would separate your two guesses. If it's the waiting, the fix is probably to decode the batch inline when the title is about to collect it, which is close to what main does today.

**@⁠Arvi23** on GitHub · 2026-12-02 10:27 UTC · [original](https://github.com/lobomfz/VIctory-xmem/pull/155#issuecomment-7342508237) Thanks, that's a clear result: about 12% lower median, the same in both pairs, so as it stands #155 costs more than it saves. I'm converting it to draft. Since the busiest title thread stays at ~70% and the thread that decoded only drops from 56–60% to 39–45%, my guess is that the title waits for `PENDING_RESULTS` within the same frame (a poll or a short wait per batch), so each batch's decode latency lands on the frame instead of running beside it. If you have time, a run with the worker at normal priority (remove the `SetThreadPriority` call at the top of `worker_loop`) would separate your two guesses. If it's the waiting, the fix is probably to decode the batch inline when the title is about to collect it, which is close to what main does today.
Author
Owner

@⁠Luxuride on GitHub · 2026-12-04 12:01 UTC · original · edited 2026-12-05 11:24 UTC

As I read it, this duplicates another open PR: this is the same fix as #175, one commit later, with the async path untested by the crate's own tests (crates/victory-hle/src/modules/apu.rs:177 returns early under cfg!(test)) and a second synchronous code path kept alive for the same behaviour.

There is also a third implementation of the same feature open today, #455 (sharded pool, VICTORY_APU_ASYNC + VICTORY_APU_THREADS, with tests that run the async path). Three people are solving one issue, and the maintainer should only have to read one of them.

#175 is the one to look at: one code path, tested concurrency, merges clean. Put your notes there — in particular apu.rs:253 raising the worker to THREAD_PRIORITY_HIGHEST, which stacks on any other thread-priority change in flight.

Whether it still has work of its own is the maintainer's call, not mine.

**@⁠Luxuride** on GitHub · 2026-12-04 12:01 UTC · [original](https://github.com/lobomfz/VIctory-xmem/pull/155#issuecomment-7410868397) · edited 2026-12-05 11:24 UTC As I read it, this duplicates another open PR: this is the same fix as #175, one commit later, with the async path untested by the crate's own tests (`crates/victory-hle/src/modules/apu.rs:177` returns early under `cfg!(test)`) and a second synchronous code path kept alive for the same behaviour. There is also a third implementation of the same feature open today, #455 (sharded pool, `VICTORY_APU_ASYNC` + `VICTORY_APU_THREADS`, with tests that run the async path). Three people are solving one issue, and the maintainer should only have to read one of them. #175 is the one to look at: one code path, tested concurrency, merges clean. Put your notes there — in particular `apu.rs:253` raising the worker to `THREAD_PRIORITY_HIGHEST`, which stacks on any other thread-priority change in flight. Whether it still has work of its own is the maintainer's call, not mine.
github-bot closed this pull request 2026-12-05 17:31:55 +00:00

Pull request closed

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