Shadow: queue card copies as they change instead of scanning all at each submission #392
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/392"
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?
Imported from TaynanGT/Vice-Tay. Originally posted by urielbeaupre on 2026-11-28T01:37:56Z.
shadow::card_dirtywalked every copy in the card's memory (CARDED) at each submission to find the few with dirty ranges: an upgrade of aWeakand an atomic load per copy. AVICTORY_PROFILEsample of the submitting thread on current main put 5.5% of its time there (Vec::retainincard_dirty), in the prison cell on a Ryzen 9 7900X / RTX 4070 Ti.This PR has each copy queue itself once, the first time it becomes dirty after the queue was last taken (
Card.queued,Card.me,CARD_QUEUE), socard_dirtyonly looks at copies that changed.me) asks for one full look (CARD_SCAN_ALL), as before.track_cardedadds the copy toCARDEDbefore it asks for that look.has_dirtyunder thedirtylock, then swapsqueuedto true and pushes the copy ifqueuedwas false. The reader swapsqueuedto false (AcqRel) before it takes the ranges. A change the reader did not take therefore always queues the copy again. The worst case is an empty take.Weakentries inCARDED, which the full scan used to drop, are pruned every 1,024 calls.VICTORY_CARD_DIRTY_SCAN=1restores the old full scan for comparison.sent.forget,note_joined,has_sent) is unchanged and runs only for dirty copies, as before.Measurements
RTX 4070 Ti 12 GB, Ryzen 9 7900X (12 cores), 63 GB, Windows 11. Release build,
-Machine 4070, Series S, windowed 1920×1080. Unattended with the intro skipped (A held); measured 100–160 s after the loader started, in the prison cell, with no pipelines being made. Same binary, the switch alternating between runs:VICTORY_CARD_DIRTY_SCANThe mean is 25.6 vs 24.75 fps (about +3%), with CPU preparation 1.4 ms lower. That is two runs each, and the second pair nearly overlaps, so read it as "about +1 fps, within run-to-run spread for one pair". On a diagnostics build of an older main, the gain was smaller (21.9 → 22.2, separate runs) and within noise. The release build is less noisy.
A further pair on
fe7c6e5with this exact commit, after its pipelines were built: queue 25.3 fps (preparation 32.4 ms) against scan 22.9 (36.2 ms). The scan run looks low against the others. Across all three pairs the queue run was ahead each time.Verification
cargo test -p victory-gpu --lib: 528 passed, including the newcard_dirty_takes_queued_copies_once_and_requeues_later_changes. The two tests that callcard_dirtyshare a lock.cargo clippy -p victory-gpu --features diagnosticsis clean, andcargo xtask policypasses.🤖 Generated with Claude Code
Imported from TaynanGT/Vice-Tay. Originally posted by akimfomin on 2026-11-28T01:56:19Z.
Source-level comparison with KLBF #57: a page bitmap limits copy work there; your queue limits which objects are visited. I enumerated the ten order-preserving interleavings of one writer and a reader around dequeue, clearing the queued bit and taking ranges. In the CPU model
clear → takeprotects each dirty event, whiletake → clearloses a later writer in one schedule. This is a bounded model, not a Rust memory-ordering proof. A compact fixture could arrange a writer between reset and drain and assert that its range is seen on one of two consecutive drains.Imported from TaynanGT/Vice-Tay. Originally posted by urielbeaupre on 2026-11-28T01:57:30Z.
Thanks, that matches what the code does: it clears, then takes. In
card_dirty'stake(shadow.rs:1092), the reader doesqueued.swap(false, AcqRel)first, then readshas_dirtyand takes the ranges under thedirtylock. The writer (card_changed_runs, shadow.rs:5334) adds its ranges and setshas_dirtyunder that lock, releases it, and only then doesqueued.swap(true, AcqRel). It pushes the copy if that swap returnsfalse.falseand queues the copy again for the next drain.queued, so the reader's swap synchronizes with it and sees the ranges.The worst case is an empty take on the next drain.
The existing test covers the sequential version:
card_dirty_takes_queued_copies_once_and_requeues_later_changesmakes a change after a take and asserts the next drain returns it. A deterministic writer between the clear and the take needs a test hook insidecard_dirty. I can add one behindcfg(test)(a callback run between the swap and the take) so the fixture you describe asserts the range is seen on one of two drains, if that's wanted here.Imported from TaynanGT/Vice-Tay. Originally posted by akimfomin on 2026-11-28T02:03:42Z.
Thanks, that confirms the exact ordering the bounded model checks. The fixture does not need a real GPU: pause the reader after it clears
queued, enqueue one second dirty range on the writer, resume the reader, and combine this drain with the next one. Assert the second range appears exactly once across the two drains. A test-only hook would cover the gap you described if the maintainer wants it. The model also has a reversal control where taking before clearing loses that second range.Imported from TaynanGT/Vice-Tay. Originally posted by urielbeaupre on 2026-11-28T02:06:47Z.
Added in
b45345c:a_change_between_clearing_queued_and_taking_is_taken_once_in_two_drains. A test-only hook (CARD_DIRTY_BETWEEN,cfg(test)) runs a writer insidecard_dirtyafterqueuedis cleared and before the ranges are taken. The test queues a first change, adds a second from the hook, and drains twice. It asserts each range appears exactly once over the two drains and that a third drain is empty.cargo test -p victory-gpu --lib: 529 passed. One note: in one of four full runs,watch::tests::a_raw_copy_in_watched_memory_keeps_the_other_asked_rangesfailed. It passed alone three times and in the next two full runs. It's in the write-watch code, which this PR doesn't touch, so it looks like a pre-existing flake from shared global state, but I haven't confirmed it on main.Imported from TaynanGT/Vice-Tay. Originally posted by akimfomin on 2026-11-28T02:08:47Z.
I checked the updated
b45345c1ed7011b1b8441511a7228ee44989d49dagainstmainfe7c6e52198f2070805ffafc4e55a7163291bad0. The pinned Git merge is clean, and the new fixture checks both ranges across two drains and an empty third drain. The single reported write-watch failure still needs an unchanged-main comparison before calling it an existing flake. I have not rerun the full driver suite. Thanks for adding the exact interleaving.Imported from TaynanGT/Vice-Tay. Originally posted by urielbeaupre on 2026-11-28T02:19:30Z.
Compared with unchanged main: 15 full runs of
cargo test -p victory-gpu --libon each, same machine, back to back.watch::tests::a_raw_copy_in_watched_memory_keeps_the_other_asked_rangesfailedfe7c6e5(clean worktree)b45345cIt fails on unchanged main too, so the flake predates this PR. 1 vs 3 of 15 is too small a sample to say whether the PR changes its rate. It doesn't touch
watch.rs, but it adds tests that run in parallel and could shift timing against that test's shared global state. I can look into what that test races with if it's worth fixing here or in its own PR.Imported from TaynanGT/Vice-Tay. Originally posted by urielbeaupre on 2026-11-28T03:13:56Z.
Root cause of the write-watch flake: stale
VIEWSstate left by another test's cleanup (drop_viewsunmapped views withouttitle_memory_unmapping), hit when the allocator reuses the address. It's unrelated to this PR. The fix is in its own PR, #393: test-only, main went from 5 of 30 failing runs to 0 of 40.Imported from TaynanGT/Vice-Tay. Originally posted by maximecmb on 2026-11-28T09:45:11Z.
Measured in play on a second card: RTX 5080 16 GB, Ryzen 7 9800X3D (8 cores), 64 GB, Series S,
-Machine auto, diagnostics build offe7c6e5with this PR and 396 merged.To have both ways in one run,
card_dirty_scan()was changed locally to answercrate::ab::pick("VICTORY_CARD_DIRTY_SCAN")first (copies are queued either way and a full look clearsqueued, so the two can alternate), and the prologue was played by hand from its save withVICTORY_AB=5,VICTORY_CARD_DIRTY_SCAN: prison, robbery, chase on foot, jet ski, docks, 420 s in the world. Frame time with the scan, less with the queue, inside each full cycle:Frames were 43 ms on average over those phases (53 ms outdoors). A sampling profile of
victory-submitover the outdoor part of the same route, onmainbefore this PR, hadcard_dirtyat 3.3 % of the thread's running time with about 3,700 copies in the card's memory, which is about the same millisecond.No error and no device loss in 7 minutes, through to the Chapter I title. The recording was reviewed from one frame every 3 s: nothing comes and goes with the phases.
Limits: one PC, one run. Outdoors alone the difference is inside the noise. 396 has no switch, so it is on both sides.
If the switch were read through
ab::pickas above, others could measure it the same way in one run.Imported from TaynanGT/Vice-Tay. Originally posted by urielbeaupre on 2026-11-28T09:47:17Z.
Thanks for measuring it through the whole prologue. Done in
fb40506:card_dirty_scan()now takescrate::ab::pick("VICTORY_CARD_DIRTY_SCAN")first, in diagnostics builds, soVICTORY_AB=<s>,VICTORY_CARD_DIRTY_SCANalternates the two in one run. Outside an A/B run it reads the variable as before. Card-dirty tests pass, clippy is clean.Imported from TaynanGT/Vice-Tay. Originally posted by Kal0s-design on 2026-11-28T18:38:14Z.
Went through
fb40506for lost changes, since that's the failure mode that would matter here (a card copy left stale until something else forces a full look).The invariant I checked is: if
queuedis true, the copy is inCARD_QUEUE, orCARD_SCAN_ALLis set and the copy will be inCARDEDby the time a full look can take it. Each path keeps it:has_dirtyunder the lock, thenqueued.swap(true); the drain swapsqueuedto false before readinghas_dirty. Both RMWs onqueued, so either the drain sees the ranges or the writer requeues. The new fixture pins that schedule.meand setsCARD_SCAN_ALL. If that full look runs beforetrack_cardedpushes the copy intoCARDED, it misses it andqueuedstays true.track_cardedthen seeshas_dirtystill true and asks for another full look, and in production nothing butcard_dirtydrainsdirty, so it can't have been cleared in between.send_up/take_dirtydrain it too, but they're in thecfg(test)impl.meset was pushed intoCARDEDfirst, so the full look covers it.upgradefails and they're skipped.CARDEDholds deadWeaks for up to 1,024 calls, which only costs memory.On native Windows (NVIDIA card) the 61
shadow::tests pass, the threecard_dirtytests passed 200 of 200 repeated runs together, and the branch merges cleanly on currentmain(fe7c6e5).One small test-only thing:
track_carded_test'sTrackeddrop takes the copy out ofCARDEDbutmestays set. A later change to that copy still queues it, andcard_dirtyhands it out, where the old code would have ignored it.unbackedcopies are leaked and the tests filter withmine, so nothing hits it now. I'm only mentioning it in case a future test assumes an untracked copy is never returned.AI-assisted: yes
Imported from TaynanGT/Vice-Tay. Originally posted by TaynanGT on 2026-11-29T11:37:27Z.
Thanks for tracing the writer/drain and pre-tracking cases. The 200 repeated runs are useful evidence. Please document the test-only untracking caveat, and retain a regression for a write arriving after the drain clears queued so no update can disappear.
Imported from TaynanGT/Vice-Tay. Originally posted by TaynanGT on 2026-11-30T17:09:57Z.
mainhas moved to a new line of the code and everything is now named Vice-Tay, so this PR no longer applies as it is (details in #468). If you'd like this change in, please rebuild it on the currentmainand open a new PR; I'll review it there.Pull request closed