Shadows & watch: fused AVX2 word_runs_and_update, linear stale_pages_in, and sorted Journal::ahead #1637

Manually merged
github merged 1 commit from github-bot/gh-1637 into main 2026-11-29 08:21:05 +00:00 AGit
Owner

@⁠Bazovsky on GitHub · 2026-11-28 22:45 UTC · original
Bazovsky:pr/shadow-watch-fused → main · +255 −28 · 2 files · 1 commits · merged 2026-11-29 08:16 UTC

Fixes #

What it changes

Cuts CPU work in shadow CPU-side diff/copy (under) and write-watch / stale-page lookups (ref):

  • Fused single-pass AVX2 word_runs_and_update (crates/victory-gpu/src/shadow.rs): when updating card-backed shadow copies in Shadow::take, compares title against held and writes updated 32-byte vectors to held in one AVX2 pass instead of running word_runs and then re-scanning with copy_from_slice. Falls back to dword_runs + copy_from_slice on unaligned or non-AVX2 paths.
  • Linear O(N) stale_pages_in (crates/victory-gpu/src/shadow.rs): advances a single linear cursor over the sorted written slice after one initial partition_point instead of binary-searching written for every page in lo..hi.
  • Sorted Journal::ahead + ahead_contains binary search (crates/victory-gpu/src/watch.rs): keeps Journal::ahead sorted and non-touching across ask_ahead and ask_rest, uses ahead_contains (linear for <= 8 entries, partition_point above), and in ask_rest tries merged kernel_written first with a per-piece fallback so a touching unwatched or separate reservation never prevents caching its watched neighbour.
  • Tests: expanded word_runs_and_update_matches_word_runs_and_copies_exactly (aligned random, unaligned/odd fallback, all-differ, none-differ), expanded pages_never_taken_are_stale_written_or_not (cursor skips below lo, gaps, and past hi), and added ask_rest_falls_back_per_piece_when_touching_unwatched_neighbour and ahead_contains_matches_linear_over_binary_threshold.

How it was tested

  • GPU and profile (Series X or S, resolution, DLSS, frame generation, ray tracing): NVIDIA RTX 5090 32 GB (driver 610.88), AMD Ryzen 9 9950X3D (16C/32T, 96 MB 3D V-Cache), 62 GB RAM, Windows 11; Series X profile, 1440p SDR, DLSS Quality, Frame Generation OFF, RT default. Identical release builds (RUSTFLAGS="-C target-cpu=znver5") on clean origin/main (d4b5d51), 90 s same-session A/B runs (aligned steady window DLSS frames 100..1800, 0 D3D12 errors).
  • Before (d4b5d51): median 26.80 ms (37.3 fps), p95 36.30 ms, Prep 26.05 ms (ref=7.50 ms, under=2.05 ms), GPU busy 63%, 1,839 DLSS frames in 90 s.
  • After (pr/shadow-watch-fused, b366bb9): median 25.40 ms (39.4 fps, +2.1 fps / +5.6%), p95 32.55 ms (-3.75 ms), Prep 24.40 ms (-1.65 ms) (ref=6.95 ms (-7.3%), under=1.45 ms (-29.3%)), GPU busy 67%, 1,908 DLSS frames (+69) in 90 s.
  • For shader changes, the wave sizes tested (32 and/or 64 lanes): N/A (no shader changes).
  • cargo test passed for the crates it touches: yes (victory-gpu: 553 passed, 0 failed — 514 non-watch + 39 watch::tests serial; clippy + --features diagnostics check clean with 0 new warnings).

Checked for overlap

  • I searched the open pull requests for the same issue and for the files this touches. Related: #1556 (touches binary search over Journal::ahead in watch.rs; no open PR touches fused AVX2 word_runs_and_update or linear stale_pages_in in shadow.rs).
**@⁠Bazovsky** on GitHub · 2026-11-28 22:45 UTC · [original](https://github.com/trustno1-coder/VIctory-NVIDIA/pull/1637) `Bazovsky:pr/shadow-watch-fused` → `main` · +255 −28 · 2 files · 1 commits · merged 2026-11-29 08:16 UTC Fixes # ## What it changes Cuts CPU work in shadow CPU-side diff/copy (`under`) and write-watch / stale-page lookups (`ref`): - **Fused single-pass AVX2 `word_runs_and_update` (`crates/victory-gpu/src/shadow.rs`)**: when updating card-backed shadow copies in `Shadow::take`, compares `title` against `held` and writes updated 32-byte vectors to `held` in one AVX2 pass instead of running `word_runs` and then re-scanning with `copy_from_slice`. Falls back to `dword_runs` + `copy_from_slice` on unaligned or non-AVX2 paths. - **Linear O(N) `stale_pages_in` (`crates/victory-gpu/src/shadow.rs`)**: advances a single linear cursor over the sorted `written` slice after one initial `partition_point` instead of binary-searching `written` for every page in `lo..hi`. - **Sorted `Journal::ahead` + `ahead_contains` binary search (`crates/victory-gpu/src/watch.rs`)**: keeps `Journal::ahead` sorted and non-touching across `ask_ahead` and `ask_rest`, uses `ahead_contains` (linear for `<= 8` entries, `partition_point` above), and in `ask_rest` tries merged `kernel_written` first with a per-piece fallback so a touching unwatched or separate reservation never prevents caching its watched neighbour. - **Tests**: expanded `word_runs_and_update_matches_word_runs_and_copies_exactly` (aligned random, unaligned/odd fallback, all-differ, none-differ), expanded `pages_never_taken_are_stale_written_or_not` (cursor skips below `lo`, gaps, and past `hi`), and added `ask_rest_falls_back_per_piece_when_touching_unwatched_neighbour` and `ahead_contains_matches_linear_over_binary_threshold`. ## How it was tested - GPU and profile (Series X or S, resolution, DLSS, frame generation, ray tracing): NVIDIA RTX 5090 32 GB (driver 610.88), AMD Ryzen 9 9950X3D (16C/32T, 96 MB 3D V-Cache), 62 GB RAM, Windows 11; Series X profile, 1440p SDR, DLSS Quality, Frame Generation OFF, RT default. Identical release builds (`RUSTFLAGS="-C target-cpu=znver5"`) on clean `origin/main` (`d4b5d51`), 90 s same-session A/B runs (aligned steady window DLSS frames 100..1800, 0 D3D12 errors). - Before (`d4b5d51`): median **26.80 ms (37.3 fps)**, p95 **36.30 ms**, Prep **26.05 ms** (`ref=7.50 ms`, `under=2.05 ms`), GPU busy **63%**, **1,839** DLSS frames in 90 s. - After (`pr/shadow-watch-fused`, `b366bb9`): median **25.40 ms (39.4 fps, +2.1 fps / +5.6%)**, p95 **32.55 ms (-3.75 ms)**, Prep **24.40 ms (-1.65 ms)** (`ref=6.95 ms (-7.3%)`, `under=1.45 ms (-29.3%)`), GPU busy **67%**, **1,908** DLSS frames (+69) in 90 s. - For shader changes, the wave sizes tested (32 and/or 64 lanes): N/A (no shader changes). - `cargo test` passed for the crates it touches: yes (`victory-gpu`: 553 passed, 0 failed — 514 non-watch + 39 `watch::tests` serial; `clippy` + `--features diagnostics` check clean with 0 new warnings). ## Checked for overlap - [x] I searched the open pull requests for the same issue and for the files this touches. Related: #1556 (touches binary search over `Journal::ahead` in `watch.rs`; no open PR touches fused AVX2 `word_runs_and_update` or linear `stale_pages_in` in `shadow.rs`).
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-28 22:46:40 +00:00
Author
Owner

@⁠POMXARK on GitHub · 2026-11-28 23:42 UTC · original

@⁠Bazovsky — ran a 20-minute gameplay A/B of the shadow.rs portion (fused AVX2 word_runs_and_update + linear O(N) stale_pages_in, since our stack already carries ahead_normalize/ahead_holds from #1094) on Windows 11 (RTX 5080 16 GB, Ryzen 7 5700X 8C/16T, 32 GB RAM, Series X 1440p, DLSS Quality + MFG 4x, release build, matched switches):

Verification

  • Unit tests: word_runs_and_update_matches_word_runs_and_copies_exactly + dword_runs + word_runs 3/3 PASS (0.00s).
  • Stability & Visuals: 0 errors over 20 minutes, refused=2, clean visuals across all 16 captured screenshots.

20-minute A/B (with draw-bucket & copy-throughput normalization)

Bucket Baseline (R27) PR #1637 (shadow.rs) Throughput delta (MB/ms)
4k–7k draws (n=50 vs n=51) 13.00 FPS (77.2 ms, cop 30.1 ms / 58.2 MB = 1.93 MB/ms) 13.70 FPS (73.0 ms, cop 26.6 ms / 53.6 MB = 2.02 MB/ms) +4.2% MB/ms (+5.4% FPS)
7k–10k draws (n=12 vs n=31) 10.95 FPS (cop 33.5 ms / 68.8 MB = 2.05 MB/ms) 9.70 FPS (cop 40.6 ms / 85.9 MB = 2.12 MB/ms) +3.0% MB/ms
under (putting title's bytes under flights) 3.4 ms 3.3 ms -0.1 ms
Peak Shared / Commit 10,525 / 32,351 MB 9,622 / 31,456 MB -903 / -895 MB

(Note: overall unweighted ALL 3D p50 differed (15.30 vs 13.55 FPS) because the 20m route spent more samples in the heavier 7k–10k bucket (n=31 vs n=12); within the matched 4k–7k bucket and in normalized copy throughput (MB/ms), fused AVX2 word_runs_and_update + linear stale_pages_in improved copy efficiency by +3.0% to +4.2% with 0 errors and lower commit!)

**@⁠POMXARK** on GitHub · 2026-11-28 23:42 UTC · [original](https://github.com/trustno1-coder/VIctory-NVIDIA/pull/1637#issuecomment-7247615044) @⁠Bazovsky — ran a 20-minute gameplay A/B of the `shadow.rs` portion (fused AVX2 `word_runs_and_update` + linear O(N) `stale_pages_in`, since our stack already carries `ahead_normalize`/`ahead_holds` from `#1094`) on Windows 11 (RTX 5080 16 GB, Ryzen 7 5700X 8C/16T, 32 GB RAM, Series X 1440p, DLSS Quality + MFG 4x, release build, matched switches): ### Verification - **Unit tests:** `word_runs_and_update_matches_word_runs_and_copies_exactly` + `dword_runs` + `word_runs` **3/3 PASS** (`0.00s`). - **Stability & Visuals:** `0` errors over 20 minutes, `refused=2`, clean visuals across all 16 captured screenshots. ### 20-minute A/B (with draw-bucket & copy-throughput normalization) | Bucket | Baseline (R27) | PR #1637 (`shadow.rs`) | Throughput delta (`MB/ms`) | |---|---|---|---:| | **4k–7k draws** (`n=50` vs `n=51`) | 13.00 FPS (`77.2 ms`, `cop 30.1 ms / 58.2 MB` = **1.93 MB/ms**) | **13.70 FPS** (`73.0 ms`, `cop 26.6 ms / 53.6 MB` = **2.02 MB/ms**) | **+4.2% MB/ms (`+5.4% FPS`)** | | **7k–10k draws** (`n=12` vs `n=31`) | 10.95 FPS (`cop 33.5 ms / 68.8 MB` = **2.05 MB/ms**) | 9.70 FPS (`cop 40.6 ms / 85.9 MB` = **2.12 MB/ms**) | **+3.0% MB/ms** | | `under` (`putting title's bytes under flights`) | 3.4 ms | **3.3 ms** | **-0.1 ms** | | Peak Shared / Commit | 10,525 / 32,351 MB | **9,622 / 31,456 MB** | **-903 / -895 MB** | *(Note: overall unweighted `ALL 3D p50` differed (`15.30` vs `13.55 FPS`) because the 20m route spent more samples in the heavier `7k–10k` bucket (`n=31` vs `n=12`); within the matched `4k–7k` bucket and in normalized copy throughput (`MB/ms`), fused AVX2 `word_runs_and_update` + linear `stale_pages_in` improved copy efficiency by **+3.0% to +4.2%** with 0 errors and lower commit!)*
Author
Owner

@⁠unueberlegt on GitHub · 2026-11-29 01:23 UTC · original

Tested this part (sorted, disjoint Journal::ahead + partition_point containment) standalone on our side while working #1521: implemented the same shape (sort + join touching ranges at both writers, binary search above 8 entries, linear below) and measured it in isolation on a machine where ahead holds only dozens of ranges: RTX 5070 12 GB, Ryzen 9 9950X3D (16C/32T), Series X 2560x1440, prologue, steady windows, medians. The refresh/journal numbers moved within scene noise there (7.0 vs 7.2 ms refreshes/frame against baseline) — expected: the linear scan over dozens of in-cache ranges was already cheap at this core count, so the win case remains the issue reporter's 8-core CPU-bound setup, where the per-span scan was cache-cold. The design matches ours, for what a second implementation is worth: the join has to include touching (not only overlapping) ranges, or a probe across two former neighbours answers falsely negative — ours joined them and the containment stayed exact.

GPU and profile: NVIDIA RTX 5070 12 GB, AMD Ryzen 9 9950X3D, 31 GB RAM, Windows 11 26300; Series X profile, 2560x1440, release builds, steady-window medians.

**@⁠unueberlegt** on GitHub · 2026-11-29 01:23 UTC · [original](https://github.com/trustno1-coder/VIctory-NVIDIA/pull/1637#issuecomment-7248749248) Tested this part (sorted, disjoint `Journal::ahead` + `partition_point` containment) standalone on our side while working #1521: implemented the same shape (sort + join touching ranges at both writers, binary search above 8 entries, linear below) and measured it in isolation on a machine where `ahead` holds only dozens of ranges: RTX 5070 12 GB, Ryzen 9 9950X3D (16C/32T), Series X 2560x1440, prologue, steady windows, medians. The refresh/journal numbers moved within scene noise there (7.0 vs 7.2 ms refreshes/frame against baseline) — expected: the linear scan over dozens of in-cache ranges was already cheap at this core count, so the win case remains the issue reporter's 8-core CPU-bound setup, where the per-span scan was cache-cold. The design matches ours, for what a second implementation is worth: the join has to include touching (not only overlapping) ranges, or a probe across two former neighbours answers falsely negative — ours joined them and the containment stayed exact. GPU and profile: NVIDIA RTX 5070 12 GB, AMD Ryzen 9 9950X3D, 31 GB RAM, Windows 11 26300; Series X profile, 2560x1440, release builds, steady-window medians.
github-bot force-pushed github-bot/gh-1637 from b366bb9143 to 03d5f57097 2026-11-29 01:46:54 +00:00 Compare
Author
Owner

@⁠Bazovsky on GitHub · 2026-11-29 01:43 UTC · original

Thanks @⁠POMXARK for the 20-minute shadow.rs A/B on Ryzen 7 5700X + RTX 5080 (+3.0% to +4.2% copy throughput MB/ms, +5.4% FPS in matched 4k–7k draws, 0 errors), and thanks @⁠unueberlegt for independently confirming the sorted/disjoint Journal::ahead + touching-range join invariant while working #1521 — great to have two independent implementations converge on the exact same semantics.

Rebased onto origin/main (d585f88 -> 03d5f57), preserving #1062's owned checks in ask_rest/asked/writes/nothing_new and 0174844's Copied lock changes. Verified with clippy (default + --features diagnostics, 0 new warnings) and native Windows victory-gpu tests (592/592 pass).

**@⁠Bazovsky** on GitHub · 2026-11-29 01:43 UTC · [original](https://github.com/trustno1-coder/VIctory-NVIDIA/pull/1637#issuecomment-7248971555) Thanks @⁠POMXARK for the 20-minute `shadow.rs` A/B on Ryzen 7 5700X + RTX 5080 (+3.0% to +4.2% copy throughput `MB/ms`, +5.4% FPS in matched 4k–7k draws, 0 errors), and thanks @⁠unueberlegt for independently confirming the sorted/disjoint `Journal::ahead` + touching-range join invariant while working #1521 — great to have two independent implementations converge on the exact same semantics. Rebased onto `origin/main` (`d585f88` -> `03d5f57`), preserving #1062's `owned` checks in `ask_rest`/`asked`/`writes`/`nothing_new` and `0174844`'s `Copied` lock changes. Verified with `clippy` (default + `--features diagnostics`, 0 new warnings) and native Windows `victory-gpu` tests (592/592 pass).
Author
Owner

@⁠POMXARK on GitHub · 2026-11-29 03:00 UTC · original

@⁠Bazovsky — quick follow-up data point on watch.rs (GetWriteWatch) + xmem.rs from our synthetic repro + 20-minute 3D A/B on Windows 11 (Ryzen 7 5700X 8C/16T + RTX 5080 16 GB, 1440p DLSS Quality + 4x FG, 0 errors):

  1. Why AHEAD_GAP = 8 MiB hurts CPU recording threads on Windows 11:
    In a real Win32 VirtualAlloc(MEM_WRITE_WATCH) microbenchmark (174 MiB heap, 160 clustered 256 KiB spans, 4,200 dirty 4 KiB pages/f), GetWriteWatch(WRITE_WATCH_FLAG_RESET) with AHEAD_GAP = 8 MiB resets untouched gap pages (4,200 vs 987), causing next-frame CPU writes to re-trap into the Windows kernel (~560 ns/page). Shrinking AHEAD_GAP to 512 KiB cut combined GetWriteWatch + next-frame CPU write time by -32% (2.85 -> 1.94 ms/f).
  2. Skipping MEM_WRITE_WATCH on strictly CPU-only XMEM_CPU allocations (flags & 2 == 0 and no PAGE_GRAPHICS_* bits):
    On a 128 MiB CPU-only buffer (8,000 scattered 4 KiB page writes/f), omitting MEM_WRITE_WATCH sped up CPU writes by 76x (4.478 -> 0.059 ms/f).
  3. 20-minute 3D A/B (AHEAD_GAP=512 KiB + unwatched XMEM_CPU, matched heavy open-world trajectory, 0 errors):
    • GetWriteWatch (ww): 6.7 ms (p90 16.9 ms) → 5.6 ms (p90 10.1 ms) (-16.4% med / -40.2% p90)
    • Submitting prep (SUB): 67.1 → 60.5 ms (-6.6 ms)
    • ALL 3D median: 13.60 → 14.15 FPS (+4.0%), 7k–10k: 10.20 → 10.60 FPS (+3.9%), >=10k: 8.80 → 8.90 FPS
    • Also verified in isolation that Windows 11 serializes GetWriteWatch across threads (1T=0.64 ms, 4T=0.61 ms, 8T=0.66 ms), so shrinking the reset footprint beats parallelizing queries.

Clean-room writeup & standalone repro suite: https://github.com/POMXARK/bcpack-gpu-hlsl/blob/main/docs/PERF_REPRO_AND_TUNING.md

**@⁠POMXARK** on GitHub · 2026-11-29 03:00 UTC · [original](https://github.com/trustno1-coder/VIctory-NVIDIA/pull/1637#issuecomment-7249780889) @⁠Bazovsky — quick follow-up data point on `watch.rs` (`GetWriteWatch`) + `xmem.rs` from our synthetic repro + 20-minute 3D A/B on Windows 11 (Ryzen 7 5700X 8C/16T + RTX 5080 16 GB, 1440p DLSS Quality + 4x FG, `0` errors): 1. **Why `AHEAD_GAP = 8 MiB` hurts CPU recording threads on Windows 11:** In a real Win32 `VirtualAlloc(MEM_WRITE_WATCH)` microbenchmark (`174 MiB` heap, `160` clustered `256 KiB` spans, `4,200` dirty 4 KiB pages/f), `GetWriteWatch(WRITE_WATCH_FLAG_RESET)` with `AHEAD_GAP = 8 MiB` resets untouched gap pages (`4,200` vs `987`), causing next-frame CPU writes to re-trap into the Windows kernel (~560 ns/page). Shrinking `AHEAD_GAP` to **`512 KiB`** cut combined `GetWriteWatch + next-frame CPU write` time by **`-32%`** (`2.85 -> 1.94 ms/f`). 2. **Skipping `MEM_WRITE_WATCH` on strictly CPU-only `XMEM_CPU` allocations (`flags & 2 == 0` and no `PAGE_GRAPHICS_*` bits):** On a `128 MiB` CPU-only buffer (`8,000` scattered 4 KiB page writes/f), omitting `MEM_WRITE_WATCH` sped up CPU writes by **`76x`** (`4.478 -> 0.059 ms/f`). 3. **20-minute 3D A/B (`AHEAD_GAP=512 KiB` + unwatched `XMEM_CPU`, matched heavy open-world trajectory, `0` errors):** - `GetWriteWatch` (`ww`): **`6.7 ms` (p90 `16.9 ms`) → `5.6 ms` (p90 `10.1 ms`)** (**`-16.4%` med / `-40.2%` p90**) - Submitting prep (`SUB`): **`67.1 → 60.5 ms` (`-6.6 ms`)** - `ALL 3D` median: **`13.60 → 14.15 FPS` (`+4.0%`)**, `7k–10k`: **`10.20 → 10.60 FPS` (`+3.9%`)**, `>=10k`: **`8.80 → 8.90 FPS`** - Also verified in isolation that Windows 11 serializes `GetWriteWatch` across threads (`1T=0.64 ms`, `4T=0.61 ms`, `8T=0.66 ms`), so shrinking the reset footprint beats parallelizing queries. Clean-room writeup & standalone repro suite: https://github.com/POMXARK/bcpack-gpu-hlsl/blob/main/docs/PERF_REPRO_AND_TUNING.md
Author
Owner

@⁠Bazovsky on GitHub · 2026-11-29 05:32 UTC · original

Thanks @⁠POMXARK for the GetWriteWatch / AHEAD_GAP (8 MiB at watch.rs:473) and unwatched XMEM_CPU measurements (ww 6.7 -> 5.6 ms med / -40.2% p90, ALL 3D +4.0%, and the Win11 GetWriteWatch serialization finding) plus the clean-room repro!
I'll keep #1637 focused on shadow.rs (fused AVX2 word_runs_and_update + linear stale_pages_in) and sorted Journal::ahead so the diff stays easy to review; tuning AHEAD_GAP and skipping MEM_WRITE_WATCH on CPU-only XMEM_CPU allocations look like a strong, well-scoped separate follow-up PR.

**@⁠Bazovsky** on GitHub · 2026-11-29 05:32 UTC · [original](https://github.com/trustno1-coder/VIctory-NVIDIA/pull/1637#issuecomment-7251312282) Thanks @⁠POMXARK for the `GetWriteWatch` / `AHEAD_GAP` (`8 MiB` at `watch.rs:473`) and unwatched `XMEM_CPU` measurements (`ww` `6.7 -> 5.6 ms` med / `-40.2%` p90, `ALL 3D +4.0%`, and the Win11 `GetWriteWatch` serialization finding) plus the clean-room repro! I'll keep #1637 focused on `shadow.rs` (fused AVX2 `word_runs_and_update` + linear `stale_pages_in`) and sorted `Journal::ahead` so the diff stays easy to review; tuning `AHEAD_GAP` and skipping `MEM_WRITE_WATCH` on CPU-only `XMEM_CPU` allocations look like a strong, well-scoped separate follow-up PR.
Author
Owner

@⁠amazedturnip on GitHub · 2026-11-29 05:38 UTC · original

Tested on 9800X3D (AVX2) + RTX 5070 Ti 16 GB in a combined perf stack across 20+ min sessions. Zero shadow/watch regressions; combined stack cut write-watch time from ~4.5 ms to ~1.9-2.3 ms/frame.

**@⁠amazedturnip** on GitHub · 2026-11-29 05:38 UTC · [original](https://github.com/trustno1-coder/VIctory-NVIDIA/pull/1637#issuecomment-7251375501) Tested on 9800X3D (AVX2) + RTX 5070 Ti 16 GB in a combined perf stack across 20+ min sessions. Zero shadow/watch regressions; combined stack cut write-watch time from ~4.5 ms to ~1.9-2.3 ms/frame.
github manually merged commit ae04968c87 into main 2026-11-29 08:21:05 +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!1637
No description provided.