perf(shadow): batch card.dirty in merge_under_writes and binary-search skip in land_card #1400

Manually merged
github merged 1 commit from github-bot/gh-1400 into main 2026-11-28 19:42:03 +00:00 AGit
Owner

@⁠qlfu on GitHub · 2026-11-28 18:59 UTC · original
qlfu:pr/merge-under-writes-perf → main · +175 −45 · 1 files · 1 commits · merged 2026-11-28 19:39 UTC

Summary

Reduces lock contention, mutex overhead, and heap churn in Shadow::merge_under_writes and Shadow::land_card (crates/victory-gpu/src/shadow.rs):

  1. Lock-free per-page result slots (UnderSlots): replaces Vec<Mutex<Done>> (~280 mutexes allocated, locked, and unwrapped per frame) with a Sync UnsafeCell<PageMerge> slice where each worker writes exclusively to its own index i.
  2. Lazy initial capacity in word_runs_avx2: reserves 128 entries on first diff instead of growing 0 -> 4 -> 8 -> ... -> 128 across contending parallel workers (still 0 bytes allocated for unchanged pages).
  3. Single card.dirty lock + single reserve(total_runs) + minimized self.merging.write() critical section:
    • Computes per-page byte totals inside the parallel copy loop and aggregates MERGED[0..3] once outside self.merging.write() (eliminating hundreds of cross-core atomic RMWs).
    • Acquires card.dirty.lock() once per busy range (instead of once per page), reserves total_runs once, coalesces via shared push_dirty_runs, swaps card.touched once, and drops before and self.merging.write() before updating self.copied and freeing the per-page Vecs.
  4. Sorted binary-search skip in Shadow::land_card: merges skip once outside card.dirty/card.sent locks and reuses a single parts buffer via outside_sorted_into (partition_point), avoiding per-piece linear scans and triple Vec allocations.

Correctness & Safety

  • self.merging.write() and self.before.lock() remain held from before reading title memory until both held and card.dirty / card.touched are updated (preserving exact synchronization with Shadow::land_card and Shadow::card_merge_relay).
  • Unchanged readable pages still mark copied[page - base] = now; over pages still count in MERGED[0] and trigger note_stale(2) identically to main.
  • Added unit tests relay_tests::outside_sorted_into_matches_outside and relay_tests::push_dirty_runs_coalesces_adjacent_and_overlapping_ranges.

Verification

  • cargo +1.98.1-x86_64-pc-windows-msvc clippy -p victory-gpu --lib --tests --locked and --all-features: 0 warnings in modified lines.
  • cargo +1.98.1-x86_64-pc-windows-msvc test -p victory-gpu --lib --locked -- --test-threads=1 --quiet: 456 passed, 0 failed, 36 ignored.
  • cargo +1.98.1-x86_64-pc-windows-msvc build --release --locked -p victory: exit 0.

Clean Idle-PC 90s A/B Benchmark (RTX 4070 Ti 12 GB + Ryzen 7 5700X, -FrameGen, vs origin/main)

Metric origin/main This PR Delta
under (merge_under_writes) median 6.60 ms (293.5 pgs / 73.5k runs) 5.60 ms (276.5 pgs / 71.8k runs) -1.00 ms (-15.2%; -13.2% per 1k runs)
under mean 6.57 ms 5.43 ms -1.14 ms (-17.4%)
Relay wait_under (blocked on self.merging) 2.13 ms 1.06 ms -1.07 ms (-50.2%)
Relay land_merge (land_card) 5.10 ms 4.71 ms -0.39 ms (-7.6%)
Relay land_all total 24.75 ms 21.35 ms -3.40 ms (-13.7%)
Submitting thread sub_all median 50.85 ms 49.00 ms -1.85 ms (-3.6%)
Frame time median / Median FPS 53.80 ms / 18.60 52.75 ms / 18.95 -1.05 ms / +1.9%
WARNs / ERRORs 0 / 0 0 / 0 Clean
**@⁠qlfu** on GitHub · 2026-11-28 18:59 UTC · [original](https://github.com/trustno1-coder/VIctory-NVIDIA/pull/1400) `qlfu:pr/merge-under-writes-perf` → `main` · +175 −45 · 1 files · 1 commits · merged 2026-11-28 19:39 UTC ## Summary Reduces lock contention, mutex overhead, and heap churn in `Shadow::merge_under_writes` and `Shadow::land_card` (`crates/victory-gpu/src/shadow.rs`): 1. **Lock-free per-page result slots (`UnderSlots`):** replaces `Vec<Mutex<Done>>` (~280 mutexes allocated, locked, and unwrapped per frame) with a `Sync` `UnsafeCell<PageMerge>` slice where each worker writes exclusively to its own index `i`. 2. **Lazy initial capacity in `word_runs_avx2`:** reserves `128` entries on first diff instead of growing `0 -> 4 -> 8 -> ... -> 128` across contending parallel workers (still 0 bytes allocated for unchanged pages). 3. **Single `card.dirty` lock + single `reserve(total_runs)` + minimized `self.merging.write()` critical section:** - Computes per-page byte totals inside the parallel copy loop and aggregates `MERGED[0..3]` once outside `self.merging.write()` (eliminating hundreds of cross-core atomic RMWs). - Acquires `card.dirty.lock()` **once** per busy range (instead of once per page), reserves `total_runs` once, coalesces via shared `push_dirty_runs`, swaps `card.touched` once, and drops `before` and `self.merging.write()` **before** updating `self.copied` and freeing the per-page `Vec`s. 4. **Sorted binary-search `skip` in `Shadow::land_card`:** merges `skip` once outside `card.dirty`/`card.sent` locks and reuses a single `parts` buffer via `outside_sorted_into` (`partition_point`), avoiding per-piece linear scans and triple `Vec` allocations. ### Correctness & Safety - `self.merging.write()` and `self.before.lock()` remain held from before reading title memory until both `held` and `card.dirty` / `card.touched` are updated (preserving exact synchronization with `Shadow::land_card` and `Shadow::card_merge_relay`). - Unchanged readable pages still mark `copied[page - base] = now`; `over` pages still count in `MERGED[0]` and trigger `note_stale(2)` identically to `main`. - Added unit tests `relay_tests::outside_sorted_into_matches_outside` and `relay_tests::push_dirty_runs_coalesces_adjacent_and_overlapping_ranges`. ## Verification - `cargo +1.98.1-x86_64-pc-windows-msvc clippy -p victory-gpu --lib --tests --locked` and `--all-features`: 0 warnings in modified lines. - `cargo +1.98.1-x86_64-pc-windows-msvc test -p victory-gpu --lib --locked -- --test-threads=1 --quiet`: **456 passed, 0 failed, 36 ignored**. - `cargo +1.98.1-x86_64-pc-windows-msvc build --release --locked -p victory`: exit `0`. ## Clean Idle-PC 90s A/B Benchmark (RTX 4070 Ti 12 GB + Ryzen 7 5700X, `-FrameGen`, vs `origin/main`) | Metric | `origin/main` | This PR | Delta | |---|---:|---:|---:| | `under` (`merge_under_writes`) median | `6.60 ms` (`293.5` pgs / `73.5k` runs) | **`5.60 ms`** (`276.5` pgs / `71.8k` runs) | **`-1.00 ms` (`-15.2%`; `-13.2%` per 1k runs)** | | `under` mean | `6.57 ms` | **`5.43 ms`** | **`-1.14 ms` (`-17.4%`)** | | Relay `wait_under` (blocked on `self.merging`) | `2.13 ms` | **`1.06 ms`** | **`-1.07 ms` (`-50.2%`)** | | Relay `land_merge` (`land_card`) | `5.10 ms` | **`4.71 ms`** | **`-0.39 ms` (`-7.6%`)** | | Relay `land_all` total | `24.75 ms` | **`21.35 ms`** | **`-3.40 ms` (`-13.7%`)** | | Submitting thread `sub_all` median | `50.85 ms` | **`49.00 ms`** | **`-1.85 ms` (`-3.6%`)** | | Frame time median / Median FPS | `53.80 ms` / `18.60` | **`52.75 ms` / `18.95`** | **`-1.05 ms` / `+1.9%`** | | WARNs / ERRORs | `0 / 0` | `0 / 0` | Clean |
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-28 19:02:22 +00:00
github-bot referenced this pull request from a commit 2026-11-28 19:41:56 +00:00
github manually merged commit f2784d61ad into main 2026-11-28 19:42:03 +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!1400
No description provided.