GPU: ask whether guarded memory is a view once per block, not per asking #490

Closed
github-bot wants to merge 2 commits from gh-pr/490/head into gh-pr/490/base
Owner

@⁠wbkepler on GitHub · 2026-11-24 04:02 UTC · original
wbkepler:gpu/guard-view-cache → main · +225 −22 · 1 files · 2 commits · closed without merging

Stacked on #464. This branch contains #464's commit; only the second commit is new. Review that commit alone, or after #464 merges.

Problem

Memory that can't be write-watched (physical-page views) goes through watch::guard. Each asking began with VirtualQuery on its start to check that the memory is a view. In the world, the 174 MB GPU heap and blocks inside it are asked about thousands of times a frame. With temporary timers on #464, that one query took 94–235 ms per world frame. By comparison, the rest of the guard took 4–7 ms of VirtualProtect and 4–10 ms of opening blocks.

Change

crates/victory-gpu/src/watch.rs only:

  • Guards gains a views set: the blocks where an asking began and that the query found to be in a view. A later asking starting in such a block skips the query.
  • The check and the insert happen under the guards' lock. title_memory_remapped takes that lock and drops the blocks in its range from views, so a block's entry can't outlive a map or unmap. The runtime calls the hook after mapping a view and before unmapping one.
  • Memory that isn't a view is never cached. It is queried every time, as before; its host copies stop asking after the first answer anyway.

Measured

Diagnostics build, RTX 5090, same save, start screen to world. One run each: #464 alone, then #464 plus this change. These are the copystat world windows. The guard time is the temporary timer around guard; the other numbers come from the existing counters.

per world frame #464 + this
guard time (temporary timer) 102–237 ms for 27–47 GB asked 24–35 ms for 21–35 GB asked
watches, total 118–264 ms 38–52 ms
preparing 195–444 ms 195–357 ms

Example world lines:

  • #464: 2.5 fps, 400.0 ms/frame (195.0 preparing: copies 129.6 ms ...), 5475 watches 118.62 ms
  • #464: 1.8 fps, 550.0 ms/frame (268.9 preparing: copies 151.9 ms ...)
  • with this: 2.0 fps, 488.8 ms/frame (194.7 preparing: copies 67.3 ms ...), 1572 draws/frame, 5475 watches 38.35 ms, 39817.9 MB checked

Frame-to-frame scenes vary a lot. The watch-time drop is the clear signal; preparing improves less visibly within the noise.

I also tried walking a guarded range by runs of blocks instead of looking up every block. It made no measurable difference, so it isn't included: the query was the cost, not the walk.

Tests

  • New watch::tests::views_are_queried_once_until_remapped. A test-only per-thread counter shows that a view is queried once per start block, and again after title_memory_remapped. Private memory is queried, and refused, every time.
  • The existing guard tests pass.
  • cargo fmt --check (watch.rs), cargo clippy --workspace --all-targets, cargo clippy -p victory --features diagnostics --all-targets (no new warnings), cargo test --workspace, and cargo xtask policy all pass.

🤖 Generated with Claude Code

**@⁠wbkepler** on GitHub · 2026-11-24 04:02 UTC · [original](https://github.com/Marvisak/VIctory/pull/490) `wbkepler:gpu/guard-view-cache` → `main` · +225 −22 · 1 files · 2 commits · closed without merging **Stacked on #464.** This branch contains #464's commit; only the second commit is new. Review that commit alone, or after #464 merges. ## Problem Memory that can't be write-watched (physical-page views) goes through `watch::guard`. Each asking began with `VirtualQuery` on its start to check that the memory is a view. In the world, the 174 MB GPU heap and blocks inside it are asked about thousands of times a frame. With temporary timers on #464, that one query took **94–235 ms per world frame**. By comparison, the rest of the guard took 4–7 ms of `VirtualProtect` and 4–10 ms of opening blocks. ## Change `crates/victory-gpu/src/watch.rs` only: - `Guards` gains a `views` set: the blocks where an asking began and that the query found to be in a view. A later asking starting in such a block skips the query. - The check and the insert happen under the guards' lock. `title_memory_remapped` takes that lock and drops the blocks in its range from `views`, so a block's entry can't outlive a map or unmap. The runtime calls the hook after mapping a view and before unmapping one. - Memory that isn't a view is never cached. It is queried every time, as before; its host copies stop asking after the first answer anyway. ## Measured Diagnostics build, RTX 5090, same save, start screen to world. One run each: #464 alone, then #464 plus this change. These are the `copystat` world windows. The guard time is the temporary timer around `guard`; the other numbers come from the existing counters. | per world frame | #464 | + this | |---|---|---| | guard time (temporary timer) | 102–237 ms for 27–47 GB asked | 24–35 ms for 21–35 GB asked | | watches, total | 118–264 ms | 38–52 ms | | preparing | 195–444 ms | 195–357 ms | Example world lines: - #464: `2.5 fps, 400.0 ms/frame (195.0 preparing: copies 129.6 ms ...)`, `5475 watches 118.62 ms` - #464: `1.8 fps, 550.0 ms/frame (268.9 preparing: copies 151.9 ms ...)` - with this: `2.0 fps, 488.8 ms/frame (194.7 preparing: copies 67.3 ms ...), 1572 draws/frame`, `5475 watches 38.35 ms, 39817.9 MB checked` Frame-to-frame scenes vary a lot. The watch-time drop is the clear signal; preparing improves less visibly within the noise. I also tried walking a guarded range by runs of blocks instead of looking up every block. It made no measurable difference, so it isn't included: the query was the cost, not the walk. ## Tests - New `watch::tests::views_are_queried_once_until_remapped`. A test-only per-thread counter shows that a view is queried once per start block, and again after `title_memory_remapped`. Private memory is queried, and refused, every time. - The existing guard tests pass. - `cargo fmt --check` (watch.rs), `cargo clippy --workspace --all-targets`, `cargo clippy -p victory --features diagnostics --all-targets` (no new warnings), `cargo test --workspace`, and `cargo xtask policy` all pass. 🤖 Generated with [Claude Code](https://claude.com/claude-code)
A copy of view memory (physical-page views, which cannot be
write-watched) guards each 64 KiB block it took read-only. A block that
is not mapped (a placeholder between views of a GPU heap) cannot be made
read-only, so it never became clean: every asking reported it written
again, and the copy read it through the per-page fallback, whose
failing kernel reads cost hundreds of ms per world frame.

Note such blocks absent (only after VirtualProtect fails and the block is
not committed). An absent block is not reported again until
title_memory_remapped, which the runtime calls after mapping or
unmapping views, clears it. The copy keeps the zeros it took.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Every asking about memory that cannot be write-watched called
VirtualQuery on its start to tell whether it is a view (which is then
guarded). In the world that query cost 100-230 ms per frame, more than
the rest of the guard together: the 174 MB GPU heap and blocks inside it
are asked about thousands of times a frame.

Remember the blocks found to be in a view where an asking began, under
the guards' lock, until title_memory_remapped (which the runtime calls
when it maps or unmaps views) drops them. Memory that is not a view is
asked about each time, as before (its copies stop asking after the first
answer).

Co-Authored-By: Claude Opus 5.5 <[email protected]>
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 04:05:57 +00:00
github-bot closed this pull request 2026-11-26 04:05:57 +00:00
Author
Owner

@⁠pablogonz12 on GitHub · 2026-11-24 16:20 UTC · original

Measured on Linux (RX 9070 XT, RADV/Mesa 26.3.1, GE-Proton11-14, vkd3d-proton master, diagnostics build), 240 s world runs, last 8 frame-rate lines, 2 runs per side. Head e6f9159 (+#464) merged onto our tree, which also carries #393/#124 — the watch.rs conflict was resolved into one writes with #393's journal fast path over this PR's views/absent bookkeeping:

copies ms/frame write-watch ms/frame fps
before #393/#124 2431 / 2571 1338 / 1351 0.29 / 0.35
with #393/#124 2036 / 1732 942 / 829 0.35 / 0.26
+ this PR 628 / 864 459 / 573 0.46 / 0.45

No device removed, no panics. victory-gpu tests under Wine: watch 7/7, shadow 4/4.
Not tested: native Windows; in-game on this PR alone (the row above is the merged tree).

**@⁠pablogonz12** on GitHub · 2026-11-24 16:20 UTC · [original](https://github.com/Marvisak/VIctory/pull/490#issuecomment-7137513855) Measured on Linux (RX 9070 XT, RADV/Mesa 26.3.1, GE-Proton11-14, vkd3d-proton master, diagnostics build), 240 s world runs, last 8 frame-rate lines, 2 runs per side. Head e6f9159 (+#464) merged onto our tree, which also carries #393/#124 — the watch.rs conflict was resolved into one `writes` with #393's journal fast path over this PR's `views`/`absent` bookkeeping: | | copies ms/frame | write-watch ms/frame | fps | |---|---|---|---| | before #393/#124 | 2431 / 2571 | 1338 / 1351 | 0.29 / 0.35 | | with #393/#124 | 2036 / 1732 | 942 / 829 | 0.35 / 0.26 | | + this PR | 628 / 864 | 459 / 573 | 0.46 / 0.45 | No device removed, no panics. victory-gpu tests under Wine: watch 7/7, shadow 4/4. Not tested: native Windows; in-game on this PR alone (the row above is the merged tree).
Author
Owner

@⁠wbkepler on GitHub · 2026-11-24 20:50 UTC · original

Rebased onto current main (ffe310e) as 1d46278, on top of the rebased #464 (9dd1507, which keeps main's saturating_add guard); no content conflicts after restacking. Gate clean.

AI-assisted (Claude Code); checked on Windows 11 / RTX 5090.

**@⁠wbkepler** on GitHub · 2026-11-24 20:50 UTC · [original](https://github.com/Marvisak/VIctory/pull/490#issuecomment-7144131345) Rebased onto current main (`ffe310e`) as `1d46278`, on top of the rebased #464 (`9dd1507`, which keeps main's `saturating_add` guard); no content conflicts after restacking. Gate clean. _AI-assisted (Claude Code); checked on Windows 11 / RTX 5090._

Pull request closed

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!490
No description provided.