Reduce lock/atomic contention on the ~88% no-op refresh fast path (21.3 ms/frame on 8-core CPU, GPU 52% busy) #1521

Closed
opened 2026-11-28 21:13:38 +00:00 by github-bot · 4 comments
Owner

@⁠GreekNinja on GitHub · 2026-11-28 21:12 UTC · original

Motivation (from 923e38c gameplay telemetry)

On an 8-core Ryzen 7 5700X + RTX 3060 12 GB (Series S, DLSS Ultra Performance), once OOM/residency stalls are eliminated, the GPU averages only 52.0% busy (45.4 ms/frame idle waiting behind the submitting thread's 49.8 ms/frame).

The single largest CPU cost on the submitting thread is refreshes: 21.3 ms/frame across ~9,964 spans/frame (44 flushes), of which 8,823 spans (88.5%) are known to copy nothing.

Bottleneck in the current no-op path

When spans.len() > FEW (256) (crates/victory-gpu/src/host.rs:9342-9374, 9488-9528):

  1. wide_each_from (shadow.rs:1947-1970) fans out across wide_threads() + 1 (7 threads on 8C/16T).
  2. For every single no-op span, Shadow::nothing_new() (shadow.rs:4442-4476) locks two separate mutexes (self.copied.lock() L4460 + self.ranged.lock() L4474), then calls watch::nothing_new() (watch.rs:961-978), which acquires and releases the global JOURNAL RwLock (SRWLock atomic RMW) and linearly scans j.ahead.iter().any(...) (watch.rs:970).
  3. Each no-op span then executes FLUSHES[2].fetch_add(1) (host.rs:9367, 9524) on a single shared cache line across all 7 worker threads.

Concrete Proposal

  1. Chunked wide_each_range (shadow.rs:1947): let each worker acquire one JOURNAL.read() guard per VICTORY_WIDE_CHUNK (e.g. 16 spans) and accumulate let mut noop = 0u64; locally before a single FLUSHES[2].fetch_add(noop) — cutting cross-core SRWLock + atomic RMW contention by 16× (~16,500 fewer cross-core atomics/frame).
  2. Fold ranged into copied's Mutex (shadow.rs:45-50): halves mutex lock/unlock ops in nothing_new().
  3. Keep j.ahead sorted (watch.rs:453) and replace linear .iter().any(...) (watch.rs:865, 886, 970) with partition_point binary search.
  4. Bonus texture-pass win (draw.rs:3656-3742): when !room and first uploads are disallowed (or used >= budget), check !host.texture_slot_made_from(index, handle) before calling mapped_bytes() (VirtualQuery kernel walk) so held slots skip VirtualQuery (currently 87–281 ms per 5 s + 91 ms recording-thread lock contention under !room).

Note: This report/comment was generated and submitted automatically by an AI assistant analyzing real local play logs and source code on behalf of the user.

**@⁠GreekNinja** on GitHub · 2026-11-28 21:12 UTC · [original](https://github.com/trustno1-coder/VIctory-NVIDIA/issues/1521) ### Motivation (from `923e38c` gameplay telemetry) On an 8-core **Ryzen 7 5700X + RTX 3060 12 GB (Series S, DLSS Ultra Performance)**, once OOM/residency stalls are eliminated, the GPU averages only **52.0% busy** (**45.4 ms/frame idle** waiting behind the submitting thread's **49.8 ms/frame**). The single largest CPU cost on the submitting thread is **`refreshes`: 21.3 ms/frame** across **~9,964 spans/frame** (44 flushes), of which **8,823 spans (88.5%) are `known to copy nothing`**. ### Bottleneck in the current no-op path When `spans.len() > FEW (256)` (`crates/victory-gpu/src/host.rs:9342-9374, 9488-9528`): 1. `wide_each_from` (`shadow.rs:1947-1970`) fans out across `wide_threads() + 1` (7 threads on 8C/16T). 2. For **every single no-op span**, `Shadow::nothing_new()` (`shadow.rs:4442-4476`) locks **two separate mutexes** (`self.copied.lock()` L4460 + `self.ranged.lock()` L4474), then calls `watch::nothing_new()` (`watch.rs:961-978`), which acquires and releases the global `JOURNAL` `RwLock` (SRWLock atomic RMW) and linearly scans `j.ahead.iter().any(...)` (`watch.rs:970`). 3. Each no-op span then executes `FLUSHES[2].fetch_add(1)` (`host.rs:9367, 9524`) on a single shared cache line across all 7 worker threads. ### Concrete Proposal 1. **Chunked `wide_each_range` (`shadow.rs:1947`)**: let each worker acquire **one `JOURNAL.read()` guard per `VICTORY_WIDE_CHUNK` (e.g. 16 spans)** and accumulate `let mut noop = 0u64;` locally before a single `FLUSHES[2].fetch_add(noop)` — cutting cross-core SRWLock + atomic RMW contention by **16×** (~16,500 fewer cross-core atomics/frame). 2. **Fold `ranged` into `copied`'s `Mutex` (`shadow.rs:45-50`)**: halves mutex lock/unlock ops in `nothing_new()`. 3. **Keep `j.ahead` sorted (`watch.rs:453`)** and replace linear `.iter().any(...)` (`watch.rs:865, 886, 970`) with `partition_point` binary search. 4. **Bonus texture-pass win (`draw.rs:3656-3742`)**: when `!room` and first uploads are disallowed (or `used >= budget`), check `!host.texture_slot_made_from(index, handle)` *before* calling `mapped_bytes()` (`VirtualQuery` kernel walk) so held slots skip `VirtualQuery` (currently **87–281 ms per 5 s** + 91 ms recording-thread lock contention under `!room`). --- *Note: This report/comment was generated and submitted automatically by an AI assistant analyzing real local play logs and source code on behalf of the user.*
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-28 21:13:38 +00:00
github-bot changed title from [Perf proposal] Reduce lock/atomic contention on the ~88% no-op refresh fast path (21.3 ms/frame on 8-core CPU, GPU 52% busy) to Reduce lock/atomic contention on the ~88% no-op refresh fast path (21.3 ms/frame on 8-core CPU, GPU 52% busy) 2026-11-28 21:37:26 +00:00
Author
Owner

@⁠AiToR51 on GitHub · 2026-11-28 23:49 UTC · original

A second data point from the same CPU, and an A/B of the switch that is already in the tree.

Host: Ryzen 7 5700X (8C/16T), RTX 3080 10 GB, 32 GB DDR4, driver 621.40, Windows 10 22H2, Agility SDK 1.620. Series X profile, 1440p, DLSS Quality. Build 32ecacfa with #43 reverted locally (#1435). Host memory pressure is on in both runs.

Method: the same save at the same indoor spot (~4,900 draws/frame), standing still. Each run is a fresh start, measured in its last 2 minutes after several minutes in the spot. The values are medians of the per-second preparing per frame lines and the 5 s frame lines.

default (VICTORY_WIDE_CHUNK unset) VICTORY_WIDE_CHUNK=16
frame 46.5 ms 47.4 ms
preparing (frame line) 34.8 ms 33.7 ms
submitting thread, in its submissions 41.4 ms 40.8 ms
refreshes 12.2 ms 12.4 ms
flushes 36 35
spans 9,910 9,795
known to copy nothing 8,905 (90 %) 8,760 (89 %)
on several threads 9,383 9,278

The log confirmed chunk=16 in the second run.

Takeaways:

  • Same shape as your numbers: about 9,900 spans a frame, about 90 % of them no-ops, and refreshes about 30 % of the submitting thread's time.
  • VICTORY_WIDE_CHUNK=16 changes nothing measurable here. It only hands the spans to the threads 16 at a time. Each span still takes its own locks and JOURNAL read and bumps the shared counter. So the cost seems to be in the per-span work, which is what your proposals 1-3 change (one JOURNAL.read() and one counter update per chunk, one mutex, a sorted ahead), not in how the spans are handed out.

One caveat: a first run of the default build, taken yesterday right after the start, measured 57.6 ms a frame with the same 11.9 ms of refreshes. The frame time falls over the first minutes of a session, so short runs from just after the start aren't comparable. Refreshes stay at 11.6-13.6 ms the whole time.

I can run the same A/B on a branch with your proposals if one appears.

**@⁠AiToR51** on GitHub · 2026-11-28 23:49 UTC · [original](https://github.com/trustno1-coder/VIctory-NVIDIA/issues/1521#issuecomment-7247685231) A second data point from the same CPU, and an A/B of the switch that is already in the tree. **Host:** Ryzen 7 5700X (8C/16T), RTX 3080 10 GB, 32 GB DDR4, driver 621.40, Windows 10 22H2, Agility SDK 1.620. Series X profile, 1440p, DLSS Quality. Build 32ecacfa with #43 reverted locally (#1435). Host memory pressure is on in both runs. **Method:** the same save at the same indoor spot (~4,900 draws/frame), standing still. Each run is a fresh start, measured in its last 2 minutes after several minutes in the spot. The values are medians of the per-second `preparing per frame` lines and the 5 s frame lines. | | default (`VICTORY_WIDE_CHUNK` unset) | `VICTORY_WIDE_CHUNK=16` | |---|---|---| | frame | 46.5 ms | 47.4 ms | | preparing (frame line) | 34.8 ms | 33.7 ms | | submitting thread, in its submissions | 41.4 ms | 40.8 ms | | refreshes | **12.2 ms** | **12.4 ms** | | flushes | 36 | 35 | | spans | 9,910 | 9,795 | | known to copy nothing | 8,905 (90 %) | 8,760 (89 %) | | on several threads | 9,383 | 9,278 | The log confirmed `chunk=16` in the second run. Takeaways: - Same shape as your numbers: about 9,900 spans a frame, about 90 % of them no-ops, and refreshes about 30 % of the submitting thread's time. - `VICTORY_WIDE_CHUNK=16` changes nothing measurable here. It only hands the spans to the threads 16 at a time. Each span still takes its own locks and `JOURNAL` read and bumps the shared counter. So the cost seems to be in the per-span work, which is what your proposals 1-3 change (one `JOURNAL.read()` and one counter update per chunk, one mutex, a sorted `ahead`), not in how the spans are handed out. One caveat: a first run of the default build, taken yesterday right after the start, measured 57.6 ms a frame with the same 11.9 ms of refreshes. The frame time falls over the first minutes of a session, so short runs from just after the start aren't comparable. Refreshes stay at 11.6-13.6 ms the whole time. I can run the same A/B on a branch with your proposals if one appears.
Author
Owner

@⁠GreekNinja on GitHub · 2026-11-29 00:15 UTC · original

Thank you @⁠AiToR51 for the controlled Ryzen 7 5700X A/B! That VICTORY_WIDE_CHUNK=16 comparison is super valuable — it isolates the bottleneck directly to the per-span JOURNAL.read() + dual-mutex + shared FLUSHES[2] atomic work rather than chunk dispatch overhead (and confirms the same ~9,900 spans / ~90% no-op shape across both Series S and Series X).

Having your steady-state indoor A/B spot ready to test a branch implementing proposals 1–3 will be a huge help if someone picks this up!


Note: This comment was generated and submitted automatically by an AI assistant on behalf of the user.

**@⁠GreekNinja** on GitHub · 2026-11-29 00:15 UTC · [original](https://github.com/trustno1-coder/VIctory-NVIDIA/issues/1521#issuecomment-7247988406) Thank you @⁠AiToR51 for the controlled Ryzen 7 5700X A/B! That `VICTORY_WIDE_CHUNK=16` comparison is super valuable — it isolates the bottleneck directly to the per-span `JOURNAL.read()` + dual-mutex + shared `FLUSHES[2]` atomic work rather than chunk dispatch overhead (and confirms the same ~9,900 spans / ~90% no-op shape across both Series S and Series X). Having your steady-state indoor A/B spot ready to test a branch implementing proposals 1–3 will be a huge help if someone picks this up! --- *Note: This comment was generated and submitted automatically by an AI assistant on behalf of the user.*
Author
Owner

@⁠kalberts0089-hash on GitHub · 2026-11-29 08:00 UTC · original

Checked where each proposal stands at e1f2eb6, and microbenched the no-op path on its own (no game here, so this is synthetic).

Status at main e1f2eb6

  1. Per-chunk guard/counter: not done. wide_each_from (crates/victory-gpu/src/shadow.rs:1990) still calls the closure once per index even with VICTORY_WIDE_CHUNK>1, and the worker path still does FLUSHES[2].fetch_add(1) per span (crates/victory-gpu/src/host.rs:9484, :9641). That fits the A/B above where VICTORY_WIDE_CHUNK=16 changed nothing.
  2. Fold ranged into copied: already done on main in 0174844 ("perf(gpu): share the shadow copy state lock"). It is now copied: Mutex<Copied> (shadow.rs:27-40, :60). Shadow::nothing_new takes one mutex (shadow.rs:4512). The pending mutex is skipped unless has_pending is set (shadow.rs:4507, from 2396eda). 0174844 is not in 923e38c, 32ecacfa or v2026.11.28.03, so none of the numbers in this thread include it.
  3. Sorted ahead: not done. watch.rs:1055 is still a linear j.ahead.iter().any(..). ahead is a plain Vec (watch.rs:205), set from sorted pieces in ask_ahead (watch.rs:529) and then appended to in ask_rest (watch.rs:854) with no sort. There is also a second global lock per span that the issue doesn't name: owned() takes OWNED.read() (watch.rs:1053, :140-143) whenever the range isn't published.
  4. Texture pass: not done. mapped_bytes (draw.rs:3726) still runs before the !room / texture_slot_made_from check (draw.rs:3777).

Microbench

This is a throwaway #[ignore] test in a worktree of e1f2eb6. It builds N spans that are all no-ops, each with its own Mutex. Each span calls the real watch::nothing_new (real JOURNAL, OWNED and published() bits) through the real wide_each/wide_each_from pool, plus a shared fetch_add. "Chunked" mimics proposals 1+3: one JOURNAL.read() and one fetch_add per 16 spans, and a partition_point look into a sorted ahead.

cargo test --locked --release -p victory-gpu --lib bench_1521 -- --ignored --nocapture

Setup: i7-13700KF, VICTORY_WIDE_THREADS=6 (7 threads, what an 8C/16T host gets by default), one flush of 9,000 no-op spans. Each cell is the range of the median of 200 flushes over two runs:

ahead entries submitting thread only as now, chunk 1 as now, chunk 16 chunked + sorted
1 229-231 us 612-714 us 609-637 us 333-382 us
64 392-402 us 704-814 us 620-711 us 340-385 us
1024 2,889-2,950 us 820-871 us 676-784 us 336-339 us

With the default 9 threads on this 24-thread CPU, "as now, chunk 1" was 641-811 us.

What this shows:

  • As the code is now, the no-op checks cost about 0.6-0.9 ms per 9,000 spans. Proposals 1+3 bring that to about 0.33-0.39 ms. That saves about 0.3-0.5 ms a flush, a small part of the 12-21 ms.
  • With a short ahead, sending pure no-ops to the threads is 2-3x slower than doing them on the submitting thread. With a long ahead, the linear scan dominates and the threads help.
  • Hypothesis, not measured in game: most of the refreshes figure is something else. It is SPLIT[6], which collects the wall time of whole flushes (host.rs:9498, :9595) plus the one-at-a-time refreshes (:9385, :9540). That includes ask_ahead's kernel askings, the texture refreshes in due (on the :9498 path), Self::update for the ~10 % that do copy, and waiting for the threads. In game, cold caches and the title's own threads would push the no-op cost above these numbers, but I doubt by 15-30x. A timer around just the nothing_new calls, or the length of j.ahead at flush time, from a play log would settle it.

Caveats: warm cache, idle machine, tiny copied vectors, and the covering ahead range placed last (the worst case for the linear scan).

**@⁠kalberts0089-hash** on GitHub · 2026-11-29 08:00 UTC · [original](https://github.com/trustno1-coder/VIctory-NVIDIA/issues/1521#issuecomment-7252829206) Checked where each proposal stands at `e1f2eb6`, and microbenched the no-op path on its own (no game here, so this is synthetic). **Status at main `e1f2eb6`** 1. Per-chunk guard/counter: not done. `wide_each_from` (`crates/victory-gpu/src/shadow.rs:1990`) still calls the closure once per index even with `VICTORY_WIDE_CHUNK>1`, and the worker path still does `FLUSHES[2].fetch_add(1)` per span (`crates/victory-gpu/src/host.rs:9484`, `:9641`). That fits the A/B above where `VICTORY_WIDE_CHUNK=16` changed nothing. 2. Fold `ranged` into `copied`: **already done on main** in 0174844 ("perf(gpu): share the shadow copy state lock"). It is now `copied: Mutex<Copied>` (`shadow.rs:27-40`, `:60`). `Shadow::nothing_new` takes one mutex (`shadow.rs:4512`). The `pending` mutex is skipped unless `has_pending` is set (`shadow.rs:4507`, from 2396eda). 0174844 is not in 923e38c, 32ecacfa or v2026.11.28.03, so none of the numbers in this thread include it. 3. Sorted `ahead`: not done. `watch.rs:1055` is still a linear `j.ahead.iter().any(..)`. `ahead` is a plain `Vec` (`watch.rs:205`), set from sorted pieces in `ask_ahead` (`watch.rs:529`) and then appended to in `ask_rest` (`watch.rs:854`) with no sort. There is also a second global lock per span that the issue doesn't name: `owned()` takes `OWNED.read()` (`watch.rs:1053`, `:140-143`) whenever the range isn't published. 4. Texture pass: not done. `mapped_bytes` (`draw.rs:3726`) still runs before the `!room` / `texture_slot_made_from` check (`draw.rs:3777`). **Microbench** This is a throwaway `#[ignore]` test in a worktree of `e1f2eb6`. It builds N spans that are all no-ops, each with its own `Mutex`. Each span calls the real `watch::nothing_new` (real `JOURNAL`, `OWNED` and `published()` bits) through the real `wide_each`/`wide_each_from` pool, plus a shared `fetch_add`. "Chunked" mimics proposals 1+3: one `JOURNAL.read()` and one `fetch_add` per 16 spans, and a `partition_point` look into a sorted `ahead`. ``` cargo test --locked --release -p victory-gpu --lib bench_1521 -- --ignored --nocapture ``` Setup: i7-13700KF, `VICTORY_WIDE_THREADS=6` (7 threads, what an 8C/16T host gets by default), one flush of 9,000 no-op spans. Each cell is the range of the median of 200 flushes over two runs: | `ahead` entries | submitting thread only | as now, chunk 1 | as now, chunk 16 | chunked + sorted | |---|---|---|---|---| | 1 | 229-231 us | 612-714 us | 609-637 us | 333-382 us | | 64 | 392-402 us | 704-814 us | 620-711 us | 340-385 us | | 1024 | 2,889-2,950 us | 820-871 us | 676-784 us | 336-339 us | With the default 9 threads on this 24-thread CPU, "as now, chunk 1" was 641-811 us. What this shows: - As the code is now, the no-op checks cost about 0.6-0.9 ms per 9,000 spans. Proposals 1+3 bring that to about 0.33-0.39 ms. That saves about 0.3-0.5 ms a flush, a small part of the 12-21 ms. - With a short `ahead`, sending pure no-ops to the threads is 2-3x slower than doing them on the submitting thread. With a long `ahead`, the linear scan dominates and the threads help. - Hypothesis, not measured in game: most of the `refreshes` figure is something else. It is `SPLIT[6]`, which collects the wall time of whole flushes (`host.rs:9498`, `:9595`) plus the one-at-a-time refreshes (`:9385`, `:9540`). That includes `ask_ahead`'s kernel askings, the texture refreshes in `due` (on the `:9498` path), `Self::update` for the ~10 % that do copy, and waiting for the threads. In game, cold caches and the title's own threads would push the no-op cost above these numbers, but I doubt by 15-30x. A timer around just the `nothing_new` calls, or the length of `j.ahead` at flush time, from a play log would settle it. Caveats: warm cache, idle machine, tiny `copied` vectors, and the covering `ahead` range placed last (the worst case for the linear scan).
Author
Owner

@⁠trustno1-coder on GitHub · 2026-11-30 15:42 UTC · original

Closing this because it was opened before v2026.11.29.01, and a great deal has changed since then. The latest release is v2026.11.30.01.

If this still happens, or still applies, on the latest release, please open a new issue. For a bug, attach both logs from that run:

  1. The play log: the newest victory-nvidia-play-<date>-<time>-<number>.log in %LOCALAPPDATA%\victory\logs\.
  2. The crash log: %LOCALAPPDATA%\VIctory\data\7952987A\crashes.log, plus the newest .crash.log in %LOCALAPPDATA%\VIctory\data\7952987A\temp\ if there is one from that run.
**@⁠trustno1-coder** on GitHub · 2026-11-30 15:42 UTC · [original](https://github.com/trustno1-coder/VIctory-NVIDIA/issues/1521#issuecomment-7283442662) Closing this because it was opened before v2026.11.29.01, and a great deal has changed since then. The latest release is [v2026.11.30.01](https://github.com/trustno1-coder/VIctory-NVIDIA/releases/tag/v2026.11.30.01). If this still happens, or still applies, on the latest release, please open a new issue. For a bug, attach both logs from that run: 1. The play log: the newest `victory-nvidia-play-<date>-<time>-<number>.log` in `%LOCALAPPDATA%\victory\logs\`. 2. The crash log: `%LOCALAPPDATA%\VIctory\data\7952987A\crashes.log`, plus the newest `.crash.log` in `%LOCALAPPDATA%\VIctory\data\7952987A\temp\` if there is one from that run.
This discussion has been locked. Commenting is limited to contributors.
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#1521
No description provided.