gpu: sync_heaps walks regions with VirtualQuery instead of a kernel read per page #3356

Open
github-bot wants to merge 3 commits from github-bot/gh-3356 into main AGit
Owner

@⁠borosf on GitHub · 2026-11-26 20:21 UTC · original
borosf:vibe/sync-heaps-query-842ca5 → main · +126 −8 · 1 files · 3 commits · open

What

descriptor::sync_heaps probed every 64 KiB page of a written heap range with a ReadProcessMemory call just to test mapped-ness (the title unmaps heap pages too). It now walks the range once with VirtualQuery: a page is synced iff its region is MEM_COMMIT and not PAGE_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 one sync_slots call 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_reads asserts the page sets are identical (including a PAGE_GUARD page in the middle, excluded by both) and prints:

cargo test -p victory-gpu --lib --locked -- --test-threads=1 --exact descriptor::tests::a_region_walk_classifies_pages_like_per_page_reads --nocapture
256 MB, 4095 pages: per-page reads 5.167ms, region walk 648.1µs   (8x)

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_slots lock 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 warnings and --all-features — clean
  • fmt gate: descriptor.rs 83 hunks vs 94 at the origin/main baseline (decreased, none added)
  • cargo xtask policy — ok

Synthetic tests only; no game data used.

**@⁠borosf** on GitHub · 2026-11-26 20:21 UTC · [original](https://github.com/Marvisak/VIctory/pull/3356) `borosf:vibe/sync-heaps-query-842ca5` → `main` · +126 −8 · 1 files · 3 commits · open ## What `descriptor::sync_heaps` probed every 64 KiB page of a written heap range with a `ReadProcessMemory` call just to test mapped-ness (the title unmaps heap pages too). It now walks the range once with `VirtualQuery`: a page is synced iff its region is `MEM_COMMIT` and not `PAGE_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 one `sync_slots` call 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_reads` asserts the page sets are identical (including a `PAGE_GUARD` page in the middle, excluded by both) and prints: ``` cargo test -p victory-gpu --lib --locked -- --test-threads=1 --exact descriptor::tests::a_region_walk_classifies_pages_like_per_page_reads --nocapture 256 MB, 4095 pages: per-page reads 5.167ms, region walk 648.1µs (8x) ``` 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_slots` lock 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 warnings` and `--all-features` — clean - fmt gate: descriptor.rs 83 hunks vs 94 at the origin/main baseline (decreased, none added) - `cargo xtask policy` — ok Synthetic tests only; no game data used.
Each 64 KiB page of a written heap range cost one ReadProcessMemory just to
test mapped-ness; one VirtualQuery walk answers the same pages (committed
and not PAGE_NOACCESS/PAGE_GUARD) in one syscall per region. Measured:
256 MB / 4,095 pages, identical page set — per-page reads 5.17 ms, region
walk 648 us (8x). At FIND-31's ~13,624 pages opened per frame that is
~17 ms -> ~2 ms per frame. Synthetic test only; no game data.

Generated by Mistral Vibe.
Co-Authored-By: Mistral Vibe <[email protected]>
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 20:26:18 +00:00
Author
Owner

@⁠Dukestar69 on GitHub · 2026-11-26 20:29 UTC · original · approved these changes

LGTM — verified on RTX 5090 (Windows / D3D12) against origin/main (63bcca4).

  • Replacing per-64 KiB-page ReadProcessMemory probes in sync_heaps with a VirtualQuery region walk (MEM_COMMIT and neither PAGE_NOACCESS nor PAGE_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 (including a_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).

**@⁠Dukestar69** on GitHub · 2026-11-26 20:29 UTC · [original](https://github.com/Marvisak/VIctory/pull/3356#pullrequestreview-6080177229) · **approved these changes** LGTM — verified on RTX 5090 (Windows / D3D12) against `origin/main` (`63bcca4`). - Replacing per-64 KiB-page `ReadProcessMemory` probes in `sync_heaps` with a `VirtualQuery` region walk (`MEM_COMMIT` and neither `PAGE_NOACCESS` nor `PAGE_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 (including `a_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).
Author
Owner

@⁠fuzeofficlal on GitHub · 2026-11-26 20:38 UTC · original · requested changes

Tested locally merged onto origin/main (63bcca43) at PR head eace224f (Windows 11 x64, NVIDIA RTX 5090 + Microsoft WARP, VICTORY_D3D12_DEBUG=1).

Blocking issue: end - start underflow in sync_heaps() when a placed heap is not 64 KiB-aligned

In crates/victory-gpu/src/descriptor.rs (L190-L204), sync_heaps() rounds at down to a 64 KiB boundary (let mut at = from & !(page_size - 1);) and then steps by VirtualQuery's RegionSize, which has 4 KiB OS-page granularity:

let mut at = from & !(page_size - 1);
while at < to {
    ...
    let next = (info.BaseAddress as usize + info.RegionSize).max(at + 1).min(to);
    if info.State == MEM_COMMIT && info.Protect & (PAGE_NOACCESS | PAGE_GUARD) == 0 {
        let (page, end) = (at.max(from), next.min(to));
        let (first, last) = ((page as u64 - start) / step, (end as u64 - start).div_ceil(step));

If a placed descriptor heap starts past a 64 KiB boundary (e.g. start = pool + 0x2000, so from = pool + 0x2000 and at = 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) returns next = pool + 0x1000 < start. Then end = next.min(to) = pool + 0x1000 < start, so (end as u64 - start) underflows:

  • In debug builds: panics immediately. Reproduced locally via a placed shader-visible heap at pool + 0x2000 with [pool + 0x1000 .. pool + 0x2000) decommitted:
    panicked at crates\victory-gpu\src\descriptor.rs:204:72: attempt to subtract with overflow
  • In release builds: wraps end - start near u64::MAX, clamping last to heap.desc.count and walking the entire descriptor heap from an unrelated preceding region.
  • On pristine origin/main (63bcca43), the same test passes because each step advances by a full 64 KiB page_size, keeping page + page_size > from >= start.

Verified fix

VirtualQuery already rounds any address down to its containing OS region's BaseAddress, so starting directly at let mut at = from; (and dropping the unused page_size local) guarantees next > at >= from >= start while also avoiding querying memory before from. 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 all descriptor::tests:: pass (9/9 on RTX 5090 and 9/9 on WARP), keeping the ~8.5x region-walk speedup (4.56 ms -> 538.6 µs on 256 MB). cargo xtask policy (191 files ok) and strict clippy (default + --all-features) are clean. Synthetic tests only.

**@⁠fuzeofficlal** on GitHub · 2026-11-26 20:38 UTC · [original](https://github.com/Marvisak/VIctory/pull/3356#pullrequestreview-6080264906) · **requested changes** Tested locally merged onto `origin/main` (`63bcca43`) at PR head `eace224f` (Windows 11 x64, NVIDIA RTX 5090 + Microsoft WARP, `VICTORY_D3D12_DEBUG=1`). ### Blocking issue: `end - start` underflow in `sync_heaps()` when a placed heap is not 64 KiB-aligned In `crates/victory-gpu/src/descriptor.rs` (`L190-L204`), `sync_heaps()` rounds `at` down to a 64 KiB boundary (`let mut at = from & !(page_size - 1);`) and then steps by `VirtualQuery`'s `RegionSize`, which has 4 KiB OS-page granularity: ```rust let mut at = from & !(page_size - 1); while at < to { ... let next = (info.BaseAddress as usize + info.RegionSize).max(at + 1).min(to); if info.State == MEM_COMMIT && info.Protect & (PAGE_NOACCESS | PAGE_GUARD) == 0 { let (page, end) = (at.max(from), next.min(to)); let (first, last) = ((page as u64 - start) / step, (end as u64 - start).div_ceil(step)); ``` If a placed descriptor heap starts past a 64 KiB boundary (e.g. `start = pool + 0x2000`, so `from = pool + 0x2000` and `at = 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)` returns `next = pool + 0x1000 < start`. Then `end = next.min(to) = pool + 0x1000 < start`, so `(end as u64 - start)` **underflows**: - In debug builds: panics immediately. Reproduced locally via a placed shader-visible heap at `pool + 0x2000` with `[pool + 0x1000 .. pool + 0x2000)` decommitted: `panicked at crates\victory-gpu\src\descriptor.rs:204:72: attempt to subtract with overflow` - In release builds: wraps `end - start` near `u64::MAX`, clamping `last` to `heap.desc.count` and walking the entire descriptor heap from an unrelated preceding region. - On pristine `origin/main` (`63bcca43`), the same test passes because each step advances by a full 64 KiB `page_size`, keeping `page + page_size > from >= start`. ### Verified fix `VirtualQuery` already rounds any address down to its containing OS region's `BaseAddress`, so starting directly at `let mut at = from;` (and dropping the unused `page_size` local) guarantees `next > at >= from >= start` while also avoiding querying memory before `from`. 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 all `descriptor::tests::` pass (9/9 on RTX 5090 and 9/9 on WARP), keeping the ~8.5x region-walk speedup (`4.56 ms` -> `538.6 µs` on 256 MB). `cargo xtask policy` (191 files ok) and strict clippy (default + `--all-features`) are clean. Synthetic tests only.
VirtualQuery rounds down to the containing region's base, so rounding the
walk down to 64 KiB first could make the first region end before a heap
placed past a boundary (start = pool + 0x2000 with the preceding 4 KiB
decommitted), underflowing (end - start) in debug and wrapping to a whole-
heap walk in release. Start at from itself; saturating_add on the region
end. Regression test: decommitted 4 KiB hole before an unaligned start.
Reported on #3356. Synthetic tests only; no game data.

Generated by Mistral Vibe.
Co-Authored-By: Mistral Vibe <[email protected]>
Author
Owner

@⁠borosf on GitHub · 2026-11-26 20:45 UTC · original

Fixed at f38b294 with exactly your fix: the walk now starts at from itself (readable_regions, extracted as a testable helper) — VirtualQuery rounds down to the containing region's base, so every next is past at >= from >= start, and BaseAddress.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 from pool + 8192 (unaligned, past the hole) — asserts the first region starts at from and every range is within [from, to) with end > start. The aligned-start case asserts the hole is skipped. On the old code this input is the debug-build attempt to subtract with overflow at the slot math.

Re-verified at f38b294 (Windows 11 x64, B580): both new tests pass; full victory-gpu lib 231 passed / 2 failed (only the known #1744/#1829 baseline); workspace clippy clean -D warnings default + --all-features; fmt gate 84 hunks vs 94 baseline; cargo xtask policy ok. 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.

**@⁠borosf** on GitHub · 2026-11-26 20:45 UTC · [original](https://github.com/Marvisak/VIctory/pull/3356#issuecomment-7203043065) Fixed at `f38b294` with exactly your fix: the walk now starts at `from` itself (`readable_regions`, extracted as a testable helper) — VirtualQuery rounds down to the containing region's base, so every `next` is past `at >= from >= start`, and `BaseAddress.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 from `pool + 8192` (unaligned, past the hole) — asserts the first region starts at `from` and every range is within `[from, to)` with `end > start`. The aligned-start case asserts the hole is skipped. On the old code this input is the debug-build `attempt to subtract with overflow` at the slot math. Re-verified at `f38b294` (Windows 11 x64, B580): both new tests pass; full `victory-gpu` lib 231 passed / 2 failed (only the known #1744/#1829 baseline); workspace clippy clean `-D warnings` default + `--all-features`; fmt gate 84 hunks vs 94 baseline; `cargo xtask policy` ok. 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.
Author
Owner

@⁠fuzeofficlal on GitHub · 2026-11-26 21:11 UTC · original · approved these changes

Thanks for the quick turnaround — re-verified at b1349a7f merged onto origin/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 at from with (info.BaseAddress as usize).saturating_add(info.RegionSize) completely resolves both the unaligned placed-heap (end - start) underflow and high-address overflow.
  • Re-ran the exact end-to-end placed-heap reproducer (start = pool + 0x2000 with [pool + 0x1000 .. pool + 0x2000) decommitted, calling sync_heaps() directly) plus high-address readable_regions(usize::MAX - 4096, usize::MAX) and all descriptor::tests::*: 10/10 pass on RTX 5090 and 10/10 pass on WARP, keeping the ~10.8x probe speedup (4.45 ms -> 413.7 µs over 256 MB / 4,095 pages).
  • cargo xtask policy (191 files ok) and cargo 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, inserting readable_regions between the existing #[cfg(windows)] attribute (line 168) and pub(crate) fn sync_heaps() (line 200) left duplicate #[cfg(windows)] attributes (and sync_heaps's doc comment) on readable_regions while leaving sync_heaps() without #[cfg(windows)] (Windows CI is unaffected, though non-Windows cargo check of victory-gpu will see it).

LGTM!

**@⁠fuzeofficlal** on GitHub · 2026-11-26 21:11 UTC · [original](https://github.com/Marvisak/VIctory/pull/3356#pullrequestreview-6080515082) · **approved these changes** Thanks for the quick turnaround — re-verified at `b1349a7f` merged onto `origin/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 at `from` with `(info.BaseAddress as usize).saturating_add(info.RegionSize)` completely resolves both the unaligned placed-heap `(end - start)` underflow and high-address overflow. - Re-ran the exact end-to-end placed-heap reproducer (`start = pool + 0x2000` with `[pool + 0x1000 .. pool + 0x2000)` decommitted, calling `sync_heaps()` directly) plus high-address `readable_regions(usize::MAX - 4096, usize::MAX)` and all `descriptor::tests::*`: **10/10 pass on RTX 5090 and 10/10 pass on WARP**, keeping the ~10.8x probe speedup (`4.45 ms` -> `413.7 µs` over 256 MB / 4,095 pages). - `cargo xtask policy` (`191 files ok`) and `cargo 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`, inserting `readable_regions` between the existing `#[cfg(windows)]` attribute (line 168) and `pub(crate) fn sync_heaps()` (line 200) left duplicate `#[cfg(windows)]` attributes (and `sync_heaps`'s doc comment) on `readable_regions` while leaving `sync_heaps()` without `#[cfg(windows)]` (Windows CI is unaffected, though non-Windows `cargo check` of `victory-gpu` will see it). LGTM!
This pull request can be merged automatically.
You are not authorized to merge this pull request.
View command line instructions

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u origin '+refs/pull/3356/head:github-bot/gh-3356'
git switch 'github-bot/gh-3356'

Merge

Merge the changes and update on Forgejo.
git switch 'main'
git merge --no-ff 'github-bot/gh-3356'
git switch 'github-bot/gh-3356'
git rebase 'main'
git switch 'main'
git merge --ff-only 'github-bot/gh-3356'
git switch 'github-bot/gh-3356'
git rebase 'main'
git switch 'main'
git merge --no-ff 'github-bot/gh-3356'
git switch 'main'
git merge --squash 'github-bot/gh-3356'
git switch 'main'
git merge --ff-only 'github-bot/gh-3356'
git switch 'main'
git merge 'github-bot/gh-3356'
git push origin 'main'
This discussion has been locked. Commenting is limited to contributors.
No reviewers
No milestone
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set

Reference
github/Marvisak--VIctory!3356
No description provided.