in_parallel shares its work with kept threads instead of making threads at every call #178

Open
github-bot wants to merge 2 commits from github-bot/gh-178 into main AGit
Owner

@⁠SPFCChris on GitHub · 2026-11-25 19:32 UTC · original
SPFCChris:perf/kept-helper-threads → main · +189 −4 · 1 files · 2 commits · open

## Problem

host::in_parallel makes up to five threads with std::thread::scope at every call, and a submission calls it some tens of times: Shadow::copy_back, the snapshots in run_deferred, the bounces and reads of prepare_bindless. With 35 to 40 submissions a world frame that is hundreds of threads made and joined a frame, each announced to every loaded library.

Sampled with VICTORY_PROFILE in the world on a Radeon 8060S (20 s, 9,415 samples of the submitting thread): 9.4 % of its samples were in ZwCreateThreadEx, all under in_parallel (copy_back:557, execute_inner:1308 and :1315 at a59f3e5).

Change

Five threads are kept (victory-helper) and offered each call's work. The caller does its own share as before; when that returns nothing is left to take, so it closes the job and waits only for threads that are already inside it. A thread that comes late finds the job closed and never touches the caller's closure. That is also why a call from inside a piece cannot stand: nobody waits for a thread that has not started.

VICTORY_HELPERS=0 makes threads at each call as before, for comparison.

One file, crates/victory-gpu/src/host.rs; no new dependency.

Measured

Radeon 8060S (Ryzen AI Max+ 395), diag build, Series X profile, the opening cutscene, 220 s in the world per run, every frame's time logged. Same binary, VICTORY_HELPERS=0 against the default, runs alternated. Median frame time of the frames in a band of draws per frame, so that the runs are compared like for like:

3,000-4,000 draws 4,000-6,000 draws all frames
threads made per call, run 1 263 ms 318 ms 265 ms
threads made per call, run 2 231 ms 271 ms 232 ms
kept threads, run 1 217 ms 234 ms 190 ms
kept threads, run 2 207 ms 240 ms 196 ms

Two further runs with kept threads, taken for another comparison, gave 200 / 238 and 216 / 244 ms in the two bands. In a profile taken afterwards ZwCreateThreadEx is gone from the submitting thread's samples.

Run 2 of "threads made per call" ended early: the title's own crash handler came up after 113 s in the world (445 frames; a first-chance access violation in ntdll, cause not looked into), so its figures cover a shorter window.

These runs were on a59f3e5 plus #148, which this GPU needs to draw the world at all. This branch is on 000ffdd; the change is the same lines.

Tested

  • cargo test -p victory-gpu --lib --locked: 116 passed, 0 failed, 4 ignored. Three of them are new: every piece done once over 500 calls on the same threads, a call from inside a piece returns, a panic in a piece reaches the caller and the threads go on.
  • cargo clippy -p victory-gpu --all-targets --locked: no warning in the new code.
  • In the title: six runs of about four minutes in the world with kept threads, no device removal, frames as before (interiors, street, car shots).

Not tested

  • Any other GPU or CPU. The cost of making a thread depends on the libraries loaded and the number of cores (32 logical here), so the gain will differ.
  • A build without diagnostics.

AI-assisted (Claude Code); no game data.

🤖 Generated with Claude Code

**@⁠SPFCChris** on GitHub · 2026-11-25 19:32 UTC · [original](https://github.com/Belobug/VIctory-Belobug-AMD/pull/178) `SPFCChris:perf/kept-helper-threads` → `main` · +189 −4 · 1 files · 2 commits · open ## Problem `host::in_parallel` makes up to five threads with `std::thread::scope` at every call, and a submission calls it some tens of times: `Shadow::copy_back`, the snapshots in `run_deferred`, the bounces and reads of `prepare_bindless`. With 35 to 40 submissions a world frame that is hundreds of threads made and joined a frame, each announced to every loaded library. Sampled with `VICTORY_PROFILE` in the world on a Radeon 8060S (20 s, 9,415 samples of the submitting thread): **9.4 % of its samples were in `ZwCreateThreadEx`**, all under `in_parallel` (`copy_back:557`, `execute_inner:1308` and `:1315` at `a59f3e5`). ## Change Five threads are kept (`victory-helper`) and offered each call's work. The caller does its own share as before; when that returns nothing is left to take, so it closes the job and waits only for threads that are already inside it. A thread that comes late finds the job closed and never touches the caller's closure. That is also why a call from inside a piece cannot stand: nobody waits for a thread that has not started. `VICTORY_HELPERS=0` makes threads at each call as before, for comparison. One file, `crates/victory-gpu/src/host.rs`; no new dependency. ## Measured Radeon 8060S (Ryzen AI Max+ 395), diag build, Series X profile, the opening cutscene, 220 s in the world per run, every frame's time logged. Same binary, `VICTORY_HELPERS=0` against the default, runs alternated. Median frame time of the frames in a band of draws per frame, so that the runs are compared like for like: | | 3,000-4,000 draws | 4,000-6,000 draws | all frames | |---|---|---|---| | threads made per call, run 1 | 263 ms | 318 ms | 265 ms | | threads made per call, run 2 | 231 ms | 271 ms | 232 ms | | kept threads, run 1 | 217 ms | 234 ms | 190 ms | | kept threads, run 2 | 207 ms | 240 ms | 196 ms | Two further runs with kept threads, taken for another comparison, gave 200 / 238 and 216 / 244 ms in the two bands. In a profile taken afterwards `ZwCreateThreadEx` is gone from the submitting thread's samples. Run 2 of "threads made per call" ended early: the title's own crash handler came up after 113 s in the world (445 frames; a first-chance access violation in `ntdll`, cause not looked into), so its figures cover a shorter window. These runs were on `a59f3e5` plus #148, which this GPU needs to draw the world at all. This branch is on `000ffdd`; the change is the same lines. ## Tested - `cargo test -p victory-gpu --lib --locked`: 116 passed, 0 failed, 4 ignored. Three of them are new: every piece done once over 500 calls on the same threads, a call from inside a piece returns, a panic in a piece reaches the caller and the threads go on. - `cargo clippy -p victory-gpu --all-targets --locked`: no warning in the new code. - In the title: six runs of about four minutes in the world with kept threads, no device removal, frames as before (interiors, street, car shots). ## Not tested - Any other GPU or CPU. The cost of making a thread depends on the libraries loaded and the number of cores (32 logical here), so the gain will differ. - A build without `diagnostics`. AI-assisted (Claude Code); no game data. 🤖 Generated with [Claude Code](https://claude.com/claude-code)
in_parallel made up to five threads with std:🧵:scope at each call,
and a submission calls it some tens of times (copy-back, the deferred
steps' snapshots, the bindless plan's bounces and reads). Sampled with
VICTORY_PROFILE in the world on a Radeon 8060S, 9.4 % of the submitting
thread's samples were in ZwCreateThreadEx under it.

Five threads are kept and offered each call's work. The caller takes its
share itself and waits only for threads already in the work, so a call
from inside a piece cannot stand. VICTORY_HELPERS=0 makes threads at each
call as before.

Co-Authored-By: Claude Fable 5.2 <[email protected]>
A short call returns before every kept thread has looked at what it
offered; those entries stayed at the front of the queue, to be taken, found
closed and put down ahead of the next call's work. They are dropped under
the queue's lock when the next call adds its own (KernelSmoker's review of
this pull request).

Co-Authored-By: Claude Fable 5.2 <[email protected]>
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 03:37:20 +00:00
Author
Owner

@⁠KernelSmoker on GitHub · 2026-11-25 19:38 UTC · original

Independent verification on PR head 6f563b1 (Bazzite Linux 45 x86_64, Rust 1.98.1, Intel Core i9-11900K, GE-Proton 10-34 / Wine 10.0):

  • Non-diagnostics build (noted as untested in the PR): cargo check --workspace --all-targets --locked --offline --target x86_64-pc-windows-gnu passed cleanly (0 warnings in host.rs).
  • Clippy (--all-features): cargo clippy -p victory-gpu --all-targets --all-features --locked --offline --target x86_64-pc-windows-gnu passed with 0 new warnings in host::helpers.
  • Unit tests under Wine:
    • host::helpers::tests (default kept threads): 3 passed, 0 failed across 10/10 consecutive runs (~0.01s per run).
    • VICTORY_HELPERS=0 (host::helpers::tests, thread-per-call): 3 passed, 0 failed (0.54s for the same 3 tests).

Code review notes on mod helpers:

  • The SeqCst Dekker handoff between running.fetch_add(1) / closed.load() (workers) and closed.store(true) / running.load() (caller Close::drop), plus the Arc<Job> lifetime keeping idle alive until notify_all() finishes and Close running on unwind, all look sound.
  • Minor queue hygiene note: JOBS is a FIFO VecDeque and Close::drop does not prune closed entries. When calls finish quickly or nest while all 5 helpers are busy (e.g. a_call_from_inside_a_piece_returns), already-closed Arc<Job> entries pile up at the front of JOBS and helpers have to pop/check/notify them before reaching a live job pushed at the back. Calling jobs.retain(|j| !j.closed.load(Ordering::Relaxed)); under JOBS.lock() right before jobs.extend(...) in helpers::run (or pruning in Close::drop) would keep JOBS bounded and avoid waking helpers into stale entries.

(No live title benchmark run performed here; this covers the non-diagnostics build, clippy, concurrency review, and Wine unit tests on Intel i9-11900K.)

**@⁠KernelSmoker** on GitHub · 2026-11-25 19:38 UTC · [original](https://github.com/Belobug/VIctory-Belobug-AMD/pull/178#issuecomment-7172581492) Independent verification on PR head `6f563b1` (Bazzite Linux 45 x86_64, Rust 1.98.1, Intel Core i9-11900K, GE-Proton 10-34 / Wine 10.0): - **Non-`diagnostics` build (noted as untested in the PR):** `cargo check --workspace --all-targets --locked --offline --target x86_64-pc-windows-gnu` passed cleanly (0 warnings in `host.rs`). - **Clippy (`--all-features`):** `cargo clippy -p victory-gpu --all-targets --all-features --locked --offline --target x86_64-pc-windows-gnu` passed with **0 new warnings** in `host::helpers`. - **Unit tests under Wine:** - `host::helpers::tests` (default kept threads): **3 passed, 0 failed** across **10/10** consecutive runs (~**0.01s** per run). - `VICTORY_HELPERS=0` (`host::helpers::tests`, thread-per-call): **3 passed, 0 failed** (**0.54s** for the same 3 tests). Code review notes on `mod helpers`: - The `SeqCst` Dekker handoff between `running.fetch_add(1)` / `closed.load()` (workers) and `closed.store(true)` / `running.load()` (caller `Close::drop`), plus the `Arc<Job>` lifetime keeping `idle` alive until `notify_all()` finishes and `Close` running on unwind, all look sound. - Minor queue hygiene note: `JOBS` is a FIFO `VecDeque` and `Close::drop` does not prune closed entries. When calls finish quickly or nest while all 5 helpers are busy (e.g. `a_call_from_inside_a_piece_returns`), already-closed `Arc<Job>` entries pile up at the front of `JOBS` and helpers have to pop/check/notify them before reaching a live job pushed at the back. Calling `jobs.retain(|j| !j.closed.load(Ordering::Relaxed));` under `JOBS.lock()` right before `jobs.extend(...)` in `helpers::run` (or pruning in `Close::drop`) would keep `JOBS` bounded and avoid waking helpers into stale entries. (No live title benchmark run performed here; this covers the non-`diagnostics` build, clippy, concurrency review, and Wine unit tests on Intel i9-11900K.)
Author
Owner

@⁠MamaLeVrai on GitHub · 2026-11-26 05:11 UTC · original

Concurrency check on Windows/RDNA2 (RX 6950 XT, Windows 11 build 26300, Rust 1.98.1). I merged this PR (6325dc1) onto main 4f62c2a plus #210, which fixes list::tests::lists_are_cut_at_waits_that_follow_work, a test that fails in every release run on main. Then I ran the release victory-gpu lib test binary 10 times, with the default thread count:

  • This PR: 9 runs passed (116 passed, 4 ignored); 1 run died without a result line, as on the base.
  • The same base without the PR: 1 run failed on the known flake watch::tests::written_pages_are_reported_to_every_asker (#102), and 1 run died without a result line.

No new failure, deadlock or hang showed up in these runs. Ten runs are a small sample for races. Not run in the game.

harness machine: rx6950xt-w11 | AMD Radeon RX 6950 XT (RDNA2), driver 32.0.21045.29003 | 32 GB RAM | Windows 11 build 26300

AI-assisted (Claude Code); no game data.

**@⁠MamaLeVrai** on GitHub · 2026-11-26 05:11 UTC · [original](https://github.com/Belobug/VIctory-Belobug-AMD/pull/178#issuecomment-7183256440) Concurrency check on Windows/RDNA2 (RX 6950 XT, Windows 11 build 26300, Rust 1.98.1). I merged this PR (`6325dc1`) onto `main` `4f62c2a` plus #210, which fixes `list::tests::lists_are_cut_at_waits_that_follow_work`, a test that fails in every release run on `main`. Then I ran the release `victory-gpu` lib test binary 10 times, with the default thread count: - This PR: 9 runs passed (116 passed, 4 ignored); 1 run died without a result line, as on the base. - The same base without the PR: 1 run failed on the known flake `watch::tests::written_pages_are_reported_to_every_asker` (#102), and 1 run died without a result line. No new failure, deadlock or hang showed up in these runs. Ten runs are a small sample for races. Not run in the game. harness machine: rx6950xt-w11 | AMD Radeon RX 6950 XT (RDNA2), driver 32.0.21045.29003 | 32 GB RAM | Windows 11 build 26300 AI-assisted (Claude Code); no game data.
Author
Owner

@⁠adrianmjim on GitHub · 2026-11-26 19:40 UTC · original

Native Windows results for 6325dc1, which includes the queue pruning from the review. Base 000ffdd. Machine: Windows 11 (build 26300), 16 logical CPUs, Radeon RX 7900 XT (driver 32.0.32029.2010), Rust 1.98.1, MSVC target. Other jobs were running on the machine at the same time.

Unit tests (victory-gpu --lib, debug build), alternating between variants:

  • PR head and 000ffdd: 106 runs each. PR merged onto current main 4f62c2a: 105 runs (the merge is clean). Plain 4f62c2a: 55 runs. PR head with VICTORY_HELPERS=0: 55 runs.
  • The three new host::helpers tests passed in every run.
  • All other failures were in tests that also fail without this PR on this machine. Runs with at least one failure in each group:
000ffdd PR PR on 4f62c2a 4f62c2a PR, VICTORY_HELPERS=0
runs 106 106 105 55 55
watch:: guard-page tests 25 18 28 6 14
frame::tests::waits_end_on_ticks_and_count_frames 1 6 3 0 1
pipeline::library::tests::concurrent_atomic_writes_publish_one_complete_file 5 4 2 1 0
queue:: (present_runs_after_a_queues_wait, once queue_signals_complete_at_once) 0 1 0 3 0
query:: (read back the 0xEEEE... stand-in value) 0 4 1 0 0
process abort, 0xC0000409 1 3 0 0 0
  • The query:: failures and the abort are not specific to this PR. I ran only the query:: tests 60 times on each side. On 000ffdd, 2 of 60 runs failed (one of them aborted). On the PR head, 1 of 60 aborted. The abort is in std::process::abort. I did not find its caller.

Cost per call, native (release build). I used a local test that is not part of the PR. It times 3000 in_parallel calls after 200 warm-up calls, with a sum over 48 items (least 8), 1024 items (least 8) and 65,536 items (least 16). Figures are medians of 5 alternating runs, in µs per call:

48 items 1024 items 65,536 items
000ffdd 171 169 169
this PR, VICTORY_HELPERS=0 172 168 168
this PR 1.2 1.7 3.2
000ffdd, after creating the host D3D12 device 200 199 206
this PR, VICTORY_HELPERS=0, after creating the device 205 202 202
this PR, after creating the device 1.2 1.7 3.1
  • On native Windows, a call that makes its five threads takes about 170 µs.
  • After the host D3D12 device is created, which loads d3d12 and the AMD driver DLLs, that goes up by about 30 µs. This is in line with the PR's point that each new thread is announced to every loaded library. I did not check the cause.
  • VICTORY_HELPERS=0 gives about the same numbers as 000ffdd.

Not tested:

  • Frame times in the title.
  • More than one thread calling in_parallel at the same time.
**@⁠adrianmjim** on GitHub · 2026-11-26 19:40 UTC · [original](https://github.com/Belobug/VIctory-Belobug-AMD/pull/178#issuecomment-7201841106) Native Windows results for 6325dc1, which includes the queue pruning from the review. Base 000ffdd. Machine: Windows 11 (build 26300), 16 logical CPUs, Radeon RX 7900 XT (driver 32.0.32029.2010), Rust 1.98.1, MSVC target. Other jobs were running on the machine at the same time. Unit tests (`victory-gpu --lib`, debug build), alternating between variants: - PR head and 000ffdd: 106 runs each. PR merged onto current main 4f62c2a: 105 runs (the merge is clean). Plain 4f62c2a: 55 runs. PR head with `VICTORY_HELPERS=0`: 55 runs. - The three new `host::helpers` tests passed in every run. - All other failures were in tests that also fail without this PR on this machine. Runs with at least one failure in each group: | | 000ffdd | PR | PR on 4f62c2a | 4f62c2a | PR, `VICTORY_HELPERS=0` | |---|---|---|---|---|---| | runs | 106 | 106 | 105 | 55 | 55 | | `watch::` guard-page tests | 25 | 18 | 28 | 6 | 14 | | `frame::tests::waits_end_on_ticks_and_count_frames` | 1 | 6 | 3 | 0 | 1 | | `pipeline::library::tests::concurrent_atomic_writes_publish_one_complete_file` | 5 | 4 | 2 | 1 | 0 | | `queue::` (`present_runs_after_a_queues_wait`, once `queue_signals_complete_at_once`) | 0 | 1 | 0 | 3 | 0 | | `query::` (read back the 0xEEEE... stand-in value) | 0 | 4 | 1 | 0 | 0 | | process abort, 0xC0000409 | 1 | 3 | 0 | 0 | 0 | - The `query::` failures and the abort are not specific to this PR. I ran only the `query::` tests 60 times on each side. On 000ffdd, 2 of 60 runs failed (one of them aborted). On the PR head, 1 of 60 aborted. The abort is in `std::process::abort`. I did not find its caller. Cost per call, native (release build). I used a local test that is not part of the PR. It times 3000 `in_parallel` calls after 200 warm-up calls, with a sum over 48 items (least 8), 1024 items (least 8) and 65,536 items (least 16). Figures are medians of 5 alternating runs, in µs per call: | | 48 items | 1024 items | 65,536 items | |---|---|---|---| | 000ffdd | 171 | 169 | 169 | | this PR, `VICTORY_HELPERS=0` | 172 | 168 | 168 | | this PR | 1.2 | 1.7 | 3.2 | | 000ffdd, after creating the host D3D12 device | 200 | 199 | 206 | | this PR, `VICTORY_HELPERS=0`, after creating the device | 205 | 202 | 202 | | this PR, after creating the device | 1.2 | 1.7 | 3.1 | - On native Windows, a call that makes its five threads takes about 170 µs. - After the host D3D12 device is created, which loads d3d12 and the AMD driver DLLs, that goes up by about 30 µs. This is in line with the PR's point that each new thread is announced to every loaded library. I did not check the cause. - `VICTORY_HELPERS=0` gives about the same numbers as 000ffdd. Not tested: - Frame times in the title. - More than one thread calling `in_parallel` at the same time.
This pull request can be merged automatically.
This branch is out-of-date with the base branch
You are not authorized to merge this pull request.
View command line instructions

Checkout

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

Merge

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