GPU: remember unmapped guarded view blocks instead of re-copying them every submission #464

Closed
github-bot wants to merge 1 commit from gh-pr/464/head into gh-pr/464/base
Owner

@⁠wbkepler on GitHub · 2026-11-24 03:43 UTC · original
wbkepler:gpu/unmapped-guard-blocks → main · +137 −13 · 1 files · 1 commits · closed without merging

Problem

Host copies of view memory (physical-page views; they cannot be write-watched) learn what the title wrote by guarding each 64 KiB block read-only once copied (watch::guard). A block in the range that is not mapped (a placeholder between views of a big GPU heap) cannot be made read-only, so it never became clean. Every asking reported it written again, and copy_stale copied it again through copy_mapped's fallback: a failing 64 KiB kernel read, then 16 failing 4 KiB reads, then zeros. A world frame makes about 150 submissions, most of which refresh the same big heap, so the same unmapped blocks were re-copied each time.

Measured with temporary timing around the fallback loop (diagnostics build, RTX 5090, world): 380–1020 ms per frame in the fallback path, 460–930 MB/frame "unmapped", ~2,200 fallbacks/frame. That was the largest cost in "preparing".

Change

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

  • Guards gains an absent set. When protect fails on a block and VirtualQuery says the block is not committed, the block is noted as absent.
  • guard reports an absent block like a clean one: not written. The first asking after it went unmapped still reports it, so the copy takes its zeros once.
  • title_memory_remapped clears absent for its range. xmem already calls it after mapping a view and before unmapping one, so a block mapped later is reported (and copied) at the next asking.

Soundness: view memory comes from placeholders, and only the runtime's mapping (which notifies) can fill those. VirtualQuery runs only on a block whose protection change failed, so blocks we can guard never pay for it. Shortcut, noted in the module docs: if memory gets committed some other way into such a block (a range reaching from a view into a plain reservation), the copy won't see it.

Measured

Same save, start screen to world, diagnostics build of main with and without this change. One run each. The numbers are the copystat lines of steady world windows, per frame:

per world frame before after
fallback path time (temporary timer) 383–1021 ms 7–67 ms
unmapped bytes re-read 458–934 MB 5–62 MB
fallbacks 2,158–2,222 65–586
guarded blocks reported written 542–934 MB 32–309 MB
preparing 214–639 ms 210–350 ms
copies (of preparing) 163–484 ms 103–256 ms

Example world lines:

  • before: 1.5 fps, 683.5 ms/frame (406.5 preparing: copies 297.3 ms ...), 2030 draws/frame, 500.34 MB unmapped; ... 2222.5 fallbacks
  • after: 1.4 fps, 700.1 ms/frame (350.2 preparing: copies 197.8 ms ...), 4532 draws/frame, 27.06 MB unmapped; ... 286.5 fallbacks
  • after: 2.6 fps, 377.7 ms/frame (210.1 preparing: copies 103.4 ms ...), 2257 draws/frame, 16.87 MB unmapped; ... 247.0 fallbacks

What's left in preparing for follow-ups (not in this PR): the guard walk itself (one set lookup per block over the 174 MB heap for every asking, about 100–210 ms/frame), and page faults from write snapshots and copy-back.

Tests

  • New watch::tests::unmapped_blocks_are_reported_once_until_mapped: a three-block placeholder with a section view in block 0. The first asking reports all three blocks. The next reports none. After block 1 is mapped and title_memory_remapped runs, only block 1 is reported.
  • The existing guard tests pass. rustfmt now formats watch.rs cleanly; it had two unformatted test statements before, and one SAFETY comment moved with them.
  • 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 03:43 UTC · [original](https://github.com/Marvisak/VIctory/pull/464) `wbkepler:gpu/unmapped-guard-blocks` → `main` · +137 −13 · 1 files · 1 commits · closed without merging ## Problem Host copies of view memory (physical-page views; they cannot be write-watched) learn what the title wrote by guarding each 64 KiB block read-only once copied (`watch::guard`). A block in the range that is not mapped (a placeholder between views of a big GPU heap) cannot be made read-only, so it never became clean. Every asking reported it written again, and `copy_stale` copied it again through `copy_mapped`'s fallback: a failing 64 KiB kernel read, then 16 failing 4 KiB reads, then zeros. A world frame makes about 150 submissions, most of which refresh the same big heap, so the same unmapped blocks were re-copied each time. Measured with temporary timing around the fallback loop (diagnostics build, RTX 5090, world): **380–1020 ms per frame in the fallback path**, 460–930 MB/frame "unmapped", ~2,200 fallbacks/frame. That was the largest cost in "preparing". ## Change `crates/victory-gpu/src/watch.rs` only: - `Guards` gains an `absent` set. When `protect` fails on a block and `VirtualQuery` says the block is not committed, the block is noted as absent. - `guard` reports an absent block like a clean one: not written. The first asking after it went unmapped still reports it, so the copy takes its zeros once. - `title_memory_remapped` clears `absent` for its range. xmem already calls it after mapping a view and before unmapping one, so a block mapped later is reported (and copied) at the next asking. Soundness: view memory comes from placeholders, and only the runtime's mapping (which notifies) can fill those. `VirtualQuery` runs only on a block whose protection change failed, so blocks we can guard never pay for it. Shortcut, noted in the module docs: if memory gets committed some other way into such a block (a range reaching from a view into a plain reservation), the copy won't see it. ## Measured Same save, start screen to world, diagnostics build of `main` with and without this change. One run each. The numbers are the `copystat` lines of steady world windows, per frame: | per world frame | before | after | |---|---|---| | fallback path time (temporary timer) | 383–1021 ms | 7–67 ms | | unmapped bytes re-read | 458–934 MB | 5–62 MB | | fallbacks | 2,158–2,222 | 65–586 | | guarded blocks reported written | 542–934 MB | 32–309 MB | | preparing | 214–639 ms | 210–350 ms | | copies (of preparing) | 163–484 ms | 103–256 ms | Example world lines: - before: `1.5 fps, 683.5 ms/frame (406.5 preparing: copies 297.3 ms ...), 2030 draws/frame`, `500.34 MB unmapped; ... 2222.5 fallbacks` - after: `1.4 fps, 700.1 ms/frame (350.2 preparing: copies 197.8 ms ...), 4532 draws/frame`, `27.06 MB unmapped; ... 286.5 fallbacks` - after: `2.6 fps, 377.7 ms/frame (210.1 preparing: copies 103.4 ms ...), 2257 draws/frame`, `16.87 MB unmapped; ... 247.0 fallbacks` What's left in preparing for follow-ups (not in this PR): the guard walk itself (one set lookup per block over the 174 MB heap for every asking, about 100–210 ms/frame), and page faults from write snapshots and copy-back. ## Tests - New `watch::tests::unmapped_blocks_are_reported_once_until_mapped`: a three-block placeholder with a section view in block 0. The first asking reports all three blocks. The next reports none. After block 1 is mapped and `title_memory_remapped` runs, only block 1 is reported. - The existing guard tests pass. rustfmt now formats `watch.rs` cleanly; it had two unformatted test statements before, and one `SAFETY` comment moved with them. - `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]>
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 04:05:00 +00:00
github-bot closed this pull request 2026-11-26 04:05:00 +00:00
Author
Owner

@⁠DaviTostes on GitHub · 2026-11-24 03:46 UTC · original

Verified 41af42e on Arch Linux x86_64, Windows MSVC release build under Wine 11.22 (victory-gpu, --test-threads=1): 44 passed, 0 failed, 1 ignored.

Not covered: the runtime mapping/unmapping a guarded title block during submission (no game data, and this Wine prefix does not exercise the same page transitions).

**@⁠DaviTostes** on GitHub · 2026-11-24 03:46 UTC · [original](https://github.com/Marvisak/VIctory/pull/464#issuecomment-7121402519) Verified `41af42e` on **Arch Linux x86_64**, Windows MSVC release build under **Wine 11.22** (`victory-gpu`, `--test-threads=1`): **44 passed, 0 failed, 1 ignored**. Not covered: the runtime mapping/unmapping a guarded title block during submission (no game data, and this Wine prefix does not exercise the same page transitions).
Author
Owner

@⁠LocranKing on GitHub · 2026-11-24 04:18 UTC · original

Tested on Windows 11 + NVIDIA GeForce RTX 3060 Ti (8 GB, MSVC 1.98.1).

Tested

  • Commit: 41af42e (pr-464, clean tree)
  • Environment: Windows 11 Home Build 26200, AMD Ryzen 5 5500, 31.8 GB RAM, NVIDIA GeForce RTX 3060 Ti (driver 32.0.16.2101), Rust 1.98.1 (x86_64-pc-windows-msvc)
  • cargo xtask policy: asset policy: 151 files ok
  • cargo test --workspace: 265 passed, 0 failed, 4 ignored
  • diag build (VICTORY_D3D12_DRED=1) live boot/first-screen run (45 s) on RTX 3060 Ti:
    • 29.9–30.0 fps, 33.3–33.4 ms/frame (0.2 ms preparing, 0.1 ms copies, 5.8–8.3 ms on the GPU, 0.0 ms copying back), 13 draws/frame, 0 dropped (frame=1024: 6656/6656 draws, 512/512 dispatches).
    • copystat (steady state): 14.8–15.3 submissions, 69–71 title copies (0.1 MB), 74–76 refreshes (62.5–64.6 MB), 10 watches (0.12–0.13 ms, 13.1–13.5 MB checked), 0–1 page faults, 0.0 fallbacks. crashes.log empty.

Not tested: World streaming / world-load heap refreshes past the first screens, AMD GPUs.

AI use: Antigravity (Gemini 4 Hydrogen) ran the checks and diagnostics run and drafted this report from the logs.

**@⁠LocranKing** on GitHub · 2026-11-24 04:18 UTC · [original](https://github.com/Marvisak/VIctory/pull/464#issuecomment-7121898687) Tested on Windows 11 + NVIDIA GeForce RTX 3060 Ti (8 GB, MSVC `1.98.1`). **Tested** - Commit: `41af42e` (`pr-464`, clean tree) - Environment: Windows 11 Home Build 26200, AMD Ryzen 5 5500, 31.8 GB RAM, NVIDIA GeForce RTX 3060 Ti (driver `32.0.16.2101`), Rust `1.98.1` (`x86_64-pc-windows-msvc`) - `cargo xtask policy`: `asset policy: 151 files ok` - `cargo test --workspace`: **265 passed, 0 failed, 4 ignored** - `diag` build (`VICTORY_D3D12_DRED=1`) live boot/first-screen run (45 s) on RTX 3060 Ti: - **29.9–30.0 fps**, `33.3–33.4 ms/frame` (`0.2 ms` preparing, `0.1 ms` copies, `5.8–8.3 ms` on the GPU, `0.0 ms` copying back), `13` draws/frame, `0` dropped (`frame=1024`: `6656/6656` draws, `512/512` dispatches). - `copystat` (steady state): `14.8–15.3` submissions, `69–71` title copies (`0.1 MB`), `74–76` refreshes (`62.5–64.6 MB`), `10` watches (`0.12–0.13 ms`, `13.1–13.5 MB` checked), `0–1` page faults, `0.0` fallbacks. `crashes.log` empty. **Not tested:** World streaming / world-load heap refreshes past the first screens, AMD GPUs. **AI use:** Antigravity (`Gemini 4 Hydrogen`) ran the checks and diagnostics run and drafted this report from the logs.
Author
Owner

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

Rebased onto current main (ffe310e) as 9dd1507; in watch.rs the remap range keeps main's overflow-safe saturating_add while also clearing the unmapped set, and both tests are kept. Gate clean.

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

**@⁠wbkepler** on GitHub · 2026-11-24 20:48 UTC · [original](https://github.com/Marvisak/VIctory/pull/464#issuecomment-7144093387) Rebased onto current main (`ffe310e`) as `9dd1507`; in `watch.rs` the remap range keeps main's overflow-safe `saturating_add` while also clearing the unmapped set, and both tests are kept. 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!464
No description provided.