HLE: physical pages come from 2 MiB sections, so remaps stop waiting on Windows' large-page worker #3175
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/3177"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Imported from Marvisak/VIctory. Originally posted by KainanP on 2026-11-26T11:12:15Z.
Problem
XMemAllocatePhysicalPagesmakes 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):remapping:summed over the opening cutsceneThe 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_MBMiB, 2 by default on Windows.0keeps 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,shownand the page table are unchanged. A run of pages (for a mapping,write_physicalor a DirectStoragepage_window) already stopped at an allocation boundary, and now it also stops at a section boundary. A window that would cross one returnsNone, and the caller'swrite_physicalpath handles it as before. The allocation moved intoallocate_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 realXMemAllocatePhysicalPages/XMemMapPhysicalPagespath. 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:
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=0against the default, watching long frames while streaming (cutscene, driving) more than the median.Tests
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_physicalwrites both bytes) and a 33-pageXMemMapPhysicalPages(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
DirectStorage writes across sections
In one prison-intro run (Venom2054, RTX 5070 Ti,
VICTORY_SECTION_MB=2), 7,531 of 96,578 physical-page writes (7.80%) touched more than one section. Those writes carried 3.56 of 6.40 GiB (55.6%). Of them, 583 (0.60% of all writes) were consecutive pages of one allocation, which had a single view before the split. With 2 MiB sections, such writes used to go through the staging buffer. Uncompressed requests are now copied into each section's pages in order, with no buffer (xmem::fill_physical). Compressed requests keep the single window or the buffer.andrewpozdnakov7-jpg's
section_size_directstorage_probemeasured the memory-source path. The table gives medians of per-process p50s over 5 fresh processes in alternating order:Imported from Marvisak/VIctory. Originally posted by Dukestar69 on 2026-11-26T13:11:04Z.
Review: APPROVED
Verified against
origin/main(63bcca4):cargo test -p victory-hle --lib modules::xmem:: -- --test-threads=1: 15/15 passed (1 ignored bench; includingsections_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).
Imported from Marvisak/VIctory. Originally posted by cloph-dsp on 2026-11-26T13:59:17Z.
Independent local verification of head
5b7611aon 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).Imported from Marvisak/VIctory. Originally posted by andrewpozdnakov7-jpg on 2026-11-26T14:58:27Z.
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 withVICTORY_SECTION_MB=0and2. 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.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. SetVICTORY_SECTION_MBto 0 or 2 for a fresh run ofcargo 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 <[email protected]>if folding it into another commit.Imported from Marvisak/VIctory. Originally posted by KainanP on 2026-11-26T20:19:48Z.
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 in9bfd4aac:*count = 0. Before, the count was left as it was. New testa_count_too_large_is_refused_as_beforefails on5b7611aand passes now.debugline was logged before the host call instead of after it.warnline gained asizefield. It is kept only when the request is split.cargo test --locked -p victory-hlepasses (154 tests, 1 ignored).cargo clippy -p victory-hle --all-targets -D warningsis clean, and the asset policy check passes. Synthetic tests only. Not run in game.AI use: Claude Code (Claude Opus 5.5).
Imported from Marvisak/VIctory. Originally posted by Venom2054 on 2026-11-27T00:20:38Z.
I measured the destination distribution requested in the DirectStorage benchmark comment. This is one Windows / RTX 5070 Ti prison-intro run with
VICTORY_SECTION_MB=2on a local renderer build incorporating this PR at9bfd4aa. The sample goes from launch through about 133 seconds after entering the 3D scene.Those 7,531 requests accounted for 3.56 of 6.40 GiB written (55.61%), counting the entire payload of each crossing request. Of those, 583 requests (0.60% of all writes) followed consecutive pages of a single original allocation. The other 6,948 already had a gap or changed original allocation. That distinction matters when estimating how many writes lose the contiguous-page fast path specifically because of the split.
The counter records a request once after a successful write and compares the actual backing sections of the destination pages in order. All counted writes were file-sourced and completed in full. Ordinary virtual-address destinations are outside the denominator. These are workload counts; the time cost of staging those requests would need a separate measurement. AI-assisted measurement and analysis.
Imported from Marvisak/VIctory. Originally posted by KainanP on 2026-11-27T13:01:14Z.
Thanks @andrewpozdnakov7-jpg for the benchmark and @Venom2054 for the distribution. Both changed the PR.
Venom2054's numbers show that 7.8% of physical-page writes touched more than one section. Those writes carried 55.6% of the bytes. Only 583 writes (0.60%) were consecutive pages of one allocation that lost the one-view path because of the split. The benchmark showed what that path costs: the staging buffer roughly doubled the time for large writes.
Change (
95c854e3): uncompressed requests into physical pages no longer go through the buffer.xmem::fill_physicalmaps one view per run of one section's pages, in order, and reads that run's part of the source (memory or file) directly into it.write_physicalnow uses the same code with a slice as the source, so the bytes land where the buffered path put them for any page list. This includes the 6,948 crossing writes that already had gaps. Compressed requests still use the single window or the buffer, because a decoder needs its whole output in one slice. A view that fails part way falls back to the buffer as before. A file that ends early fails the request withEND_OF_FILE. Nothing outside the destination is written in either case.Your benchmark, cherry-picked with your authorship (
87c6020e). Release build, Windows 11. Each setting ran in 5 fresh processes in alternating order. The table gives the median of the per-process p50s. The before and after runs were back to back on the same machine. My absolute times are lower than yours:With 2 MiB sections, 4 MiB and 8 MiB writes now take about the same time as with one section. A 64 KiB crossing write still costs about 5 µs more, because it maps a second view. The benchmark's
directcolumn still reports whether a single view fits. Its byte and sentinel checks pass at both settings.New tests:
copies_across_sections_need_no_buffer: copies across 2 to 5 sections, from memory and from a file, with aligned and unaligned starts and ends. It checks every destination byte, the bytes on both sides, and that no buffer was used.copies_across_sections_fail_whole: a file that ends early (END_OF_FILE); a view that fails once (the buffered retry writes every byte); a view that always fails (E_INVALIDARG); an unknown page (nothing written, and the queued status isE_INVALIDARG). No case writes a byte outside the destination.cargo test --locked -p victory-hle --libpasses: 156 passed, 2 ignored.cargo clippy -p victory-hle --all-targets -D warningsis clean with default features and with--all-features. The asset policy check passes. Synthetic tests only. Not run in game.AI use: Claude Code (Claude Opus 5.5).
Imported from Marvisak/VIctory. Originally posted by kilolimaVI on 2026-11-27T14:08:00Z.
Probe numbers from a laptop, since the PR has none yet: head
95c854e, Windows 11, Ryzen 7 8845HS (Radeon 780M), toolchain 1.98.1,--release -- section_size_directstorage_probe --ignored --nocapture, p50 / p95 of 31 runs after 4 warm-ups.VICTORY_SECTION_MB=0(one window)VICTORY_SECTION_MB=2So the split copy into 2 MiB sections costs about the same as a single window at 4 and 8 MiB, and about 5 µs more for a 64 KiB copy straddling a section.
cargo test -p victory-hle --lib --locked -- dstorage xmem --test-threads=1: 34 passed, 2 ignored. One run of the probe each, so the small cases are within noise.AI-assisted (Claude).
Imported from Marvisak/VIctory. Originally posted by KainanP on 2026-11-29T12:06:42Z.
@kilolimaVI thanks for the AMD laptop numbers. They agree with the PR's "cost when nothing stalls" section: a split copy at 4 and 8 MiB costs about the same as one window, and a 64 KiB copy across a section boundary costs a few µs more. Your probe had no allocation churn running alongside, so the rebuild worker stall this PR targets wouldn't show up in it.
AI use: Claude Code (Claude Opus 5.5).
View command line instructions
Checkout
From your project repository, check out a new branch and test the changes.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.