Skip to content

Memory: sparse tiles the CPU leaves alone leave the write-watch look (#582, off by default) - #1034

Open
AlexLopezDevelop wants to merge 1 commit into
xcl-dev:mainfrom
AlexLopezDevelop:sparse-cool
Open

AlexLopezDevelop wants to merge 1 commit into
xcl-dev:mainfrom
AlexLopezDevelop:sparse-cool

Conversation

@AlexLopezDevelop

@AlexLopezDevelop AlexLopezDevelop commented Nov 24, 2026 •

Copy link
Copy Markdown

Part of #582: step 1-2 of the plan there (direction agreed in #582 (comment) and claimed in #582 (comment)). Off by default: XCL_SPARSE_COOL=1 turns it on; it does nothing under Wine (util::under_wine) until a run there shows a gain.

One commit, b3c944b, on main 930e8b4. 775 lines added: about 230 of them are tests, so the code is about 545, over the ~400 guideline; it is one mechanism (cool, warm, and the places that must warm), and I did not find a split where each half stands on its own.

What is in it

  • hle/d3d12x/sparse_cool.rs (new): a 64 KiB tile of a sparse window with no written page for 8 looks is made read-only and left out of the GetWriteWatch look. Its first CPU write faults; a vectored handler makes it writable again, and the next look takes it back with that write in the write watch as before. At most 64 tiles are cooled and 1,024 examined per look; the tile list is rebuilt at most every 64 looks.
  • A tile written between the look and the protect is not cooled: after VirtualProtect a non-resetting GetWriteWatch on the tile undoes it if a page was written meanwhile.
  • warm_tile raises the module's generation before VirtualProtect, under the read-only set's lock, so a write right after the warm is in the next submit's ranges.
  • Writes that do not fault are warmed first: kernelx::lazy::ensure (so DirectStorage destinations and cpu_writes), eh::is_writable (the CLZERO / CLZERO-loop emulation on CPUs without CLZERO), the title's VirtualProtect, same-process VirtualProtectEx and sparse commit, and sparse::committed / decommitted.
  • A warmed tile is not cooled again for 5 s, and hk_read_file pins its buffer for the whole call (sparse_cool::pin), so a cooling pass on another thread cannot protect a tile between the warm and the kernel's write, nor under an overlapped read that lands after the call returns. Pins and warm times are two lock-free arrays of 2^18 slots (a slot is shared by tiles 16 GiB apart; sharing only keeps a tile writable longer), so the hot paths that warm on every call take no lock.
  • The title never sees the read-only protection: XMemVirtualQuery reports a cooled tile as read-write, and VirtualProtectEx warms first, so its old protection is the title's.
  • The handler only takes the module's own lock, never sparse's STATE, so a readback that writes title memory under STATE and faults does not deadlock.
  • The look's ranges are made as on main and cached on the same three keys; the read-only tiles are taken out afterwards, in a second cache keyed on those ranges and the module's generation (a sorted walk of the read-only set, about 0.25 ms here). Cooling raises the generation in batches (256 tiles or 64 looks); a warm still raises it at once. Before this, the generation was part of the first cache's key and @zorde23 measured the ranges made again 2.4× as often on an RX 6750 XT, eating most of the gain.
  • Only runs of at least 16 read-only tiles in a row (1 MiB) leave the look: each cut is one more GetWriteWatch call, about 1 µs, and a 64 KiB tile costs about 0.09 µs to look at on this CPU. A read-only tile left in the look is still warmed by its first write.
  • The sparse timing line gets a second line: ms and MB per look, how often the ranges were made again and filtered again (and the filter's time), the read-only / cooled / warmed tile counts, and the cooling pass's own time per look.

Tested

  • Intel i9-9900K (Coffee Lake, 1.4 ms per GiB of watched memory in my D3D12.X: the write-watch look scans 1.5 GiB before every submit while 4 pages are written (measurement and plan) #582 bench), NVIDIA RTX 2080 Ti (driver 621.24), 32 GB, Windows 11, rustc 1.100.0, release, DXC 1.9.2609, XCL_PSO_WAIT_MS=50. Retail 1.0.2.1, unattended (first menus), 120 s each.
  • From the timing lines of the b3c944b pair below, at 1,024 looks: 802 MB a look with XCL_SPARSE_COOL=1 (13,428 tiles read-only, 618 warmed) against 1,177 MB unset; ranges made again 28 times with the switch and 92 without, filtered again 317 times in 79 ms in all; cooling pass 0.39 ms a look (0.74 over the first 512 looks, while it cools 64 tiles a look).
  • Not a time gain on this CPU in these menus: 7.94 ms a look with the switch, 6.84 without, in this pair. A look's time here is mostly resetting written pages (about 320,000 a window in both runs), not the clean bytes (D3D12.X: the write-watch look scans 1.5 GiB before every submit while 4 pages are written (measurement and plan) #582), and it moves a lot between runs. Before the 16-tile minimum, single-tile cuts gave 8.39 ms with 895 MB against 7.37 ms with 1,502 MB (40dfd85); one run with the minimum gave 7.00 ms with 705 MB. The gain to expect is where the clean bytes dominate (@zorde23's world load on an RX 6750 XT: looks 20 % cheaper).
  • A first version of the pins and grace kept them in two Mutex<HashMap>s and recorded every tile of every warm: the cooling pass went to 2.5 ms a look. The arrays above brought it back.
  • Frames in 120 s swing about 20 % between runs here, in both directions (on/off pairs: 577/506, 414/516, 425/471, 366/409, 408/324; main 694, 462 and 332). The three blocks below were run back to back; the description check had compared an earlier main block (462) with the 366 run and failed. Taken together these runs show no frame-rate change either way.
  • cargo test --workspace -- --test-threads=4, debug, after cargo build --workspace: 761 passed, 0 failed, 41 ignored. SKIPPED GPU tests: gpu_geometry_wave32_prefix_count_over_a_mask, gpu_wave32_program_in_wave64_hardware (no wave64 on this GPU).
  • Tests: in sparse_cool, a cooled tile is warmed by its first write and the write is still watched; a tile written since the look is not cooled; warm restores write access; a pinned or just-warmed tile is not cooled; a pin warms a cooled tile; only tiles quiet for several looks are cooled; only runs of 16 read-only tiles leave the look; a cut is exact at a run's ends. eh::is_writable_warms_a_cooled_sparse_tile. kernelx::a_cooled_sparse_tile_looks_read_write_to_the_title (XMemVirtualQuery reports read-write; same-process VirtualProtectEx returns read-write as the old protection and puts the tile back in the look), the control @thehollowedone described.

Regression run

main:

xcl-smoke v1
commit: 930e8b49c1c2ec683d22c724a398b39111cd0e56
gpu: NVIDIA GeForce RTX 2080 Ti
title: 66a3dbd4
seconds: 120
exit: 0x0
frames shown: 332 (PresentX 332)
presents per 2 s (last 3): 6 6 6
device removals: 0
crashes: 0
peak commit: 15585 MB

PR (default, XCL_SPARSE_COOL unset):

xcl-smoke v1
commit: b3c944b0465ae9b55b44cb9c4d108538783fb5e7
gpu: NVIDIA GeForce RTX 2080 Ti
title: 66a3dbd4
seconds: 120
exit: 0x0
frames shown: 324 (PresentX 324)
presents per 2 s (last 3): 7 7 6
device removals: 0
crashes: 0
peak commit: 15544 MB

PR with XCL_SPARSE_COOL=1:

xcl-smoke v1
commit: b3c944b0465ae9b55b44cb9c4d108538783fb5e7
gpu: NVIDIA GeForce RTX 2080 Ti
title: 66a3dbd4
seconds: 120
exit: 0x0
frames shown: 408 (PresentX 408)
presents per 2 s (last 3): 8 8 9
device removals: 0
crashes: 0
peak commit: 15573 MB

Not tested

  • Wine (the switch does nothing there now); AMD GPUs; a debug build of the title run.
  • The world load and later: these runs take no input. A kernel write into a read-only tile that XCL does not route would fail with ERROR_NOACCESS rather than leave stale data; that is why it stays off by default.
  • A run on a CPU without CLZERO that reaches the emulated CLZERO on a cooled tile: covered by the unit test only. (The i9-9900K has no CLZERO, but I can't tell from these runs whether the title reached it on a cooled tile.)

AI use

  • Claude Code (Claude Opus 5.5): wrote the code and the tests, ran the builds, the tests and the smoke runs on the machine above, and wrote this description; reviewed and run by me.

🤖 Generated with Claude Code

@dcastell1

Copy link
Copy Markdown

Verified on Windows 11 10.0.26300, NVIDIA GeForce RTX 5090 (driver 621.24), Rust 1.100.0 MSVC 14.44.35207, debug build, on main@67a3735 (baseline 735 passed, 0 failed, 41 ignored on this box):

patch -p1 < this PR            # clean, no fuzz
cargo test --workspace -- --test-threads=4
739 passed; 0 failed; 41 ignored   (exit 0)

Four more passing tests than the baseline; the sparse-tile behaviour when the CPU leaves a tile alone ships its own coverage and it runs green here.

Tree restored afterwards (full diff against a clean checkout), hardware adapter, no Wine, no game data. AI-assisted (opencode): applied the patch to a built tree, ran the suite, reverted it.

@DavidM1911

Copy link
Copy Markdown
Contributor

Independent verification, merged onto current main 384f7f7 locally (clean merge).

Machine: Windows 11, Ryzen 9 7900X + RTX 3060 Ti; Rust stable MSVC, debug.

  • cargo test -p xcl --lib sparse: 20 passed, 0 failed, 1 ignored — all four new sparse_cool tests pass (a_tile_written_since_the_look_is_not_cooled, warm_restores_write_access_for_kernel_writes, a_cooled_tile_is_warmed_by_its_first_write_and_the_write_is_watched, only_tiles_quiet_for_several_looks_are_cooled), and the GPU test gpu_sparse_vram_descriptors_buffers_and_xmem passed on this run.
  • Scope note: the diff is ~412 changed lines (new sparse_cool.rs is 381), slightly over the ~400-line PR guideline in CONTRIBUTING — may be fine as one logical change, maintainer's call.

AI use: test runs by Devin (Cognition, SWE-2); no code changes.

@Rainmaker33

Rainmaker33 commented Nov 24, 2026 •

Copy link
Copy Markdown

Smoke pair on a fast CPU, as asked in #582: Zen 4, where the look is short to begin with.

Tested

  • AMD Ryzen 5 8600G (Zen 4, 6 cores), 32 GB, NVIDIA RTX 3090 (driver 32.0.16.1088), Windows 11 26H2 (26300), rustc 1.92.0, release build, DXC 1.8.2502.11.
  • Retail 1.0.2.1, unattended at the first menus, 120 s each, scripts/smoke.ps1: main 6efdabc, then this PR's head c31cdfe with XCL_SPARSE_COOL unset and with it set to 1. No overlays running.

Result: on this CPU the cooling pass takes back about half of what it saves.

last timing line (~75 s, 8,192 looks) unset XCL_SPARSE_COOL=1
ms per look 1.298 0.882
MB per look 2,036 1,198
write-watch total 10,634 ms 7,224 ms
chunk scan (with the cooling pass) 73 ms 1,959 ms
read-only tiles at the end (cooled / warmed) 0 13,407 (15,835 / 2,428)
  • The look is 32% shorter: 0.42 ms a look saved.
  • The cooling pass adds 0.23 ms a look, so the net saving is about 0.19 ms a look (−14%).
  • Unset, the PR matches main (10,494 ms write-watch, 72 ms chunk scan).
  • The menus are frame-capped at 30 fps (60 presents per 2 s) in all three runs, so the presents can't show a difference.
  • An earlier set of the same three runs, with Discord and MSI Afterburner (no RTSS) running, gave the same picture: 1.262 → 0.874 ms a look, cooling pass +0.26 ms a look.

main:

xcl-smoke v1
commit: 6efdabcdaac93ce5cc123a85e85deba5d49cb294
gpu: NVIDIA GeForce RTX 3090
title: retail title
seconds: 120
exit: 0x0
frames shown: 3370 (PresentX 3370)
presents per 2 s (last 3): 60 60 60
device removals: 0
crashes: 0
peak commit: 14735 MB

PR, XCL_SPARSE_COOL unset:

xcl-smoke v1
commit: c31cdfe196182966ec51c020b75933289788958a
gpu: NVIDIA GeForce RTX 3090
title: retail title
seconds: 120
exit: 0x0
frames shown: 3418 (PresentX 3418)
presents per 2 s (last 3): 60 60 60
device removals: 0
crashes: 0
peak commit: 14598 MB

PR, XCL_SPARSE_COOL=1:

xcl-smoke v1
commit: c31cdfe196182966ec51c020b75933289788958a
gpu: NVIDIA GeForce RTX 3090
title: retail title
seconds: 120
exit: 0x0
frames shown: 3362 (PresentX 3363)
presents per 2 s (last 3): 60 60 60
device removals: 0
crashes: 0
peak commit: 14723 MB

AI-assisted (Claude Code)

@sander110419

Copy link
Copy Markdown

Linux/Wine numbers for XCL_SPARSE_COOL=1 on the newer retail title: the looks get smaller, but under Wine both the look and the chunk-scan phase take longer, so the submitting thread spends about twice as long in this code.

Written by Claude (claude-opus-5-5) through Hermes, agent of the account owner, posted with his permission. These are Linux/Wine runs, not Windows smoke.ps1 runs.

Setup

The timing lines at 8192 submits

A: without this PR B: this PR, XCL_SPARSE_COOL=1
reached at 219.7 s 198.3 s
sparse windows: … ms spent 20774 ms 40270 ms
write-watch 41479 ms (2600215 dirty pages) 56968 ms (2349807 dirty pages)
lock wait 12590 ms 6881 ms
chunk scan 4780 ms 31158 ms
upload 3394 ms (810351 runs) 2222 ms (504258 runs)
per look 5.06 ms (41479 ms / 8192) 6.954 ms and 2880 MB a look
tiles read-only 60819 (cooled 86737, warmed 25918)

The B run's line at 2048 submits read write-watch look: 9.044 ms and 3787 MB a look, with chunk scan at 9680 ms. In the earlier run without this PR, chunk scan was 289 ms at 2048 submits.

What I take from it:

  • The look got smaller, but not faster. By 8192 submits it watches about 24 % less than at 2048 submits (2880 MB against 3787 MB), yet each look takes longer than A's average.
  • Chunk scan grew by about 26 s. That is the phase after_look runs in. Spread over the 86737 cooled tiles, it comes to about 0.3 ms per tile, which would be the VirtualProtect + GetWriteWatch pair in cool_tile, plus the warm-backs. That per-tile split is my estimate; I haven't timed the calls on their own.
  • A possible reason, not checked: each read-only tile may become a separate kernel mapping (VMA). Wine's kernel write-watch path then scans over many more mappings. I haven't counted /proc/<pid>/maps during a run yet; I will on the next run.
  • Neither run survives. Both end with the title's own stop at +0x23cf683 (A at 284 s, B at 272 s). In A the submitting thread is still inside title_textures::refresh → sparse::before_execute in the thread dumps at 240 s and 298 s. The thread in XMemMapPhysicalPagesScatter → map_pages → release_at waits on sparse's STATE at 298 s.

So on Wine this PR's trade (protect calls instead of watched bytes) currently costs more than it saves. Run A has no watched-bytes line (that line comes with this PR), so I can't give A a per-GiB figure. In B the looks cost about 2.4 ms per GiB, both at 2048 submits (9.044 ms for 3787 MB) and at 8192 (6.954 ms for 2880 MB). That is about 1.7× the 1.4 ms per GiB of your Windows bench.

AlexLopezDevelop added a commit to AlexLopezDevelop/xcl that referenced this pull request Nov 24, 2026
…, and skips cooled tiles

Rainmaker33's Zen 4 run on xcl-dev#1034 measured the pass at 0.23 ms a look, half of
what the shorter look saved there. Each look examined up to 4096 tiles through
a HashMap and asked the read-only set again for every tile already cooled. Now
the quiet state is a Vec indexed like the tile list (rebuilt when the mapped
tiles change), a cooled tile is marked in it and skipped until a write marks
it again, and a look examines at most 1024 tiles.

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

Copy link
Copy Markdown
Author

@Rainmaker33, thanks for the Zen 4 pair: it showed the cooling pass eating half the saving, which I had folded into the chunk-scan line. Pushed 3bc6aa0:

  • the quiet state is a Vec indexed like the tile list, not a HashMap, and a cooled tile is skipped until a write marks it again (each look used to ask the read-only set again for every cooled tile);
  • a look examines at most 1,024 tiles instead of 4,096;
  • the tile list (~118,000 tiles here) is rebuilt at most every 64 looks; it was rebuilt at every map or unmap, which the title does all through a load;
  • the timing line now prints the pass's own time (cooling pass … ms a look).

On the i9-9900K the pass is now 0.095-0.159 ms a look in two 120 s runs, against about 0.6 ms before. The description has the new smoke blocks for 3bc6aa0 (default and XCL_SPARSE_COOL=1) and the suite (731 passed, 0 failed, 41 ignored). If you can rerun the same pair on the 8600G, the cooling pass figure in the write-watch look: line would show the cost there directly.

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

@Rainmaker33

Copy link
Copy Markdown

Rerun of the same pair at 3bc6aa0 on the 8600G: the cooling pass is now 0.015 ms a look here, against about 0.23 ms at c31cdfe, so almost all of the saving is kept.

Tested: same machine and setup as above (Ryzen 5 8600G, RTX 3090, driver 32.0.16.1088, Windows 11 26300, rustc 1.92.0, release). Retail 1.0.2.1, unattended at the first menus, 120 s each, scripts/smoke.ps1, no overlays running. main is unchanged at 6efdabc, so the main block above still applies.

last timing line (~75 s, 8,192 looks) unset XCL_SPARSE_COOL=1
ms per look 1.315 0.910
MB per look 2,065 1,142
cooling pass 0.000 ms a look 0.015 ms a look
write-watch total 10,771 ms 7,450 ms
chunk scan 74 ms 192 ms
read-only tiles at the end (cooled / warmed) 0 13,406 (15,831 / 2,425)
  • The look is 31% shorter: 0.41 ms a look saved, and the pass takes back 0.015 ms of it. The net is about 0.39 ms a look (−30%), against −14% at c31cdfe.
  • The chunk-scan line agrees with the pass's own figure: +118 ms over 8,192 looks is 0.014 ms a look.
  • The menus are still frame-capped (60 presents per 2 s in both runs).

PR, XCL_SPARSE_COOL unset:

xcl-smoke v1
commit: 3bc6aa062e57e4e7beaf0698cec96727f33ffc96
gpu: NVIDIA GeForce RTX 3090
title: retail title
seconds: 120
exit: 0x0
frames shown: 3358 (PresentX 3358)
presents per 2 s (last 3): 60 60 60
device removals: 0
crashes: 0
peak commit: 14720 MB

PR, XCL_SPARSE_COOL=1:

xcl-smoke v1
commit: 3bc6aa062e57e4e7beaf0698cec96727f33ffc96
gpu: NVIDIA GeForce RTX 3090
title: retail title
seconds: 120
exit: 0x0
frames shown: 3409 (PresentX 3409)
presents per 2 s (last 3): 60 60 60
device removals: 0
crashes: 0
peak commit: 14727 MB

AI-assisted (Claude Code)

@ivall

ivall commented Nov 24, 2026

Copy link
Copy Markdown
Collaborator

Thanks – this follows the direction agreed in #582, and the measurements in the description and from @Rainmaker33 and @sander110419 are what was needed. Not merged yet: with XCL_SPARSE_COOL=1 I see the following. Points 1–4 are from reading 3bc6aa0, not reproduced in a run; tell me if I misread one.

  1. Crash on CPUs without CLZERO. eh.rs is_writable asks VirtualQuery, so a cooled tile is "not writable": ConsoleOnly::Clzero and ClzeroLoop then return 0, and the title gets the illegal instruction instead of a fault your handler could warm. A ClzeroLoop range with one read-only tile in it also loses the fast path (the region ends there). is_writable should warm the range first, with a test.
  2. ReadFile against the cooling pass. hk_read_file warms the buffer and then calls the original. A tile that was not cooled yet but has been quiet for QUIET_LOOKS can be cooled by a submitting thread in between, and the kernel's write then fails with ERROR_NOACCESS. Buffers a title reads into while it loads are just the kind of tile that has been quiet. A warm has to keep the range from being cooled until the call is done (a pin for the call), or at least for some time after it. The same holds for any other caller that warms for a kernel write.
  3. A write the next submit does not see. warm_tile raises GEN after VirtualProtect and after the lock is dropped. Between the two another thread can write the tile without a fault and submit; cached_watch_ranges still has the ranges of the old GEN, without the tile, so that write is only seen at the submit after. Raising GEN before VirtualProtect, under the lock, closes it.
  4. The title can see the protection. VirtualProtectEx does not warm (and reports read-only as the old protection), and XMemVirtualQuery reports a cooled tile as read-only.
  5. Wine. It costs more than it saves there in @sander110419's run. That run was posted before 3bc6aa0, so it is worth repeating; until it wins there, the switch should do nothing under Wine (util::under_wine).
  6. Please squash the two sparse_cool: commits into the first one.

Tested: 3bc6aa0 merged onto main 930e8b4 (no conflict), AMD, debug: cargo test --workspace --no-fail-fast -- --test-threads=4: 755 passed, 0 failed, 41 ignored, with XCL_SPARSE_COOL at 0 and at 1. The suite hardly ever makes enough looks for a tile to be cooled, so that says little about the switch.

Not tested: a retail title with the switch on.

@thehollowedone

Copy link
Copy Markdown

Confirmed point 4 on 3bc6aa0 applied to 930e8b4. Windows 11 build 26300, Rust 1.99.1 MSVC, debug.

On two synthetic write-watched tiles, XMemVirtualQuery returned PAGE_READONLY for the cooled tile and PAGE_READWRITE for its neighbor. A same-process VirtualProtectEx(..., PAGE_READWRITE) also returned PAGE_READONLY as the old protection. It made the tile writable, but the next CPU write still fell outside the ranges XCL would scan, even though native GetWriteWatch saw the dirty page. Repeating with XCL’s existing VirtualProtect wrapper returned PAGE_READWRITE as the old protection and kept the dirty tile in the scan. That gives a control for a same-process VirtualProtectEx regression test.

The stock first-write warming test passed. This was a synthetic memory check. AI-assisted. Codex (GPT-6, exact variant unavailable) wrote and ran the scratch fixture and drafted this report.

@AlexLopezDevelop

Copy link
Copy Markdown
Author

@ivall, thanks, you read all four right. Pushed 572d0dd (one commit, on 930e8b4):

  1. eh::is_writable warms the range first, so the emulated CLZERO and CLZERO loop see a cooled tile as writable and the loop keeps its fast path. Test: is_writable_warms_a_cooled_sparse_tile.
  2. hk_read_file pins its buffer for the whole call (sparse_cool::pin), and any warm keeps the tile from being cooled for 5 s after it, which also covers an overlapped read landing after the call returns and the other callers that warm for a kernel write. The cooling pass checks pins and warm times under the read-only set's write lock, which a warm waits for. Tests: a_pinned_or_just_warmed_tile_is_not_cooled, a_pin_warms_a_cooled_tile.
    My first version kept pins and warm times in two Mutex<HashMap>s and recorded every tile of every warm, and the cooling pass went from about 0.1 to 2.5 ms a look on the i9-9900K. They are now two lock-free arrays of 2^18 slots. Tiles 16 GiB apart share a slot, which can only keep a tile writable longer. The pass is back to 0.1 to 0.3 ms a look.
  3. warm_tile raises the generation before VirtualProtect, under the lock.
  4. Same-process VirtualProtectEx warms first, so it returns the title's read-write as the old protection, and XMemVirtualQuery reports a cooled tile as read-write. Test: kernelx::a_cooled_sparse_tile_looks_read_write_to_the_title, built from the control @thehollowedone described (thanks).
  5. enabled() is false under Wine.
  6. Squashed.

Tested: i9-9900K + RTX 2080 Ti, Windows 11. cargo test --workspace -- --test-threads=4 twice: 759 passed, 0 failed, 41 ignored. Smoke pair from main and 572d0dd is in the description: 0 crashes in all three, 929 MB a look with the switch against 1,746 MB without. Frames in 120 s swing about 20 % between runs on this machine in both directions, so these runs show no frame-rate change either way.

Not tested: Wine, AMD, the world load.

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

@zorde23

zorde23 commented Nov 24, 2026

Copy link
Copy Markdown

AMD RX 6750 XT data point for #1034 (RDNA2, driver 32.0.21045.29003; Ryzen 7 5700X, 32 GB; Windows 11 Pro 26200; release builds, all on 6efdabc)

Retail title of #505, 150 s per run with A pressed into world loading, XCL_NETWORK=0 XCL_ACTIVITY=1 XCL_PSO_PRELOAD=0. A local diagnostic counter (not part of either change) sums, per run, the time refresh_once spends in cached_watch_ranges and in the write-watch looks; two runs each:

cached_watch_ranges computed again time in it write-watch looks largest watched set frames
main 6efdabc 2 406 / 2 392 3.0 / 2.6 s (peak 385 / 447 ms per 2 s) 22.9 / 22.6 s 13.9 / 13.3 GB 2 254 / 2 441
#1034 3bc6aa0, XCL_SPARSE_COOL=1 5 654 / 5 860 6.8 / 6.0 s (peak 662 / 685 ms per 2 s) 18.3 / 17.6 s 10.8 / 10.4 GB 2 251 / 2 427
  • The looks get ~20% cheaper and the watched set ~22% smaller here, so the cooling works on AMD too.
  • But the watched ranges are computed again about 2.4x as often: sparse_cool::gen() is part of the cache key in cached_watch_ranges, and each cooling pass that cools a tile and each warmed tile bump it, so on this card the extra recomputation (+3.4-3.8 s per run) eats most of what the looks save (-4.6-5.0 s). Keeping the cooled set out of the key (e.g. filtering it after the cached computation, or recomputing only the sparse part) would keep both gains.
  • For reference, a local change that only adds new textures and tiles to the cached list (my comment on D3D12.X: the write-watch look scans 1.5 GiB before every submit while 4 pages are written (measurement and plan) #582) brought the recomputations to 17 (15 ms) in the same kind of run, with looks at 20.1 s; the two look complementary.
  • cargo test --workspace --no-fail-fast -- --test-threads=4 at 3bc6aa0: 728 passed, 3 failed, 41 ignored; the 3 failures (title_textures::placed_title_textures_share_vram_and_preserve_pixels, title_textures::transitions_apply_when_their_list_is_submitted, vram::d32s8_targets_take_their_own_size_of_video_memory) fail on this GPU on main too.

Smoke (default settings, so XCL_SPARSE_COOL off; the ~52 GB peak is the preload of #1035, not this PR):

main:

xcl-smoke v1
commit: 6efdabcdaac93ce5cc123a85e85deba5d49cb294
gpu: AMD Radeon RX 6750 XT
title: retail-a
seconds: 60
exit: 0x0
frames shown: 1540 (PresentX 1540)
presents per 2 s (last 3): 60 60 60
device removals: 0
crashes: 0
peak commit: 52979 MB

PR:

xcl-smoke v1
commit: 3bc6aa062e57e4e7beaf0698cec96727f33ffc96
gpu: AMD Radeon RX 6750 XT
title: retail-a
seconds: 60
exit: 0x0
frames shown: 1526 (PresentX 1526)
presents per 2 s (last 3): 60 60 60
device removals: 0
crashes: 0
peak commit: 52102 MB

Written with Claude Code from runs on this machine.

…vall#582)

Behind XCL_SPARSE_COOL=1 (off by default, and off under Wine), a 64 KiB tile
of a sparse window with no written page for 8 looks is made read-only and left
out of the GetWriteWatch look. Its first CPU write faults; a vectored handler
makes it writable again and the next look takes it back, the write itself in
the write watch as before. A tile written between the look and the protect
stays writable. The look's ranges are made again before a warmed tile can be
written without a fault.

Writes that do not fault warm the range first: kernelx::lazy::ensure (the
ReadFile hook, DirectStorage, cpu_writes), the emulated CLZERO's writability
check, and the title's commit, decommit, VirtualProtect and VirtualProtectEx.
A ReadFile pins its buffer for the call, and any warmed tile stays warm for
5 s, so a cooling pass cannot protect it under a kernel write in progress.
XMemVirtualQuery reports a cooled tile as the title left it (read-write).

The cooling pass keeps one slot per tile in a Vec indexed like the tile list,
skips cooled tiles, examines at most 1024 tiles and cools at most 64 a look,
and rebuilds the tile list at most every 64 looks. The timing line adds ms and
MB per look, the read-only tile counts and the pass's own time per look.

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

Copy link
Copy Markdown
Author

@zorde23, thanks, you were right: sparse_cool::gen() in the cache key made the ranges again on every cooling pass. Pushed b3c944b:

  • The ranges are made and cached on the same three keys as on main. The read-only tiles are taken out afterwards, in a second cache keyed on those ranges and on gen(), by a sorted walk of the cooled set.
  • Cooling raises gen() in batches (256 tiles or 64 looks); a warm still raises it at once.
  • Only runs of at least 16 read-only tiles in a row leave the look. With single-tile cuts the look on my i9-9900K got slower despite fewer bytes (8.39 ms with 895 MB against 7.37 ms with 1,502 MB): each cut is one more GetWriteWatch call, and that costs more than one 64 KiB tile does.
  • The timing line now prints how often the ranges were made again and filtered again, and the filter's time.

Tested (i9-9900K + RTX 2080 Ti, Windows 11, release, the first menus of the same title as in the description, 120 s, at 1,024 looks):

  • With XCL_SPARSE_COOL=1: ranges made again 28 times, filtered again 317 times in 79 ms in all, 802 MB a look.
  • Without: ranges made again 92 times, 1,177 MB a look.
  • The look time is no better on this CPU in these menus (7.94 against 6.84 ms in this pair). It is mostly resetting written pages here, and it moves a lot between runs.
  • cargo test --workspace -- --test-threads=4: 761 passed, 0 failed, 41 ignored.

Not tested: the world load. A rerun of your counter at b3c944b would show whether both gains now hold on the 6750 XT.

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

@z961089510-dev

z961089510-dev commented Nov 24, 2026 •

Copy link
Copy Markdown

A heavier configuration than the 1080p runs already posted — 4K display target, a 16 GB NVIDIA card and 31 GB of RAM — and one outcome none of them had: with the feature on the run survives at 30 fps until memory ends it.

Machine

CPU Intel Core Ultra 7 265K (Arrow Lake, 20C/20T, no SMT)
GPU NVIDIA GeForce RTX 5070 Ti, 16 GB (15995 MiB), driver 32.0.16.2101
RAM 31 GB
OS Windows 11 Pro, build 26200
XCL main 930e8b4 + this PR (b3c944b), release build, cargo build --release --workspace
DXC 1.9.2609.5 (dxcompiler/dxil from X:\game\dxc19\bin\x64)
Title RockstarGamesInc.28872443D20A7 1.0.2.1, unpacked, on a spinning HDD
Display 3840x2160 display targets (the title picks 4K here)
Settings XCL_NETWORK=0, XCL_ACTIVITY=1
Caches warm: 25,647 DXIL files / 2.27 GB + 200 driver blobs (xcl shaders --precompile)

A/B: the same script, the same run length (300 s) and XCL_PSO_WAIT_MS=50 in both; only XCL_SPARSE_COOL differs.

at 16,384 looks (~150 s) unset XCL_SPARSE_COOL=1
write-watch look 3,246 MB, 2.262 ms 1,359 MB, 1.359 ms
write-watch cumulative 37,056 ms (4,471,266 pages) 22,258 ms (2,284,024 pages)
cooling pass — 0.045 ms a look (18,966 read-only, 23,977 cooled, 5,011 warmed)
fps over the last 300 22.4 -> 21.9 -> 7.1 -> 8.0 25.0-30.0 throughout (still 25.0 at 253 s)
frames 5,951 / 300 s 6,900 / 253 s
memory committed 43,202 MB at 300 s 64,022 MB at 255 s

The look dominates enough here that the smaller scan set takes the run from 7-8 fps to 25-30; note that the baseline also sits at 43 GB by 300 s, so part of its slowdown is paging, not the look alone.

All three runs at the same point (4,096 looks, ~45 s), the third with the default pipeline waits:

at 4,096 looks A: unset B: XCL_SPARSE_COOL=1 C: =1, XCL_PSO_WAIT_MS unset
write-watch look 1,686 MB, 1.209 ms 299 MB, 0.293 ms 345 MB, 1.035 ms
read-only tiles 0 13,386 13,406

What ends the run with it on: the process aborts, not the title's watchdog:

memory: 64022 MB committed by the process      (241 s 62,772 MB -> 255 s 64,022 MB, ~89 MB/s)
exit 0xC0000409                                (Rust allocator abort)
report.json: exit_code 3221226505

With the feature off the same title is still running at 300 s. So the growth is not caused by the cooling: it just runs long enough at a usable frame rate to hit it. 4K targets and 31 GB of RAM probably make this the first place it bites — worth knowing before XCL_SPARSE_COOL=1 becomes the default.

A third run, same machine and warm caches, XCL_SPARSE_COOL=1 with the default pipeline waits (XCL_PSO_WAIT_MS unset, 600 s cap): 30.0 fps at 600 frames, 4096 draws (0 skipped). Then commit runs 57,503 -> 60,579 -> 63,436 MB in four seconds (~1.5 GB/s) and the process is gone at ~50 s. So once the look is out of the way the run is memory-bound, and the faster the title gets, the sooner it reaches it: 15 MB/s while precompiling, ~89 MB/s in the A/B run above, ~1.5 GB/s here.

Data from my runs and logs on the machine above. No game files attached.

@zorde23

zorde23 commented Nov 24, 2026

Copy link
Copy Markdown

Thanks, re-ran it at b3c944b on the RX 6750 XT (same machine and method as above; release builds, XCL_NETWORK=0 XCL_ACTIVITY=1 XCL_PSO_PRELOAD=0 XCL_SPARSE_COOL=1, A pressed into world loading). Main is 930e8b4, the PR's base; both have b8b1f49 reverted locally, since with it memory grows ~1 GB/s here (#1307). A local memory cap stopped every run at 108-118 s on this 32 GB machine, so the numbers are for the first 102 s of each run; two runs each:

full recomputes time in cached_watch_ranges write-watch looks largest watched set presents
main 930e8b4 2 069 / 2 148 1.75 / 1.81 s (peak 464 / 387 ms per 2 s) 18.0 / 18.6 s 9.2 / 7.1 GB 2 152 / 2 191
#1034 b3c944b 2 154 / 2 165 2.84 / 2.60 s (peak 502 / 546 ms per 2 s) 13.4 / 13.2 s 6.5 / 8.2 GB 2 141 / 2 164
  • The full recomputations are back to main's count (they were ~2.4x at 3bc6aa0). The second cache filters again ~3 500 times by the 8 192nd look (3 518 / 3 469 in 613 / 597 ms, your "filtered again" line), which is most of the +0.8-1.1 s left in cached_watch_ranges.
  • The looks are ~27% cheaper (-4.6 to -5.4 s per 102 s), so the net gain is now 3.5-4.6 s per 102 s, against 1.7-2.3 s at 3bc6aa0 over the same window of the earlier runs (on 6efdabc).
  • The largest watched set varies with how far loading got in each run, so I would not read much into that column.
  • cargo test --workspace --no-fail-fast -- --test-threads=4 at b3c944b (b8b1f49 reverted): 757 passed, 3 failed, 41 ignored; the same 3 GPU tests as before fail on this card on main too.

Written with Claude Code from runs on this machine.

@0x6461726B

Copy link
Copy Markdown
Contributor

One missed site in b3c944b (found by reading the code + testing XCL_SPARSE_COOL=1 on a retail title):

crates/xcl/src/hle/dstorage.rs (writable()) calls crate::hle::kernelx::lazy::ensure(p, n) and then checks VirtualQuery for m.Protect & rw != 0 (PAGE_READWRITE | ...) before copying into a virtual destination. If a sparse tile in p..p+n was cooled to PAGE_READONLY by sparse_cool, writable() returns false and the DirectStorage request fails with E_ACCESS_VIOLATION (0x89240009).

Calling crate::hle::d3d12x::sparse_cool::warm(p, n); right after lazy::ensure(p, n); in dstorage::writable() avoids that (same pattern as eh::is_writable and vfs_hooks.rs).

Also, on RTX 2060 / i5-10400F (XCL_SPARSE_COOL=1 + #1075), GetWriteWatch over 4,096 submits dropped from ~34.5 s (~8.4 ms/look) to ~18.3 s (~4.5 ms/look) with ~66k sparse tiles cooled.

AI-assisted review (Antigravity, Gemini 4 Neon High).

@shelbykb2

Copy link
Copy Markdown
Contributor

A possible hole in the warm path, found on an AMD RX 6800 with a newer retail game. Read against head b3c944b; my runs use an older head of this PR in a local branch with other changes, so please check it against your code rather than take my word.

warm_tile restores read-write with one VirtualProtect over the whole 64 KiB tile, and its comment says a failure means the tile was decommitted and nothing is left to restore. VirtualProtect also fails when only part of the range is no longer committed. The tile then leaves the read-only set while its still-committed pages stay PAGE_READONLY, and the next write there faults with no entry in the set, so the handler passes it on.

What I saw: an access violation on a write into a sparse-window tile, from the layer's own code (a descriptor written into the title's heap). VirtualQuery at the crash showed the read-only region starting 15 pages into the tile, which fits a tile whose first part had been given back. This was with a local experiment that cools far more eagerly than this PR (2 quiet looks, up to 2,048 tiles a look); one crash in one run at 219 s. I have not seen it with this PR's own settings, and I did not find which call decommitted part of the tile without warming it first (a physical-page remap is my guess).

What fixed it here: when the whole-tile call fails, walk the tile with VirtualQuery and restore each committed read-only run separately. Two unit tests: a cooled tile with a decommitted page in the middle is fully writable after the warm; one of them fails without the change. After that, one 300 s run with the eager setting had no crash and no leftover read-only pages.

Separately, my numbers for this PR on the RX 6800 as it stands (no eager setting): the per-submit refresh went from 19.2 to 16.4 ms a call in one pair of runs, and I saw no picture change.

Measured in runs on my machine; the analysis and this report were written with Claude Code (Claude Opus 5.5, reviewed with Claude Fable 5.2).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

goal:world-load Bears on the retail title getting past world loading

Projects

None yet

Development

Successfully merging this pull request may close these issues.