GPU: remember which title memory is view memory until it is remapped #1277

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

@⁠TG0000 on GitHub · 2026-11-24 17:02 UTC · original
TG0000:perf/view-memory-cache → main · +159 −22 · 3 files · 1 commits · closed without merging

Why

The write guards ask VirtualQuery whether a range is view memory at each check, about 900 times in a world frame. In the title's big heap each call walks the page tables.

What

  • Blocks known to be view memory are remembered until they are remapped. Memory that is not a view is asked about each time, as before.
  • The query runs without the guards' lock, which title threads take at their writes to guarded blocks. An answer from before a remapping is not remembered.
  • The runtime reported an unmapping before doing it, so a check in between would have remembered a view that was going away (and left its block guarded). It now reports it after.

Measured

Time of a looped world frame's submissions (preparation, GPU, copy-back), all ways in one launch, RX 6800 on Windows; 8 frames, each repeated with every way. This is not frame time, and nothing here is an fps claim. One launch, one area, in a build of main with measuring tools and other local changes, the same in every row.

submissions, ms per frame (about 285 of it GPU in every row)
this change and four other copy changes of mine (#1267 and others, not all open) 551
the same four without this change +98 (62 to 171) against the row above

Medians of the paired differences with the range over the 8 frames; a control (same switches, another order) differed by 9 ms. A build that also asked each time found no wrong answer in 23,140 checks in the world.

Tests

  • remapped_memory_is_asked_about_again, a_check_during_the_unmapping_leaves_nothing_behind (fails when the report comes before the unmapping). cargo test --workspace --locked (344 passed), clippy with -D warnings with and without --all-features on Windows, clippy on macOS: pass at 6739366.
  • One recorded frame (24 submissions with shader writes) replayed with this and without: title memory and the copies are byte for byte equal. This exercises the copies, not remapping during a frame.

Overlap and conflicts

  • #1025 changes kernel_will_write and watch.rs, #1122 the guards of all views of physical pages and the unmapping in xmem.rs: both conflict here. If either goes in first I rebase.
  • #943, #991, #1010, #1024, #1124, #1215 and #1267 merge cleanly.

Not verified

  • NVIDIA and Intel GPUs; Linux and Proton.
  • Frame time and fps in the game.
  • A remapping while the world streams, beyond the tests above and the 23,140 checks.

AI-assisted (Claude Code).

🤖 Generated with Claude Code

**@⁠TG0000** on GitHub · 2026-11-24 17:02 UTC · [original](https://github.com/Marvisak/VIctory/pull/1277) `TG0000:perf/view-memory-cache` → `main` · +159 −22 · 3 files · 1 commits · closed without merging ## Why The write guards ask `VirtualQuery` whether a range is view memory at each check, about 900 times in a world frame. In the title's big heap each call walks the page tables. ## What - Blocks known to be view memory are remembered until they are remapped. Memory that is not a view is asked about each time, as before. - The query runs without the guards' lock, which title threads take at their writes to guarded blocks. An answer from before a remapping is not remembered. - The runtime reported an unmapping before doing it, so a check in between would have remembered a view that was going away (and left its block guarded). It now reports it after. ## Measured Time of a looped world frame's submissions (preparation, GPU, copy-back), all ways in one launch, RX 6800 on Windows; 8 frames, each repeated with every way. **This is not frame time, and nothing here is an fps claim.** One launch, one area, in a build of `main` with measuring tools and other local changes, the same in every row. | | submissions, ms per frame (about 285 of it GPU in every row) | |---|---| | this change and four other copy changes of mine (#1267 and others, not all open) | 551 | | the same four without this change | +98 (62 to 171) against the row above | Medians of the paired differences with the range over the 8 frames; a control (same switches, another order) differed by 9 ms. A build that also asked each time found no wrong answer in 23,140 checks in the world. ## Tests - `remapped_memory_is_asked_about_again`, `a_check_during_the_unmapping_leaves_nothing_behind` (fails when the report comes before the unmapping). `cargo test --workspace --locked` (344 passed), clippy with `-D warnings` with and without `--all-features` on Windows, clippy on macOS: pass at 6739366. - One recorded frame (24 submissions with shader writes) replayed with this and without: title memory and the copies are byte for byte equal. This exercises the copies, not remapping during a frame. ## Overlap and conflicts - #1025 changes `kernel_will_write` and `watch.rs`, #1122 the guards of all views of physical pages and the unmapping in `xmem.rs`: both conflict here. If either goes in first I rebase. - #943, #991, #1010, #1024, #1124, #1215 and #1267 merge cleanly. ## Not verified - NVIDIA and Intel GPUs; Linux and Proton. - Frame time and fps in the game. - A remapping while the world streams, beyond the tests above and the 23,140 checks. AI-assisted (Claude Code). 🤖 Generated with [Claude Code](https://claude.com/claude-code)
The write guards asked VirtualQuery whether a range is a view at each check,
~900 times per world frame; in the title's big heap each walks the page
tables: 60 to 137 ms per frame (RX 6800), now 10 to 15 for whole checks.
It is asked without holding the guards' lock, and an answer from before a
remapping is not remembered. The runtime reported an unmapping before doing
it: a check in between would have remembered a view that was going away, so
it reports it after. A build that asked as well in the world found no wrong
answer in 23,140.

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

@⁠adamdotpy on GitHub · 2026-11-24 20:09 UTC · original

Reviewed at head 6739366 (worktree on the PR head, isolated target dir, Windows 11 / Rust 1.98.1). Findings, ranked:

1. The caching is correct about invalidation. guard() now remembers, per block, that the memory is a mapped view (Guards.views), so the world heap's ~900 checks per frame skip the VirtualQuery (the 60–130 ms you measured). The cache is dropped for a range on title_memory_remapped, and a remaps counter handles the check-in-flight race: guard() drops the lock for the query, re-locks, and only records the answer if remaps is unchanged — so a remap that happened during the query discards the stale answer instead of caching it for good.

2. The unmap path fixes a real ordering bug. xmem::unmap previously called title_memory_remapped before UnmapViewOfFile2, so a guard() racing inside that window would query the still-mapped view, guard the blocks, and remember them — for good, since the later clearing had already run. The new unmap_title_view (exported from victory-gpu) unmaps first and then forgets via title_memory_remapped, and the map failure path uses it too. The new a_check_during_the_unmapping_leaves_nothing_behind test pins exactly that interleaving (guard during unmap → after, views/protected/clean are empty for the range, the next guard returns None, and fresh private memory at the same addresses is not guarded). remapped_memory_is_asked_about_again pins the cache-and-invalidate behavior. Both pass here, plus the pre-existing watch tests: cargo test -p victory-gpu --lib watch 5 passed.

3. Full checks on 6739366: cargo test -p victory-gpu 52 passed / 0 failed / 1 ignored; cargo test -p victory-hle 94 passed + 1 integration suite, 0 failed; cargo clippy -p victory-gpu -p victory-hle --all-targets --all-features --locked -- -D warnings clean; cargo build --release -p victory builds. rustfmt --check: xmem.rs clean, watch.rs has 8 hunks vs main's 2 pre-existing — about 6 are in the new code (cosmetic; CI has no fmt job).

4. One residual race, noted not blocking: if a remap lands between guard()'s query and its re-lock, the query answer is discarded for the views cache but the block loop still guards based on the stale (pre-remap) MEM_MAPPED answer. The fault handler re-queries at write time and unguards non-view memory, so it self-heals on first touch; making guard() re-query when remaps changed would close it fully if you want zero window.

Nothing found that would block this.

**@⁠adamdotpy** on GitHub · 2026-11-24 20:09 UTC · [original](https://github.com/Marvisak/VIctory/pull/1277#issuecomment-7143132185) Reviewed at head `6739366` (worktree on the PR head, isolated target dir, Windows 11 / Rust 1.98.1). Findings, ranked: **1. The caching is correct about invalidation.** `guard()` now remembers, per block, that the memory is a mapped view (`Guards.views`), so the world heap's ~900 checks per frame skip the `VirtualQuery` (the 60–130 ms you measured). The cache is dropped for a range on `title_memory_remapped`, and a `remaps` counter handles the check-in-flight race: `guard()` drops the lock for the query, re-locks, and only records the answer if `remaps` is unchanged — so a remap that happened during the query discards the stale answer instead of caching it for good. **2. The unmap path fixes a real ordering bug.** `xmem::unmap` previously called `title_memory_remapped` *before* `UnmapViewOfFile2`, so a `guard()` racing inside that window would query the still-mapped view, guard the blocks, and remember them — for good, since the later clearing had already run. The new `unmap_title_view` (exported from `victory-gpu`) unmaps first and then forgets via `title_memory_remapped`, and the `map` failure path uses it too. The new `a_check_during_the_unmapping_leaves_nothing_behind` test pins exactly that interleaving (guard during unmap → after, `views`/`protected`/`clean` are empty for the range, the next `guard` returns `None`, and fresh private memory at the same addresses is not guarded). `remapped_memory_is_asked_about_again` pins the cache-and-invalidate behavior. Both pass here, plus the pre-existing watch tests: `cargo test -p victory-gpu --lib watch` 5 passed. **3. Full checks on `6739366`:** `cargo test -p victory-gpu` 52 passed / 0 failed / 1 ignored; `cargo test -p victory-hle` 94 passed + 1 integration suite, 0 failed; `cargo clippy -p victory-gpu -p victory-hle --all-targets --all-features --locked -- -D warnings` clean; `cargo build --release -p victory` builds. `rustfmt --check`: `xmem.rs` clean, `watch.rs` has 8 hunks vs main's 2 pre-existing — about 6 are in the new code (cosmetic; CI has no fmt job). **4. One residual race, noted not blocking:** if a remap lands *between* `guard()`'s query and its re-lock, the query answer is discarded for the `views` cache but the block loop still guards based on the stale (pre-remap) `MEM_MAPPED` answer. The fault handler re-queries at write time and unguards non-view memory, so it self-heals on first touch; making `guard()` re-query when `remaps` changed would close it fully if you want zero window. Nothing found that would block this.
Author
Owner

@⁠Marvisak on GitHub · 2026-11-25 00:36 UTC · original

Automated triage for the maintainers. Closing because this change is already in main, or main has since replaced the code it patches.

Caches the per-check view query of the old guard; main has no per-check query (trackers ask once when made) and remapping is reported by the runtime.

If this is mistaken, reply here with what was missed (or push a change and say so) and it will be looked at again. Thank you for the contribution.

**@⁠Marvisak** on GitHub · 2026-11-25 00:36 UTC · [original](https://github.com/Marvisak/VIctory/pull/1277#issuecomment-7148689617) Automated triage for the maintainers. Closing because this change is already in `main`, or `main` has since replaced the code it patches. Caches the per-check view query of the old guard; main has no per-check query (trackers ask once when made) and remapping is reported by the runtime. If this is mistaken, reply here with what was missed (or push a change and say so) and it will be looked at again. Thank you for the contribution.

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