Cut write-watch guard CPU ~3x and lock contention up to 6x in buffer refreshes #4247

Closed
vilaris wants to merge 12 commits from upstream-pr/4247 into main
vilaris commented 2026-11-30 17:45:03 +00:00 (Migrated from github.com)

Imported from trustno1-coder/VIctory-NVIDIA. Originally posted by vilaris on 2026-11-30T17:45:03Z.


Why

During gameplay, buffer refreshes keep the submitting thread busy for ~18 ms a frame of wall time. Splitting watch::writes by the way it answers showed where the time goes. The kernel's own write-watch askings (GetWriteWatch) were cheap: about 45 calls and 0.2-0.5 ms a frame. Nearly all of the cost was in watch::guard. Each refresh span over view memory that only the guards can answer called guard itself from its wide_each_scoped worker, and every one of those calls queued on the guards' single global lock.

Change

  • watch::guard_first (VICTORY_GUARD_FIRST, on by default, 0 turns it off). Before the flush spreads its updates over threads, the spans that only guarding can answer are guarded once, on the flushing thread, with one call per run of touching spans. Each span's own writes then finds its blocks published and answers from the bits, with no guard call and no lock queue.
  • Coherence is unchanged. Only touching spans are joined, so exactly the same blocks are guarded as before. A block the title writes between the pre-pass and its span's update faults, stops being published, and that span guards it itself, as before.

Before and after

Same release build and launcher on an RTX 4070 (12 GB, driver 621.1), same gameplay route, with the same local per-branch counter in both runs (not part of this PR) and VICTORY_REFRESH_WHY off. Numbers are per frame, from the one-second report lines of the steady gameplay window. Gameplay input was manual.

Metric (per frame) Before, per-span guards (session 28480) After, VICTORY_GUARD_FIRST (session 5972) Change
guard CPU, summed over threads 34-81 ms 9-31 ms ~3x less
Waiting for the guards' lock 15-41 ms 2.3-13 ms ~4-6x less
Covering other views 3.2-6.6 ms 2.2-3.8 ms lower
VirtualProtect 0.4-0.9 ms 0.5-0.9 ms same
Refresh wall time on the submitting thread ~18-25 ms (steady) ~18-25 ms (steady) unchanged
Frame median 50-62 ms 52-65 ms unchanged

What this shows:

  • Worker CPU spent in write-watch guarding drops about 3x, and lock contention between refresh workers mostly goes away. That frees cores, which other work on those threads can use.
  • This PR does not improve frame time on its own. The lock queue was not what bounded the refresh's wall time. After the change, the refresh is bound by volume (~52 MB copied and ~1,700 spans updated a frame) and by weak parallelism (37.8 ms of updates summed for 18.3 ms of wall time). The next steps target that.

Selected report lines, transcribed from the local logs:

Before  2026-11-30 17:11:20 UTC  session 28480
  guard: 141 calls, 37.82 ms; waiting for the guards' lock: 297 calls, 16.71 ms
  protecting (VirtualProtect): 22 calls, 0.52 ms; covering other views: 263 calls, 3.24 ms

After   2026-11-30 17:16:07 UTC  session 5972
  published: 876 calls, 2.43 ms
  guard: 146 calls, 13.43 ms; waiting for the guards' lock: 310 calls, 4.37 ms
  protecting (VirtualProtect): 29 calls, 0.47 ms; covering other views: 285 calls, 2.56 ms

In the "after" run, the guard call count includes the pre-pass's own calls. Its time is what dropped.

Validation

  • cargo test -p victory-gpu --lib --locked: same failures as current main (list::thin::tests::bindings_record_no_work, shader_cache::tests::every_switch_of_the_translator_is_in_the_key); watch:: tests are flaky on main too (3/10 runs).
  • cargo clippy -p victory-gpu --all-targets -- -D warnings stops on three existing errors in victory-shader (emit/lower/analysis.rs), also present on main.
  • cargo xtask policy --staged
  • git diff --check

🤖 Generated with Claude Code

<!-- greyfinch-sync:trustno1-coder/VIctory-NVIDIA:issue:5570603155 --> Imported from [trustno1-coder/VIctory-NVIDIA](https://github.com/trustno1-coder/VIctory-NVIDIA/pull/4247). Originally posted by [vilaris](https://github.com/vilaris) on 2026-11-30T17:45:03Z. --- ## Why During gameplay, buffer refreshes keep the submitting thread busy for ~18 ms a frame of wall time. Splitting `watch::writes` by the way it answers showed where the time goes. The kernel's own write-watch askings (`GetWriteWatch`) were cheap: about 45 calls and 0.2-0.5 ms a frame. Nearly all of the cost was in `watch::guard`. Each refresh span over view memory that only the guards can answer called `guard` itself from its `wide_each_scoped` worker, and every one of those calls queued on the guards' single global lock. ## Change - **`watch::guard_first`** (`VICTORY_GUARD_FIRST`, **on by default**, `0` turns it off). Before the flush spreads its updates over threads, the spans that only guarding can answer are guarded once, on the flushing thread, with one call per run of touching spans. Each span's own `writes` then finds its blocks published and answers from the bits, with no `guard` call and no lock queue. - **Coherence is unchanged.** Only touching spans are joined, so exactly the same blocks are guarded as before. A block the title writes between the pre-pass and its span's update faults, stops being published, and that span guards it itself, as before. ## Before and after Same release build and launcher on an RTX 4070 (12 GB, driver 621.1), same gameplay route, with the same local per-branch counter in both runs (not part of this PR) and `VICTORY_REFRESH_WHY` off. Numbers are per frame, from the one-second report lines of the steady gameplay window. Gameplay input was manual. | Metric (per frame) | Before, per-span guards (session 28480) | After, `VICTORY_GUARD_FIRST` (session 5972) | Change | | --- | ---: | ---: | ---: | | `guard` CPU, summed over threads | 34-81 ms | 9-31 ms | **~3x less** | | Waiting for the guards' lock | 15-41 ms | 2.3-13 ms | **~4-6x less** | | Covering other views | 3.2-6.6 ms | 2.2-3.8 ms | lower | | `VirtualProtect` | 0.4-0.9 ms | 0.5-0.9 ms | same | | Refresh wall time on the submitting thread | ~18-25 ms (steady) | ~18-25 ms (steady) | unchanged | | Frame median | 50-62 ms | 52-65 ms | unchanged | What this shows: - Worker CPU spent in write-watch guarding drops about 3x, and lock contention between refresh workers mostly goes away. That frees cores, which other work on those threads can use. - **This PR does not improve frame time on its own.** The lock queue was not what bounded the refresh's wall time. After the change, the refresh is bound by volume (~52 MB copied and ~1,700 spans updated a frame) and by weak parallelism (37.8 ms of updates summed for 18.3 ms of wall time). The next steps target that. Selected report lines, transcribed from the local logs: ~~~ Before 2026-11-30 17:11:20 UTC session 28480 guard: 141 calls, 37.82 ms; waiting for the guards' lock: 297 calls, 16.71 ms protecting (VirtualProtect): 22 calls, 0.52 ms; covering other views: 263 calls, 3.24 ms After 2026-11-30 17:16:07 UTC session 5972 published: 876 calls, 2.43 ms guard: 146 calls, 13.43 ms; waiting for the guards' lock: 310 calls, 4.37 ms protecting (VirtualProtect): 29 calls, 0.47 ms; covering other views: 285 calls, 2.56 ms ~~~ In the "after" run, the `guard` call count includes the pre-pass's own calls. Its time is what dropped. ## Validation - `cargo test -p victory-gpu --lib --locked`: same failures as current `main` (`list::thin::tests::bindings_record_no_work`, `shader_cache::tests::every_switch_of_the_translator_is_in_the_key`); `watch::` tests are flaky on `main` too (3/10 runs). - `cargo clippy -p victory-gpu --all-targets -- -D warnings` stops on three existing errors in `victory-shader` (`emit/lower/analysis.rs`), also present on `main`. - `cargo xtask policy --staged` - `git diff --check` 🤖 Generated with [Claude Code](https://claude.com/claude-code)
IRennie74 commented 2026-11-30 18:23:06 +00:00 (Migrated from github.com)

Imported from trustno1-coder/VIctory-NVIDIA. Originally posted by IRennie74 on 2026-11-30T18:23:06Z.


Two things to settle before this goes in on by default:

  1. The pre-pass is serial on the flushing thread. guard_first runs in the Batch front (host.rs, just before FRONT[1] is added), before wide_each_scoped spreads the spans. So every guard it does now runs one after another on the submitting thread, not beside each other on the workers. Your table shows the summed guard CPU dropping (34-81 -> 9-31 ms). It does not show how much of the remaining 9-31 ms is now the pre-pass's own wall time on the submitting thread. FRONT[1] already brackets it (the lap after ask_ahead now includes guard_first), so its before/after would answer that directly.

    This matters most on small CPUs. On a Ryzen 5 5600X + RTX 3060 12 GB (Series S, DLSS Performance, diagnostics build), the submitting thread is the frame's wall:

    • it works about 47 ms of a 55 ms standing frame, 11-15 ms of that in refreshes;
    • the title waits for it 11-16 ms a frame at its presents;
    • most 1-2 ms savings on one stage were taken up by another.

    So moving work from the workers onto the submitting thread is the direction that can hurt there. The same rig also spends 10-13 ms a frame waiting for the guards' lock, so less contention is welcome. The open question is only where the remaining guard time lands.

  2. Default. The PR says frame time is unchanged (50-62 -> 52-65 ms median). With no frame gain measured yet, asked_for! (off unless 1) seems the safer default until a run on a 6-core shows FRONT[1] not growing. Turning it on later is a one-line change.

Minor: guard_first widens each span to whole 64 KiB blocks before merged, so a run can include view memory no span asked about. That does not affect coherence (writes only notes and publishes), but it can materialize a lazy view a little earlier than before.

Related: #4027 takes the guards' lock off the title's copy path (opened_for_writing). The two attack the same lock from different callers and should compose.

<!-- greyfinch-sync:trustno1-coder/VIctory-NVIDIA:comment:7287770719 --> Imported from [trustno1-coder/VIctory-NVIDIA](https://github.com/trustno1-coder/VIctory-NVIDIA/pull/4247#issuecomment-7287770719). Originally posted by [IRennie74](https://github.com/IRennie74) on 2026-11-30T18:23:06Z. --- Two things to settle before this goes in on by default: 1. **The pre-pass is serial on the flushing thread.** `guard_first` runs in the `Batch` front (host.rs, just before `FRONT[1]` is added), before `wide_each_scoped` spreads the spans. So every `guard` it does now runs one after another on the submitting thread, not beside each other on the workers. Your table shows the summed `guard` CPU dropping (34-81 -> 9-31 ms). It does not show how much of the remaining 9-31 ms is now the pre-pass's own wall time on the submitting thread. `FRONT[1]` already brackets it (the lap after `ask_ahead` now includes `guard_first`), so its before/after would answer that directly. This matters most on small CPUs. On a Ryzen 5 5600X + RTX 3060 12 GB (Series S, DLSS Performance, diagnostics build), the submitting thread is the frame's wall: - it works about 47 ms of a 55 ms standing frame, 11-15 ms of that in refreshes; - the title waits for it 11-16 ms a frame at its presents; - most 1-2 ms savings on one stage were taken up by another. So moving work from the workers onto the submitting thread is the direction that can hurt there. The same rig also spends 10-13 ms a frame waiting for the guards' lock, so less contention is welcome. The open question is only where the remaining guard time lands. 2. **Default.** The PR says frame time is unchanged (50-62 -> 52-65 ms median). With no frame gain measured yet, `asked_for!` (off unless `1`) seems the safer default until a run on a 6-core shows `FRONT[1]` not growing. Turning it on later is a one-line change. Minor: `guard_first` widens each span to whole 64 KiB blocks before `merged`, so a run can include view memory no span asked about. That does not affect coherence (`writes` only notes and publishes), but it can `materialize` a lazy view a little earlier than before. Related: #4027 takes the guards' lock off the title's copy path (`opened_for_writing`). The two attack the same lock from different callers and should compose.
vilaris commented 2026-11-30 19:15:26 +00:00 (Migrated from github.com)

Imported from trustno1-coder/VIctory-NVIDIA. Originally posted by vilaris on 2026-11-30T19:15:26Z.


Two things to settle before this goes in on by default:

  1. The pre-pass is serial on the flushing thread. guard_first runs in the Batch front (host.rs, just before FRONT[1] is added), before wide_each_scoped spreads the spans. So every guard it does now runs one after another on the submitting thread, not beside each other on the workers. Your table shows the summed guard CPU dropping (34-81 -> 9-31 ms). It does not show how much of the remaining 9-31 ms is now the pre-pass's own wall time on the submitting thread. FRONT[1] already brackets it (the lap after ask_ahead now includes guard_first), so its before/after would answer that directly.
    This matters most on small CPUs. On a Ryzen 5 5600X + RTX 3060 12 GB (Series S, DLSS Performance, diagnostics build), the submitting thread is the frame's wall:

    • it works about 47 ms of a 55 ms standing frame, 11-15 ms of that in refreshes;
    • the title waits for it 11-16 ms a frame at its presents;
    • most 1-2 ms savings on one stage were taken up by another.

    So moving work from the workers onto the submitting thread is the direction that can hurt there. The same rig also spends 10-13 ms a frame waiting for the guards' lock, so less contention is welcome. The open question is only where the remaining guard time lands.

  2. Default. The PR says frame time is unchanged (50-62 -> 52-65 ms median). With no frame gain measured yet, asked_for! (off unless 1) seems the safer default until a run on a 6-core shows FRONT[1] not growing. Turning it on later is a one-line change.

Minor: guard_first widens each span to whole 64 KiB blocks before merged, so a run can include view memory no span asked about. That does not affect coherence (writes only notes and publishes), but it can materialize a lazy view a little earlier than before.

Related: #4027 takes the guards' lock off the title's copy path (opened_for_writing). The two attack the same lock from different callers and should compose.

Done

<!-- greyfinch-sync:trustno1-coder/VIctory-NVIDIA:comment:7289094434 --> Imported from [trustno1-coder/VIctory-NVIDIA](https://github.com/trustno1-coder/VIctory-NVIDIA/pull/4247#issuecomment-7289094434). Originally posted by [vilaris](https://github.com/vilaris) on 2026-11-30T19:15:26Z. --- > Two things to settle before this goes in on by default: > > 1. **The pre-pass is serial on the flushing thread.** `guard_first` runs in the `Batch` front (host.rs, just before `FRONT[1]` is added), before `wide_each_scoped` spreads the spans. So every `guard` it does now runs one after another on the submitting thread, not beside each other on the workers. Your table shows the summed `guard` CPU dropping (34-81 -> 9-31 ms). It does not show how much of the remaining 9-31 ms is now the pre-pass's own wall time on the submitting thread. `FRONT[1]` already brackets it (the lap after `ask_ahead` now includes `guard_first`), so its before/after would answer that directly. > This matters most on small CPUs. On a Ryzen 5 5600X + RTX 3060 12 GB (Series S, DLSS Performance, diagnostics build), the submitting thread is the frame's wall: > > * it works about 47 ms of a 55 ms standing frame, 11-15 ms of that in refreshes; > * the title waits for it 11-16 ms a frame at its presents; > * most 1-2 ms savings on one stage were taken up by another. > > So moving work from the workers onto the submitting thread is the direction that can hurt there. The same rig also spends 10-13 ms a frame waiting for the guards' lock, so less contention is welcome. The open question is only where the remaining guard time lands. > 2. **Default.** The PR says frame time is unchanged (50-62 -> 52-65 ms median). With no frame gain measured yet, `asked_for!` (off unless `1`) seems the safer default until a run on a 6-core shows `FRONT[1]` not growing. Turning it on later is a one-line change. > > Minor: `guard_first` widens each span to whole 64 KiB blocks before `merged`, so a run can include view memory no span asked about. That does not affect coherence (`writes` only notes and publishes), but it can `materialize` a lazy view a little earlier than before. > > Related: #4027 takes the guards' lock off the title's copy path (`opened_for_writing`). The two attack the same lock from different callers and should compose. Done
KruIN689 commented 2026-11-30 19:26:05 +00:00 (Migrated from github.com)

Imported from trustno1-coder/VIctory-NVIDIA. Originally posted by KruIN689 on 2026-11-30T19:26:05Z.


Independent test on different hardware: RTX 5080 (driver 621.40), Ryzen 9 9950X3D, 47 GB, Windows 11. Series X, native 1440p, DLSS and FG off, VICTORY_HW_RT=1.

Method: one release build of this branch merged with main e1251c31, A/B by the switch (VICTORY_GUARD_FIRST=0 for off), 2+2 alternating runs of auto-driven roaming (walk and drive), 6 min each, stats over the last 4 min:

run fps frames over 50 ms per 1,000 mean p99 submitting thread refreshes
on 29.1 50 58.2 ms 32.7 ms 13.3 ms
off 29.8 45 55.3 ms 31.9 ms 12.7 ms
on 31.3 28 52.2 ms 26.6 ms 9.6 ms
off 31.0 40 54.7 ms 29.2 ms 10.7 ms

No errors in any run. On this 16-thread X3D the difference sits within run-to-run noise, both for refresh time and for fps. That fits your note that about half of the guard time was lock waiting: with fewer spans per flush here there may be little contention to remove. It costs nothing measurable either. A 6- or 8-core CPU would show it better than this one.

(Measured and written with Claude Code, for KruIN689.)

<!-- greyfinch-sync:trustno1-coder/VIctory-NVIDIA:comment:7289385416 --> Imported from [trustno1-coder/VIctory-NVIDIA](https://github.com/trustno1-coder/VIctory-NVIDIA/pull/4247#issuecomment-7289385416). Originally posted by [KruIN689](https://github.com/KruIN689) on 2026-11-30T19:26:05Z. --- Independent test on different hardware: RTX 5080 (driver 621.40), Ryzen 9 9950X3D, 47 GB, Windows 11. Series X, native 1440p, DLSS and FG off, `VICTORY_HW_RT=1`. Method: one release build of this branch merged with main `e1251c31`, A/B by the switch (`VICTORY_GUARD_FIRST=0` for off), 2+2 alternating runs of auto-driven roaming (walk and drive), 6 min each, stats over the last 4 min: | run | fps | frames over 50 ms per 1,000 | mean p99 | submitting thread | refreshes | |---|---:|---:|---:|---:|---:| | on | 29.1 | 50 | 58.2 ms | 32.7 ms | 13.3 ms | | off | 29.8 | 45 | 55.3 ms | 31.9 ms | 12.7 ms | | on | 31.3 | 28 | 52.2 ms | 26.6 ms | 9.6 ms | | off | 31.0 | 40 | 54.7 ms | 29.2 ms | 10.7 ms | No errors in any run. On this 16-thread X3D the difference sits within run-to-run noise, both for refresh time and for fps. That fits your note that about half of the guard time was lock waiting: with fewer spans per flush here there may be little contention to remove. It costs nothing measurable either. A 6- or 8-core CPU would show it better than this one. (Measured and written with Claude Code, for KruIN689.)
gabrielecabrini commented 2026-11-30 20:04:23 +00:00 (Migrated from github.com)

Imported from trustno1-coder/VIctory-NVIDIA. Originally posted by gabrielecabrini on 2026-11-30T20:04:23Z.


Tested in game on Linux (Proton). With VICTORY_GUARD_FIRST on, the frame was faster both standing and moving.

Setup: RTX 4070 SUPER 12 GB, i5-14600KF, NVIDIA 620.73.04, Proton Experimental 11.0-20261123b, Series S, DLSS off. Release build of this PR on main e1251c31. Same save and route in both runs: about 60 s standing still, then about 60 s walking and turning the camera. One run each: A is the default, B is VICTORY_GUARD_FIRST=0.

fps standing fps moving write-watch time, standing write-watch time, moving
A (on) 21.1 17.6 6.4 ms 9.6 ms
B (off) 20.0 16.0 8.9 ms 12.7 ms
  • The submitting thread's write-watch time (which is where the guards' time shows up) drops 24-28%. fps goes up 6% standing and 10% moving.
  • B drew 5-10% more draws, and there was one run per arm, so take the moving numbers as a direction, not a size.
  • No errors. The texture glitches came with the switch off (B): 18 "texture picked by index changed" warnings against 3 in A. I'd call that the old behaviour, not this PR. The same warning was logged 13 and 18 times in earlier runs without this PR.
<!-- greyfinch-sync:trustno1-coder/VIctory-NVIDIA:comment:7290363476 --> Imported from [trustno1-coder/VIctory-NVIDIA](https://github.com/trustno1-coder/VIctory-NVIDIA/pull/4247#issuecomment-7290363476). Originally posted by [gabrielecabrini](https://github.com/gabrielecabrini) on 2026-11-30T20:04:23Z. --- Tested in game on Linux (Proton). With `VICTORY_GUARD_FIRST` on, the frame was faster both standing and moving. **Setup:** RTX 4070 SUPER 12 GB, i5-14600KF, NVIDIA 620.73.04, Proton Experimental 11.0-20261123b, Series S, DLSS off. Release build of this PR on main e1251c31. Same save and route in both runs: about 60 s standing still, then about 60 s walking and turning the camera. One run each: A is the default, B is `VICTORY_GUARD_FIRST=0`. | | fps standing | fps moving | write-watch time, standing | write-watch time, moving | |---|---:|---:|---:|---:| | A (on) | **21.1** | **17.6** | **6.4 ms** | **9.6 ms** | | B (off) | 20.0 | 16.0 | 8.9 ms | 12.7 ms | - The submitting thread's write-watch time (which is where the guards' time shows up) drops 24-28%. fps goes up 6% standing and 10% moving. - B drew 5-10% more draws, and there was one run per arm, so take the moving numbers as a direction, not a size. - No errors. The texture glitches came with the switch off (B): 18 "texture picked by index changed" warnings against 3 in A. I'd call that the old behaviour, not this PR. The same warning was logged 13 and 18 times in earlier runs without this PR.
trustno1-coder commented 2026-12-01 18:45:00 +00:00 (Migrated from github.com)

Imported from trustno1-coder/VIctory-NVIDIA. Originally posted by trustno1-coder on 2026-12-01T18:45:00Z.


Closing this as part of moving main to the texture and video-memory rework (v2026.12.01.01-test). A lot of the code changed, and this pull request no longer applies cleanly to it; it would need to be rewritten against the new main rather than rebased. Thank you for the work. If the change is still needed, please open a new pull request based on the current main.

<!-- greyfinch-sync:trustno1-coder/VIctory-NVIDIA:comment:7321468872 --> Imported from [trustno1-coder/VIctory-NVIDIA](https://github.com/trustno1-coder/VIctory-NVIDIA/pull/4247#issuecomment-7321468872). Originally posted by [trustno1-coder](https://github.com/trustno1-coder) on 2026-12-01T18:45:00Z. --- Closing this as part of moving main to the texture and video-memory rework (v2026.12.01.01-test). A lot of the code changed, and this pull request no longer applies cleanly to it; it would need to be rewritten against the new main rather than rebased. Thank you for the work. If the change is still needed, please open a new pull request based on the current main.

Pull request closed

Sign in to join this conversation.
No reviewers
No labels
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.

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