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)
This pull request can be merged automatically.
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.