xmem: mapping pages asks the host only for what it needs #2914

Open
0xkn0wh3r3 wants to merge 2 commits from upstream-pr/2916 into main
0xkn0wh3r3 commented 2026-11-25 23:53:25 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by 0xkn0wh3r3 on 2026-11-25T23:53:25Z.


What

XMemMapPhysicalPages and map_alias now skip two of the four host calls a mapping took (unmap, split, map, commit):

  • No split of a placeholder that is already the view's own. Unmapping a whole view records its placeholder (address and length). A later view of exactly that range is mapped straight over it. If the title has joined that placeholder with others since, MapViewOfFile3 fails (a replaced placeholder must be exactly the view's size) and the code splits and maps as before. The recorded placeholders are only a hint: nothing is recorded with VICTORY_MAP_LEAN=0, a map drops every one its range meets (the title releases and joins placeholders with the host's own VirtualFree, which we don't see), and at most 2^17 are kept, after which the map starts afresh.
  • No commit of section pages that are already committed. Each section keeps one bit per page, set when any view commits the page (the title's views or our own Views for DirectStorage writes). A section page committed through one view is committed in every view of it.

VICTORY_MAP_LEAN=0 makes both calls every time, as before.

Why

The title streams through one thread, which maps a batch's pages and then submits it, and every page gets mapped twice: into the title's pool, then by the GPU into its tiled heap (map_alias). Each call waits on the process's address-space lock, behind the GPU side's guards and the driver. The fork measured 200–400 µs per page in the world, against about 10 µs in a menu, so models arrived only as fast as their pages could be mapped.

Uncontended on this machine (map_bench, ignored; 64 pages mapped and unmapped 3,000 times, alternating runs), the time per page went from about 6.5 µs to 5.0 µs. Under lock contention each call costs far more, so dropping two of four should matter more there; I couldn't reproduce that contention here.

Nothing here depends on the GPU vendor.

Where it comes from

From the avatarmanz/VIctory-7900XTX fork, commit 49d7126, only its VICTORY_MAP_LEAN part, rewritten on main's xmem. Left out: its streaming log (VICTORY_DSTORAGE_LOG), its priority lift of the streaming thread (#2903 handles the title's idle-priority threads instead), and its thread-affinity names.

Related open PRs: #234 also keeps a per-page commit bitmap, bundled with persistent section views and overlapped DirectStorage reads. #1224 joins split placeholders; if both land, a recorded placeholder that #1224 has joined falls back to the split as described above.

Tested (Windows 11, RTX 5070 Ti)

  • cargo test -p victory-hle: 154 passed, 1 ignored (the benchmark). New tests:
    • pages_mapped_again_hold_what_they_held: three pages moved around eight slots for six rounds keep their bytes at both ends, with the placeholders joined halfway through (from the fork).
    • a_page_mapped_again_takes_its_own_placeholder: the page's commit bit is set; unmapping records the placeholder; mapping again uses it and finds the page committed; after the title joins the placeholders, mapping there still works and the bytes are kept.
  • cargo clippy -p victory-hle --all-targets -- -D warnings: clean.

Not tested

  • Not run with the title, so the in-world gain is unmeasured here; the 200–400 µs figure is the fork's.
  • Not tested under Wine. Sections there are committed whole and commit already returns early; the split path is unchanged.
<!-- greyfinch-sync:Marvisak/VIctory:issue:6616219866 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/2916). Originally posted by [0xkn0wh3r3](https://github.com/0xkn0wh3r3) on 2026-11-25T23:53:25Z. --- ## What `XMemMapPhysicalPages` and `map_alias` now skip two of the four host calls a mapping took (unmap, split, map, commit): - **No split of a placeholder that is already the view's own.** Unmapping a whole view records its placeholder (address and length). A later view of exactly that range is mapped straight over it. If the title has joined that placeholder with others since, `MapViewOfFile3` fails (a replaced placeholder must be exactly the view's size) and the code splits and maps as before. The recorded placeholders are only a hint: nothing is recorded with `VICTORY_MAP_LEAN=0`, a map drops every one its range meets (the title releases and joins placeholders with the host's own `VirtualFree`, which we don't see), and at most 2^17 are kept, after which the map starts afresh. - **No commit of section pages that are already committed.** Each section keeps one bit per page, set when any view commits the page (the title's views or our own `View`s for DirectStorage writes). A section page committed through one view is committed in every view of it. `VICTORY_MAP_LEAN=0` makes both calls every time, as before. ## Why The title streams through one thread, which maps a batch's pages and then submits it, and every page gets mapped twice: into the title's pool, then by the GPU into its tiled heap (`map_alias`). Each call waits on the process's address-space lock, behind the GPU side's guards and the driver. The fork measured 200–400 µs per page in the world, against about 10 µs in a menu, so models arrived only as fast as their pages could be mapped. Uncontended on this machine (`map_bench`, ignored; 64 pages mapped and unmapped 3,000 times, alternating runs), the time per page went from about 6.5 µs to 5.0 µs. Under lock contention each call costs far more, so dropping two of four should matter more there; I couldn't reproduce that contention here. Nothing here depends on the GPU vendor. ## Where it comes from From the avatarmanz/VIctory-7900XTX fork, commit [`49d7126`](https://github.com/avatarmanz/VIctory-7900XTX/commit/49d71266b25f8d432cfc86ec3d27c304a5191e66), only its `VICTORY_MAP_LEAN` part, rewritten on main's xmem. Left out: its streaming log (`VICTORY_DSTORAGE_LOG`), its priority lift of the streaming thread (#2903 handles the title's idle-priority threads instead), and its thread-affinity names. Related open PRs: #234 also keeps a per-page commit bitmap, bundled with persistent section views and overlapped DirectStorage reads. #1224 joins split placeholders; if both land, a recorded placeholder that #1224 has joined falls back to the split as described above. ## Tested (Windows 11, RTX 5070 Ti) - `cargo test -p victory-hle`: 154 passed, 1 ignored (the benchmark). New tests: - `pages_mapped_again_hold_what_they_held`: three pages moved around eight slots for six rounds keep their bytes at both ends, with the placeholders joined halfway through (from the fork). - `a_page_mapped_again_takes_its_own_placeholder`: the page's commit bit is set; unmapping records the placeholder; mapping again uses it and finds the page committed; after the title joins the placeholders, mapping there still works and the bytes are kept. - `cargo clippy -p victory-hle --all-targets -- -D warnings`: clean. ## Not tested - Not run with the title, so the in-world gain is unmeasured here; the 200–400 µs figure is the fork's. - Not tested under Wine. Sections there are committed whole and `commit` already returns early; the split path is unchanged.
kotekompotek commented 2026-11-26 00:03:17 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by kotekompotek on 2026-11-25T23:54:30Z.

Review: COMMENTED


Verified the mechanism: a second MEM_COMMIT on an already-committed page is a documented no-op, so tracking per-section commit bits and skipping the redundant call only drops the syscall+address-space-lock wait — never observable state. fetch_or marking is idempotent under races (two threads can only ever commit the same page twice, which was already legal). Default-on with the =0 escape is reasonable since behavior is unchanged. One note for maintainers: the atomic bitmap is per-Section and lives as long as the handle does, so views that outlive unmap/remap keep correct bookkeeping since committed state follows the section, not the view. LGTM.

<!-- greyfinch-sync:Marvisak/VIctory:review:6066656852 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/2916#pullrequestreview-6066656852). Originally posted by [kotekompotek](https://github.com/kotekompotek) on 2026-11-25T23:54:30Z. **Review:** COMMENTED --- Verified the mechanism: a second MEM_COMMIT on an already-committed page is a documented no-op, so tracking per-section commit bits and skipping the redundant call only drops the syscall+address-space-lock wait — never observable state. fetch_or marking is idempotent under races (two threads can only ever commit the same page twice, which was already legal). Default-on with the =0 escape is reasonable since behavior is unchanged. One note for maintainers: the atomic bitmap is per-Section and lives as long as the handle does, so views that outlive unmap/remap keep correct bookkeeping since committed state follows the section, not the view. LGTM.
AwsAli05 commented 2026-11-26 01:03:43 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by AwsAli05 on 2026-11-26T00:06:37Z.

Review: COMMENTED


unmap adds an entry to the process-wide Physical::holes map. map removes it only when the same base is mapped again. When the title releases a reservation or coalesces its placeholders, the remaining entries for those addresses stay in the map. Repeating this at new addresses grows the cache for the process lifetime, including with VICTORY_MAP_LEAN=0. Since this is a hint and a miss already falls back to splitting, could the map have a fixed bound and skip insertion when the optimization is disabled?

<!-- greyfinch-sync:Marvisak/VIctory:review:6066748817 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/2916#pullrequestreview-6066748817). Originally posted by [AwsAli05](https://github.com/AwsAli05) on 2026-11-26T00:06:37Z. **Review:** COMMENTED --- `unmap` adds an entry to the process-wide `Physical::holes` map. `map` removes it only when the same base is mapped again. When the title releases a reservation or coalesces its placeholders, the remaining entries for those addresses stay in the map. Repeating this at new addresses grows the cache for the process lifetime, including with `VICTORY_MAP_LEAN=0`. Since this is a hint and a miss already falls back to splitting, could the map have a fixed bound and skip insertion when the optimization is disabled?
fuzeofficlal commented 2026-11-26 01:03:43 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by fuzeofficlal on 2026-11-26T00:06:37Z.

Review: COMMENTED


Verified head ab91aa4d cleanly on origin/main (63bcca43) on Windows 11 x64.

Code & Safety Review

  • Section::committed (Acquire/Release atomic bitmap) safely avoids redundant VirtualAlloc(..., MEM_COMMIT) on already-committed NT section slices, and Physical::holes cleanly reuses exact placeholders freed by unmap while automatically falling back to the split-and-map path if MapViewOfFile3 fails (e.g. if an external release/coalesce invalidated a cached hole).
  • Complementary with partial-run rollback (#2742) and gated behind VICTORY_MAP_LEAN=0 escape hatch. Minor note (non-blocking): XMemVirtualFree(..., MEM_RELEASE) doesn't prune stale entries from holes; correctness is unaffected due to the MapViewOfFile3 fallback, only minor map retention if addresses aren't reused.

Local Verification & Benchmark

  • cargo test --locked -p victory-hle: 154/154 unit tests passed (1 ignored map_bench; includes pages_mapped_again_hold_what_they_held and a_page_mapped_again_takes_its_own_placeholder), plus fidelityfx (1/1) and no_dll_planting (1/1).
  • --release map_bench (--ignored --nocapture, 3 alternating runs):
    • lean false: 4.2 us / 3.8 us / 4.3 us
    • lean true: 3.0 us / 2.9 us / 3.5 us (~20–30% faster)
  • cargo clippy --locked -p victory-hle --tests -- -D warnings (default and --all-features) & cargo xtask policy (191 files ok): clean.

Looks clean and ready to merge.

<!-- greyfinch-sync:Marvisak/VIctory:review:6066748840 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/2916#pullrequestreview-6066748840). Originally posted by [fuzeofficlal](https://github.com/fuzeofficlal) on 2026-11-26T00:06:37Z. **Review:** COMMENTED --- Verified head `ab91aa4d` cleanly on `origin/main` (`63bcca43`) on Windows 11 x64. ### Code & Safety Review - `Section::committed` (`Acquire`/`Release` atomic bitmap) safely avoids redundant `VirtualAlloc(..., MEM_COMMIT)` on already-committed NT section slices, and `Physical::holes` cleanly reuses exact placeholders freed by `unmap` while automatically falling back to the split-and-map path if `MapViewOfFile3` fails (e.g. if an external release/coalesce invalidated a cached hole). - Complementary with partial-run rollback (`#2742`) and gated behind `VICTORY_MAP_LEAN=0` escape hatch. Minor note (non-blocking): `XMemVirtualFree(..., MEM_RELEASE)` doesn't prune stale entries from `holes`; correctness is unaffected due to the `MapViewOfFile3` fallback, only minor map retention if addresses aren't reused. ### Local Verification & Benchmark - `cargo test --locked -p victory-hle`: **154/154** unit tests passed (1 ignored `map_bench`; includes `pages_mapped_again_hold_what_they_held` and `a_page_mapped_again_takes_its_own_placeholder`), plus `fidelityfx` (1/1) and `no_dll_planting` (1/1). - `--release` `map_bench` (`--ignored --nocapture`, 3 alternating runs): - `lean false`: `4.2 us` / `3.8 us` / `4.3 us` - `lean true`: `3.0 us` / `2.9 us` / `3.5 us` (**~20–30% faster**) - `cargo clippy --locked -p victory-hle --tests -- -D warnings` (default and `--all-features`) & `cargo xtask policy` (`191 files ok`): clean. Looks clean and ready to merge.
Kajanos commented 2026-11-26 01:03:57 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by Kajanos on 2026-11-26T00:07:20Z.


Windows 11 / NVIDIA test run, head ab91aa4d64 merged onto main 63bcca43c6.

Setup: Windows 11 Home build 26300, RTX 5080, NVIDIA 621.40, rustc 1.98.1 MSVC, VS 2022 Build Tools, Windows SDK 10.0.26100 (dxil.dll from the SDK).

  • cargo test --locked -p victory-hle: 153 passed, 1 failed, 1 ignored.
  • The one failure is lifecycle::tests::showing_a_title_window_shows_its_stand_in, and it also fails on main in this run. The tests ran over SSH, i.e. in session 0 with no interactive desktop, so the window can't be shown. On the desktop session the same test passes on main (12/12 lifecycle). This PR doesn't touch lifecycle.

Not run: the retail title, clippy, and other crates.

AI-assisted (Claude Code)

<!-- greyfinch-sync:Marvisak/VIctory:comment:7178417446 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/2916#issuecomment-7178417446). Originally posted by [Kajanos](https://github.com/Kajanos) on 2026-11-26T00:07:20Z. --- **Windows 11 / NVIDIA test run**, head `ab91aa4d64` merged onto `main` `63bcca43c6`. Setup: Windows 11 Home build 26300, RTX 5080, NVIDIA 621.40, rustc 1.98.1 MSVC, VS 2022 Build Tools, Windows SDK 10.0.26100 (`dxil.dll` from the SDK). - `cargo test --locked -p victory-hle`: 153 passed, 1 failed, 1 ignored. - The one failure is `lifecycle::tests::showing_a_title_window_shows_its_stand_in`, and it also fails on `main` in this run. The tests ran over SSH, i.e. in session 0 with no interactive desktop, so the window can't be shown. On the desktop session the same test passes on `main` (12/12 `lifecycle`). This PR doesn't touch lifecycle. Not run: the retail title, clippy, and other crates. AI-assisted (Claude Code)
Marvisak commented 2026-11-26 03:05:10 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by Marvisak on 2026-11-26T02:22:56Z.


Automated triage for the maintainers.

Merge candidate (a recommendation to look, not an approval). In modules/xmem.rs, map now maps straight over a placeholder that an earlier whole-view unmap left (Physical::holes). If that fails, for example because the title has joined placeholders since, it falls back to split-then-map. Section::commit_shown also skips committing section pages that are already committed through any view. I checked that a failed direct map falls through to the old split path and that the hole hint is dropped on any overlapping map. The tests cover joined placeholders and bytes surviving remaps. VICTORY_MAP_LEAN=0 restores the old calls. CI check passes. The in-world gain is not measured here (uncontended, about 6.5 to 5.0 µs per page). Related: #234 (commit bitmap) and #1224 (joining placeholders) overlap.


Generated by Claude Code

<!-- greyfinch-sync:Marvisak/VIctory:comment:7180751451 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/2916#issuecomment-7180751451). Originally posted by [Marvisak](https://github.com/Marvisak) on 2026-11-26T02:22:56Z. --- <!-- victory-triage v1 head=4dd9a4a123c9f09226d324b1474dba999c935710 verdict=merge-candidate at=2026-11-26T02:22:49Z --> Automated triage for the maintainers. **Merge candidate** (a recommendation to look, not an approval). In `modules/xmem.rs`, `map` now maps straight over a placeholder that an earlier whole-view unmap left (`Physical::holes`). If that fails, for example because the title has joined placeholders since, it falls back to split-then-map. `Section::commit_shown` also skips committing section pages that are already committed through any view. I checked that a failed direct map falls through to the old split path and that the hole hint is dropped on any overlapping map. The tests cover joined placeholders and bytes surviving remaps. `VICTORY_MAP_LEAN=0` restores the old calls. CI `check` passes. The in-world gain is not measured here (uncontended, about 6.5 to 5.0 µs per page). Related: #234 (commit bitmap) and #1224 (joining placeholders) overlap. --- _Generated by [Claude Code](https://claude.ai/code)_
Dukestar69 commented 2026-11-26 09:05:26 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by Dukestar69 on 2026-11-26T08:13:13Z.

Review: APPROVED


Verified against origin/main (63bcca4):

  • cargo test -p victory-hle --lib modules::xmem:: -- --test-threads=1: 16/16 passed (1 ignored bench; including a_page_mapped_again_takes_its_own_placeholder and pages_mapped_again_hold_what_they_held).
  • cargo clippy -p victory-hle --all-targets -- -D warnings: clean.
  • cargo xtask policy: 191 files ok.

Cleanly reduces redundant host placeholder/mapping calls when mapping pages in xmem.

Synthetic tests only (no game files used). AI-assisted (Gemini 4 Neon).

<!-- greyfinch-sync:Marvisak/VIctory:review:6070515079 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/2916#pullrequestreview-6070515079). Originally posted by [Dukestar69](https://github.com/Dukestar69) on 2026-11-26T08:13:13Z. **Review:** APPROVED --- Verified against `origin/main` (`63bcca4`): - `cargo test -p victory-hle --lib modules::xmem:: -- --test-threads=1`: 16/16 passed (1 ignored bench; including `a_page_mapped_again_takes_its_own_placeholder` and `pages_mapped_again_hold_what_they_held`). - `cargo clippy -p victory-hle --all-targets -- -D warnings`: clean. - `cargo xtask policy`: 191 files ok. Cleanly reduces redundant host placeholder/mapping calls when mapping pages in `xmem`. Synthetic tests only (no game files used). AI-assisted (Gemini 4 Neon).
This pull request cannot be merged automatically due to conflicts.
Merge manually to resolve the conflicts.
View command line instructions

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u origin 'upstream-pr/2916:upstream-pr/2916'
git switch 'upstream-pr/2916'

Merge

Merge the changes and update on Forgejo.

Warning: The "Autodetect manual merge" setting is not enabled for this repository, you will have to mark this pull request as manually merged afterwards.

git switch 'main'
git merge --no-ff 'upstream-pr/2916'
git switch 'upstream-pr/2916'
git rebase 'main'
git switch 'main'
git merge --ff-only 'upstream-pr/2916'
git switch 'upstream-pr/2916'
git rebase 'main'
git switch 'main'
git merge --no-ff 'upstream-pr/2916'
git switch 'main'
git merge --squash 'upstream-pr/2916'
git switch 'main'
git merge --ff-only 'upstream-pr/2916'
git switch 'main'
git merge 'upstream-pr/2916'
git push origin 'main'
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!2914
No description provided.