Repository navigation
xmem: mapping pages asks the host only for what it needs - #2916
0xkn0wh3r3 wants to merge 2 commits into
Conversation
The title streams through one thread of its own, which maps the pages of a batch and then submits it, and every page is mapped twice: into the title's pool, then by the GPU into its tiled heap (map_alias). Each mapping took four system calls: unmap the old view, split the placeholder, map the section, commit its pages. Each waits for the process's address-space lock, behind the GPU side's guards and the driver; the fork measured 200 to 400 us a page in the world against 10 in a menu, and loads that came only as fast as their pages could be mapped. - No split of a placeholder that is already the view's own: an unmap of a whole view notes its placeholder, and a view of exactly that range is mapped over it as it is. If the title has joined it with others since, that map fails and the placeholder is split as before. - No commit of section pages already committed: each section keeps a bit per page, set when a view (the title's or one of ours) commits it; committed through one view, a page is committed in every view. VICTORY_MAP_LEAN=0 does both every time, as before. A page mapped and unmapped again takes two calls instead of four: uncontended on this machine, about 6.5 us a page before and 5.0 after (map_bench, ignored). Tests: pages keep their bytes over six rounds of moves, also after the title joins the placeholders; a page mapped again where it was takes its own placeholder and is not committed again, and after a join it is mapped by the split as before. From avatarmanz/VIctory-7900XTX 49d7126 (its VICTORY_MAP_LEAN part; the streaming log, the priority lift of the streaming thread and the stack affinity names are left out). Co-authored-by: local <local@localhost>
kotekompotek
left a comment
There was a problem hiding this comment.
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
left a comment
There was a problem hiding this comment.
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
left a comment
There was a problem hiding this comment.
Verified head ab91aa4d cleanly on origin/main (63bcca43) on Windows 11 x64.
Code & Safety Review
Section::committed(Acquire/Releaseatomic bitmap) safely avoids redundantVirtualAlloc(..., MEM_COMMIT)on already-committed NT section slices, andPhysical::holescleanly reuses exact placeholders freed byunmapwhile automatically falling back to the split-and-map path ifMapViewOfFile3fails (e.g. if an external release/coalesce invalidated a cached hole).- Complementary with partial-run rollback (
#2742) and gated behindVICTORY_MAP_LEAN=0escape hatch. Minor note (non-blocking):XMemVirtualFree(..., MEM_RELEASE)doesn't prune stale entries fromholes; correctness is unaffected due to theMapViewOfFile3fallback, only minor map retention if addresses aren't reused.
Local Verification & Benchmark
cargo test --locked -p victory-hle: 154/154 unit tests passed (1 ignoredmap_bench; includespages_mapped_again_hold_what_they_heldanda_page_mapped_again_takes_its_own_placeholder), plusfidelityfx(1/1) andno_dll_planting(1/1).--releasemap_bench(--ignored --nocapture, 3 alternating runs):lean false:4.2 us/3.8 us/4.3 uslean 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.
|
Windows 11 / NVIDIA test run, head 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 (
Not run: the retail title, clippy, and other crates. AI-assisted (Claude Code) |
The placeholders an unmap leaves were noted for good: an entry went only when its address was mapped again, so placeholders the title releases or joins (with the host's own VirtualFree, which we do not see) stayed, and doing that at new addresses grew the map for the whole run, also with VICTORY_MAP_LEAN=0. (Review on Marvisak#2916.) - Nothing is noted while VICTORY_MAP_LEAN=0. - A map forgets every noted placeholder its range meets, also one that begins below it, not only one at its own address. - At most 2^17 are noted (8 GiB of pages unmapped one by one); past that the map starts afresh. They are only a hint: a miss splits as before.
With Marvisak#2916: the unmap through title_memory_change still records its hole when it unmapped whole.
|
Automated triage for the maintainers. Merge candidate (a recommendation to look, not an approval). In Generated by Claude Code |
… (its new commit)
View::map (MapViewOfFile, then committing the view) now returns the Windows error when the host refuses, and xmem::write_physical passes it on. The warning for a request whose physical pages could not be written gives it (error = ...), as does the info line for one written on a retry. The retries (up to seven, 254 ms in all) now run only while the error is ERROR_NOT_ENOUGH_MEMORY, ERROR_NO_SYSTEM_RESOURCES or ERROR_COMMITMENT_LIMIT, refusals for want of memory that can pass as memory is freed or the page file grows. Any other error, or a retry that meets one, fails the request at once. Test: other_refusals_are_not_retried (unknown pages and other errors are not retried); host_refusals_are_retried_with_longer_waits now gives the errors. (demo: Section::commit_shown, from Marvisak#2916, passes the error on too.)
Dukestar69
left a comment
There was a problem hiding this comment.
Verified against origin/main (63bcca4):
cargo test -p victory-hle --lib modules::xmem:: -- --test-threads=1: 16/16 passed (1 ignored bench; includinga_page_mapped_again_takes_its_own_placeholderandpages_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).
…ly for what it needs
…at it needs Off by default until measured in the world. Two parts, both from the pull request, fitted to this tree's xmem (sections of 2 MiB, a map that looks at the placeholders of its range): - a view mapped where an unmap of exactly that range left its placeholder is mapped over it at once, without the queries and splits of the range; if the title joined it with others since the host refuses and the range is handled as before. The recorded placeholders are only a hint (at most 2^17; a map drops every one its range meets); - each section keeps a bit a page, set when any view commits the page; a section page committed through one view is committed in every view of it, so the others do not call for it again. Each saves calls that wait for the process's address-space lock. The tests of the pull request are taken (pages keep their bytes over joined placeholders; a page mapped again takes its own placeholder). Source: Marvisak/VIctory pull request Marvisak#2916, from avatarmanz/VIctory-7900XTX 49d7126. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
…from upstream and the forks The host device is made on the adapter the system ranks first for performance, never a software one, and the log names it (VICTORY_D3D12_ADAPTER picks another; after Marvisak/VIctory pull request Marvisak#3073). A texture's clear value the host cannot take is dropped (Marvisak#3065), a raw buffer view's range stays inside its buffer (fxceless/victory-fork pull request Marvisak#1266), the structure builder turns off when title memory by address has no copy number left (Marvisak#1265), concurrent crash records are kept whole (Marvisak#3092), and the title's idle and lowest threads run below normal (Marvisak#2903; VICTORY_TITLE_PRIORITIES=0 leaves them). Unordered views the title places in memory get a texture of the host's own (after fxceless/victory-fork pull request Marvisak#114 and the same design in another fork); a placed view at address 0 is the title's null descriptor and is a null view without a warning: the old "not mirrored" line was that, for a sampling view. For debugging: VICTORY_CRASH_DUMP_DIR writes a full-memory minidump at an unhandled exception, outside the repository only (Geckomacabre/vx-layer 707b75be0), and `cargo xtask refusals <log>` says what a run lost and why (Marvisak#2892). To be measured, off until then: VICTORY_MAP_LEAN=1 (mapping pages asks the host only for what it needs, Marvisak#2916) and VICTORY_DSTORAGE_STAGING_MB (a floor for the staging limit). Runs of 2026-11-26 on an RTX 4070 Ti before the last commit: first room 74.7 ms a frame, the open world's bedroom 94.8 ms; pictures right, no error and no device loss. No placed unordered texture view came up in either run, so that path is not exercised yet. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
What
XMemMapPhysicalPagesandmap_aliasnow skip two of the four host calls a mapping took (unmap, split, map, commit):MapViewOfFile3fails (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 withVICTORY_MAP_LEAN=0, a map drops every one its range meets (the title releases and joins placeholders with the host's ownVirtualFree, which we don't see), and at most 2^17 are kept, after which the map starts afresh.Views for DirectStorage writes). A section page committed through one view is committed in every view of it.VICTORY_MAP_LEAN=0makes 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 itsVICTORY_MAP_LEANpart, 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
commitalready returns early; the split path is unchanged.