perf(gpu): scope standalone ahead queries to covered page ranges #312
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/lobomfz--VIctory-xmem!312
Loading…
Reference in a new issue
No description provided.
Delete branch "github-bot/gh-312"
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?
@tommiehansen on GitHub · 2026-12-03 12:21 UTC · original
tommiehansen:perf/phase3-scoped-watch→main· +422 −24 · 3 files · 9 commits · openWhat this does
Scope standalone ahead queries to covered page ranges.
Checks
asking_ranges_matches_bruteforce_over_randomized_operationsand independent native tests reported; interleaving regression and lock-order invariant added; all five targeted tests passed at current head. Explicit fail-before evidence remains.cargo xtask policyon the final revision; no new game assets or build output intended.Verified
Current review head:
31a6bd57c590d6e7758baf0d1b6158c82de258d5. Existing measurements and commands are retained below at their original revisions. No fresh compile, policy or hardware test is claimed by this metadata review.Cost
Smaller claimed query range, potential mutex cost; no proven frame-time benefit.
Detailed implementation and historical validation
Problem
Standalone ahead batches take a whole-address-space write-watch hold even when they query a few disjoint ranges. This can unnecessarily stall other threads asking unrelated pages.
Implementation and invariants
With
VICTORY_ASK_AHEAD_SCOPED=1, acquire the coalesced union of actual kernel query runs atomically, including gaps queried while combining neighboring intervals. Keep the claim until the journal publication completes. Preserve serialization among standalone ahead batches and retain the full-range hold for two-part submission queries. Overlapping resets and publication still serialize; the same kernel and unmapped-view checks remain in force. Unset the switch to restore the baseline.Source and expected impact
Inspired by MiniVI #324. Its broader Windows implementation reported average 13.61 → 16.54 FPS in one world run and virtually unchanged FPS in a GPU-bound 4K run. This narrower xmem change targets the standalone CPU wait only. No expected FPS is inferred.
External benchmark context
MiniVI #324, RTX 4060 Ti 8 GB, paired 90-second runs:
Its diagnostics also reported range-lock waits of 1.16–1.28 ms versus 0.43–0.45 ms per frame. Its implementation changes both ahead and rest submission queries; this PR deliberately preserves the full-range hold for two-part submission queries. The results are not predictions for xmem.
Validation
The portable interval test passed for ordered, adjacent and overlapping ranges. The diagnostics Windows-target check and diff check passed. Measure standalone ask waits, CPU stall time and write/coherence parity, including mixed with existing refresh batching.
Hardware verification
No local game execution or GPU benchmarking was available. Please A/B the same save and route on RX 9060 XT 16 GB at native 1080p and RX 9070 XT at native 1440p, with RT on and frame generation off.
Pass 2B — sorted range table and native write-watch validation
The sorted table in
c105943keeps each holder’s half-open ranges distinct. Insertion and overlap checks share the existing lock, and exact-match removal drops only the owning guard’s entries. The precedingcbc421bhas the samewatch.rsimplementation ase7c3699, which uses the Vec-based range table. The measurements below are from the published local microbenchmarks, Apple M5, CPU only,rustc -O, eight A/B/B/A blocks. They compare two implementations of the interval operations.The exact timing driver and raw samples from that run are unavailable here. The four portable production tests were rerun successfully at
c105943:Independent native Windows validation at
c105943exercisedGetWriteWatchand confirmed entry into the scoped branch with the switch set. It also checked 400,000 randomized hold operations, 100,000 run-builder inputs, overlapping and touching holds, and mixedask_ahead,writesandask_oncecalls. The full library test run had the same five failures as main under the reviewer’s test setup, including the shared flaky write-watch test. Standalone ahead batches still serialize with each other. Linux/Wine contention and frame-time impact remain unverified.Pending: GitHub reports a merge conflict; resolve against main before ready.
Follow-up review fixes (2026-12-04)
Head:
31a6bd57c590d6e7758baf0d1b6158c82de258d5.Document the AHEAD_BATCH/ASKING lock-order invariant. Extract the scoped acquisition and post-wait publication check into a guard helper, keeping range-guard release before batch-mutex release. Added
scoped_wait_preserves_whole_submission_publication, run in isolation usingcargo test --locked -p victory-gpu --lib --features diagnostics -- scoped_wait_preserves_whole_submission_publication --ignored --test-threads=1.Pinned CI audit started for this revision. Final-head compilation, tests and policy results are pending; historical measurements have not been relabelled.
Final-head CI update
At
31a6bd57c590d6e7758baf0d1b6158c82de258d5, Windows MSVCcargo check --locked -p victory-gpu --all-targets --features diagnosticsand 5 targeted tests (asking_ranges::tests plus isolated scoped_wait_preserves_whole_submission_publication) passed, with zero test failures (audit). The upstream merge snapshot also passed asset policy; with the independent repository-wide policy fix #528 applied, all four switch tests pass (verification). No dependency on #528 for runtime behavior. Hardware/gameplay verification and any remaining checklist items are still open for review. Ready for review does not imply those are completed.Queue refresh — 2026-12-05
Merged upstream main
b581f7e0a8086158953a64068ba29305d5450d68into the existing branch without rewriting history. New head:d34f1cb433b891a84f6740ac2b644a8c7dedc916.Fresh pinned CI audit is the verification record for this revision; previous tests apply to the previously stated heads. #528 remains a separate policy prerequisite, not a runtime dependency. No new gameplay or GPU timing claim.
@Kal0s-design on GitHub · 2026-12-03 19:39 UTC · original
Tested
e7c3699on Windows 11 natively (MSVC, toolchain 1.98.1), so the write watch is the real GetWriteWatch with the reset flag, not Wine. CPU-only, victory-gpu lib tests.Reading the change I didn't find a problem. AheadAsking::hold checks for overlap and inserts the union under the same ASKING lock, so two overlapping holders can't both get in, and hold_all and a scoped hold wait for each other. Lock order is AHEAD_BATCH then ASKING then the journal, and ask_once passes held=true so it never takes AHEAD_BATCH. The guards outlive journal publication and get dropped on the early kept return and on unwind. The reset only ever covers a run span or half of one, which is inside the claim, and the gap pages inside a run are part of the span. With the variable unset it's the same hold_all and publication as before.
One thing worth knowing is that the existing tests never reach the scoped branch. I added a temporary print there and nothing in the suite hit it with the variable set, since the only standalone ask_ahead in the tests passes a single range. So I wrote a few throwaway tests locally (not pushed). Disjoint holders and a writes-style hold in the gap between two runs get through, overlapping ones block until drop, hold_all and a scoped hold block each other both ways, and a panic while holding releases the ranges. On two small write-watched allocations, ask_ahead over three ranges of the first one answers the first one's written page from the journal and leaves a write in the second allocation for writes() to find. Three threads mixing ask_ahead, writes and ask_once finished without hanging. All of these passed with the variable set, and the last two also with it unset.
Also, because of AHEAD_BATCH two standalone batches still run one at a time even when they don't overlap. The doc comment says so, I'm just mentioning it in case the title reads like they run in parallel.
Results
The watch failure is already on main and isn't from this PR. It passes alone (20/20). Run right after bits_go_at_every_view in the same process it fails every time on main and here. A stale VIEWS bit is left on an address that VirtualAlloc hands out again, so refused_as_view makes writes() return None even though GetWriteWatch would have answered. I don't know if Wine reuses addresses the same way.
AI-assisted: yes
@tommiehansen on GitHub · 2026-12-03 19:53 UTC · original
Sorted Disjoint
AskingRanges& Zero-Sort Fast Path (c105943)Merged
origin/main(45591b0) cleanly with zero conflicts and extracted the interval hold/run logic into pure-Rustcrates/victory-gpu/src/watch/asking_ranges.rswhile keepingVICTORY_ASK_AHEAD_SCOPED,AHEAD_BATCH, andAsking::hold_all()behavior intact.Technical changes
AskingRangestable: Replaces the linearVec<(usize, usize)>scans andO(H log U)retain(binary_search)withO(log H)partition_pointsingle-range overlap checks,O(H + U)two-pointer batch overlap/merge sweeps, and a zero-alloc in-placeO(H + U)two-pointerremove_batchsweep.normalize_batch): Whenask_ahead_heldsupplies already-sorted disjoint run spans (separated byAHEAD_GAP),normalize_batchreturns the vector in place without sorting or reallocatingunion.macOS ARM64 CPU Benchmark Results (
rustc -O, A/B/B/A 8-block median, direct productionasking_ranges.rs)Vec)AskingRanges)hold + dropgap check (H=256)H=256)hold + drop(U=256,H=16)Multi-threaded disjoint contention check (4 threads x 4,000 non-overlapping
hold/dropcalls while an ahead batch holds 128 spans):hold_all()took 3,411 us (4 blocked workers) vs scopedAskingRanges2,534 us (0 blocked workers).Verification & Reproduction
rustc --edition 2024 --test crates/victory-gpu/src/watch/asking_ranges.rs -o /tmp/test_asking_ranges && /tmp/test_asking_ranges(4 passed, 0 failed, 512 randomized single/batch hold-overlap-remove steps).cargo check -p victory-gpu --lib --target x86_64-pc-windows-msvc --features blake3/pureand--all-targets --features blake3/pure,diagnostics(0 errors, 0 warnings, check-onlylibmimalloc-sysshim), pluscargo --offline xtask policy --staged(2 files ok) andxtask -- switches(0 new errors).GetWriteWatchcontention and frame-time impact require external Windows/AMD hardware testing.@Kal0s-design on GitHub · 2026-12-03 20:21 UTC · original
Re-tested at
c105943on Windows 11 natively (MSVC), so real GetWriteWatch again, CPU-only, victory-gpu lib tests.The thing I wanted to check first was whether the sorted table could let one holder's drop take away another holder's protection. It can't as far as I can tell. insert_range and insert_batch never merge entries in the table, they only insert, so each holder's ranges stay as separate entries. Live entries are non-empty and pairwise disjoint (the overlap check and the insert happen under the same lock), so no two holders can own an equal entry and removing by exact match only ever hits the owner's. Touching holders like [a,b) and [b,c) are both allowed, same half-open rule as the old overlaps_asking.
normalize_batch does verify its input rather than trusting it. The fast path needs every range non-empty and a strict gap between neighbours, so touching, overlapping, empty or unsorted input goes to the sort and merge. That matters a bit because the run spans aren't always separated by AHEAD_GAP. When a run is cut by AHEAD_MAX the next run can start before the previous one ends, and those spans get merged there, which is fine.
Two small behaviour differences, both harmless as far as I can see. An empty Asking::hold is no longer stored (before, an entry (a,a) could make an ahead batch whose union contained a wait), and build_ahead_runs now uses saturating_add for the gap.
What I ran locally (not pushed)
Results
I didn't measure timings, so nothing to add on the performance side.
AI-assisted: yes
@Luxuride on GitHub · 2026-12-04 12:09 UTC · original
Porting the overlap check to a sorted disjoint set with
overlaps_batch/insert_batchand four unit tests is the right shape, and scoping the hold to the queried runs is the right idea. The boring part is missing.What I checked:
crates/victory-gpu/src/watch.rs:595(static AHEAD_BATCH: Mutex<()>) serialises scoped batches, andAheadAsking::holdstill blocks on theASKINGcondvar while that mutex is held (watch.rs:612-625). It is deadlock-free today only because the whole-submission path never takesAHEAD_BATCH. That is one refactor away from a hang. Put that invariant in a comment atwatch.rs:595; it is the single most valuable line in the PR.crates/victory-gpu/src/watch.rs:735-742: the doublejournal().keptcheck is correct, but the second one returns while a scopedAheadAskingguard is alive — the guard'sDroptakesASKING, notifies, and then the function returns. The empty-publication path atwatch.rs:1204(normalize_batch) covers it; no test pins it. One test where a whole-submission publish lands during a scoped wait would nail it down.VICTORY_ASK_AHEAD_SCOPEDis indocs/SETTINGS.md:33but not inrun.conf.default, unlikeVICTORY_LATE_FENCEandVICTORY_BAKE_SOURCES_ONLY. Same gap as the other experiment-shaped PRs in this batch.56 commits behind, merges clean (
AHEAD_BATCHand the refresh-front work do not touch the same files). Comment on the mutex, one interleaving test, onerun.conf.defaultrow, and it is ready.watch: scope standalone ahead queries to covered page rangesto perf(gpu): scope standalone ahead queries to covered page ranges@tommiehansen on GitHub · 2026-12-05 10:19 UTC · original
Thanks @Luxuride for identifying the lock-order and interleaving coverage gap. 31a6bd5 documents AHEAD_BATCH/ASKING ordering and adds the whole-submission publication regression during a scoped wait. MSVC compilation and targeted tests passed, including on the refreshed head. Gameplay benefit remains unmeasured.
github-bot referenced this pull request2026-12-05 11:23:58 +00:00
View command line instructions
Checkout
From your project repository, check out a new branch and test the changes.Merge
Merge the changes and update on Forgejo.