Skip to content

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

Closed
TG0000 wants to merge 1 commit into
Marvisak:mainfrom
TG0000:perf/view-memory-cache
Closed

TG0000 wants to merge 1 commit into
Marvisak:mainfrom
TG0000:perf/view-memory-cache

Conversation

@TG0000

@TG0000 TG0000 commented Nov 24, 2026

Copy link
Copy Markdown
Contributor

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

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

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 <noreply@anthropic.com>
edsuuu referenced this pull request in edsuuu/VI-BR-PORT Nov 24, 2026
… it is remapped

Head 6739366 (TG0000). Conflicts in watch.rs: guard() asks our
allocation-type query (`mapped`, now `mapped_until`, which also gives
the view region's end) once per block until a remap, outside the lock,
and keeps #1025's announced blocks open. title_memory_remapped keeps
our empty-range return, placed-texture eviction and bindless refresh,
and counts remaps and forgets the remembered views. xmem.rs: every
unmapping of a view, the undo of a failed mapping too, goes through
unmap_title_view (reported after the unmapping).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
edsuuu referenced this pull request in edsuuu/VI-BR-PORT Nov 24, 2026
…re-copying them every submission

Head 41af42e (wbkepler). Conflicts in watch.rs: Guards holds our
announced blocks, #1277's view cache and #464's absent blocks;
title_memory_remapped forgets all three for the range and keeps our
placed-texture eviction. Tests: both sides'.

#490 (one view query per block) is left out: #1277, merged before,
does the same with the query outside the lock and the unmapping
reported after it.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
edsuuu referenced this pull request in edsuuu/VI-BR-PORT Nov 24, 2026
#1215 #1135 #1296 #1277 #1238 #464 #1073 #1057 #1084 #416 #704 #621.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@adamdotpy

Copy link
Copy Markdown

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.

@Marvisak

Copy link
Copy Markdown
Owner

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 Marvisak closed this Nov 25, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants