gpu/shadow: O(dirty) CopiedPages bitmask, lock-free mirrors & AVX2 merge/diff (2/4 of #51) #57

Open
github-bot wants to merge 1 commit from github-bot/gh-57 into beta AGit
Owner

@⁠Vladimir-Human on GitHub · 2026-11-26 00:39 UTC · original
Vladimir-Human:perf/split-copied-pages-avx2 → beta · +1342 −307 · 1 files · 1 commits · open

Split 2/4 of #51 cleanly on top of beta (independent of #56, Part 3 ReBAR, and Part 4 Lazy Copy-Back).

Summary of Changes (crates/victory-gpu/src/shadow.rs)

  • O(dirty) CopiedPages bitmask + SmallRuns inline coalescer: pairs per-page copied stamps with a 64-page-per-word uncopied_bits bitmask and inline 8-entry SmallRuns so Shadow::copy_stale skips clean spans 64 pages at a time, plans coalesced copy runs under self.copied, and releases the mutex before issuing memory copies.
  • Lock-free fast-path mirrors: adds Shadow::has_pending (AtomicBool) and Card::{has_dirty, has_sent} (AtomicBools with Release/Acquire ordering in card_dirty and card_merge_relay) to avoid mutex traffic on read-only windows and clean carded copies.
  • AVX2 merge/diff kernels with scalar fallbacks: adds has_avx2() runtime dispatch for first_difference, last_difference, changed_runs, merge_piece, merge_sync_card, and merge_back_piece, eliminating back_piece's per-piece mask allocation.
  • Lock-free WideDone worker completion, panic propagation, and TLS scratch reuse: moves copy_wide and Shadow::differing onto the kept worker pool with WideDone, adaptive WIDE_MIN_SPLIT batching, nested IN_WIDE reentrancy guard, and SCRATCH_TITLE / parts+rest buffer reuse.
  • Unit tests & opt-in microbenchmark: copied_and_simd_tests::{plan_stale_matches_oracle_across_boundaries_and_partial_init, simd_diff_and_merge_helpers_match_scalar_oracles, nested_wide_runs_without_deadlock, wide_propagates_worker_panic_without_deadlocking_pool} and #[ignore] bench_copied_and_simd.

Verification (VICTORY_D3D12_DEBUG=1)

  • cargo test -p victory-gpu -> passed
  • cargo test -p victory-gpu --all-features -> passed
  • cargo run -p xtask -- policy --staged -> asset policy: 1 files ok (shadow.rs = 222,729 B)

Related: #51, #56

**@⁠Vladimir-Human** on GitHub · 2026-11-26 00:39 UTC · [original](https://github.com/PastiiJay/klbf-lab/pull/57) `Vladimir-Human:perf/split-copied-pages-avx2` → `beta` · +1342 −307 · 1 files · 1 commits · open Split 2/4 of #51 cleanly on top of `beta` (independent of #56, Part 3 ReBAR, and Part 4 Lazy Copy-Back). ### Summary of Changes (`crates/victory-gpu/src/shadow.rs`) - **O(dirty) `CopiedPages` bitmask + `SmallRuns` inline coalescer**: pairs per-page copied stamps with a 64-page-per-word `uncopied_bits` bitmask and inline 8-entry `SmallRuns` so `Shadow::copy_stale` skips clean spans 64 pages at a time, plans coalesced copy runs under `self.copied`, and releases the mutex before issuing memory copies. - **Lock-free fast-path mirrors**: adds `Shadow::has_pending` (`AtomicBool`) and `Card::{has_dirty, has_sent}` (`AtomicBool`s with Release/Acquire ordering in `card_dirty` and `card_merge_relay`) to avoid mutex traffic on read-only windows and clean carded copies. - **AVX2 merge/diff kernels with scalar fallbacks**: adds `has_avx2()` runtime dispatch for `first_difference`, `last_difference`, `changed_runs`, `merge_piece`, `merge_sync_card`, and `merge_back_piece`, eliminating `back_piece`'s per-piece `mask` allocation. - **Lock-free `WideDone` worker completion, panic propagation, and TLS scratch reuse**: moves `copy_wide` and `Shadow::differing` onto the kept worker pool with `WideDone`, adaptive `WIDE_MIN_SPLIT` batching, nested `IN_WIDE` reentrancy guard, and `SCRATCH_TITLE` / `parts`+`rest` buffer reuse. - **Unit tests & opt-in microbenchmark**: `copied_and_simd_tests::{plan_stale_matches_oracle_across_boundaries_and_partial_init, simd_diff_and_merge_helpers_match_scalar_oracles, nested_wide_runs_without_deadlock, wide_propagates_worker_panic_without_deadlocking_pool}` and `#[ignore] bench_copied_and_simd`. ### Verification (`VICTORY_D3D12_DEBUG=1`) - `cargo test -p victory-gpu` -> passed - `cargo test -p victory-gpu --all-features` -> passed - `cargo run -p xtask -- policy --staged` -> `asset policy: 1 files ok` (`shadow.rs` = 222,729 B) Related: #51, #56
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 05:53:54 +00:00
Author
Owner

@⁠HandshakeHackin on GitHub · 2026-11-26 02:53 UTC · original

I tested ea3f336 on a Windows x64 runner that confirmed AVX2 was available. Across 22,380 placements of a changed byte range (including empty spans) in 31–97 byte buffers, the first/last differences and exact changed runs matched the expected values. With CPU edits at bytes 30, 31, 32, 63 and 64 where present, the copy-back merge also preserved those edits, returned the expected disjoint ranges, and left the host and title buffers byte-identical to the oracle. This exercises all-changed 32-byte blocks, ends of vectors and scalar tails using synthetic bytes. Test and log

**@⁠HandshakeHackin** on GitHub · 2026-11-26 02:53 UTC · [original](https://github.com/PastiiJay/klbf-lab/pull/57#issuecomment-7181202783) I tested `ea3f336` on a Windows x64 runner that confirmed AVX2 was available. Across 22,380 placements of a changed byte range (including empty spans) in 31–97 byte buffers, the first/last differences and exact changed runs matched the expected values. With CPU edits at bytes 30, 31, 32, 63 and 64 where present, the copy-back merge also preserved those edits, returned the expected disjoint ranges, and left the host and title buffers byte-identical to the oracle. This exercises all-changed 32-byte blocks, ends of vectors and scalar tails using synthetic bytes. [Test and log](https://github.com/HandshakeHackin/klbf-lab/actions/runs/45744902455/job/139658477513)
Author
Owner

@⁠PastiiJay on GitHub · 2026-11-26 10:42 UTC · original

Thanks for the 22k-placement check against the oracle, that's thorough. We'll test the AVX2 path in game.

**@⁠PastiiJay** on GitHub · 2026-11-26 10:42 UTC · [original](https://github.com/PastiiJay/klbf-lab/pull/57#issuecomment-7189720479) Thanks for the 22k-placement check against the oracle, that's thorough. We'll test the AVX2 path in game.
Author
Owner

@⁠Proudnoob4 on GitHub · 2026-11-26 14:39 UTC · original

Live in-game GTA 6 runtime oracle audit + 10-minute benchmark (0 mismatches, -65% noting the runs, -82% finding the pages)

We ported this full shadow/watch optimization stack (O(dirty) TOUCHED_CARDS, 64-page-per-word CopiedPages bitmask, lock-free has_pending, direct merge_under_writes, FOUND_LOOSE thread-local cache, in-place joined(), and AVX2 first_difference / last_difference / merge_piece / merge_sync_card / changed_runs) into our RTX 4080 / i5-13600K (Windows 10) GTA 6 Series S build and verified it both for live correctness and steady-state performance:

  1. 10-minute live GTA 6 runtime oracle audit (31,249 log lines, real gameplay frames):

    • Instrumented all 7 fast paths (AVX2 first_difference/last_difference/changed_runs/merge_piece/merge_sync_card, TOUCHED_CARDS dirty scan, CopiedPages bitmask, FOUND_LOOSE cache, and in-place joined()) to compare against the scalar / stock reference on live frames.
    • Result: 0 mismatches (ERROR=0) across the entire 10-minute live run.
  2. 10-minute release benchmark (RTX 4080 16 GB, i5-13600K, Win10, render_scale=0.6667, VICTORY_CARD_BUFFERS_MIN_KB=64, VICTORY_FETCH_CHECK=1, VICTORY_SKIP_WC_MIN_KB=1024, RT skipped):

    • VICTORY_LATE_WAIT=2 (212 pages looked at, 77,290 changed runs, 2.38 MB):
      • finding the pages: ~0.28 ms -> 0.05 ms (-82%)
      • noting the runs: ~3.7 ms -> 1.28 ms (-65%)
      • comparing pages: 1.93 ms wall (1.65 ms reading + 6.71 ms comparing/putting in across worker threads)
    • End-to-end settled performance (n=104, tail-40 n=40):
      • 32.00 fps median (31.50 p10, 32.60 p90, 32.70 tail-40 max, 33.20 overall max; 0 tail frames >55 ms)
      • 4,505 draws/frame, 317 pieces landed/frame (4.5 MB dirty 4 KiB pages, 0.84 MB dwords)
      • Visual verification (t=140..600s): 0/230 black frames, 120/120 clean consecutive burst frames (4 fps contact sheets at t=420–435s and t=520–535s).

The AVX2 paths and O(dirty) bookkeeping held up 100% clean in real GTA 6 gameplay!

**@⁠Proudnoob4** on GitHub · 2026-11-26 14:39 UTC · [original](https://github.com/PastiiJay/klbf-lab/pull/57#issuecomment-7195133760) ### Live in-game GTA 6 runtime oracle audit + 10-minute benchmark (`0` mismatches, `-65%` `noting the runs`, `-82%` `finding the pages`) We ported this full shadow/watch optimization stack (`O(dirty)` `TOUCHED_CARDS`, 64-page-per-word `CopiedPages` bitmask, lock-free `has_pending`, direct `merge_under_writes`, `FOUND_LOOSE` thread-local cache, in-place `joined()`, and AVX2 `first_difference` / `last_difference` / `merge_piece` / `merge_sync_card` / `changed_runs`) into our RTX 4080 / i5-13600K (Windows 10) GTA 6 Series S build and verified it both for live correctness and steady-state performance: 1. **10-minute live GTA 6 runtime oracle audit (`31,249` log lines, real gameplay frames):** - Instrumented all 7 fast paths (AVX2 `first_difference`/`last_difference`/`changed_runs`/`merge_piece`/`merge_sync_card`, `TOUCHED_CARDS` dirty scan, `CopiedPages` bitmask, `FOUND_LOOSE` cache, and in-place `joined()`) to compare against the scalar / stock reference on live frames. - **Result: `0` mismatches (`ERROR=0`) across the entire 10-minute live run.** 2. **10-minute release benchmark (RTX 4080 16 GB, i5-13600K, Win10, `render_scale=0.6667`, `VICTORY_CARD_BUFFERS_MIN_KB=64`, `VICTORY_FETCH_CHECK=1`, `VICTORY_SKIP_WC_MIN_KB=1024`, RT skipped):** - **`VICTORY_LATE_WAIT=2` (`212` pages looked at, `77,290` changed runs, `2.38 MB`):** - `finding the pages`: **`~0.28 ms -> 0.05 ms` (-82%)** - `noting the runs`: **`~3.7 ms -> 1.28 ms` (-65%)** - `comparing pages`: **`1.93 ms`** wall (`1.65 ms` reading + `6.71 ms` comparing/putting in across worker threads) - **End-to-end settled performance (`n=104`, tail-40 `n=40`):** - **`32.00 fps` median** (`31.50` p10, `32.60` p90, `32.70` tail-40 max, `33.20` overall max; `0` tail frames `>55 ms`) - **`4,505` draws/frame**, `317` pieces landed/frame (`4.5 MB` dirty 4 KiB pages, `0.84 MB` dwords) - **Visual verification (`t=140..600s`):** `0/230` black frames, `120/120` clean consecutive burst frames (`4 fps` contact sheets at `t=420–435s` and `t=520–535s`). The AVX2 paths and `O(dirty)` bookkeeping held up 100% clean in real GTA 6 gameplay!
Author
Owner

@⁠PastiiJay on GitHub · 2026-11-26 14:49 UTC · original

Thanks @⁠Proudnoob4, a 10-minute live oracle with zero mismatches is the strongest check this one has had, and the drop in page-finding time is a nice bonus. If you have the setup ready, the same oracle-plus-benchmark on #59 (lazy copy-back) would be the most useful next run, since it touches the same shadow path.

**@⁠PastiiJay** on GitHub · 2026-11-26 14:49 UTC · [original](https://github.com/PastiiJay/klbf-lab/pull/57#issuecomment-7195375503) Thanks @⁠Proudnoob4, a 10-minute live oracle with zero mismatches is the strongest check this one has had, and the drop in page-finding time is a nice bonus. If you have the setup ready, the same oracle-plus-benchmark on #59 (lazy copy-back) would be the most useful next run, since it touches the same shadow path.
Author
Owner

@⁠Proudnoob4 on GitHub · 2026-11-26 14:58 UTC · original

@⁠PastiiJay Sounds like a plan! We're running a 10-minute verification pass on our current batch (fxceless#1184 + #1274 + #1276) right now, and right after that we'll port #59 (VICTORY_LAZY_COPYBACK=1) onto our tree, run the live GTA 6 coherence/oracle + 10-minute benchmark on RTX 4080 / i5-13600K (Win10), and post the full measured results and visual check on #59.

**@⁠Proudnoob4** on GitHub · 2026-11-26 14:58 UTC · [original](https://github.com/PastiiJay/klbf-lab/pull/57#issuecomment-7195610617) @⁠PastiiJay Sounds like a plan! We're running a 10-minute verification pass on our current batch (`fxceless#1184` + `#1274` + `#1276`) right now, and right after that we'll port `#59` (`VICTORY_LAZY_COPYBACK=1`) onto our tree, run the live GTA 6 coherence/oracle + 10-minute benchmark on RTX 4080 / i5-13600K (Win10), and post the full measured results and visual check on `#59`.
Author
Owner

@⁠PastiiJay on GitHub · 2026-11-26 15:18 UTC · original

Sounds good, thanks. We will watch #59 for the results; a visual check of the same scenes with and without VICTORY_LAZY_COPYBACK=1 would be the part we care about most.

**@⁠PastiiJay** on GitHub · 2026-11-26 15:18 UTC · [original](https://github.com/PastiiJay/klbf-lab/pull/57#issuecomment-7196091611) Sounds good, thanks. We will watch #59 for the results; a visual check of the same scenes with and without `VICTORY_LAZY_COPYBACK=1` would be the part we care about most.
This pull request can be merged automatically.
This branch is out-of-date with the base branch
You are not authorized to merge this pull request.
View command line instructions

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u origin '+refs/pull/57/head:github-bot/gh-57'
git switch 'github-bot/gh-57'

Merge

Merge the changes and update on Forgejo.
git switch 'beta'
git merge --no-ff 'github-bot/gh-57'
git switch 'github-bot/gh-57'
git rebase 'beta'
git switch 'beta'
git merge --ff-only 'github-bot/gh-57'
git switch 'github-bot/gh-57'
git rebase 'beta'
git switch 'beta'
git merge --no-ff 'github-bot/gh-57'
git switch 'beta'
git merge --squash 'github-bot/gh-57'
git switch 'beta'
git merge --ff-only 'github-bot/gh-57'
git switch 'beta'
git merge 'github-bot/gh-57'
git push origin 'beta'
This discussion has been locked. Commenting is limited to contributors.
No description provided.