Watch: unmapped view blocks are parked, not asked about at every check #812

Open
github-bot wants to merge 1 commit from github-bot/gh-812 into main AGit
Owner

@⁠Peppacodes on GitHub · 2026-11-25 08:32 UTC · original
Peppacodes:vf/view-guard-parking → main · +398 −9 · 4 files · 1 commits · open

The guards' walk over a view range asks Windows (VirtualQuery, through uncommitted_end) about every block it noted absent, at every check. In the first room ~86,000-96,000 such blocks lie in the ranges host copies cover, nearly all of them 64 KiB placeholders, each its own region, so each question covers one block: ~88,000 questions a frame.

Why this matters

  • On an RTX 5080 with a Ryzen 7 5700X3D the guards take 26.2 ms of the submitting thread's frame on main (9376fd6), and 20 ms on an integration branch of 310c6eb.
  • Where the submitting thread sets the frame, that is frame time: on that integration branch the frame went from 135.8-144.7 ms to a median of 127.9 ms.

The fix (crates/victory-gpu/src/watch.rs, behind VICTORY_VIEW_SET, on by default; VICTORY_VIEW_SET=0 is the walk exactly as before)

  • A placeholder can only be filled by a call that replaces it: a plain VirtualAlloc(MEM_COMMIT) into one fails. On title memory the only such call is xmem::map, which already calls title_memory_remapped. So a block found uncommitted inside a placeholder is parked: it is not asked about again until it is remapped.
  • VirtualQuery cannot tell a placeholder from an ordinary no-access reservation (same state, type and protection). QueryVirtualMemoryInformation(MemoryRegionInfo) can: flag bit 8 (placeholder reservation) is set on fresh, split and unmapped-to placeholders, and never on ordinary reservations. A one-time check on a placeholder of our own keeps the walk wherever the flag is not filled (Wine may not fill it).
  • Parked blocks that are not in a placeholder are looked at again once per 32 submissions' checks (VICTORY_PARK_EPOCH). XMemVirtualAlloc now announces its commits (victory_gpu::title_memory_committed), so a commit through the runtime is seen at once. A commit nobody announces is seen within an epoch; the walk saw it at the next check.
  • Checks are answered from a set of the view blocks that are not clean. A range with none is one lookup; otherwise only those blocks are walked, with the walk's rules. The set changes wherever clean does (the walk, protect_here, open_here, remaps). protect_alias never marks a block clean, so the alias guards from 8dd30ab are untouched.
  • known_view also answers yes for a range already known to be view memory, so a range whose first block was written asks no failing write-watch question (follows #670).
  • A frame line counts the checks answered in one lookup, the walked ones, the parked blocks and the questions to Windows.

How it was found. Per frame there were ~10,500 view checks, ~5,200 of which walked ~87,000 blocks that were not clean, while only ~280 guard faults a frame marked blocks as newly written. The blocks that stayed not clean were the unmapped ones. #685 (open) also walks only the blocks that are not clean, through an interval map, but unmapped blocks are not clean, so it still asks about each one at every check. Here a not-clean set alone left the guards at 19.7 ms; parking took them to 7-8 ms.

Tested on main (9376fd6) plus this commit, on Windows 11 with an RTX 5080 (driver 621.24), a Ryzen 7 5700X3D and 32 GB:

  • One five-minute game run each way, first room, 1080p (median of the last 20 frame lines):

    VICTORY_VIEW_SET frame guards all write checks submitting thread, kernel
    0 (main as it is) 253.1 ms 26.2 ms 35.9 ms 71.9 ms
    1 250.8 ms 14.4 ms 21.4 ms 64.6 ms

    On main the frame here is set by the copy-back thread (~160 ms, ~107 of it waiting for fetches of card copies' pieces), so the saved time does not show in the frame yet.

  • The same change on an integration branch of 310c6eb (with #220, #144 and other open PRs), before the alias guards came in: 123.1 / 155.0 / 127.9 ms a frame over three runs with guards at 7.0-8.2 ms, against 135.8 and 144.7 ms for the walk with guards at 20 ms. Frames swing ~15 % between identical runs on this machine, so the guards line is the measure.

  • Dead ends measured on the way: an answer per submission for the uncommitted regions (238.6 ms a frame, because each block is its own region) and a look at every parked block once per 32 submissions (200.9 ms, ~200,000 questions a frame).

  • cargo test -p victory-gpu --features diagnostics --lib watch::: 13 passed, with VICTORY_VIEW_SET unset and with =0 (the four alias tests included). New test parked_blocks_come_back_when_committed covers announced and unannounced commits. writes_in_partly_committed_view_blocks_are_reported now steps the epoch for its unannounced commit, and unmapped_blocks_are_reported_once_until_mapped checks that its placeholder blocks are parked and not looked at again.

  • cargo test -p victory-hle --lib xmem: 12 passed.

  • cargo clippy --workspace --all-targets --locked -- -D warnings, with and without --all-features: the only errors are three already on main, none in a file this touches (an unused placed_filler_twins in resource.rs, an unread depth_written in indirect.rs, clippy::unnecessary_sort_by in draw.rs). With those three allowed: clean.

  • cargo test --workspace --locked: 541 passed, 0 failed, 10 ignored.

  • The game runs were of this change before two small edits: the one-time placeholder check, and a binary search for the views in title_memory_remapped. The tests and clippy ran on this commit.

Not checked: Wine.

AI-assisted (Claude Code).

**@⁠Peppacodes** on GitHub · 2026-11-25 08:32 UTC · [original](https://github.com/fxceless/victory-fork/pull/812) `Peppacodes:vf/view-guard-parking` → `main` · +398 −9 · 4 files · 1 commits · open The guards' walk over a view range asks Windows (`VirtualQuery`, through `uncommitted_end`) about every block it noted absent, at every check. In the first room ~86,000-96,000 such blocks lie in the ranges host copies cover, nearly all of them 64 KiB placeholders, each its own region, so each question covers one block: ~88,000 questions a frame. **Why this matters** - On an RTX 5080 with a Ryzen 7 5700X3D the guards take 26.2 ms of the submitting thread's frame on `main` (`9376fd6`), and 20 ms on an integration branch of `310c6eb`. - Where the submitting thread sets the frame, that is frame time: on that integration branch the frame went from 135.8-144.7 ms to a median of 127.9 ms. **The fix** (`crates/victory-gpu/src/watch.rs`, behind `VICTORY_VIEW_SET`, on by default; `VICTORY_VIEW_SET=0` is the walk exactly as before) - A placeholder can only be filled by a call that replaces it: a plain `VirtualAlloc(MEM_COMMIT)` into one fails. On title memory the only such call is `xmem::map`, which already calls `title_memory_remapped`. So a block found uncommitted inside a placeholder is **parked**: it is not asked about again until it is remapped. - `VirtualQuery` cannot tell a placeholder from an ordinary no-access reservation (same state, type and protection). `QueryVirtualMemoryInformation(MemoryRegionInfo)` can: flag bit 8 (placeholder reservation) is set on fresh, split and unmapped-to placeholders, and never on ordinary reservations. A one-time check on a placeholder of our own keeps the walk wherever the flag is not filled (Wine may not fill it). - Parked blocks that are not in a placeholder are looked at again once per 32 submissions' checks (`VICTORY_PARK_EPOCH`). `XMemVirtualAlloc` now announces its commits (`victory_gpu::title_memory_committed`), so a commit through the runtime is seen at once. A commit nobody announces is seen within an epoch; the walk saw it at the next check. - Checks are answered from a set of the view blocks that are not clean. A range with none is one lookup; otherwise only those blocks are walked, with the walk's rules. The set changes wherever `clean` does (the walk, `protect_here`, `open_here`, remaps). `protect_alias` never marks a block clean, so the alias guards from `8dd30ab` are untouched. - `known_view` also answers yes for a range already known to be view memory, so a range whose first block was written asks no failing write-watch question (follows #670). - A frame line counts the checks answered in one lookup, the walked ones, the parked blocks and the questions to Windows. **How it was found.** Per frame there were ~10,500 view checks, ~5,200 of which walked ~87,000 blocks that were not clean, while only ~280 guard faults a frame marked blocks as newly written. The blocks that stayed not clean were the unmapped ones. #685 (open) also walks only the blocks that are not clean, through an interval map, but unmapped blocks are not clean, so it still asks about each one at every check. Here a not-clean set alone left the guards at 19.7 ms; parking took them to 7-8 ms. **Tested** on `main` (`9376fd6`) plus this commit, on Windows 11 with an RTX 5080 (driver 621.24), a Ryzen 7 5700X3D and 32 GB: - One five-minute game run each way, first room, 1080p (median of the last 20 frame lines): | `VICTORY_VIEW_SET` | frame | guards | all write checks | submitting thread, kernel | |---|---|---|---|---| | `0` (main as it is) | 253.1 ms | 26.2 ms | 35.9 ms | 71.9 ms | | `1` | 250.8 ms | 14.4 ms | 21.4 ms | 64.6 ms | On `main` the frame here is set by the copy-back thread (~160 ms, ~107 of it waiting for fetches of card copies' pieces), so the saved time does not show in the frame yet. - The same change on an integration branch of `310c6eb` (with #220, #144 and other open PRs), before the alias guards came in: 123.1 / 155.0 / 127.9 ms a frame over three runs with guards at 7.0-8.2 ms, against 135.8 and 144.7 ms for the walk with guards at 20 ms. Frames swing ~15 % between identical runs on this machine, so the guards line is the measure. - Dead ends measured on the way: an answer per submission for the uncommitted regions (238.6 ms a frame, because each block is its own region) and a look at every parked block once per 32 submissions (200.9 ms, ~200,000 questions a frame). - `cargo test -p victory-gpu --features diagnostics --lib watch::`: 13 passed, with `VICTORY_VIEW_SET` unset and with `=0` (the four alias tests included). New test `parked_blocks_come_back_when_committed` covers announced and unannounced commits. `writes_in_partly_committed_view_blocks_are_reported` now steps the epoch for its unannounced commit, and `unmapped_blocks_are_reported_once_until_mapped` checks that its placeholder blocks are parked and not looked at again. - `cargo test -p victory-hle --lib xmem`: 12 passed. - `cargo clippy --workspace --all-targets --locked -- -D warnings`, with and without `--all-features`: the only errors are three already on `main`, none in a file this touches (an unused `placed_filler_twins` in `resource.rs`, an unread `depth_written` in `indirect.rs`, `clippy::unnecessary_sort_by` in `draw.rs`). With those three allowed: clean. - `cargo test --workspace --locked`: 541 passed, 0 failed, 10 ignored. - The game runs were of this change before two small edits: the one-time placeholder check, and a binary search for the views in `title_memory_remapped`. The tests and clippy ran on this commit. Not checked: Wine. AI-assisted (Claude Code).
The guards' walk over a view range asked Windows (VirtualQuery, through
uncommitted_end) about every block it had noted absent, at every check.
In the first room ~86,000-96,000 such blocks lie in the ranges host
copies cover, nearly all 64 KiB placeholders, each its own region: some
88,000 questions a frame, 26 ms of the submitting thread's frame on an
RTX 5080.

A placeholder can only be filled by a call that replaces it (a plain
VirtualAlloc commit into one fails), and on title memory the only such
call is xmem's map, which tells title_memory_remapped. So a block found
uncommitted in a placeholder is parked: not asked about again until it
is remapped. QueryVirtualMemoryInformation(MemoryRegionInfo) tells a
placeholder apart by flag bit 8; VirtualQuery cannot. A one-time check
on a placeholder of our own keeps the walk where the flag is not filled.
Parked blocks that are no placeholder are looked at once per 32
submissions' checks (VICTORY_PARK_EPOCH), and XMemVirtualAlloc announces
its commits (title_memory_committed).

Checks are answered from a set of the view blocks that are not clean: a
range with none is one lookup, otherwise only those blocks are walked,
with the walk's rules. The set changes wherever clean does; the alias
guards never mark a block clean. known_view also answers for a known
view range. VICTORY_VIEW_SET=0 is the walk as before. A frame line
counts the checks, parked blocks and questions to Windows.

RTX 5080, first room, one run each way on this base: guards 26.2 ->
14.4 ms, all write checks 35.9 -> 21.4 ms, frame 253.1 -> 250.8 ms (the
copy-back thread sets the frame here). On an integration branch of
310c6eb with the same change, guards 20 -> 7-8 ms and the frame median
135.8-144.7 -> 127.9 ms over three runs.

Tests: watch tests with the switch on and off; new
parked_blocks_come_back_when_committed; the partly committed test steps
the epoch for its unannounced commit; the placeholder test checks its
blocks are parked and not looked at.
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 03:51:40 +00:00
Author
Owner

@⁠MamaLeVrai on GitHub · 2026-11-25 09:54 UTC · original

Test report on real hardware: AMD RX 6950 XT (RDNA2), Windows 11, Rust 1.98.1, with a GPU present. I merged this PR's head e277b98 into main 9376fd6 in a throwaway tree (no conflict). cargo clippy --workspace --all-targets --locked -- -D warnings fails, with the same two errors as main itself on this machine (function placed_filler_twins is never used, field depth_written is never read, victory-gpu): this PR adds none and removes none. cargo test --workspace --locked: 541 passed, 0 failed, 10 ignored, two runs (main alone: 540 passed). The title was not run, so this says nothing about in-game effect. No game data.

AI-assisted (Claude Code); no game data.

**@⁠MamaLeVrai** on GitHub · 2026-11-25 09:54 UTC · [original](https://github.com/fxceless/victory-fork/pull/812#issuecomment-7158952035) Test report on real hardware: AMD RX 6950 XT (RDNA2), Windows 11, Rust 1.98.1, with a GPU present. I merged this PR's head `e277b98` into `main` `9376fd6` in a throwaway tree (no conflict). `cargo clippy --workspace --all-targets --locked -- -D warnings` fails, with the same two errors as `main` itself on this machine (`function placed_filler_twins is never used`, `field depth_written is never read`, `victory-gpu`): this PR adds none and removes none. `cargo test --workspace --locked`: 541 passed, 0 failed, 10 ignored, two runs (`main` alone: 540 passed). The title was not run, so this says nothing about in-game effect. No game data. AI-assisted (Claude Code); no game data.
Author
Owner

@⁠Ym1r1710 on GitHub · 2026-11-25 21:29 UTC · original

Heads up, found a problem with this one in my tree. When the title maps one physical page at several addresses, a write through one view opens all of them, and the check of the first view guards them all again. protect_alias (and the batched run version if you also have #1059) then does g.unclean.remove(&block) for the other views' blocks too, so when those views get checked there is nothing left to look at and their host copies keep the bytes from before the write.

watch::tests::a_write_is_reported_at_every_checked_view fails with this PR applied. Leaving the block in the unclean set there (just insert into protected, no remove; guard_unclean already takes it out once it reports it) makes the test pass again. Ran it in game on an RTX 5080 after the change, two runs to world+800, picture fine and frame time unchanged.

**@⁠Ym1r1710** on GitHub · 2026-11-25 21:29 UTC · [original](https://github.com/fxceless/victory-fork/pull/812#issuecomment-7175151400) Heads up, found a problem with this one in my tree. When the title maps one physical page at several addresses, a write through one view opens all of them, and the check of the first view guards them all again. protect_alias (and the batched run version if you also have #1059) then does g.unclean.remove(&block) for the other views' blocks too, so when those views get checked there is nothing left to look at and their host copies keep the bytes from before the write. watch::tests::a_write_is_reported_at_every_checked_view fails with this PR applied. Leaving the block in the unclean set there (just insert into protected, no remove; guard_unclean already takes it out once it reports it) makes the test pass again. Ran it in game on an RTX 5080 after the change, two runs to world+800, picture fine and frame time unchanged.
Author
Owner

@⁠grosterr on GitHub · 2026-11-25 23:03 UTC · original

Two notes from the #770 stack, which carries this PR, on an RTX 3060 12 GB:

  1. Main's VICTORY_ABSENT_ONCE (44bff61) goes after the same cost as this PR. Main's README for run 279 counts ~690,000 questions to Windows in 20 seconds about view blocks already known to be unmapped. With 100 ms notes, 40–50 % of the answers come from the notes, and no frame gain is shown yet. This PR's parking stops asking about such blocks in a placeholder at all until the runtime tells of a mapping there. With both in one tree, main's test a_block_noted_uncommitted_is_asked_about_again_only_when_its_note_is_old fails: it maps a block into a placeholder "without a word" and expects the next asking to find it, but here the block is parked. With VICTORY_VIEW_SET=0 the test passes. I made its expectation depend on view_set() (details in #770). If main takes this PR, ABSENT_ONCE may matter only where VIEW_SET is off (Wine, where placeholders are not told apart).

  2. On Ym1r1710's alias problem: at this PR's head (e277b98) and in the #770 stack (d034ac1), protect_alias only inserts into protected (and viewed) and does not touch unclean, and a_write_is_reported_at_every_checked_view passes in our tree. #1059 itself has no unclean (it is based on main without this PR), so the g.unclean.remove(&block) in protect_alias was probably added when the two were merged together in that tree. Anyone merging this PR with #1059 should keep protect_alias as it is here and run that test.

AI-assisted (Claude Code); checked by me.

**@⁠grosterr** on GitHub · 2026-11-25 23:03 UTC · [original](https://github.com/fxceless/victory-fork/pull/812#issuecomment-7177091899) Two notes from the #770 stack, which carries this PR, on an RTX 3060 12 GB: 1. **Main's `VICTORY_ABSENT_ONCE` (`44bff61`) goes after the same cost as this PR.** Main's README for run 279 counts ~690,000 questions to Windows in 20 seconds about view blocks already known to be unmapped. With 100 ms notes, 40–50 % of the answers come from the notes, and no frame gain is shown yet. This PR's parking stops asking about such blocks in a placeholder at all until the runtime tells of a mapping there. With both in one tree, main's test `a_block_noted_uncommitted_is_asked_about_again_only_when_its_note_is_old` fails: it maps a block into a placeholder "without a word" and expects the next asking to find it, but here the block is parked. With `VICTORY_VIEW_SET=0` the test passes. I made its expectation depend on `view_set()` (details in #770). If main takes this PR, `ABSENT_ONCE` may matter only where `VIEW_SET` is off (Wine, where placeholders are not told apart). 2. **On Ym1r1710's alias problem:** at this PR's head (`e277b98`) and in the #770 stack (`d034ac1`), `protect_alias` only inserts into `protected` (and `viewed`) and does not touch `unclean`, and `a_write_is_reported_at_every_checked_view` passes in our tree. #1059 itself has no `unclean` (it is based on main without this PR), so the `g.unclean.remove(&block)` in `protect_alias` was probably added when the two were merged together in that tree. Anyone merging this PR with #1059 should keep `protect_alias` as it is here and run that test. AI-assisted (Claude Code); checked by me.
This pull request has changes conflicting with the target branch.
  • crates/victory-gpu/src/host.rs
  • crates/victory-gpu/src/lib.rs
  • crates/victory-gpu/src/watch.rs
  • crates/victory-hle/src/modules/xmem.rs
View command line instructions

Manual merge helper

Use this merge commit message when completing the merge manually.

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u origin '+refs/pull/812/head:github-bot/gh-812'
git switch 'github-bot/gh-812'

Merge

Merge the changes and update on Forgejo.
git switch 'main'
git merge --no-ff 'github-bot/gh-812'
git switch 'github-bot/gh-812'
git rebase 'main'
git switch 'main'
git merge --ff-only 'github-bot/gh-812'
git switch 'github-bot/gh-812'
git rebase 'main'
git switch 'main'
git merge --no-ff 'github-bot/gh-812'
git switch 'main'
git merge --squash 'github-bot/gh-812'
git switch 'main'
git merge --ff-only 'github-bot/gh-812'
git switch 'main'
git merge 'github-bot/gh-812'
git push origin 'main'
This discussion has been locked. Commenting is limited to contributors.
No description provided.