d3d12: memory nobody touches leaves the working set, a little at a time (LEONIDA_ROLL_TRIM, off by default) #3332

Open
Krashgamer wants to merge 3 commits from upstream-pr/3332 into main
Krashgamer commented 2026-12-03 13:39:09 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by Krashgamer on 2026-12-03T13:39:09Z.


Claimed in the coordination issue as: D3D12-ROLLING-TRIM (https://github.com/ProjectLeonida/mandy/issues/2570#issuecomment-7371584107)

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.

  • A step (at most 4 MB, cut at fixed addresses) of which half was back at the next turn is in use, and left alone for four turns.
  • Steps with locked memory are left alone; a lock a call took away all the same is put back.
  • It runs on one below-normal thread of the host's sweep, and sleeps between bundles of steps.
  • Nothing is freed: the commit stays as it is, only the working set shrinks.

Tested

main d9b618f10 alone, RTX 3080 10 GB, 32 GB of RAM, Series X, 2560x1440, game gate 300 s in the world, the same route in every run. With LEONIDA_ROLL_TRIM=120 / without; memory: second half of the run; paging: from a minute in to the end (231 s).

game's working set, MB PC's free memory, MB frames a second, median (p5) stutters of 250 ms, of 1 s and more paged out, in, MB
-HardwareRT 12,100 / 27,708 6,838 / 799 19.48 (11.2) / 19.63 (10.4) 24, 0 / 41, 5 101, 3,508 / 67, 6,192
-HardwareRT 11,903 / 26,884 6,765 / 901 19.32 (11.0) / 19.82 (10.9) 22, 1 / 35, 2 76, 3,433 / 971, 4,886
-HardwareRT, the form in this pull request 12,081 / 27,728 6,721 / 644 19.19 (10.9) / 20.17 (10.9) 23, 0 / 31, 1 72, 3,806 / 0, 4,668
no ray tracing, the form in this pull request 11,360 / 26,275 7,286 / 935 24.23 (15.0) / 24.14 (14.2) 2, 0 / 0, 0 63, 4,322 / 1,135, 5,772
  • Speed: with -HardwareRT 19.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.
  • Texture loading's memory brake: while the PC is short of memory, textures picked by index may take 64 MB a frame in place of 192 MB (residency::prefetch_limit). The once-a-second looks that found the PC short, with the trim / without, whole run: -HardwareRT 33 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.
  • Not seen: card memory and textures alive are the same in all pairs; no error in any run, no crash, no device loss.
  • An earlier form, 15 minutes in the world, one run: no errors.
  • The first two pairs are of the form before the locked-memory rule, the last row and the last -HardwareRT row are of this one.
  • The calls take 1.4 to 5.9 s of a turn of 120 s.

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 of leonida-gpu on this head: 808 passed, 0 failed, 59 ignored (with the 12 of rolltrim.rs again 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

  • Based on current main and merges cleanly (a conflicting pull request is labeled needs rebase).
  • One topic; follow-up work goes in a new pull request.
  • No game data, keys or decryption (CONTRIBUTING.md), including in logs or reports quoted here.
  • A new translator rule is in the contract table of docs/design.md, with how it was confirmed. (No translator rule.)

🤖 Generated with Claude Code

<!-- greyfinch-sync:ubiquitarianism/leonida:issue:5627618429 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/3332). Originally posted by [Krashgamer](https://github.com/Krashgamer) on 2026-12-03T13:39:09Z. --- Claimed in the coordination issue as: D3D12-ROLLING-TRIM (https://github.com/ProjectLeonida/mandy/issues/2570#issuecomment-7371584107) 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. - A step (at most 4 MB, cut at fixed addresses) of which half was back at the next turn is in use, and left alone for four turns. - Steps with locked memory are left alone; a lock a call took away all the same is put back. - It runs on one below-normal thread of the host's sweep, and sleeps between bundles of steps. - Nothing is freed: the commit stays as it is, only the working set shrinks. ### Tested `main` `d9b618f10` alone, RTX 3080 10 GB, 32 GB of RAM, Series X, 2560x1440, game gate 300 s in the world, the same route in every run. With `LEONIDA_ROLL_TRIM=120` / without; memory: second half of the run; paging: from a minute in to the end (231 s). | | game's working set, MB | PC's free memory, MB | frames a second, median (p5) | stutters of 250 ms, of 1 s and more | paged out, in, MB | |---|---|---|---|---|---| | `-HardwareRT` | 12,100 / 27,708 | 6,838 / 799 | 19.48 (11.2) / 19.63 (10.4) | 24, 0 / 41, 5 | 101, 3,508 / 67, 6,192 | | `-HardwareRT` | 11,903 / 26,884 | 6,765 / 901 | 19.32 (11.0) / 19.82 (10.9) | 22, 1 / 35, 2 | 76, 3,433 / 971, 4,886 | | `-HardwareRT`, the form in this pull request | 12,081 / 27,728 | 6,721 / 644 | 19.19 (10.9) / 20.17 (10.9) | 23, 0 / 31, 1 | 72, 3,806 / 0, 4,668 | | no ray tracing, the form in this pull request | 11,360 / 26,275 | 7,286 / 935 | 24.23 (15.0) / 24.14 (14.2) | 2, 0 / 0, 0 | 63, 4,322 / 1,135, 5,772 | - **Speed:** with `-HardwareRT` 19.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. - **Texture loading's memory brake:** while the PC is short of memory, textures picked by index may take 64 MB a frame in place of 192 MB (`residency::prefetch_limit`). The once-a-second looks that found the PC short, with the trim / without, whole run: `-HardwareRT` 33 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. - **Not seen:** card memory and textures alive are the same in all pairs; no error in any run, no crash, no device loss. - **An earlier form, 15 minutes in the world, one run:** no errors. - **The first two pairs** are of the form before the locked-memory rule, the last row and the last `-HardwareRT` row are of this one. - The calls take 1.4 to 5.9 s of a turn of 120 s. 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 of `leonida-gpu` on this head: 808 passed, 0 failed, 59 ignored (with the 12 of `rolltrim.rs` again 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 - [x] Based on current `main` and merges cleanly (a conflicting pull request is labeled `needs rebase`). - [x] One topic; follow-up work goes in a new pull request. - [x] No game data, keys or decryption (CONTRIBUTING.md), including in logs or reports quoted here. - [x] A new translator rule is in the contract table of `docs/design.md`, with how it was confirmed. (No translator rule.) 🤖 Generated with [Claude Code](https://claude.com/claude-code)
merzleckeier commented 2026-12-03 13:52:24 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by merzleckeier on 2026-12-03T13:52:24Z.


Keep rejected trims eligible on the next turn. In one_turn at d7c311df, the code records Left::Went before VirtualUnlock returns. If the call returns an error other than ERROR_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 into Rests(4). State update and error branch

I exercised the exact one_turn and judge functions with a synthetic Win32 boundary. With a resident step and an injected ERROR_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 after ERROR_NOT_LOCKED and a subsequent return of the pages. VirtualUnlock result semantics

This checks the state transition with controlled API results. AI-assisted (Codex).

<!-- greyfinch-sync:ubiquitarianism/leonida:comment:7380524106 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/3332#issuecomment-7380524106). Originally posted by [merzleckeier](https://github.com/merzleckeier) on 2026-12-03T13:52:24Z. --- **Keep rejected trims eligible on the next turn.** In `one_turn` at `d7c311df`, the code records `Left::Went` before `VirtualUnlock` returns. If the call returns an error other than `ERROR_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 into `Rests(4)`. [State update and error branch](https://github.com/ProjectLeonida/mandy/blob/d7c311df382d7000ecee59275b1cde1edf1d0326/d3d12/crates/leonida-gpu/src/rolltrim.rs#L210-L222) I exercised the exact `one_turn` and `judge` functions with a synthetic Win32 boundary. With a resident step and an injected `ERROR_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 after `ERROR_NOT_LOCKED` and a subsequent return of the pages. [VirtualUnlock result semantics](https://learn.microsoft.com/en-us/windows/win32/api/memoryapi/nf-memoryapi-virtualunlock#remarks) This checks the state transition with controlled API results. AI-assisted (Codex).
Kal0s-design commented 2026-12-03 14:08:51 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by Kal0s-design on 2026-12-03T14:08:51Z.


Ran natively on Windows 11, head d7c311df3 merged into current main (b053a1813, clean merge).

I drove the real parts, steps, there, judge and one_turn from throwaway tests in the test process. The turns ran back to back with seconds = 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 (flags 0x9, 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. So VirtualUnlock takes pages out below a hard minimum. Both switches are opt-in, but someone on a 16 GB PC could plausibly turn on both. Maybe start() should stay off when LEONIDA_MIN_WORKING_SET_MB is set, or the docs should say they cancel out.

Clippy. cargo clippy -p leonida-gpu --all-targets -- -D warnings fails on one line, in this PR's own test a_part_is_one_whatever_its_protection (rolltrim.rs:401, clippy::int_plus_one on at as usize <= base + len - 1). With that written as (at as usize) < base + len the 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:

turn sampled page hits a lock block after the turn state locked pages
0 no 3 of 256 resident (let go) Went 3 of 3
1 yes 256 resident (skipped) Empty 3 of 3
2 no 3 of 256 resident (let go again) Went 3 of 3
3 to 5 no 256 resident Rests(4) down to Rests(2) 3 of 3

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_LOCKED branch fire with real memory. Every range I tried returned 158.

VirtualUnlock over return, error after
RW, PAGE_GUARD, RW 0, 158 guard kept, 0 of 3 resident
RW, PAGE_NOACCESS, RW 0, 158 0 of 3 resident
RW, decommitted, RW 0, 158 both RW pages out
RW, RW, decommitted 0, 158 both RW pages out
fully decommitted 0, 158
released 0, 158
read-only 0, 158 0 of 2 resident
KUSER_SHARED_DATA (0x7FFE0000) 0, 158 still resident

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 parts includes 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)

region in parts protection changed resident before, after
private RW, 64 KiB yes no 16, 0
PAGE_GUARD page no no, guard kept 0, 0
thread stack guard page no no 0, 0
PAGE_NOACCESS no no
MEM_RESERVE only no
MEM_IMAGE (own code page) no no 1, 1
MEM_MAPPED, pagefile-backed view, 1 MB yes no 256, 0, bytes intact after
KUSER_SHARED_DATA yes no 1, 1

After 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, GetWriteWatch gave 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)

turn time working set after, KB block resident private bytes, KB reading 256 MB again
before 273,580 65,536 284,572
0 23.7 ms 11,144 0 284,580 47.7 ms
1 to 4 (rests) 4.0 to 5.1 ms about 273,650 65,536 284,580 0.7 to 0.8 ms
5 24.2 ms 11,548 0 284,580 47.2 ms
6 4.0 ms 273,652 65,536 284,580 0.7 ms

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_TRIM result
0, -1, -0, abc, 5s, 1.5, empty off
5, +5, tab 7 newline 5, 5, 7
18446744073709551615 accepted as is
18446744073709551616 off

Read, not run: every off value except 0 logs the "not a number" warning. A huge value isn't clamped. pause_us saturates, 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 rolltrim 10 passed, 0 failed. Whole leonida-gpu --lib 726 passed, 0 failed, 59 ignored. cargo fmt --check differs across most of the tree on main already. rolltrim.rs itself is clean. The new mod rolltrim; is out of sorted order in lib.rs, inside a block that main already 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 in switches.md). It also adds a findings.md conflict to #3198 and #3140, which already conflict with main in switches.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).

<!-- greyfinch-sync:ubiquitarianism/leonida:comment:7380966740 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/3332#issuecomment-7380966740). Originally posted by [Kal0s-design](https://github.com/Kal0s-design) on 2026-12-03T14:08:51Z. --- Ran natively on Windows 11, head `d7c311df3` merged into current `main` (`b053a1813`, clean merge). I drove the real `parts`, `steps`, `there`, `judge` and `one_turn` from throwaway tests in the test process. The turns ran back to back with `seconds = 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 (flags `0x9`, 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. So `VirtualUnlock` takes pages out below a hard minimum. Both switches are opt-in, but someone on a 16 GB PC could plausibly turn on both. Maybe `start()` should stay off when `LEONIDA_MIN_WORKING_SET_MB` is set, or the docs should say they cancel out. **Clippy.** `cargo clippy -p leonida-gpu --all-targets -- -D warnings` fails on one line, in this PR's own test `a_part_is_one_whatever_its_protection` (rolltrim.rs:401, `clippy::int_plus_one` on `at as usize <= base + len - 1`). With that written as `(at as usize) < base + len` the 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: | turn | sampled page hits a lock | block after the turn | state | locked pages | |---|---|---|---|---| | 0 | no | 3 of 256 resident (let go) | `Went` | 3 of 3 | | 1 | yes | 256 resident (skipped) | `Empty` | 3 of 3 | | 2 | no | 3 of 256 resident (let go again) | `Went` | 3 of 3 | | 3 to 5 | no | 256 resident | `Rests(4)` down to `Rests(2)` | 3 of 3 | 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_LOCKED` branch fire with real memory. Every range I tried returned 158. | `VirtualUnlock` over | return, error | after | |---|---|---| | RW, PAGE_GUARD, RW | 0, 158 | guard kept, 0 of 3 resident | | RW, PAGE_NOACCESS, RW | 0, 158 | 0 of 3 resident | | RW, decommitted, RW | 0, 158 | both RW pages out | | RW, RW, decommitted | 0, 158 | both RW pages out | | fully decommitted | 0, 158 | | | released | 0, 158 | | | read-only | 0, 158 | 0 of 2 resident | | `KUSER_SHARED_DATA` (0x7FFE0000) | 0, 158 | **still resident** | 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 `parts` includes 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) | region | in `parts` | protection changed | resident before, after | |---|---|---|---| | private RW, 64 KiB | yes | no | 16, 0 | | PAGE_GUARD page | no | no, guard kept | 0, 0 | | thread stack guard page | no | no | 0, 0 | | PAGE_NOACCESS | no | no | | | MEM_RESERVE only | no | | | | MEM_IMAGE (own code page) | no | no | 1, 1 | | MEM_MAPPED, pagefile-backed view, 1 MB | yes | no | 256, 0, bytes intact after | | KUSER_SHARED_DATA | yes | no | 1, 1 | After 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, `GetWriteWatch` gave 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) | turn | time | working set after, KB | block resident | private bytes, KB | reading 256 MB again | |---|---|---|---|---|---| | before | | 273,580 | 65,536 | 284,572 | | | 0 | 23.7 ms | 11,144 | 0 | 284,580 | 47.7 ms | | 1 to 4 (rests) | 4.0 to 5.1 ms | about 273,650 | 65,536 | 284,580 | 0.7 to 0.8 ms | | 5 | 24.2 ms | 11,548 | 0 | 284,580 | 47.2 ms | | 6 | 4.0 ms | 273,652 | 65,536 | 284,580 | 0.7 ms | 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_TRIM` | result | |---|---| | `0`, `-1`, `-0`, `abc`, `5s`, `1.5`, empty | off | | ` 5 `, `+5`, tab 7 newline | 5, 5, 7 | | `18446744073709551615` | accepted as is | | `18446744073709551616` | off | Read, not run: every off value except `0` logs the "not a number" warning. A huge value isn't clamped. `pause_us` saturates, 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 rolltrim` 10 passed, 0 failed. Whole `leonida-gpu --lib` 726 passed, 0 failed, 59 ignored. `cargo fmt --check` differs across most of the tree on `main` already. `rolltrim.rs` itself is clean. The new `mod rolltrim;` is out of sorted order in lib.rs, inside a block that `main` already 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 in `switches.md`). It also adds a `findings.md` conflict to #3198 and #3140, which already conflict with `main` in `switches.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 commented 2026-12-03 14:14:35 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by merzleckeier on 2026-12-03T14:14:35Z.


The sparse-lock result in the Windows report has a small state-transition fix. I tested d7c311df on b053a1813 (tree e7ef9a18), keeping the exact there sampler, judge, and one_turn and stubbing the Windows queries for one always-resident synthetic step.

  • A lock first sampled on turn 1 erases Went and triggers another unlock on turn 2. A lock first sampled on turn 2 erases Rests(4) and triggers another unlock on turn 3.
  • Returning (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).

<!-- greyfinch-sync:ubiquitarianism/leonida:comment:7381121518 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/3332#issuecomment-7381121518). Originally posted by [merzleckeier](https://github.com/merzleckeier) on 2026-12-03T14:14:35Z. --- The sparse-lock result in the [Windows report](https://github.com/ProjectLeonida/mandy/pull/3332#issuecomment-7380966740) has a small state-transition fix. I tested `d7c311df` on `b053a1813` (tree `e7ef9a18`), keeping the exact `there` sampler, `judge`, and `one_turn` and stubbing the Windows queries for one always-resident synthetic step. - A lock first sampled on turn 1 erases `Went` and triggers another unlock on turn 2. A lock first sampled on turn 2 erases `Rests(4)` and triggers another unlock on turn 3. - Returning `(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 commented 2026-12-03 14:15:17 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by Krashgamer on 2026-12-03T14:15:17Z.


@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 test what_was_not_let_go_is_tried_again checks your case. All unit tests: 808 passed, 0 failed.

AI-assisted (Claude Code, Claude Opus 5.5).

<!-- greyfinch-sync:ubiquitarianism/leonida:comment:7381140815 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/3332#issuecomment-7381140815). Originally posted by [Krashgamer](https://github.com/Krashgamer) on 2026-12-03T14:15:17Z. --- @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 test `what_was_not_let_go_is_tried_again` checks your case. All unit tests: 808 passed, 0 failed. AI-assisted (Claude Code, Claude Opus 5.5).
merzleckeier commented 2026-12-03 14:20:20 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by merzleckeier on 2026-12-03T14:20:20Z.


Confirmed the refused-trim fix on fe4aa92b merged onto new main 9039f6a3 (tree 4156a25a). With the same synthetic Win32 boundary, the injected rejection leaves no state and the next turn retries. An ERROR_NOT_LOCKED trim followed by resident pages still enters Rests(4). Both tests passed.

The separate sampled-lock case still resets the history: the two tests for a lock sampled after Went or during Rests(4) still reproduce the early trim. Retaining before in that branch passes both cases and all controls on this head. AI-assisted (Codex).

<!-- greyfinch-sync:ubiquitarianism/leonida:comment:7381278682 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/3332#issuecomment-7381278682). Originally posted by [merzleckeier](https://github.com/merzleckeier) on 2026-12-03T14:20:20Z. --- Confirmed the refused-trim fix on `fe4aa92b` merged onto new main `9039f6a3` (tree `4156a25a`). With the same synthetic Win32 boundary, the injected rejection leaves no state and the next turn retries. An `ERROR_NOT_LOCKED` trim followed by resident pages still enters `Rests(4)`. Both tests passed. The separate sampled-lock case still resets the history: the two tests for a lock sampled after `Went` or during `Rests(4)` still reproduce the early trim. Retaining `before` in that branch passes both cases and all controls on this head. AI-assisted (Codex).
Krashgamer commented 2026-12-03 14:26:33 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by Krashgamer on 2026-12-03T14:26:33Z.


@merzleckeier right again, fixed in 35f52131f the 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 test a_lock_seen_for_a_turn_does_not_forget_the_step has both of your cases and the controls. The 12 tests in rolltrim.rs pass.

AI-assisted (Claude Code, Claude Opus 5.5).

<!-- greyfinch-sync:ubiquitarianism/leonida:comment:7381452130 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/3332#issuecomment-7381452130). Originally posted by [Krashgamer](https://github.com/Krashgamer) on 2026-12-03T14:26:33Z. --- @merzleckeier right again, fixed in 35f52131f the 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 test `a_lock_seen_for_a_turn_does_not_forget_the_step` has both of your cases and the controls. The 12 tests in `rolltrim.rs` pass. AI-assisted (Claude Code, Claude Opus 5.5).
MoonEyes commented 2026-12-03 16:45:37 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by MoonEyes on 2026-12-03T16:45:37Z.


In-game gate run: PASS, no device lost (head 35f52131f, merge c95e85da9, LEONIDA_ROLL_TRIM=120, no FG, 420 s).

  • Median working set 7,255 MB, against 35,295 MB for main (max over the window 19,620 against 35,425). Private memory unchanged (33,862 against 34,085 MB).
  • 35.5 fps against 34.2 (p99 43.3 against 49.9 ms): no frame loss. Card 97-98 % busy in both.
  • Trim rounds take 0.8 to 3.0 s of calls per 120 s round.

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 acb0657e2 and compared with that main alone; DLSS quality 1706x960 → 2560x1440, Series X profile, LEONIDA_WINDOWS_BAR_MB=8192.

<!-- greyfinch-sync:ubiquitarianism/leonida:comment:7385399191 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/3332#issuecomment-7385399191). Originally posted by [MoonEyes](https://github.com/MoonEyes) on 2026-12-03T16:45:37Z. --- In-game gate run: **PASS**, no device lost (head `35f52131f`, merge `c95e85da9`, `LEONIDA_ROLL_TRIM=120`, no FG, 420 s). - **Median working set 7,255 MB, against 35,295 MB for main** (max over the window 19,620 against 35,425). Private memory unchanged (33,862 against 34,085 MB). - 35.5 fps against 34.2 (p99 43.3 against 49.9 ms): no frame loss. Card 97-98 % busy in both. - Trim rounds take 0.8 to 3.0 s of calls per 120 s round. 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 `acb0657e2` and compared with that main alone; DLSS quality 1706x960 → 2560x1440, Series X profile, `LEONIDA_WINDOWS_BAR_MB=8192`.
MoonEyes commented 2026-12-03 17:10:53 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by MoonEyes on 2026-12-03T17:10:53Z.


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).

<!-- greyfinch-sync:ubiquitarianism/leonida:comment:7386094311 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/3332#issuecomment-7386094311). Originally posted by [MoonEyes](https://github.com/MoonEyes) on 2026-12-03T17:10:53Z. --- 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).
This pull request cannot be merged automatically due to conflicts.
Merge manually to resolve the conflicts.
View command line instructions

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u origin 'upstream-pr/3332:upstream-pr/3332'
git switch 'upstream-pr/3332'

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.

git switch 'main'
git merge --no-ff 'upstream-pr/3332'
git switch 'upstream-pr/3332'
git rebase 'main'
git switch 'main'
git merge --ff-only 'upstream-pr/3332'
git switch 'upstream-pr/3332'
git rebase 'main'
git switch 'main'
git merge --no-ff 'upstream-pr/3332'
git switch 'main'
git merge --squash 'upstream-pr/3332'
git switch 'main'
git merge --ff-only 'upstream-pr/3332'
git switch 'main'
git merge 'upstream-pr/3332'
git push origin 'main'
Sign in to join this conversation.
No reviewers
No labels
No milestone
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.

Reference
ProjectLeonida/mandy!3332
No description provided.