in_parallel runs on kept threads instead of starting new ones on every call (#163) #253

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

@⁠ySKELLETONX on GitHub · 2026-11-26 13:01 UTC · original
ySKELLETONX:perf/parallel-pool → main · +248 −8 · 3 files · 1 commits · open

in_parallel started up to five threads with std::thread::scope on every call, and the submitting thread calls it several times a submission (snapshots, read-backs, bounce copies). The profile in #163 put 1.2% of that thread in ZwCreateThreadEx.

Five threads are now kept (victory-parallel-0..4, started on first use, in crates/victory-gpu/src/parallel.rs); in_parallel keeps its pieces and its split, only where they run changes.

How a call works

  • It offers its work to the kept threads and does its own share.
  • Once the caller is done the offer is withdrawn, and the caller waits only for the threads that took it. So a call never waits for a thread busy elsewhere (another call at the same time, or a call nested in its own work): no deadlock, and a call no thread helps runs as it would alone.
  • A panic in a kept thread is caught there and raised in the caller, as thread::scope did; the thread goes on.
  • If no thread can be started, calls run on the caller.

Tests (parallel::tests, no game data): every item done once; at most five helper threads over 50 calls; a call nested in a call; 8 threads calling at once; a panic in a kept thread reaches the caller and the pool still works; zero helpers runs on the caller. 20/20 runs green.

  • cargo test -p victory-gpu --lib --locked: 119 passed, 0 failed
  • cargo clippy -p victory-gpu --lib --tests: nothing in the changed files
  • cargo xtask policy --staged: ok

Measured (release, Ryzen 7 5700X3D, RX 7600 XT, Windows 11), start and join per call: std::thread::scope about 280 µs, the kept threads under 1 µs. At several calls a submission that is roughly the 1.2% from #163; I have not measured it in the world.

**@⁠ySKELLETONX** on GitHub · 2026-11-26 13:01 UTC · [original](https://github.com/Belobug/VIctory-Belobug-AMD/pull/253) `ySKELLETONX:perf/parallel-pool` → `main` · +248 −8 · 3 files · 1 commits · open `in_parallel` started up to five threads with `std::thread::scope` on every call, and the submitting thread calls it several times a submission (snapshots, read-backs, bounce copies). The profile in #163 put 1.2% of that thread in `ZwCreateThreadEx`. Five threads are now kept (`victory-parallel-0..4`, started on first use, in `crates/victory-gpu/src/parallel.rs`); `in_parallel` keeps its pieces and its split, only where they run changes. **How a call works** - It offers its work to the kept threads and does its own share. - Once the caller is done the offer is withdrawn, and the caller waits only for the threads that took it. So a call never waits for a thread busy elsewhere (another call at the same time, or a call nested in its own work): no deadlock, and a call no thread helps runs as it would alone. - A panic in a kept thread is caught there and raised in the caller, as `thread::scope` did; the thread goes on. - If no thread can be started, calls run on the caller. **Tests** (`parallel::tests`, no game data): every item done once; at most five helper threads over 50 calls; a call nested in a call; 8 threads calling at once; a panic in a kept thread reaches the caller and the pool still works; zero helpers runs on the caller. 20/20 runs green. - `cargo test -p victory-gpu --lib --locked`: 119 passed, 0 failed - `cargo clippy -p victory-gpu --lib --tests`: nothing in the changed files - `cargo xtask policy --staged`: ok **Measured** (release, Ryzen 7 5700X3D, RX 7600 XT, Windows 11), start and join per call: `std::thread::scope` about 280 µs, the kept threads under 1 µs. At several calls a submission that is roughly the 1.2% from #163; I have not measured it in the world.
std:🧵:scope started up to five threads afresh on each call, and the
submitting thread calls in_parallel several times a submission; a profile in
#163 put 1.2% of that thread in ZwCreateThreadEx. Five threads are now kept
(victory-parallel-0..4, started on first use).

A call offers its work to them and does its own share; the offer is
withdrawn once the caller is done, and the caller waits only for the
threads that took it. So a call never waits for a thread busy elsewhere
(another call, or a call nested in its own work), and a panic in a kept
thread is raised in the caller as before.

Start and join per call, release build, Ryzen 7 5700X3D:
std:🧵:scope about 280 us, the kept threads under 1 us.
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 13:03:15 +00:00
Author
Owner

@⁠adrianmjim on GitHub · 2026-11-26 21:44 UTC · original

Native Windows results for 21ee210 on its base 4f62c2a. Machine: Windows 11 (build 26300), 16 logical CPUs, Radeon RX 7900 XT (driver 32.0.32029.2010), Rust 1.98.1, MSVC target. #178 does the same job with five kept threads. To compare the two in the same session, I merged #178 (6325dc1) onto 4f62c2a locally; the merge is clean. Other jobs held the CPU at 96-100% during all runs. The base cost per call is about twice what I measured in my #178 comment (about 170 µs then).

Tests:

  • victory-gpu --lib debug runs, interleaved: 4 runs each of base, this PR and #178. The six new parallel:: tests passed every time. The only failures were the known watch:: guard-page flakes: 1 run on base and 1 on this PR.
  • Only the parallel:: tests: 50 debug runs and 50 release runs, all passed.
  • Clippy (--lib --tests): no new warnings.

Cost per call. I used the same local test as in my #178 comment: 3000 calls, a sum over the items, release build, medians of 5 alternating runs, in µs per call.

48 items 1024 items 65,536 items
4f62c2a 342 300 325
this PR 0.22 0.44 4.6
#178 1.0 2.0 4.7
4f62c2a, after creating the host device 348 386 357
this PR, after creating the device 0.22 0.44 4.0
#178, after creating the device 0.56 1.9 5.0

I also counted the items done on threads other than the caller (medians of the same runs):

  • This PR: 0% at 48 items, 0% at 1024 and under 1% at 65,536. The caller finished these calls before a kept thread took a piece.
  • #178: 5-10% at 48 items and about 40% at 1024 and at 65,536.

Then I gave each item more work (64 rounds of a multiply and shift mix). Each call had 256 to 16,384 items. No device, medians of 5 runs, µs per call, with the share of items done off the caller:

items (least) one thread this PR #178
256 (16) 24 7.7 (77%) 6.3 (81%)
1024 (64) 93 34.6 (73%) 24.6 (83%)
4096 (256) 378 95.8 (81%) 84.0 (83%)
16,384 (1024) 1515 315 (81%) 309 (83%)
  • Here the kept threads do join on this PR, but they take a smaller share. At 25-400 µs of work this PR was 14-41% slower than #178. At 16,384 items the gap was 2%.
  • After creating the device the pattern was the same.
  • A heavier run (65,536 items, 6-7 ms on one thread) was too noisy to rank. Both PRs ranged from 1.1 to 2.3 ms per call.

Nesting and several callers:

  • A call made inside each of 64 pieces of another call returned every time. Per outer call: this PR 29 µs, #178 50 µs without the device; 49 and 52 µs after creating it.
  • Four threads calling at the same time gave correct sums and no hang. Times were too noisy under this load to rank the two PRs.

Design differences I noticed:

  • Waking: here the kept threads share one mpsc::Receiver behind a Mutex, and the lock is held during recv. So only one thread waits in recv and the others wait for the lock. #178 uses a VecDeque with Condvar::notify_all. I did not check whether this causes the lower share above.
  • Withdrawn offers: here they stay in the channel and are popped and skipped later. #178 drops closed offers before it queues new ones.
  • Panics: this PR re-raises the kept thread's own panic payload. #178 raises "a helper thread panicked".
  • Off switch: #178 has VICTORY_HELPERS=0 to start threads per call as before. This PR has none.
  • Shutdown: neither PR joins its threads. The threads stay idle until the process exits. All test and bench processes exited normally.

Not tested: frame times in the title.

**@⁠adrianmjim** on GitHub · 2026-11-26 21:44 UTC · [original](https://github.com/Belobug/VIctory-Belobug-AMD/pull/253#issuecomment-7204009384) Native Windows results for 21ee210 on its base 4f62c2a. Machine: Windows 11 (build 26300), 16 logical CPUs, Radeon RX 7900 XT (driver 32.0.32029.2010), Rust 1.98.1, MSVC target. #178 does the same job with five kept threads. To compare the two in the same session, I merged #178 (6325dc1) onto 4f62c2a locally; the merge is clean. Other jobs held the CPU at 96-100% during all runs. The base cost per call is about twice what I measured in my #178 comment (about 170 µs then). Tests: - `victory-gpu --lib` debug runs, interleaved: 4 runs each of base, this PR and #178. The six new `parallel::` tests passed every time. The only failures were the known `watch::` guard-page flakes: 1 run on base and 1 on this PR. - Only the `parallel::` tests: 50 debug runs and 50 release runs, all passed. - Clippy (`--lib --tests`): no new warnings. Cost per call. I used the same local test as in my #178 comment: 3000 calls, a sum over the items, release build, medians of 5 alternating runs, in µs per call. | | 48 items | 1024 items | 65,536 items | |---|---|---|---| | 4f62c2a | 342 | 300 | 325 | | this PR | 0.22 | 0.44 | 4.6 | | #178 | 1.0 | 2.0 | 4.7 | | 4f62c2a, after creating the host device | 348 | 386 | 357 | | this PR, after creating the device | 0.22 | 0.44 | 4.0 | | #178, after creating the device | 0.56 | 1.9 | 5.0 | I also counted the items done on threads other than the caller (medians of the same runs): - This PR: 0% at 48 items, 0% at 1024 and under 1% at 65,536. The caller finished these calls before a kept thread took a piece. - #178: 5-10% at 48 items and about 40% at 1024 and at 65,536. Then I gave each item more work (64 rounds of a multiply and shift mix). Each call had 256 to 16,384 items. No device, medians of 5 runs, µs per call, with the share of items done off the caller: | items (least) | one thread | this PR | #178 | |---|---|---|---| | 256 (16) | 24 | 7.7 (77%) | 6.3 (81%) | | 1024 (64) | 93 | 34.6 (73%) | 24.6 (83%) | | 4096 (256) | 378 | 95.8 (81%) | 84.0 (83%) | | 16,384 (1024) | 1515 | 315 (81%) | 309 (83%) | - Here the kept threads do join on this PR, but they take a smaller share. At 25-400 µs of work this PR was 14-41% slower than #178. At 16,384 items the gap was 2%. - After creating the device the pattern was the same. - A heavier run (65,536 items, 6-7 ms on one thread) was too noisy to rank. Both PRs ranged from 1.1 to 2.3 ms per call. Nesting and several callers: - A call made inside each of 64 pieces of another call returned every time. Per outer call: this PR 29 µs, #178 50 µs without the device; 49 and 52 µs after creating it. - Four threads calling at the same time gave correct sums and no hang. Times were too noisy under this load to rank the two PRs. Design differences I noticed: - Waking: here the kept threads share one `mpsc::Receiver` behind a `Mutex`, and the lock is held during `recv`. So only one thread waits in `recv` and the others wait for the lock. #178 uses a `VecDeque` with `Condvar::notify_all`. I did not check whether this causes the lower share above. - Withdrawn offers: here they stay in the channel and are popped and skipped later. #178 drops closed offers before it queues new ones. - Panics: this PR re-raises the kept thread's own panic payload. #178 raises "a helper thread panicked". - Off switch: #178 has `VICTORY_HELPERS=0` to start threads per call as before. This PR has none. - Shutdown: neither PR joins its threads. The threads stay idle until the process exits. All test and bench processes exited normally. Not tested: frame times in the title.
This pull request can be merged automatically.
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/253/head:github-bot/gh-253'
git switch 'github-bot/gh-253'

Merge

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