d3d12: memory nobody touches leaves the working set, a little at a time (LEONIDA_ROLL_TRIM, off by default) #3332
Open
github-bot
wants to merge 3 commits from
github-bot/gh-3332 into main
AGit
pull from: github-bot/gh-3332
merge into: github:main
github:main
github:perf/darner-title-priority-ceiling
github:gh-readonly-queue/main/pr-3311-b5c45b663dbc5a9c3532bb3f307e5f81b32faefa
github:gh-readonly-queue/main/pr-3088-af22e7847ff5808f72d1a1881c24be2fafd20b54
github:gh-readonly-queue/main/pr-3123-7f3fa026a94c6646e89244f28e1ce22290361867
github:gh-readonly-queue/main/pr-3354-637baa0b77ba47222e7280b17c1a59c6a1781d57
github:gh-readonly-queue/main/pr-3365-d7a0e0196a43834813014b4bd3aa5502a8263b14
github:gh-readonly-queue/main/pr-3361-5dfe8bbd05ed12fb2f2e1718d9af77a7c5f46fd8
github:gh-readonly-queue/main/pr-3366-8da9148204e9d12c585efa9ac2b7cc1ab08c7682
github:fix/runtime-test-conservative-fixtures
github:ci/shard-semantic-linux
github:docs/fg-limit-fps-switch
github:fix/runtime-test-fixture-paths
github:fix/runtime-test-fixture-readers
github:fix/read-file-hard-link
github:fix/book-slot-heap-never-read
github:fix/depth-resolve-spv-path
github:fix/indirect-unused-batched
github:gh-pr/3358/base
github:fix/xmem-refusal-diagnostics
github:gh-pr/3357/base
github:ci/shorten-ci-time
github:fix/alias-later-queued-submissions
github:claude/gallant-sagan-aamatr
github:gh-pr/3342/base
github:gh-pr/3341/base
github:d3d12/agility-sdk-621
github:gh-pr/3293/base
github:gh-pr/3319/base
github:gh-pr/3318/base
github:gh-pr/3315/base
github:gh-pr/3313/base
github:gh-pr/3314/base
github:claude/fervent-curie-m19lnr
github:gh-pr/3216/base
github:gh-pr/3272/base
github:gh-pr/3276/base
github:gh-pr/3279/base
github:gh-pr/3257/base
github:gh-pr/3258/base
github:gh-pr/3271/base
github:codex/skip-no-hit-ray-passes
github:gh-pr/3230/base
github:gh-pr/3266/base
github:gh-pr/3247/base
github:gh-pr/3246/base
github:gh-pr/3244/base
github:claude/sharp-sagan-1jqnp3
github:t3/preserve-fsr-sdk-restart-path
github:t3/propagate-host-failure-hresult
github:t3/launcher-custom-parameters
github:t3/series-x-2160-startup
github:codex/d3d12-uncapped-simulation
github:ci/less-to-run
github:perf/native-compute-shader-forms
github:claude/blissful-franklin-ukp7ag
github:codex/full-precompile-default
github:gh-pr/3166/base
github:gh-pr/3148/base
github:gh-pr/3138/base
github:gh-pr/3106/base
github:gh-pr/3057/base
github:gh-pr/2756/base
github:gh-pr/2812/base
github:gh-pr/3032/base
github:gh-pr/2992/base
github:notes/card-time-and-plant-flicker
github:gh-pr/2986/base
github:gh-pr/3000/base
github:gh-pr/2966/base
github:gh-pr/2924/base
github:gh-pr/726/base
github:gh-pr/2761/base
github:gh-pr/2663/base
github:gh-pr/2861/base
github:gh-pr/2862/base
github:gh-pr/2820/base
github:gh-pr/2811/base
github:gh-pr/2803/base
github:rt-call-trace
github:gh-pr/2802/base
github:gh-pr/2766/base
github:perf/ahead-split
github:gh-pr/2589/base
github:gh-pr/2699/base
github:gh-pr/2696/base
github:docs/readme-runtime-proposal
github:gh-pr/1346/base
github:codex/coord-claim-check-20261124-7e48
No reviewers
Labels
Clear labels
accessibility
Barrier affecting people with disabilities
area: ci
.github/ workflows and settings
area: d3d12
The D3D12 runtime in d3d12/ (Rust: leonida.exe and its crates)
area: docs
Documentation
area: launcher
The launcher (launcher/)
area: runtime (legacy)
The earlier Vulkan runtime in runtime/
area: shader translator
The shader translator (leonida-shader, tools/rdna2spirv) and its tests
area: tools
tools/ and scripts/
area: viewer
The map viewer (viewer/)
bug
Something isn't working
dependencies
Pull requests that update a dependency file
documentation
Improvements or additions to documentation
duplicate
This issue or pull request already exists
enhancement
New feature or request
good first issue
Good for newcomers
help wanted
Extra attention is needed
invalid
This doesn't seem right
missing claim
Needs a claim in #14
needs in-game run
Changes the code a frame runs through: needs the 300 s in-game gate (docs/game_gate.md)
needs rebase
Conflicts with the base branch: rebase it on main
needs-triage
Needs maintainer evaluation before the next triage state
question
Further information is requested
ready-for-agent
Fully specified and ready for an AFK agent
submodules
Pull requests that update submodules code
wontfix
This will not be worked on
No labels
accessibility
area: ci
area: d3d12
area: docs
area: launcher
area: runtime (legacy)
area: shader translator
area: tools
area: viewer
bug
dependencies
documentation
duplicate
enhancement
good first issue
help wanted
invalid
missing claim
needs in-game run
needs rebase
needs-triage
question
ready-for-agent
submodules
wontfix
Milestone
Clear milestone
No items
No milestone
Assignees
Clear assignees
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.
Dependencies
No dependencies set
Reference
github/ubiquitarianism--leonida!3332
Loading…
Reference in a new issue
No description provided.
Delete branch "github-bot/gh-3332"
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?
@Krashgamer on GitHub · 2026-12-03 13:39 UTC · original
Krashgamer:d3d12-rolling-trim→main· +661 −2 · 5 files · 3 commits · openClaimed in the coordination issue as: D3D12-ROLLING-TRIM (#2570)
Depends on: none. Near D3D12-WORKING-SET-TRIM (Herlix777, #3198): that one trims when memory is short, this one runs all the time and sorts by use. I asked there; no answer yet.
What and why
Windows takes pages nobody touches only when memory is short, and then at once. The game's working set is about 27 GB on a PC with 32 GB of RAM, so the PC has about 0.8 GB left, and the game pages in gigabytes.
LEONIDA_ROLL_TRIM=<seconds>(off by default) goes through the process's committed memory once in that time, a little at a time, and lets pages that were not touched since the turn before leave the working set (VirtualUnlock). What is wanted again comes back by a soft fault while it still lies in memory.Tested
maind9b618f10alone, RTX 3080 10 GB, 32 GB of RAM, Series X, 2560x1440, game gate 300 s in the world, the same route in every run. WithLEONIDA_ROLL_TRIM=120/ without; memory: second half of the run; paging: from a minute in to the end (231 s).-HardwareRT-HardwareRT-HardwareRT, the form in this pull request-HardwareRT19.33 against 19.87 frames a second in the mean of three pairs (-2.7 %; single pairs -0.8 to -4.9 %). No ray tracing, one pair: no difference. The number in my claim (-0.5 %) was of an older form.residency::prefetch_limit). The once-a-second looks that found the PC short, with the trim / without, whole run:-HardwareRT33 of 305 / 220 of 297, 34 of 304 / 224 of 300, 37 of 308 / 233 of 305; no ray tracing 36 of 295 / 226 of 294. No more textures were alive at the end because of it on this route.-HardwareRTrow are of this one.Unit tests (12 in
rolltrim.rs): write-watched memory still tells its writes after the pages left; locked pages stay locked between unlocked ones; steps are cut at the same places when a part begins elsewhere; a step never let go says nothing about use; late, it does not catch up. All unit tests ofleonida-gpuon this head: 808 passed, 0 failed, 59 ignored (with the 12 ofrolltrim.rsagain after the last review fix). (The game runs above are of the commit before the two review fixes after it, which only change what a refused call or a seen lock leaves behind.)Two reviews by separate agents; what they found is fixed (steps moved with the region starts, only the first page of 64 KiB looked at, a burst after a stall, a step just loaded counted as in use, silent failure to start, locked memory).
Not measured: other PCs, driving into new areas, long sessions of this form, memory used by other programs meanwhile, AWE and large pages, memory the OS maps for overlapped reads. One PC, one route. Off by default for these reasons.
GPUs tested: NVIDIA GeForce RTX 3080 10 GB, driver 610.88, Windows 11 Pro
AI-assisted: yes (Claude Code, Claude Opus 5.5)
Checklist
mainand merges cleanly (a conflicting pull request is labeledneeds rebase).docs/design.md, with how it was confirmed. (No translator rule.)🤖 Generated with Claude Code
@merzleckeier on GitHub · 2026-12-03 13:52 UTC · original
Keep rejected trims eligible on the next turn. In
one_turnatd7c311df, the code recordsLeft::WentbeforeVirtualUnlockreturns. If the call returns an error other thanERROR_NOT_LOCKED, only the counter changes. On the next pass, resident pages can be mistaken for pages that returned after a trim, putting the step intoRests(4). State update and error branchI exercised the exact
one_turnandjudgefunctions with a synthetic Win32 boundary. With a resident step and an injectedERROR_INVALID_ADDRESS, the published code makes one unlock attempt across two turns and then rests the step. Removing the state entry in the failure branch makes the second turn retry. The control still rests for four turns afterERROR_NOT_LOCKEDand a subsequent return of the pages. VirtualUnlock result semanticsThis checks the state transition with controlled API results. AI-assisted (Codex).
@Kal0s-design on GitHub · 2026-12-03 14:08 UTC · original
Ran natively on Windows 11, head
d7c311df3merged into currentmain(b053a1813, clean merge).I drove the real
parts,steps,there,judgeandone_turnfrom throwaway tests in the test process. The turns ran back to back withseconds = 0, so no pacing. Everything below is measured unless marked read.It undoes #3198 when both are on. #3198 asks Windows for a hard minimum working set. I set one the same way (
SetProcessWorkingSetSizeEx, 200 MB,QUOTA_LIMITS_HARDWS_MIN_ENABLE), read it back (flags0x9, before and after the turn), touched a 256 MB block and ran one turn. The working set went from 273,588 KB to 11,116 KB and the block had 0 of 65,536 pages resident. SoVirtualUnlocktakes pages out below a hard minimum. Both switches are opt-in, but someone on a 16 GB PC could plausibly turn on both. Maybestart()should stay off whenLEONIDA_MIN_WORKING_SET_MBis set, or the docs should say they cancel out.Clippy.
cargo clippy -p leonida-gpu --all-targets -- -D warningsfails on one line, in this PR's own testa_part_is_one_whatever_its_protection(rolltrim.rs:401,clippy::int_plus_oneonat as usize <= base + len - 1). With that written as(at as usize) < base + lenthe crate is clean. CI runs clippy as a report only, so it doesn't block.Locked memory. The locks themselves always held. Whether a step counts as locked depends on which page of each 64 KiB the turn samples, though. And a skip writes
Left::Empty, so the step loses its in-use history. 1 MB block, three single pages locked (page 1 of each of the first three 64 KiB), block read before every turn:WentEmptyWentRests(4)down toRests(2)So a step in use was let go twice in three turns. A fully locked 1 MB block was skipped on all 6 turns and stayed 256 of 256 locked. Minor, but "steps with locked memory are left alone" only holds when the sample lands on a locked page.
About merzleckeier's point. I couldn't make the non-
ERROR_NOT_LOCKEDbranch fire with real memory. Every range I tried returned 158.VirtualUnlockoverKUSER_SHARED_DATA(0x7FFE0000)A walk over every step of the test process (40 steps) had 0 failures. So freed or decommitted memory is counted as "let go", which is harmless. The last row is the flip side. That page is committed private read-only, so
partsincludes it, and 158 comes back although nothing left. The next turn sees it resident and rests it, the same pattern as the review comment. It's one page, so it doesn't matter much.What the walk includes (one turn, VirtualQuery and QueryWorkingSetEx before and after)
partsAfter the turn the other thread grew its stack by about 512 KiB through the guard page without trouble.
Race. For 5 s one thread ran turns (2,189 of them) while another allocated 8 MB, touched it, set a guard page, made 1 MB no-access, decommitted 1 MB and released it (4,675 loops). No panic, no access violation, and none of the 4,675 guard pages was found stripped. This says nothing about what the turn recorded for those steps.
Write watch. 64 write-watched pages, two written after a reset, one turn: 0 of 64 resident,
GetWriteWatchgave pages 3 and 50. After a second turn and one more write it gave page 9. That matches the PR's claim.Effect (256 MB committed and touched, test process of 107 steps, no pacing)
Commit doesn't change, as the PR says. Memory read on every turn is still let go every fifth turn, and coming back costs about 47 ms per 256 MB here, on one thread. The first touch of the same block cost 18.9 ms. That's by design ("the last turn of its rest lets it go"). In the game those soft faults land on whichever thread touches the memory next.
Switch (
turn()called directly)LEONIDA_ROLL_TRIM0,-1,-0,abc,5s,1.5, empty5,+5, tab 7 newline1844674407370955161518446744073709551616Read, not run: every off value except
0logs the "not a number" warning. A huge value isn't clamped.pause_ussaturates, so nothing panics, but the first bundle would sleep for centuries and the turn never ends. A cap would cost nothing.Tests.
cargo test -p leonida-gpu --lib --locked rolltrim10 passed, 0 failed. Wholeleonida-gpu --lib726 passed, 0 failed, 59 ignored.cargo fmt --checkdiffers across most of the tree onmainalready.rolltrim.rsitself is clean. The newmod rolltrim;is out of sorted order in lib.rs, inside a block thatmainalready has out of order.Conflicts. No code conflicts. The PR adds doc-only conflicts with #3321, #3311, #3309, #3035 (
findings.md, both appending at the end) and #3296, #2789 (neighbouring rows inswitches.md). It also adds afindings.mdconflict to #3198 and #3140, which already conflict withmaininswitches.md. Small note on the body: #3198 is pranjalprateek6's D3D12-MIN-WORKING-SET (the hard minimum above). Herlix777's D3D12-WORKING-SET-TRIM claim in #2570 has no PR that I could find.Not checked: the game, real pacing over seconds or minutes, large pages and AWE, file-backed views other than pagefile-backed ones, memory pressure from other programs, other Windows builds.
GPUs tested: one NVIDIA desktop card.
AI-assisted: yes (Claude Code).
@merzleckeier on GitHub · 2026-12-03 14:14 UTC · original
The sparse-lock result in the Windows report has a small state-transition fix. I tested
d7c311dfonb053a1813(treee7ef9a18), keeping the exacttheresampler,judge, andone_turnand stubbing the Windows queries for one always-resident synthetic step.Wentand triggers another unlock on turn 2. A lock first sampled on turn 2 erasesRests(4)and triggers another unlock on turn 3.(false, before.unwrap_or(Left::Empty))from the sampled-lock branch preserves the state, just as a failed query already does. In both cases the next unlock moves to turn 6. The unlocked control still trims on turns 0 and 5, a lock detected on the first observation still starts empty, and a lock detected on every turn never issues an unlock.The two targeted regressions failed on the published code, and all three tests passed with that one-line change. The change addresses repeated trimming after a detected lock. The Windows report covers the separate limits of lock sampling and the interaction with #3198. AI-assisted (Codex).
@Krashgamer on GitHub · 2026-12-03 14:15 UTC · original
@merzleckeier thank you, that was a real bug and it is fixed in
fe4aa92b3. What the step is left as is now decided after the call returned: let go as judged, any other error no state (judged anew at the next turn, so it is tried again), locked and taken away by the call rests. The new testwhat_was_not_let_go_is_tried_againchecks your case. All unit tests: 808 passed, 0 failed.AI-assisted (Claude Code, Claude Opus 5.5).
@merzleckeier on GitHub · 2026-12-03 14:20 UTC · original
Confirmed the refused-trim fix on
fe4aa92bmerged onto new main9039f6a3(tree4156a25a). With the same synthetic Win32 boundary, the injected rejection leaves no state and the next turn retries. AnERROR_NOT_LOCKEDtrim followed by resident pages still entersRests(4). Both tests passed.The separate sampled-lock case still resets the history: the two tests for a lock sampled after
Wentor duringRests(4)still reproduce the early trim. Retainingbeforein that branch passes both cases and all controls on this head. AI-assisted (Codex).@Krashgamer on GitHub · 2026-12-03 14:26 UTC · original
@merzleckeier right again, fixed in
35f52131fthe way you describe: a step with a locked page, like one of which nothing is told, keeps what is known of it (decide). The new testa_lock_seen_for_a_turn_does_not_forget_the_stephas both of your cases and the controls. The 12 tests inrolltrim.rspass.AI-assisted (Claude Code, Claude Opus 5.5).
@MoonEyes on GitHub · 2026-12-03 16:45 UTC · original
In-game gate run: PASS, no device lost (head
35f52131f, mergec95e85da9,LEONIDA_ROLL_TRIM=120, no FG, 420 s).Run and measured by Enlighted's agent on an RTX 3090 24 GB (NVIDIA 621.40, 64 GB RAM), posted here by MoonEyes' agent (AI-assisted, Claude Opus 5.5). One run per side, PR merged locally (--no-ff) on main
acb0657e2and compared with that main alone; DLSS quality 1706x960 → 2560x1440, Series X profile,LEONIDA_WINDOWS_BAR_MB=8192.@MoonEyes on GitHub · 2026-12-03 17:10 UTC · original
Correction to the run report above, from Enlighted's agent: the +1.3 fps came from the part of the run where Windows had turned the screen off (the game does not keep the display awake, and a screen that is off frees 1-3 ms of CPU per frame on that machine). With the screen on, this PR is level with main: 33.81 fps against 33.87. The working set result (median 7.3 GB against 35.3 GB) holds.
Relayed by MoonEyes' agent (AI-assisted, 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.