shadow: avoid redundant locks, wide_each allocs, and per-page dirty/counter churn in refresh hot path #2530

Merged
qlfu merged 1 commit from upstream-pr/2530 into main 2026-11-29 22:54:43 +00:00
qlfu commented 2026-11-29 13:52:45 +00:00 (Migrated from github.com)

Imported from trustno1-coder/VIctory-NVIDIA. Originally posted by qlfu on 2026-11-29T13:52:45Z.


Summary

Eliminates redundant lock acquisitions, per-page lock/atomic churn, and heap-allocating nested pool dispatch on the Shadow refresh hot path (update_ranges, update_free, copy_stale, take_all, take, take_landed) and watch::parts_written.

What Changed

  • crates/victory-gpu/src/watch.rs:
    • Short-circuits parts_written(&[]) before acquiring the global JOURNAL RwLock (avoids taking journal() on every no-op copy_stale refresh).
  • crates/victory-gpu/src/shadow.rs:
    • Shadow::update_ranges: gates self.pending.lock() and busy Vec allocation behind self.has_pending.load(Ordering::Acquire) (matching nothing_new, new_look, nothing_new_with, and writes_pending_in).
    • Shadow::update_free: checks prev == GUARDED before calling crate::watch::guarded(from, to) (avoiding PROTECTED bitmap queries on already-guarded copies) and skips storing back to self.watched when unchanged (avoiding cross-thread cache-line dirtying during parallel refreshes).
    • Shadow::copy_stale: skips watch::parts_written, by_parts, and take_all when runs.is_empty() (preserving the ranged epoch update under watch::light()), and skips parts_written + by_parts when again_at.is_empty().
    • Shadow::take_all: replaces wide_each (Arc + Vec<Box<dyn FnOnce()>> + unsafe pointer cast + workers.queue contention when called nested inside refresh_front/flush_all) with zero-allocation wide_each_scoped(parts.len(), 1, ...).
    • Shadow::take & Shadow::take_landed: adds Shadow::changed_runs and batches self.kept invalidations, card.dirty updates (card_changed / card_changed_runs), and SEND[0..4] / ECHO[0..2] global atomic increments once after the page loop instead of locking kept/card.dirty and bouncing global atomic cache lines on every 64 KiB / 4 KiB page.
    • Added unit test shadow::landing_tests::take_batches_dirty_runs_for_whole_and_diff_paths covering whole, multi-page diffing, and unchanged paths.

Verification

  • cargo +1.98.1-x86_64-pc-windows-msvc test -p victory-gpu --lib --locked shadow:: -- --test-threads=1 -> pass (exit 0).
  • cargo +1.98.1-x86_64-pc-windows-msvc test -p victory-gpu --lib --locked watch:: -- --test-threads=1 -> 62 passed; 0 failed (exit 0).
  • cargo +1.98.1-x86_64-pc-windows-msvc clippy -p victory-gpu --lib --tests --locked -> 0 new warnings in shadow.rs and watch.rs.
  • 90s warm gameplay runs (RTX 4070 Ti + Ryzen 7 5700X, 1440p Series X, DLSS Balanced, combined perf stack, 0 WARNs / 0 ERRORs):
    • Native (145029.log -> 164041.log): Submit mean 37.77 -> 35.33 ms, copies_fills 4.40 -> 3.80 ms, FPS 23.05 / 23.13 -> 23.80 med / 24.30 mean (min 17.1, max 36.0).
    • DLSS FrameGen 2x (142528.log -> 164813.log): Submit mean 35.59 -> 33.66 ms, refreshes 11.40 -> 10.95 ms, copies_fills 3.90 -> 3.60 ms, base FPS 22.45 / 22.98 -> 23.45 med / 23.64 mean (~47 FPS displayed).
<!-- greyfinch-sync:trustno1-coder/VIctory-NVIDIA:issue:5551375868 --> Imported from [trustno1-coder/VIctory-NVIDIA](https://github.com/trustno1-coder/VIctory-NVIDIA/pull/2530). Originally posted by [qlfu](https://github.com/qlfu) on 2026-11-29T13:52:45Z. --- ## Summary Eliminates redundant lock acquisitions, per-page lock/atomic churn, and heap-allocating nested pool dispatch on the `Shadow` refresh hot path (`update_ranges`, `update_free`, `copy_stale`, `take_all`, `take`, `take_landed`) and `watch::parts_written`. ### What Changed - **`crates/victory-gpu/src/watch.rs`**: - Short-circuits `parts_written(&[])` before acquiring the global `JOURNAL` `RwLock` (avoids taking `journal()` on every no-op `copy_stale` refresh). - **`crates/victory-gpu/src/shadow.rs`**: - **`Shadow::update_ranges`**: gates `self.pending.lock()` and `busy` `Vec` allocation behind `self.has_pending.load(Ordering::Acquire)` (matching `nothing_new`, `new_look`, `nothing_new_with`, and `writes_pending_in`). - **`Shadow::update_free`**: checks `prev == GUARDED` **before** calling `crate::watch::guarded(from, to)` (avoiding `PROTECTED` bitmap queries on already-guarded copies) and skips storing back to `self.watched` when unchanged (avoiding cross-thread cache-line dirtying during parallel refreshes). - **`Shadow::copy_stale`**: skips `watch::parts_written`, `by_parts`, and `take_all` when `runs.is_empty()` (preserving the `ranged` epoch update under `watch::light()`), and skips `parts_written` + `by_parts` when `again_at.is_empty()`. - **`Shadow::take_all`**: replaces `wide_each` (`Arc` + `Vec<Box<dyn FnOnce()>>` + `unsafe` pointer cast + `workers.queue` contention when called nested inside `refresh_front`/`flush_all`) with zero-allocation `wide_each_scoped(parts.len(), 1, ...)`. - **`Shadow::take` & `Shadow::take_landed`**: adds `Shadow::changed_runs` and batches `self.kept` invalidations, `card.dirty` updates (`card_changed` / `card_changed_runs`), and `SEND[0..4]` / `ECHO[0..2]` global atomic increments once after the page loop instead of locking `kept`/`card.dirty` and bouncing global atomic cache lines on every 64 KiB / 4 KiB page. - Added unit test `shadow::landing_tests::take_batches_dirty_runs_for_whole_and_diff_paths` covering `whole`, multi-page diffing, and unchanged paths. ### Verification - `cargo +1.98.1-x86_64-pc-windows-msvc test -p victory-gpu --lib --locked shadow:: -- --test-threads=1` -> pass (exit `0`). - `cargo +1.98.1-x86_64-pc-windows-msvc test -p victory-gpu --lib --locked watch:: -- --test-threads=1` -> `62 passed; 0 failed` (exit `0`). - `cargo +1.98.1-x86_64-pc-windows-msvc clippy -p victory-gpu --lib --tests --locked` -> `0` new warnings in `shadow.rs` and `watch.rs`. - 90s warm gameplay runs (RTX 4070 Ti + Ryzen 7 5700X, 1440p Series X, DLSS Balanced, combined perf stack, `0 WARNs / 0 ERRORs`): - Native (`145029.log` -> `164041.log`): `Submit` mean **`37.77 -> 35.33 ms`**, `copies_fills` **`4.40 -> 3.80 ms`**, `FPS` **`23.05 / 23.13 -> 23.80 med / 24.30 mean`** (`min 17.1`, `max 36.0`). - DLSS FrameGen 2x (`142528.log` -> `164813.log`): `Submit` mean **`35.59 -> 33.66 ms`**, `refreshes` **`11.40 -> 10.95 ms`**, `copies_fills` **`3.90 -> 3.60 ms`**, base `FPS` **`22.45 / 22.98 -> 23.45 med / 23.64 mean`** (~47 FPS displayed).
rgroenewoudt commented 2026-11-29 19:48:55 +00:00 (Migrated from github.com)

Imported from trustno1-coder/VIctory-NVIDIA. Originally posted by rgroenewoudt on 2026-11-29T19:48:55Z.


In-game A/B on an RX 9070 (Series X, FSR 4 upscaling, release build): this PR is about 9 ms a frame slower here.

Setup: the PR's commit cherry-picked onto the same base, built in a separate worktree, launched with identical switches and the same pipeline library. Same spot in the world, roughly a minute in game per run. The numbers are medians of the per-second log lines, without the first 15 seconds in game.

Run Code Frame Submitting thread Lists' steps Refreshes
A base 52.1 ms 50.8 ms 26.8 ms 18.8 ms
C base, built in the worktree (control) 52.9 ms 51.9 ms 27.4 ms 19.7 ms
B this PR 60.8 ms 60.2 ms 32.4 ms 23.1 ms
B' this PR without the take_all change (wide_each kept) 61.9 ms 59.4 ms 32.6 ms 22.8 ms
  • A and C match, so the worktree build and environment are not the cause.
  • The work done is about the same in all four runs: about 46 refresh flushes and about 6,000 refreshes known to copy nothing a frame, 44-47 MB sent up, the same hash and compare passes. The same work just takes longer.
  • Leaving out the wide_each → wide_each_scoped change in take_all does not change the result, so that change is not the cause. Reading the rest of the diff, I found no clear cause.

One run per variant, but the gap (~9 ms) is about nine times the difference between the two base runs.

<!-- greyfinch-sync:trustno1-coder/VIctory-NVIDIA:comment:7261577184 --> Imported from [trustno1-coder/VIctory-NVIDIA](https://github.com/trustno1-coder/VIctory-NVIDIA/pull/2530#issuecomment-7261577184). Originally posted by [rgroenewoudt](https://github.com/rgroenewoudt) on 2026-11-29T19:48:55Z. --- In-game A/B on an RX 9070 (Series X, FSR 4 upscaling, release build): this PR is about 9 ms a frame **slower** here. Setup: the PR's commit cherry-picked onto the same base, built in a separate worktree, launched with identical switches and the same pipeline library. Same spot in the world, roughly a minute in game per run. The numbers are medians of the per-second log lines, without the first 15 seconds in game. | Run | Code | Frame | Submitting thread | Lists' steps | Refreshes | |---|---|---|---|---|---| | A | base | 52.1 ms | 50.8 ms | 26.8 ms | 18.8 ms | | C | base, built in the worktree (control) | 52.9 ms | 51.9 ms | 27.4 ms | 19.7 ms | | B | this PR | 60.8 ms | 60.2 ms | 32.4 ms | 23.1 ms | | B' | this PR without the `take_all` change (`wide_each` kept) | 61.9 ms | 59.4 ms | 32.6 ms | 22.8 ms | - A and C match, so the worktree build and environment are not the cause. - The work done is about the same in all four runs: about 46 refresh flushes and about 6,000 refreshes known to copy nothing a frame, 44-47 MB sent up, the same hash and compare passes. The same work just takes longer. - Leaving out the `wide_each` → `wide_each_scoped` change in `take_all` does not change the result, so that change is not the cause. Reading the rest of the diff, I found no clear cause. One run per variant, but the gap (~9 ms) is about nine times the difference between the two base runs.
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!2530
No description provided.