XMem: physical pages can be kept in our working set so they are not paged out behind the title (VICTORY_KEEP_PAGES=1, off by default) #447

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

@⁠Jesper-Andersson on GitHub · 2026-11-27 09:37 UTC · original
Jesper-Andersson:keep-pages → main · +186 −5 · 1 files · 1 commits · open

A port of fxceless/victory-fork#1294 by @⁠flxppr, adapted to main and with a free-RAM floor added. Credit for the idea and the first measurements goes there.

When the title unmaps the last view of physical pages it wrote (it remaps all the time), Windows takes the pages out of our working set and puts them on the modified list. From there it writes them to the pagefile, though the title still holds them, and they fault back in at the next mapping (crates/victory-hle/src/modules/xmem.rs, show and View::map). On a 32 GB PC this shows as a 40-60 s stretch after entering the world: free RAM drops below 1 GB, page faults jump to thousands a frame, and single frames take 0.6-4 s.

Change

  • VICTORY_KEEP_PAGES=1, off by default and under Wine (its sections are sparse files). Each section of physical pages gets its whole view at allocation, Section::whole. That is the view DirectStorage writes already go through (Section::window); it is now mapped eagerly and still unmapped only when the section goes. Every page that a title mapping (show) or a write view (View::map) commits is read once through it (Section::keep, one byte per 4 KiB), so it stays in our working set when the title's view goes. Nothing is locked, and no working-set minimum is set: Windows can still trim us.
  • The read faults in pages the title never touched, so the switch can raise resident memory. VICTORY_KEEP_PAGES_FREE_MB (default 512, 0 = no limit) is a floor: below that much free RAM it stops reading pages in, and starts again a quarter above (keep_band; free RAM is sampled at most every 100 ms). The PR this ports has no floor. A 2 GB floor was tried first and paused exactly where the switch helps (see below), so the default is only a guard against running out of RAM.
  • Log: VICTORY_KEEP_PAGES=1: physical pages held in our working set ... with asked_mb/granted_mb/headroom_mb at the first section and each GiB after; a warning no kept view for physical pages if Windows refuses a view; an info line when the floor pauses or resumes.

Where it does nothing

  • Switch unset: keeping() is false, keep_section and Section::keep return at once; the window is still mapped lazily as before.
  • VICTORY_SECTION_WINDOW=0 still works: the per-write views stay, and only keeping uses the whole view.

Measured

These numbers are from a local branch, not from main itself. It is main plus about 50 merged PRs (local test @ 68b0055), with this change on top and one unrelated switch that stays off. The commit here is the same change applied to main.

RTX 2070 SUPER 8 GB (driver 621.14), Ryzen 7 5800X, 32 GB, Windows 11. Diag build, Series S + DLSS, intro skipped, the same scripted route into the world, about 150 s in the world. Only frames with at least 1,000 draws count; "steady" is the last 60 s.

off (3 runs) KEEP_PAGES=1, FREE_MB=0 (2 runs) KEEP_PAGES=1, 2 GB floor (1 run)
world median frame 102 / 105 / 99 ms 104 / 96 ms
p90 frame 137 / 367 / 334 ms 121 / 106 ms 176 ms
worst frame 634 / 1,479 / 4,036 ms 146 / 176 ms 1,511 ms
free RAM, min 2,559 / 537 / 739 MB 1,892 / 2,193 MB 679 MB
page faults per frame 6,178 / 11,440 / 5,920 6,716 / 4,773
steady median 96 / 94 / 97 ms 102 / 94 ms
  • The bad stretch after entering the world went away: worst frames 146-176 ms against 0.6-4 s, and p90 106-121 ms against 137-367 ms.
  • It does not make the usual frame faster: medians and the steady state are the same within noise.
  • Free RAM stayed higher with the switch, not lower.
  • With the 2 GB floor it paused (free RAM low, pages no longer read in free_mb=2044) and kept little of the gain. With the default 512 MB floor, those two runs would have gone exactly as with no floor.
  • Only 2-3 runs each, on one PC.

Later runs on the same local branch, with the 512 MB default floor:

  • One more 150 s run: p90 118 ms, worst 157 ms, free RAM min 1,756 MB.
  • One 30-minute run (26 min in the world, 340 frame lines): median 92 ms, p90 101 ms. Private bytes grew from 22.2 to 22.8 GB, free RAM never fell under 1.2 GB, the floor was never reached, and there was no crash.
  • In all, 6 of 7 runs with the switch had p90 106-156 ms and worst frames 0.15-0.4 s. One had a 1.1 s p90 anyway, so it reduces the stalls rather than removing every one.

The original PR measured the pagefile writes directly. On a SATA SSD with 48 GB, they fell from 8.5 GB in 8 min to 0.3 GB with the switch. On 16 GB with an NVMe it was neutral to slightly worse, because Windows trims the working set anyway there. That is why it stays opt-in.

Checks

  • cargo test -p victory-hle --lib --locked: 162 passed, 2 ignored (new: keep_pages_is_on_only_for_1, keep_headroom_defaults_to_512_mib_and_0_turns_it_off, keep_band_stops_below_headroom_and_resumes_a_quarter_above)
  • VICTORY_KEEP_PAGES=1 cargo test -p victory-hle --lib --locked xmem: 20 passed, 1 ignored
  • cargo clippy -p victory-hle --all-targets --locked: no warnings in victory-hle (4 in victory-gpu, as on main)
  • cargo xtask policy --staged: 1 file ok
  • In game: the runs above, on the local branch named above, not on main itself.

Not checked

  • AMD cards, 16 GB or 64 GB PCs, a SATA drive for the pagefile, Wine (off there).
  • Pagefile write volume on this PC; only frame times, free RAM and page faults were measured.
  • The most useful check: the same A/B on a 16 GB PC with VICTORY_KEEP_PAGES_FREE_MB=0 and with the 512 default.
  • fxceless/victory-fork#1294 (the original, by @⁠flxppr): this ports it, with the floor added.
  • #373 (32 GB, half the process paged out) may be the same pressure; not tested there.
  • #356 (lazy or file-backed physical pages for the commit limit) also touches xmem.rs but changes how pages are backed. It does not keep pages resident.

This change was made with AI assistance (Claude Code).

**@⁠Jesper-Andersson** on GitHub · 2026-11-27 09:37 UTC · [original](https://github.com/avatarmanz/VIctory-7900XTX/pull/447) `Jesper-Andersson:keep-pages` → `main` · +186 −5 · 1 files · 1 commits · open A port of fxceless/victory-fork#1294 by @⁠flxppr, adapted to main and with a free-RAM floor added. Credit for the idea and the first measurements goes there. When the title unmaps the last view of physical pages it wrote (it remaps all the time), Windows takes the pages out of our working set and puts them on the modified list. From there it writes them to the pagefile, though the title still holds them, and they fault back in at the next mapping (`crates/victory-hle/src/modules/xmem.rs`, `show` and `View::map`). On a 32 GB PC this shows as a 40-60 s stretch after entering the world: free RAM drops below 1 GB, page faults jump to thousands a frame, and single frames take 0.6-4 s. ## Change - `VICTORY_KEEP_PAGES=1`, off by default and under Wine (its sections are sparse files). Each section of physical pages gets its whole view at allocation, `Section::whole`. That is the view DirectStorage writes already go through (`Section::window`); it is now mapped eagerly and still unmapped only when the section goes. Every page that a title mapping (`show`) or a write view (`View::map`) commits is read once through it (`Section::keep`, one byte per 4 KiB), so it stays in our working set when the title's view goes. Nothing is locked, and no working-set minimum is set: Windows can still trim us. - The read faults in pages the title never touched, so the switch can raise resident memory. `VICTORY_KEEP_PAGES_FREE_MB` (default 512, 0 = no limit) is a floor: below that much free RAM it stops reading pages in, and starts again a quarter above (`keep_band`; free RAM is sampled at most every 100 ms). The PR this ports has no floor. A 2 GB floor was tried first and paused exactly where the switch helps (see below), so the default is only a guard against running out of RAM. - Log: `VICTORY_KEEP_PAGES=1: physical pages held in our working set ...` with `asked_mb`/`granted_mb`/`headroom_mb` at the first section and each GiB after; a warning `no kept view for physical pages` if Windows refuses a view; an info line when the floor pauses or resumes. ## Where it does nothing - Switch unset: `keeping()` is false, `keep_section` and `Section::keep` return at once; the window is still mapped lazily as before. - `VICTORY_SECTION_WINDOW=0` still works: the per-write views stay, and only keeping uses the whole view. ## Measured **These numbers are from a local branch, not from main itself.** It is main plus about 50 merged PRs (local `test` @ 68b0055), with this change on top and one unrelated switch that stays off. The commit here is the same change applied to main. RTX 2070 SUPER 8 GB (driver 621.14), Ryzen 7 5800X, 32 GB, Windows 11. Diag build, Series S + DLSS, intro skipped, the same scripted route into the world, about 150 s in the world. Only frames with at least 1,000 draws count; "steady" is the last 60 s. | | off (3 runs) | `KEEP_PAGES=1`, `FREE_MB=0` (2 runs) | `KEEP_PAGES=1`, 2 GB floor (1 run) | |---|---|---|---| | world median frame | 102 / 105 / 99 ms | 104 / 96 ms | | | p90 frame | 137 / 367 / 334 ms | 121 / 106 ms | 176 ms | | worst frame | 634 / 1,479 / 4,036 ms | 146 / 176 ms | 1,511 ms | | free RAM, min | 2,559 / 537 / 739 MB | 1,892 / 2,193 MB | 679 MB | | page faults per frame | 6,178 / 11,440 / 5,920 | 6,716 / 4,773 | | | steady median | 96 / 94 / 97 ms | 102 / 94 ms | | - The bad stretch after entering the world went away: worst frames 146-176 ms against 0.6-4 s, and p90 106-121 ms against 137-367 ms. - It does not make the usual frame faster: medians and the steady state are the same within noise. - Free RAM stayed higher with the switch, not lower. - With the 2 GB floor it paused (`free RAM low, pages no longer read in free_mb=2044`) and kept little of the gain. With the default 512 MB floor, those two runs would have gone exactly as with no floor. - Only 2-3 runs each, on one PC. Later runs on the same local branch, with the 512 MB default floor: - One more 150 s run: p90 118 ms, worst 157 ms, free RAM min 1,756 MB. - One 30-minute run (26 min in the world, 340 frame lines): median 92 ms, p90 101 ms. Private bytes grew from 22.2 to 22.8 GB, free RAM never fell under 1.2 GB, the floor was never reached, and there was no crash. - In all, 6 of 7 runs with the switch had p90 106-156 ms and worst frames 0.15-0.4 s. One had a 1.1 s p90 anyway, so it reduces the stalls rather than removing every one. The original PR measured the pagefile writes directly. On a SATA SSD with 48 GB, they fell from 8.5 GB in 8 min to 0.3 GB with the switch. On 16 GB with an NVMe it was neutral to slightly worse, because Windows trims the working set anyway there. That is why it stays opt-in. ## Checks - `cargo test -p victory-hle --lib --locked`: 162 passed, 2 ignored (new: `keep_pages_is_on_only_for_1`, `keep_headroom_defaults_to_512_mib_and_0_turns_it_off`, `keep_band_stops_below_headroom_and_resumes_a_quarter_above`) - `VICTORY_KEEP_PAGES=1 cargo test -p victory-hle --lib --locked xmem`: 20 passed, 1 ignored - `cargo clippy -p victory-hle --all-targets --locked`: no warnings in victory-hle (4 in victory-gpu, as on main) - `cargo xtask policy --staged`: 1 file ok - In game: the runs above, on the local branch named above, not on main itself. ## Not checked - AMD cards, 16 GB or 64 GB PCs, a SATA drive for the pagefile, Wine (off there). - Pagefile write volume on this PC; only frame times, free RAM and page faults were measured. - The most useful check: the same A/B on a 16 GB PC with `VICTORY_KEEP_PAGES_FREE_MB=0` and with the 512 default. ## Related - fxceless/victory-fork#1294 (the original, by @⁠flxppr): this ports it, with the floor added. - #373 (32 GB, half the process paged out) may be the same pressure; not tested there. - #356 (lazy or file-backed physical pages for the commit limit) also touches `xmem.rs` but changes how pages are backed. It does not keep pages resident. This change was made with AI assistance (Claude Code).
After fxceless/victory-fork#1294 by flxppr. When the title unmaps the last
view of physical pages it wrote (it remaps all the time), Windows moves
them to the modified list and writes them to the pagefile though the
title still holds them. With the switch each section gets its whole view
at allocation (the view DirectStorage writes already go through,
Section::window, now mapped eagerly) and every page a title mapping or a
write view commits is read once through it, so the pages stay in our
working set. Nothing is locked and no working-set minimum is set.

The read faults in pages the title never touched, so below
VICTORY_KEEP_PAGES_FREE_MB of free RAM (default 512, 0 = no limit) it
stops reading pages in and resumes a quarter above. Logs an info line at
the first section (and per GiB more) with asked_mb/granted_mb, a warning
for each refused view, and an info line when the floor pauses or resumes.
Off under Wine. Off by default: nothing changes then.

This change was made with AI assistance (Claude Code).
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-27 10:38:53 +00:00
This pull request can be merged automatically.
You are not authorized to merge this pull request.
View command line instructions

Checkout

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

Merge

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