perf(watch): lock-free publish via CLEAN/HELD bits, batched cover_run, and zero-VirtualProtect view/page invalidations #3945
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/3945"
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 trustno1-coder/VIctory-NVIDIA. Originally posted by qlfu on 2026-11-30T12:36:41Z.
Summary
Reduces
GUARDSmutex contention and redundantVirtualProtectsyscalls incrates/victory-gpu/src/watch.rs:publish(): mirrorsGuards::cleanandGuards::heldinto atomicCLEAN/HELDBits(allSeqCst) updated strictly aftercover()completes and cleared ahead ofPUBLISHED.clear.publish()uses aSeqCstDekker recheck instead of acquiringGUARDS(~184 fewerGUARDSmutex acquisitions/frame).VirtualProtectinvalidations onviews_mapped()andtitle_memory_written(pages == true): clearscleanandPUBLISHEDon already-guarded blocks (and aliases) while keeping existing viewsPAGE_READONLY, avoiding RW/ROVirtualProtectchurn while still forcing refresh andcover()on newly mapped views.cover_run& single-lockguard(): coalesces contiguousg.protected && !g.cleanblocks inguard()into onecover(from, to)(VIEWS.read()) call, avoids double-lockingGUARDSon!PROTECTEDentry, and skipsg.alias()inprotect_alias()when an unprotected alias is resting or left alone.open_unlocked(): computestook = done && old != PAGE_READWRITEafter restoring non-RW/RO protections (done = false), preventing failedVirtualProtectcalls from clearingg.protectedinsettle().Verification
cargo test -p victory-gpu --lib --locked -- --test-threads=1 watch::(67 passed, 0 failed) +cargo check -p victory-gpu --all-targets --all-features --locked(exit 0).refreshes10.50 -> 9.45 msmed (11.13 -> 9.88 msmean),prepdown to22.8 ms,copiesdown to11.1 ms,0WARNs /0ERRORs.Imported from trustno1-coder/VIctory-NVIDIA. Originally posted by qlfu on 2026-11-30T15:05:15Z.
Added word-batched
Bits::set_wordforGuards::mark_cleanso marking contiguous clean blocks emits oneSeqCst fetch_orper 64-block word instead of per block. Verified:cargo check -p victory-gpu --all-targets --all-features --locked+watch::unit tests passed + cleanmerge-treeonto41adb551.Imported from trustno1-coder/VIctory-NVIDIA. Originally posted by upipibi on 2026-12-01T02:43:11Z.
Race review of #3945 (head
701519a4): it holds. I found no lost write and no crash. Itsopen_unlockedchange also fixes somethingmainstill has.Reading.
publish()withoutGUARDS: it setsPUBLISHED, then checksCLEAN/HELD/BESIDE/unsettledagain (allSeqCst). Every path that takes a block out ofg.cleanorg.heldgoes throughunmark_clean/unmark_held, and where the block may have been published it clearsPUBLISHEDafter the mirror. The exception iscover()(:3313), which un-cleans only blocks noted clean in the same locked pass, beforemark_clean, as onmain. So one of the two sides always sees the other.CLEANis only set bymark_clean, which readsg.cleanaftercover()(protect), so it never runs ahead of the books.took = done && old != PAGE_READWRITEinopen_unlocked: onmain, aVirtualProtectthat fails leavestook = true.settlethen takes a block that is still read-only out ofg.protected. A write there finds nothing to open, and the handler gives up with 0xc0000005. That is the state I traced in #3760, by another way in. This PR closes that way.Stress test. This is the test from my #3760 review: one 16 MiB view, four threads guarding it in a loop,
writing()on another thread, 48 writer threads for 20 s. Debug test build, Ryzen 9 5950X, Windows 11, alternating runs. The PR was tested at its base, since it conflicts withmainnow.41adb551watch::on the PR, 3 runs: 65 passed, 0 failed twice. Once it was 64 passed, 1 failed:ask_rest_falls_back_per_piece_when_touching_unwatched_neighbour, a test-isolation flake onmainthat #4619 fixes. The worst-wait ranges overlap, so I wouldn't read much into them.It conflicts with
maininwatch.rs, so it needs a rebase before it can go in. Thetookline alone would also make a small fix by itself.Tested with the help of Claude Code (Claude Opus 5.5).
Imported from trustno1-coder/VIctory-NVIDIA. Originally posted by trustno1-coder on 2026-12-01T18:47:40Z.
Closing this as part of moving main to the texture and video-memory rework (v2026.12.01.01-test). A lot of the code changed, and this pull request no longer applies cleanly to it; it would need to be rewritten against the new main rather than rebased. Thank you for the work. If the change is still needed, please open a new pull request based on the current main.
Pull request closed