gpu: sync_heaps walks regions with VirtualQuery instead of a kernel read per page #3356
No reviewers
Labels
No labels
accessibility
area: build
area: formats
area: gpu
area: loader
area: runtime
area: shader
bug
documentation
duplicate
enhancement
good first issue
gpu: amd
gpu: intel
gpu: nvidia
help wanted
host: wine
invalid
out of scope
question
request
run report
triage: confirmed
triage: has patch
triage: merge candidate
triage: needs info
triage: needs rebase
triage: needs work
triage: proposed close
wontfix
No milestone
No assignees
1 participant
Notifications
Due date
No due date set.
Dependencies
No dependencies set
Reference
github/Marvisak--VIctory!3356
Loading…
Reference in a new issue
No description provided.
Delete branch "github-bot/gh-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?
@borosf on GitHub · 2026-11-26 20:21 UTC · original
borosf:vibe/sync-heaps-query-842ca5→main· +126 −8 · 1 files · 3 commits · openWhat
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.
@Dukestar69 on GitHub · 2026-11-26 20:29 UTC · original · approved these changes
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).
@fuzeofficlal on GitHub · 2026-11-26 20:38 UTC · original · requested changes
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.@borosf on GitHub · 2026-11-26 20:45 UTC · original
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.@fuzeofficlal on GitHub · 2026-11-26 21:11 UTC · original · approved these changes
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!
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.