gpu: sync_heaps walks regions with VirtualQuery instead of a kernel read per page #3356
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/3356"
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 Marvisak/VIctory. Originally posted by borosf on 2026-11-26T20:21:51Z.
What
descriptor::sync_heapsprobed every 64 KiB page of a written heap range with aReadProcessMemorycall just to test mapped-ness (the title unmaps heap pages too). It now walks the range once withVirtualQuery: a page is synced iff its region isMEM_COMMITand notPAGE_NOACCESS/PAGE_GUARD— exactly the pages the kernel read fails on, so the synced slot set is identical. Fewer kernel calls (one per region instead of one per page) and onesync_slotscall per region instead of per page.Claimed on #1633 (comment 7202563021).
Measurement (Windows 11 x64, Intel Arc B580, synthetic VirtualAlloc only, no game data)
New test
descriptor::tests::a_region_walk_classifies_pages_like_per_page_readsasserts the page sets are identical (including aPAGE_GUARDpage in the middle, excluded by both) and prints:At FIND-31's ~13,624 pages opened per frame, that is ~17 ms → ~2 ms per frame on the probe alone, plus fewer
sync_slotslock acquisitions.Verification
cargo test -p victory-gpu --lib --locked --no-fail-fast— 230 passed, 2 failed: only the known B580 baseline (dynamic_slots_are_held_until_released,lists_share_a_table_and_copies_keep_their_views, #1744/#1829)cargo clippy --workspace --all-targets --locked -- -D warningsand--all-features— cleancargo xtask policy— okSynthetic tests only; no game data used.
Imported from Marvisak/VIctory. Originally posted by Dukestar69 on 2026-11-26T20:29:59Z.
Review: APPROVED
LGTM — verified on RTX 5090 (Windows / D3D12) against
origin/main(63bcca4).ReadProcessMemoryprobes insync_heapswith aVirtualQueryregion walk (MEM_COMMITand neitherPAGE_NOACCESSnorPAGE_GUARD) preserves exact page classification while cutting syscall overhead dramatically.cargo test -p victory-gpu --lib descriptor:: -- --skip dynamic_slots_are_held_until_released --test-threads=1: 22 passed, 0 failed (includinga_region_walk_classifies_pages_like_per_page_reads).cargo clippy -p victory-gpu --all-targets -- -D warnings: clean.cargo xtask policy: 191 files ok.Synthetic tests only (no game files used). AI-assisted (Gemini 4 Neon).
Imported from Marvisak/VIctory. Originally posted by fuzeofficlal on 2026-11-26T20:38:13Z.
Review: CHANGES_REQUESTED
Tested locally merged onto
origin/main(63bcca43) at PR headeace224f(Windows 11 x64, NVIDIA RTX 5090 + Microsoft WARP,VICTORY_D3D12_DEBUG=1).Blocking issue:
end - startunderflow insync_heaps()when a placed heap is not 64 KiB-alignedIn
crates/victory-gpu/src/descriptor.rs(L190-L204),sync_heaps()roundsatdown to a 64 KiB boundary (let mut at = from & !(page_size - 1);) and then steps byVirtualQuery'sRegionSize, which has 4 KiB OS-page granularity:If a placed descriptor heap starts past a 64 KiB boundary (e.g.
start = pool + 0x2000, sofrom = pool + 0x2000andat = pool) and[pool .. pool + 0x1000)is a separate committed 4 KiB region (for instance,[pool + 0x1000 .. pool + 0x2000)is decommitted or has a different protection),VirtualQuery(pool)returnsnext = pool + 0x1000 < start. Thenend = next.min(to) = pool + 0x1000 < start, so(end as u64 - start)underflows:pool + 0x2000with[pool + 0x1000 .. pool + 0x2000)decommitted:panicked at crates\victory-gpu\src\descriptor.rs:204:72: attempt to subtract with overflowend - startnearu64::MAX, clampinglasttoheap.desc.countand walking the entire descriptor heap from an unrelated preceding region.origin/main(63bcca43), the same test passes because each step advances by a full 64 KiBpage_size, keepingpage + page_size > from >= start.Verified fix
VirtualQueryalready rounds any address down to its containing OS region'sBaseAddress, so starting directly atlet mut at = from;(and dropping the unusedpage_sizelocal) guaranteesnext > at >= from >= startwhile also avoiding querying memory beforefrom. Using(info.BaseAddress as usize).saturating_add(info.RegionSize)also avoids any high-address overflow.Verified locally with
let mut at = from;: the unaligned-heap reproducer plus alldescriptor::tests::pass (9/9 on RTX 5090 and 9/9 on WARP), keeping the ~8.5x region-walk speedup (4.56 ms->538.6 µson 256 MB).cargo xtask policy(191 files ok) and strict clippy (default +--all-features) are clean. Synthetic tests only.Imported from Marvisak/VIctory. Originally posted by borosf on 2026-11-26T20:45:33Z.
Fixed at
f38b294with exactly your fix: the walk now starts atfromitself (readable_regions, extracted as a testable helper) — VirtualQuery rounds down to the containing region's base, so everynextis pastat >= from >= start, andBaseAddress.saturating_add(RegionSize)removes the high-address overflow. The old round-down-to-64-KiB start is gone.Regression test
descriptor::tests::readable_regions_of_an_unaligned_start_never_reach_before_it: decommits a 4 KiB page inside a pool and walks frompool + 8192(unaligned, past the hole) — asserts the first region starts atfromand every range is within[from, to)withend > start. The aligned-start case asserts the hole is skipped. On the old code this input is the debug-buildattempt to subtract with overflowat the slot math.Re-verified at
f38b294(Windows 11 x64, B580): both new tests pass; fullvictory-gpulib 231 passed / 2 failed (only the known #1744/#1829 baseline); workspace clippy clean-D warningsdefault +--all-features; fmt gate 84 hunks vs 94 baseline;cargo xtask policyok. Speedup kept: 256 MB probe 4.80 ms per-page reads vs 786 µs region walk this run (~6-8x). Synthetic tests only; no game data.Imported from Marvisak/VIctory. Originally posted by fuzeofficlal on 2026-11-26T21:11:16Z.
Review: APPROVED
Thanks for the quick turnaround — re-verified at
b1349a7fmerged ontoorigin/main(63bcca43) on Windows 11 x64 (NVIDIA GeForce RTX 5090 + Microsoft WARP,VICTORY_D3D12_DEBUG=1):readable_regions(crates/victory-gpu/src/descriptor.rs:175-198) starting directly atfromwith(info.BaseAddress as usize).saturating_add(info.RegionSize)completely resolves both the unaligned placed-heap(end - start)underflow and high-address overflow.start = pool + 0x2000with[pool + 0x1000 .. pool + 0x2000)decommitted, callingsync_heaps()directly) plus high-addressreadable_regions(usize::MAX - 4096, usize::MAX)and alldescriptor::tests::*: 10/10 pass on RTX 5090 and 10/10 pass on WARP, keeping the ~10.8x probe speedup (4.45 ms->413.7 µsover 256 MB / 4,095 pages).cargo xtask policy(191 files ok) andcargo clippy --locked -p victory-gpu --tests -- -D warnings(both default and--features diagnostics) are clean.One tiny non-blocking nit for a quick cleanup if you touch this again: at
crates/victory-gpu/src/descriptor.rs:168-200, insertingreadable_regionsbetween the existing#[cfg(windows)]attribute (line 168) andpub(crate) fn sync_heaps()(line 200) left duplicate#[cfg(windows)]attributes (andsync_heaps's doc comment) onreadable_regionswhile leavingsync_heaps()without#[cfg(windows)](Windows CI is unaffected, though non-Windowscargo checkofvictory-gpuwill see it).LGTM!
Imported from Marvisak/VIctory. Originally posted by KainanP on 2026-11-27T12:31:15Z.
Contention numbers for the
SHADER_HEAPSwait in #3425, this PR (b1349a7) against its base63bcca4:sync_heaps, RTX, 12 runsIt was slower in every setup. My guess at the cause: the bench heap is a
MEM_WRITE_WATCHallocation like the title's, and our guards split it into many small regions with different protections.VirtualQueryis slow on such pools (watch::runtime_pagesnotes "askingVirtualQuerycosts milliseconds in the runtime's big pools").a_region_walk_classifies_pages_like_per_page_readstimes a plain committed allocation, which has none of this. No views were lost or stale, and the PR's tests pass. The fullvictory-gpusuite on RTX fails only the two tests thatmainalso fails here.Bench:
sync_contention_bench(#[ignore]d; commitf34bcfb8, cherry-picks onto any tree). A placed, write-watched view heap of 2^20 slots (the host heap caps it at 934,464 here). Each frame, one thread rewrites 200 random 64 KiB pages (50 changed records each) and runs onesync_heaps, while 5 title threads write a placed-buffer SRV every 20 µs into pages of their own. "Wait" is the title threads' time in view writes beyond the uncontended 2–3 µs, summed per frame. Release build, RTX 4060 Laptop, runs interleaved with the base, medians of 30 frames per run. The bench also checks that every view written before a sync is the slot's view address after it, and that every slot is right after a final quiet sync.AI use: Claude Code (Claude Opus 5.5).
Imported from Marvisak/VIctory. Originally posted by borosf on 2026-11-27T13:12:00Z.
Closing this on @KainanP's contention numbers, which I accept over my own.
Their bench (
sync_contention_bench, a placed write-watched view heap of 2^20 slots with title threads writing views during eachsync_heaps) measured the change as a regression in every configuration:sync_heaps9.6 → 13.4 ms on RTX, 7.9 → 14.2 ms on the game-shaped heap, 10.1 → 15.8 ms on Intel UHD, and title-thread wait 49.5 → 69.1 ms. That is not a disagreement about a noisy number; it is the same direction on four setups.The cause is one the code already documents and my measurement missed:
VirtualQuerycosts milliseconds in the title's big pools, which areMEM_WRITE_WATCHallocations split into many small regions with differing protections. My timing test (a_region_walk_classifies_pages_like_per_page_reads) used a plain committed allocation, which has none of that structure — so it measured the one case where the region walk wins, and generalised from it. The 4.56 ms → 538 µs I reported was real for that allocation and irrelevant to the title.For the record, since this PR also carried a genuine fix that should not be lost: the round-down-to-64-KiB start made
end - startunderflow when a placed heap began past a boundary, which panicked in debug and walked a whole heap from an unrelated region in release. #3429's report of it stands, and if a future PR wants the region walk it should keep (a) starting the walk atfromrather than below it, and (b) using it only where the pool is not write-watched, with the per-page kernel read kept for the title's pools.Thanks for the bench and for pushing back with numbers rather than impressions.
Pull request closed