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

Closed
wbkepler wants to merge 2 commits from upstream-pr/490 into main
wbkepler commented 2026-11-24 04:02:33 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by wbkepler on 2026-11-24T04:02:33Z.


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

<!-- greyfinch-sync:Marvisak/VIctory:issue:6574327744 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/490). Originally posted by [wbkepler](https://github.com/wbkepler) on 2026-11-24T04:02:33Z. --- **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)
pablogonz12 commented 2026-11-24 17:04:23 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by pablogonz12 on 2026-11-24T16:20:41Z.


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).

<!-- greyfinch-sync:Marvisak/VIctory:comment:7137513855 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/490#issuecomment-7137513855). Originally posted by [pablogonz12](https://github.com/pablogonz12) on 2026-11-24T16:20:41Z. --- 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).
wbkepler commented 2026-11-24 21:03:42 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by wbkepler on 2026-11-24T20:50:39Z.


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.

<!-- greyfinch-sync:Marvisak/VIctory:comment:7144131345 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/490#issuecomment-7144131345). Originally posted by [wbkepler](https://github.com/wbkepler) on 2026-11-24T20:50:39Z. --- 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._
upstream-sync closed this pull request 2026-11-24 23:04:12 +00:00

Pull request closed

Sign in to join this conversation.
No reviewers
No labels
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.

Reference
Marvisak/VIctory!490
No description provided.