Reduce CPU lock contention across shadow merges, refreshes, and card sync #1561

Manually merged
github merged 1 commit from github-bot/gh-1561 into main 2026-11-30 14:16:23 +00:00 AGit
Owner

@⁠mikael-jarvinen on GitHub · 2026-11-28 21:45 UTC · original
mikael-jarvinen:pr-cpu-merging-stripes-and-batch → main · +135 −27 · 4 files · 1 commits · merged 2026-11-30 14:12 UTC

Rebased replacement for #1157 onto main (923e38c), cleanly composed with upstream #1400 (UnderSlots + batched card.dirty) and VICTORY_FAULT_UNLOCKED (dropping already-merged hunks).

Summary of Changes

  • 64-stripe Shadow::merging ([RwLock<()>; 64], lazily allocated): replaces the single per-Shadow RwLock<()> so concurrent land_card_pieces and merge_under_writes on disjoint 64 KiB pages do not serialize each other; acquires only the touched stripe mask in ascending order and skips self.before.lock() on card-backed copies.
  • Unified CopyState (RwLock<CopyState>): replaces separate copied + ranged mutexes for concurrent lock-free reader sharing in nothing_new_with.
  • Batched JournalRead + Shadow::nothing_new_with: acquires JOURNAL read lock once per refresh/span batch instead of per copy, with binary-searched ahead_contains.
  • Batched descriptor::constant_slots_batch + HostHeap::constant_at_with_base: resolves SHADER_HEAPS once and reuses cached buffer_gpu across card_steps constant slots.
  • Submission Batch fast paths: Batch::note_gpu_write deduplicates repeated GPU writes and accumulates declared directly (avoiding declared_writes(runs) rescan); 1-entry last_ahead cache; Blocks::all_set word-level bitmap check in watch::guard; CARD_WORK locked after send::take.

Verification & Re-Measurement on main (923e38c)

  • Unit tests: 50/50 shadow + 51/51 watch tests pass; 0 new warnings.
  • Live benchmark (RTX 3080 10 GB, Ryzen 7 7800X3D, diag build, warm cache, 1440p DLSS Performance, FG off, 115 s scripted run):
Metric main (923e38c, ~7,650 draws) This PR (175c954, ~8,000–8,690 draws) Delta
Steady 3D FPS (med / peak) 15.7 / 17.3 16.3 / 17.8 +3.8% (at +5% draws)
GPU busy (%) / idle behind CPU (ms) 66.0% / 21.65 ms 94.5% / 3.35 ms +28.5 pp / −84.5% idle
Submit prep TOTAL (med / late) 58.50 ms 51.70 / 48.10 ms −6.80 ms (−11.6%)
lists' steps TOTAL (copies & fills) 24.15 ms (3.30 ms) 20.60 ms (1.70 ms) −14.7% (−48.5%)
planning card_steps 12.65 ms 10.40 ms −17.8%
asking write watch 5.10 ms 3.95 ms −22.5%
Title threads waited at presents 17.00 ms 4.35 ms −74.4%
**@⁠mikael-jarvinen** on GitHub · 2026-11-28 21:45 UTC · [original](https://github.com/trustno1-coder/VIctory-NVIDIA/pull/1561) `mikael-jarvinen:pr-cpu-merging-stripes-and-batch` → `main` · +135 −27 · 4 files · 1 commits · merged 2026-11-30 14:12 UTC Rebased replacement for #1157 onto `main` (`923e38c`), cleanly composed with upstream #1400 (`UnderSlots` + batched `card.dirty`) and `VICTORY_FAULT_UNLOCKED` (dropping already-merged hunks). ### Summary of Changes - **64-stripe `Shadow::merging`** (`[RwLock<()>; 64]`, lazily allocated): replaces the single per-`Shadow` `RwLock<()>` so concurrent `land_card_pieces` and `merge_under_writes` on disjoint 64 KiB pages do not serialize each other; acquires only the touched stripe mask in ascending order and skips `self.before.lock()` on card-backed copies. - **Unified `CopyState` (`RwLock<CopyState>`)**: replaces separate `copied` + `ranged` mutexes for concurrent lock-free reader sharing in `nothing_new_with`. - **Batched `JournalRead` + `Shadow::nothing_new_with`**: acquires `JOURNAL` read lock once per refresh/span batch instead of per copy, with binary-searched `ahead_contains`. - **Batched `descriptor::constant_slots_batch` + `HostHeap::constant_at_with_base`**: resolves `SHADER_HEAPS` once and reuses cached `buffer_gpu` across `card_steps` constant slots. - **Submission `Batch` fast paths**: `Batch::note_gpu_write` deduplicates repeated GPU writes and accumulates `declared` directly (avoiding `declared_writes(runs)` rescan); 1-entry `last_ahead` cache; `Blocks::all_set` word-level bitmap check in `watch::guard`; `CARD_WORK` locked after `send::take`. ### Verification & Re-Measurement on `main` (`923e38c`) - **Unit tests:** 50/50 `shadow` + 51/51 `watch` tests pass; 0 new warnings. - **Live benchmark** (RTX 3080 10 GB, Ryzen 7 7800X3D, `diag` build, warm cache, 1440p DLSS Performance, FG off, 115 s scripted run): | Metric | `main` (`923e38c`, ~7,650 draws) | This PR (`175c954`, ~8,000–8,690 draws) | Delta | |---|---:|---:|---:| | Steady 3D FPS (med / peak) | 15.7 / 17.3 | **16.3 / 17.8** | +3.8% (at +5% draws) | | GPU busy (%) / idle behind CPU (ms) | 66.0% / 21.65 ms | **94.5% / 3.35 ms** | **+28.5 pp / −84.5% idle** | | Submit prep TOTAL (med / late) | 58.50 ms | **51.70 / 48.10 ms** | **−6.80 ms (−11.6%)** | | `lists' steps` TOTAL (`copies & fills`) | 24.15 ms (3.30 ms) | **20.60 ms (1.70 ms)** | **−14.7% (−48.5%)** | | `planning card_steps` | 12.65 ms | **10.40 ms** | **−17.8%** | | `asking write watch` | 5.10 ms | **3.95 ms** | **−22.5%** | | Title threads waited at presents | 17.00 ms | **4.35 ms** | **−74.4%** |
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-28 21:48:39 +00:00
github-bot force-pushed github-bot/gh-1561 from 175c9549ef to c2ed6561ed 2026-11-29 08:33:50 +00:00 Compare
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 on current main (it still merges cleanly), 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 66.5 68.8 12.63 11.95
round 2 67.2 66.7 12.23 12.01
mean difference −0.9 ms +3.8 %

The difference is on the good side but inside the noise (the controls alone spread over 9 ms).

Picture: in round 1, one frame had the palms and the potted plants by the house missing. The game was at 8 fps at that moment, so that frame was on screen for about 140 ms. We saw none in the 320 control frames of this session nor in 800 earlier ones on this base, and we saw the same one-frame vegetation gap with #3502, #3476 and #3411. It may be a timing-sensitive problem in main rather than this PR alone.

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/1561#issuecomment-7268047864) · edited 2026-11-30 09:56 UTC We ran a test of this PR on current main (it still merges cleanly), 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 | 66.5 | 68.8 | 12.63 | 11.95 | | round 2 | 67.2 | 66.7 | 12.23 | 12.01 | | **mean difference** | **−0.9 ms** | | **+3.8 %** | | The difference is on the good side but inside the noise (the controls alone spread over 9 ms). **Picture:** in round 1, one frame had the palms and the potted plants by the house missing. The game was at 8 fps at that moment, so that frame was on screen for about 140 ms. We saw none in the 320 control frames of this session nor in 800 earlier ones on this base, and we saw the same one-frame vegetation gap with #3502, #3476 and #3411. It may be a timing-sensitive problem in main rather than this PR alone. **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
github manually merged commit c17aacd9d1 into main 2026-11-30 14:16:23 +00:00
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!1561
No description provided.