gpu/shadow: O(dirty) CopiedPages bitmask, lock-free mirrors, and AVX2 Shadow::take #192
No reviewers
Labels
No labels
accessibility
bug
documentation
duplicate
enhancement
good first issue
help wanted
invalid
question
wontfix
No milestone
No assignees
1 participant
Notifications
Due date
No due date set.
Dependencies
No dependencies set
Reference
github/TaynanGT--Vice-Tay!192
Loading…
Reference in a new issue
No description provided.
Delete branch "github-bot/gh-192"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
@Vladimir-Human on GitHub · 2026-11-27 07:43 UTC · original
Vladimir-Human:perf/copied-pages-odirty-avx2→main· +1211 −138 · 1 files · 2 commits · merged 2026-11-27 08:14 UTCSummary
Pure CPU-side optimization of
crates/victory-gpu/src/shadow.rs(watch.rsuntouched, zero visual change — exact byte-for-byte host-copy and copy-back parity):O(dirty)CopiedPages64-page uncopied bitmaskMutex<Vec<u64>>inShadow::copiedwithMutex<CopiedPages>(at: Vec<u64>,uncopied_bits: Vec<u64>,uncopied: usize).CopiedPages::plan_stale,stale_pages_in,has_stale_in,range_all_copied, and O(1)copied_countsoShadow::copy_stale,Shadow::stale_under_writes,Shadow::merge_under_writes,Shadow::nothing_new, andwindow_useskip initialized regions 64 pages at a time and run inO(dirty)time.rangedcoverage,(from, to, known)run splitting,by_parts4 KiB partial-page splitting,landed,stale_pages, card/window/in-place paths, andcopied -> rangedlock ordering while releasingself.copiedbeforeparts_writtenandtake_all.AtomicBoolmirrors (has_pending,has_dirty,has_sent)Shadow::has_pendingso read-only buffers and windows skip lockingShadow::pendingacrosswrites_pending,writes_pending_in,stale_under_writes,merge_under_writes,update,nothing_new, andsend_copied.Card::touchedwithCard::has_dirtyandCard::has_sent(backed bySentRuns::is_empty()):card_dirtypublisheshas_sent(Release) before clearinghas_dirty(Release) underdirty, prunes stalesententries when!has_dirty && relay_on && has_sent, and letscard_merge_relay,land_card, andcard_dirty_nowskip locking on clean copies (has_dirtychecked first, thenhas_sent).Shadow::take(word_runs_avx2/word_runs_put_avx2)word_runs_avx2andword_runs_put_avx2to handle unaligned prefixes, 256-byte + 32-byte AVX2 loops, 4-byte tails, and unaligned tails across all alignments.Shadow::takethroughtake_chunk->word_runs_putinstead of scalardword_runs, preservingupcheck,keeps,card_changed_runs, andSENDcounters.shadow::copied_and_take_tests(CopiedPagesbitmask/word-boundary oracle,plan_stalewithranged+ partial init oracle,stale_pages_in/has_stale_inoracle,has_pendingmirror,has_dirty/has_sentmirror + 4-statecard_merge_relayparity,word_runs/word_runs_put/take_chunkscalar vs AVX2 parity across all 4 alignments & 33 lengths, and end-to-endShadow::takescalar oracle across alignments & multi-page spans) plus an#[ignore]release benchmark (bench_copied_pages_and_word_runs).Verification
crates/victory-gpu/src/shadow.rs:327,913bytes (7,486lines,< 640 KiBlimit)cargo run -p xtask --locked -- policy:asset policy: 303 files okcargo test -p xtask: 11 passed; 0 failedgit diff --check origin/main..HEAD: cleancargo clippy -p victory-gpu --all-targets --locked -- -D warnings: 0 warningscargo clippy -p victory-gpu --all-targets --all-features --locked -- -D warnings: 0 warningsVICTORY_D3D12_DEBUG=1,--test-threads=1):cargo test -p victory-gpu shadow::: 54 passed; 0 failed; 4 ignoredcargo test -p victory-gpu --features diagnostics shadow::: 54 passed; 0 failed; 4 ignoredcargo test -p victory-gpu --release --locked shadow::: 54 passed; 0 failed; 4 ignoredunchanged_ranges_are_known(1/1),only_what_differs_goes_up(1/1 default and 1/1 withVICTORY_SEND_CHANGED=1)VICTORY_RELAY=1:shadow::copied_and_take_tests(7 passed; 0 failed; 1 ignored),shadow::land_tests(2 passed; 0 failed)bench_copied_pages_and_word_runs, x86_64 AVX2):plan_stale(160 MiB / 2,560 pages, 4 dirty): 9.4 ns/callShadow::takediff: scalar8.14 ms-> AVX2word_runs_put5.11 ms (1.59x faster, fused compare + copy)@efsebayern on GitHub · 2026-11-27 07:59 UTC · original · reviewed
I reviewed this on the PR head (
db28bb8).cargo test -p victory-gpu --libgave 463 passed, 0 failed, 43 ignored.cargo clippy -p victory-gpu --all-targets -- -D warningswas clean.One concurrency concern in
copy_stale(crates/victory-gpu/src/shadow.rs, around lines 5002-5041)The old code held
copiedfor the whole function,take_allincluded, so refreshes of oneShadowran one at a time. Nowplan_stalestamps the pages withat = now, and the lock is dropped beforetake_allcopies the bytes. That leaves a window where the pages count as copied but the copy hasn't finished.updateornothing_newon an overlapping range during the copy. B seeswrite <= atand treats the range as current, so it can record or upload the shadow while A is still writing it.nowboth plan runs on the same pages. Both thentakeintoself.data/heldthrough raw pointers at the same time, which can tear bytes.If
updateis never reached from two threads for the same shadow, this is moot. I didn't trace every caller ofhost.rs:9083. If it can be reached that way, holdingcopiedacrosstake_all, or adding a per-shadow "copy in flight" guard, would close the window.Minor:
card_dirty(around lines 1076-1081) now callsforget(fetched)on clean copies that havehas_sentset, where before it only ran on touched ones. That widens an existing race withcard_merge_relaywhenfetched_after < fetched; it doesn't add a new one.Checked and looks correct:
CopiedPages: masks atlo%64andhi%64, high bits of the last word kept zero, single-word ranges.plan_stalefast and sparse paths, compared with the old dense loop.stale_pages_in/has_stale_in.aligned_end <= n, scalar head and tail, output matchesdword_runsfor all four values ofat & 3.has_pending/has_dirty/has_sent: the Release/Acquire ordering is sound.AI-assisted review (Claude Code).
@TaynanGT on GitHub · 2026-11-27 08:07 UTC · original
@efsebayern Thanks for tracing the refresh order. I’ve kept the page-state lock through the byte copy and the range update, and preserved the sent ranges for clean card copies. @Vladimir-Human, that adjustment is in
c289a95on this PR. The Windows diagnostics run passed 54 focused shadow tests. Review@Garu667 on GitHub · 2026-11-27 08:22 UTC · original
An NVIDIA data point for this PR, on a CPU-bound card: RTX 3080 10 GB, i5-13600K. Diag build of main
e939566, with and without this branch merged on top.-Machine 4070, Series S, prison cell, standing.Each run started from a fresh copy of the same warmed cache and save, in ABBA order. FPS is the median of the last 18 frame lines (about 90 s):
🤖 Generated with Claude Code
@TaynanGT on GitHub · 2026-11-27 08:24 UTC · original
@Garu667 Thanks for running the ABBA control. The 14.95 to 14.85 FPS difference is within the run spread in the cell, with the same visible output. Yes, a matched drive would be useful if you have one available. The copy time and dirty-page counts for that stretch would show whether it exercises the changed path more than the cell does.
@Vladimir-Human on GitHub · 2026-11-27 08:33 UTC · original
Thank you
@efsebayernfor catching thecopy_stalelock release window and the clean-copysentprune incard_dirty, and thank you@TaynanGTfor fixing both cleanly inc289a95before merge! We backported the exact same hardening to our open PRs sharingCopiedPages/card_dirty.Thank you as well
@Garu667for the RTX 3080 ABBA run — glad to see zero faults and neutral frame time within run-to-run spread in the static interior scene, and agreed that high-churn traversal / streaming scenes are where dirty-page tracking and AVX2Shadow::takecarry measurable weight.@Garu667 on GitHub · 2026-11-27 08:35 UTC · original
Follow-up in the open world, same machine and builds. Each run started from a fresh copy of the same open-world save. I walked the same loop around a house, played by hand, about 90 s in the world each:
Neutral here too, within noise. Preparation is about 2 ms lower with the branch, but that's not significant on two runs. No faults.
🤖 Generated with Claude Code
@TaynanGT on GitHub · 2026-11-27 08:38 UTC · original
@Vladimir-Human Thanks for carrying both changes over to the ports. Keeping the page lock through the copy and preserving the sent ranges on clean copies gives those branches the same refresh behaviour as the merged revision here.
@TaynanGT on GitHub · 2026-11-27 08:39 UTC · original
@Garu667 Thanks for doing the house loop from the same save. The whole-run result is neutral on that pair: 12.0 to 12.1 FPS, with preparation at 60 and 58 ms. This adds a traversal check with both runs completing cleanly.