Skip to content

HLE: physical pages come from 2 MiB sections, so remaps stop waiting on Windows' large-page worker - #3177

Open
KainanP wants to merge 2 commits into
Marvisak:mainfrom
KainanP:perf/physical-sections-2mib
Open

KainanP wants to merge 2 commits into
Marvisak:mainfrom
KainanP:perf/physical-sections-2mib

Conversation

@KainanP

@KainanP KainanP commented Nov 26, 2026

Copy link
Copy Markdown

Problem

XMemAllocatePhysicalPages makes one pagefile-backed section per request. Grand Theft Auto VI asks for 6.1 GiB in one call and maps it one 64 KiB view at a time, so that one section ends up with views by the hundred thousand. Windows' large-page rebuild worker (MiRebuildLargePages → MiTradePage → MiTrimSection) trades a section's pages one at a time. For each page it walks the section's views (MiViewMayContainPage) while it holds the section's lock. The title's map and unmap calls (MiInsertVad, MiDeleteVad) wait behind that lock.

RyanCase94 traced this with xperf on fxceless/victory-fork#1089. Fork testers then measured it in the game. Main has the same allocation and mapping code (xmem.rs):

Who What One section per request 2 MiB sections
RyanCase94, RTX 4070 Ti (#1089) map/unmap calls of 1 ms or more in the first 90 s of the prison ~11,000, ~60 s 31, 55 ms
Exopus-0x65f7ad0, RTX 5060 Ti 16 GB RAM (#1089) remapping: summed over the opening cutscene 32-65 s (5 runs) 2.5 s (1 run)
Garu667, RTX 3080 (fork #1274) remapping per frame while driving 42.8 ms 5.0 ms
CuteBooty, RTX 3070 (fork #1274) texture refresh waiting on remaps per frame, worst wait 37 ms, 816 ms ~1 ms, 28-62 ms

The median frame time did not change in those runs. What changed was the long frames: the 0.3-0.8 s freezes while driving, and HLVM04's p90 went from about 118 ms to 105 ms.

Change

A request is now backed by sections of at most VICTORY_SECTION_MB MiB, 2 by default on Windows. 0 keeps one section per request, and that stays the default under Wine, where sections are committed sparse files and this stall does not apply. Page numbers, views, shown and the page table are unchanged. A run of pages (for a mapping, write_physical or a DirectStorage page_window) already stopped at an allocation boundary, and now it also stops at a section boundary. A window that would cross one returns None, and the caller's write_physical path handles it as before. The allocation moved into allocate_physical(n, per) so that tests can pick the section size.

The idea and first patch come from RyanCase94 (fxceless/victory-fork#1089). CuteBooty ported it to the fork, off by default there (fxceless/victory-fork#1274). The fork kept it off because of its in-place heap census, which needs whole sections. Main has no such code, which is why this PR turns it on by default. It is a one-line change if you prefer it off.

Measured (synthetic, no game)

physical_remap_bench (included, #[ignore]) runs the real XMemAllocatePhysicalPages / XMemMapPhysicalPages path. It maps 4 GiB a page per view and touches every page. It then remaps a random page at a random address for 10 s while another thread allocates, touches and frees 2 GiB, the kind of memory churn that wakes the rebuild worker. The run alternates the two modes over 5 rounds in one process.

i7-14650HX, 32 GB, Windows 11 26200, release build, median of 5 rounds:

remaps/s median call p99 call calls of 1 ms or more (time)
one section (main's behaviour) 6,644 16.3 µs 4,093 µs 1,386 (8.6 s of 10 s)
2 MiB sections 50,607 14.4 µs 36.8 µs 28 (1.0 s)

Per round, one section ran 3,033 / 58,814 / 16,898 / 6,644 / 3,134 remaps/s. In one round of five the worker did not run, and the two modes were then equal. 2 MiB sections ran 46,726-54,744 remaps/s in every round. A standalone version (raw MapViewOfFile3, no VIctory code) gives the same picture: 1,784 calls of 1 ms or more against 45 in 15 s. With a thread that only empties the working set and makes no allocations, neither mode stalls. So the trigger is memory being made and freed, which a streaming title does constantly.

Cost when nothing stalls: with no other thread making memory (VICTORY_SECTION_BENCH=4096,5,3,0), 2 MiB sections are about 8 % slower per remap (median 10.0 µs against 9.1 µs; 77,858 against 84,785 remaps/s). A request of 6.1 GiB also costs about 3,100 section handles instead of one.

Not measured: the game itself (I don't run it), and Wine (unchanged there by default). The in-game numbers above come from the fork, not from main. It would help if a game owner could run an A/B on main with VICTORY_SECTION_MB=0 against the default, watching long frames while streaming (cutscene, driving) more than the median.

Tests

  • New test sections_hold_at_most_the_pages_asked: 33 pages give runs [33] or [32, 1] with consecutive numbers. It also covers a two-byte write across the 2 MiB boundary (no window in place, write_physical writes both bytes) and a 33-page XMemMapPhysicalPages (two views, both bytes visible, writes through the view land in the right page). It then unmaps everything back to a reservation.
  • cargo test --locked -p victory-hle: 153 passed, 1 ignored (the benchmark). The existing xmem tests now run with the 2 MiB default.
  • cargo clippy --locked -p victory-hle --all-targets -- -D warnings, with and without --all-features: clean. cargo check --workspace --all-targets: clean. cargo xtask policy: 191 files ok. rustfmt: the lines I added are formatted, and the file's other rustfmt differences come from main.

AI use: Claude Code (Claude Opus 5.5) wrote the port, the benchmark and this description, and ran the measurements on my machine. No game files or game-derived data were used.

🤖 Generated with Claude Code

…on Windows' large-page worker

XMemAllocatePhysicalPages made one pagefile-backed section per request
(Grand Theft Auto VI asks for 6.1 GiB at once), and the title maps it a
64 KiB view at a time, so that one section carries views by the hundred
thousand. Windows' large-page rebuild worker trades a section's pages one
by one and walks the section's views under its lock for each, holding
every map and unmap of title physical memory meanwhile
(fxceless/victory-fork#1089, kernel trace by RyanCase94).

A request is now backed by sections of at most VICTORY_SECTION_MB MiB
(default 2 on Windows; 0 keeps one section per request, the default under
Wine, whose sections are committed sparse files). Page numbering, views
and the page table are unchanged; a run of pages for a mapping or a
DirectStorage window stops at a section boundary, as it already did at an
allocation boundary.

Synthetic benchmark included (ignored test physical_remap_bench): 4 GiB
mapped a page per view, single-page remaps for 10 s while another thread
makes, touches and frees 2 GiB. i7-14650HX, 32 GB, Windows 11,
median of 5 rounds: one section 6,644 remaps/s and 1,386 remaps of 1 ms or
more (8.6 s of the 10 s); 2 MiB sections 50,607 remaps/s and 28 (1.0 s).

Idea and first patch: RyanCase94 (fxceless/victory-fork#1089); fork port
by CuteBooty (fxceless/victory-fork#1274).

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

@Dukestar69 Dukestar69 left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Verified against origin/main (63bcca4):

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

Cleanly caps physical page backing sections at 2 MiB to avoid large-page section lock contention on Windows remaps.

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

@cloph-dsp

Copy link
Copy Markdown

Independent local verification of head 5b7611a on Windows 11, Rust 1.100.0/MSVC:

  • cargo test --locked -p victory-hle --lib -- modules::xmem::tests::sections_hold_at_most_the_pages_asked --exact — 1 passed, 0 failed.
  • git diff --check origin/main HEAD — clean.

Used a worktree-local CARGO_TARGET_DIR. Synthetic page-count inputs only; no game files, extracted shaders or DRM material used. AI-assisted (Hy4 flash). GPU/driver N/A (CPU-only HLE test).

KevinPratap added a commit to KevinPratap/ApexIV that referenced this pull request Nov 26, 2026
…Y_SECTION_MB)

Ported from Marvisak/VIctory#3177 (5b7611ae0 by KainanP, after RyanCase94 and CuteBooty). When VICTORY_SECTION_MB=<mb> is set (e.g. 2), XMemAllocatePhysicalPages backs physical pages with <mb>-MiB sections instead of one giant section so Windows page/view remaps avoid section-wide large-page rebuild stalls. Off by default.
@andrewpozdnakov7-jpg

Copy link
Copy Markdown
Contributor

I measured the DirectStorage side of the section-size change on exact 5b7611a (Windows 10 19045, release). The same executable ran the real uncompressed memory-source request path with VICTORY_SECTION_MB=0 and 2. Each setting ran in five fresh processes, alternating order, with four warmups and 31 measured calls per case. The table gives the median of the five per-process medians.

Destination range One section 2 MiB sections
64 KiB within a section 0.0324 ms 0.0326 ms
64 KiB crossing a section 0.0377 ms 0.0556 ms
Aligned 2 MiB 0.780 ms 0.835 ms
Unaligned 4 MiB 2.01 ms 4.17 ms
8 MiB at a 64 KiB boundary 4.26 ms 8.11 ms

The cross-section cases used the staging-buffer fallback with the 2 MiB setting. All checked destination bytes and both adjacent sentinels matched. The 16 existing DirectStorage tests passed at that setting. These timings cover RAM-to-page writes under a synthetic load. The title's request sizes, file and decompression times, and your measured map/unmap stall are separate measurements.

The reproducible ignored benchmark is f74d1c4. Set VICTORY_SECTION_MB to 0 or 2 for a fresh run of cargo test --release --locked -p victory-hle --lib section_size_directstorage_probe -- --ignored --nocapture. If you have a representative DirectStorage destination-size trace, the share of requests crossing the 2 MiB boundary would help weigh this cost against the remap improvement.

Please retain the Git author if reusing the benchmark, or add Co-authored-by: andrewpozdnakov7-jpg <andrewpozdnakov7-jpg@users.noreply.github.com> if folding it into another commit.

…s before

The split allocator changed the one-section path in small ways: a page
count too large to multiply set the title's count to 0 (before: left as
it was), the debug line came before the host call instead of after it,
the failure line gained a size field, and page numbers were written to
the title after the page table's lock was let go instead of as each page
went in. make_sections/number_pages keep the old order and lines for one
section per request; test a_count_too_large_is_refused_as_before fails
on the previous head.

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

KainanP commented Nov 26, 2026

Copy link
Copy Markdown
Author

Checked this PR against the regression reported in LooperSalty/AnyXboxX#428. The AnyXboxX port of this change (#352 there) caused a TDR on an RTX 3080 Ti even with the switch unset. The cause was on the GPU side: the AnyXboxX port added a hook that unmapped our own view of a section as soon as the section closed. This PR has no GPU side: no close hook, no unmap. So that bug does not apply here.

I did find small differences in the one-section path (VICTORY_SECTION_MB=0, and Wine), fixed in 9bfd4aa:

  • A page count too large to multiply set *count = 0. Before, the count was left as it was. New test a_count_too_large_is_refused_as_before fails on 5b7611a and passes now.
  • The debug line was logged before the host call instead of after it.
  • The failure warn line gained a size field. It is kept only when the request is split.
  • Page numbers were written to the title's array after the page-table lock was released. Before, each number was written as its page went in.

cargo test --locked -p victory-hle passes (154 tests, 1 ignored). cargo clippy -p victory-hle --all-targets -D warnings is clean, and the asset policy check passes. Synthetic tests only. Not run in game.

AI use: Claude Code (Claude Opus 5.5).

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.

4 participants