Guards: a waiter tries the guards' lock again a while before it sleeps (VICTORY_GUARD_SPIN=<us>, off by default) #3523

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

@⁠lexical-scope on GitHub · 2026-11-30 00:26 UTC · original
lexical-scope:pr-guard-spin → main · +40 −1 · 1 files · 1 commits · open

What

guards() takes the guards' lock with try_lock, and a thread that finds it held sleeps on lock() at once. With VICTORY_GUARD_SPIN=<microseconds> (at most 1000) it first tries again for that long, then sleeps as before.

Unset or 0: unchanged.

Why

The guards' lock is held about 11 µs at a time, some 1,160 times a frame. A waiter that sleeps is woken through the scheduler, and threads that arrive meanwhile take the lock ahead of it, so a sleeping wait costs far more than a hold.

With 200 µs, in a diagnostics build (our own lock-timing lines, not part of this PR), 474 waits a frame had the lock by trying again and 20 slept for it after all.

Measured

RTX 3080 Ti, 24 logical processors, Windows 11, Series S, -Dlss off, release build, same settings except the switch. Medians of the 5-second frame lines, split by draws per frame. Each cell is frame / preparing / waiting on the GPU, in ms:

Draws per frame VICTORY_GUARD_SPIN=200 unset
1,500–4,000 46.4 / 38.0 / 1.5 53.0 / 43.5 / 2.7
4,000–6,000 53.8 / 44.0 / 2.8 71.6 / 53.0 / 5.9
6,000–8,000 65.0 / 53.2 / 5.2 85.1 / 63.6 / 10.0
fps (median) 18.5 14.8
lines under 8 fps 1% 12%
gameplay 8.0 min 5.6 min

Preparing (the submitting thread) was 5–10 ms shorter with the switch at every scene weight. Two more runs with it on, in other sessions, prepared in about the same time as the left column (37–40 / 48–49 / 54–56 ms).

The frame times vary more between sessions than this switch does. In one of those other runs with the switch on, frames took about as long as in the right column, while preparing still took as little time as on the left. So the fps lines above are one pair of runs, and the preparing times are the result.

Tests

  • cargo clippy -p victory-gpu --all-targets --locked, with and without --all-features: no warnings in the changed lines.
  • cargo test -p victory-gpu --lib --locked: 921 passed; one fails, as it does on main already (shader_cache::tests::every_switch_of_the_translator_is_in_the_key: VICTORY_SCALAR_SPLITS is not read by the translator any more).
  • With --features diagnostics: 993 passed, the same failure, and compose::tests::bgrx_plane_selects_a_typed_view (diagnostics only, compose.rs, not touched here).
  • Played with it on and off on main with our other branch commits, release and diagnostics builds.

🤖 Generated with Claude Code

**@⁠lexical-scope** on GitHub · 2026-11-30 00:26 UTC · [original](https://github.com/trustno1-coder/VIctory-NVIDIA/pull/3523) `lexical-scope:pr-guard-spin` → `main` · +40 −1 · 1 files · 1 commits · open ### What `guards()` takes the guards' lock with `try_lock`, and a thread that finds it held sleeps on `lock()` at once. With `VICTORY_GUARD_SPIN=<microseconds>` (at most 1000) it first tries again for that long, then sleeps as before. Unset or `0`: unchanged. ### Why The guards' lock is held about 11 µs at a time, some 1,160 times a frame. A waiter that sleeps is woken through the scheduler, and threads that arrive meanwhile take the lock ahead of it, so a sleeping wait costs far more than a hold. With 200 µs, in a diagnostics build (our own lock-timing lines, not part of this PR), 474 waits a frame had the lock by trying again and 20 slept for it after all. ### Measured RTX 3080 Ti, 24 logical processors, Windows 11, Series S, `-Dlss off`, release build, same settings except the switch. Medians of the 5-second frame lines, split by draws per frame. Each cell is frame / preparing / waiting on the GPU, in ms: | Draws per frame | `VICTORY_GUARD_SPIN=200` | unset | |---|---|---| | 1,500–4,000 | 46.4 / **38.0** / 1.5 | 53.0 / 43.5 / 2.7 | | 4,000–6,000 | 53.8 / **44.0** / 2.8 | 71.6 / 53.0 / 5.9 | | 6,000–8,000 | 65.0 / **53.2** / 5.2 | 85.1 / 63.6 / 10.0 | | fps (median) | 18.5 | 14.8 | | lines under 8 fps | 1% | 12% | | gameplay | 8.0 min | 5.6 min | Preparing (the submitting thread) was 5–10 ms shorter with the switch at every scene weight. Two more runs with it on, in other sessions, prepared in about the same time as the left column (37–40 / 48–49 / 54–56 ms). The frame times vary more between sessions than this switch does. In one of those other runs with the switch on, frames took about as long as in the right column, while preparing still took as little time as on the left. So the fps lines above are one pair of runs, and the preparing times are the result. ### Tests - `cargo clippy -p victory-gpu --all-targets --locked`, with and without `--all-features`: no warnings in the changed lines. - `cargo test -p victory-gpu --lib --locked`: 921 passed; one fails, as it does on `main` already (`shader_cache::tests::every_switch_of_the_translator_is_in_the_key`: `VICTORY_SCALAR_SPLITS is not read by the translator any more`). - With `--features diagnostics`: 993 passed, the same failure, and `compose::tests::bgrx_plane_selects_a_typed_view` (diagnostics only, `compose.rs`, not touched here). - Played with it on and off on `main` with our other branch commits, release and diagnostics builds. 🤖 Generated with [Claude Code](https://claude.com/claude-code)
The guards' lock is held some 11 us at a time, more than a thousand times
a frame, by refreshes (RTX 3080 Ti, 24 logical processors, diagnostics).
A waiter that sleeps is woken through the scheduler, and threads that come
meanwhile take the lock ahead of it. With the switch a waiter tries again
for up to the given microseconds (at most 1000) before it sleeps. Off by
default.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-30 00:28:48 +00:00
Author
Owner

@⁠DoubleD17 on GitHub · 2026-11-30 03:37 UTC · original · edited 2026-11-30 09:56 UTC

We ran a test of this PR with VICTORY_GUARD_SPIN=200 on a CPU-bound machine (about 66 ms of CPU preparation per frame).

CPU prep (ms/frame) controls at that point fps (mean) controls at that point
round 1 72.3 66.8 11.86 12.14
round 2 63.1 62.0 13.80 13.19
mean difference +3.3 ms +1.1 %

No gain we can measure here: the controls alone spread over 9 ms, so a difference under about 5 ms is inside the noise. The picture was clean (0 defects in 160 frames).

One difference from your setup: 16 logical processors here against your 24, so fewer threads queue on the guards' lock at once, and the spin may have less to win.

Setup:

  • Ryzen 7 5800X (8 cores, 16 threads), RTX 5070 12 GB (DXGI budget 11,175 MB), 64 GB RAM, Windows 11;
  • Series X, 1440p with DLAA (DLSS at native resolution; corrected, this line said DLSS Performance at first), frame generation and ray tracing off;
  • the PR merged locally onto main 41adb551 (only its own commits on top), against 41adb551 itself;
  • a warm-up run per build, then 150 s runs in two rounds (the second in reverse order), with four control runs of 41adb551 spread through the session;
  • every run starts from the same save, restored before each run;
  • figures are medians of the 5 s host lines. The four controls measured 60.4–69.4 ms of CPU preparation per frame and drifted during the session, so each run is compared with the controls interpolated to its own start.

Picture check: a burst of 10 captures a second at the end of each run (80 frames), looked at frame by frame.

AI Assisted

**@⁠DoubleD17** on GitHub · 2026-11-30 03:37 UTC · [original](https://github.com/trustno1-coder/VIctory-NVIDIA/pull/3523#issuecomment-7268046607) · edited 2026-11-30 09:56 UTC We ran a test of this PR with `VICTORY_GUARD_SPIN=200` on a CPU-bound machine (about 66 ms of CPU preparation per frame). | | CPU prep (ms/frame) | controls at that point | fps (mean) | controls at that point | |---|---|---|---|---| | round 1 | 72.3 | 66.8 | 11.86 | 12.14 | | round 2 | 63.1 | 62.0 | 13.80 | 13.19 | | **mean difference** | **+3.3 ms** | | **+1.1 %** | | No gain we can measure here: the controls alone spread over 9 ms, so a difference under about 5 ms is inside the noise. The picture was clean (0 defects in 160 frames). One difference from your setup: 16 logical processors here against your 24, so fewer threads queue on the guards' lock at once, and the spin may have less to win. **Setup:** - Ryzen 7 5800X (8 cores, 16 threads), RTX 5070 12 GB (DXGI budget 11,175 MB), 64 GB RAM, Windows 11; - Series X, 1440p with DLAA (DLSS at native resolution; corrected, this line said DLSS Performance at first), frame generation and ray tracing off; - the PR merged locally onto main 41adb551 (only its own commits on top), against 41adb551 itself; - a warm-up run per build, then 150 s runs in two rounds (the second in reverse order), with four control runs of 41adb551 spread through the session; - every run starts from the same save, restored before each run; - figures are medians of the 5 s host lines. The four controls measured 60.4–69.4 ms of CPU preparation per frame and drifted during the session, so each run is compared with the controls interpolated to its own start. Picture check: a burst of 10 captures a second at the end of each run (80 frames), looked at frame by frame. AI Assisted
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/3523/head:github-bot/gh-3523'
git switch 'github-bot/gh-3523'

Merge

Merge the changes and update on Forgejo.
git switch 'main'
git merge --no-ff 'github-bot/gh-3523'
git switch 'github-bot/gh-3523'
git rebase 'main'
git switch 'main'
git merge --ff-only 'github-bot/gh-3523'
git switch 'github-bot/gh-3523'
git rebase 'main'
git switch 'main'
git merge --no-ff 'github-bot/gh-3523'
git switch 'main'
git merge --squash 'github-bot/gh-3523'
git switch 'main'
git merge --ff-only 'github-bot/gh-3523'
git switch 'main'
git merge 'github-bot/gh-3523'
git push origin 'main'
This discussion has been locked. Commenting is limited to contributors.
No reviewers
No milestone
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set

Reference
github/trustno1-coder--VIctory-NVIDIA!3523
No description provided.