Sparse windows: commits before the device are merged page ranges, not a list scanned per page #204

Closed
github-bot wants to merge 1 commit from gh-pr/204/head into gh-pr/204/base
Owner

@⁠Kalmarv on GitHub · 2026-11-23 20:47 UTC · original
Kalmarv:fix/sparse-early-ranges → main · +177 −7 · 1 files · 1 commits · closed without merging

Before the D3D12 device exists, sparse::committed appended one (addr, len) per call to EARLY, and sparse::decommitted ran EARLY.retain(..) over the whole list each time. Moving N pages away therefore cost O(N²). This was found in the #187 review: #187 and #203 put more of the retail title's memory in sparse ranges, which sends it down this path.

Complementary to #203. #203 fixes the same pattern after the device exists: decommit_tiles pruned to_zero at every decommit, and #203 prunes it once. This PR is the pre-device path only and does not touch decommit_tiles. gw1337x's watchdog stack on #187 (unmap_away → sparse::decommitted → Vec::retain) fits either retain, and #203 ties the same watchdog to the post-device one.

Change

EARLY is now a BTreeMap of disjoint page ranges (start → end), merged with their neighbours. A commit or decommit before the device is O(log n). init commits the ranges once the windows exist, as before.

A range counts every page it touches, as committed / decommitted already do once the state exists. A decommit takes out only its own pages.

One behaviour change. The old list dropped every entry a decommit overlapped. So after commit_homes committed several pages in one call and move_home_away gave one of them back, init left the pages that were still committed as holes. They are now kept.

Timing

Release build, Ryzen 9 9950X, CPU only (no device, so the early path), one run each.

early_commit_timing (new, ignored) commits 64 KiB pages one at a time, then decommits them one at a time in reverse:

pages decommit, main decommit, this PR
4,096 1.9 ms 0.1 ms
8,192 7.4 ms 0.2 ms
16,384 34.2 ms 0.3 ms
32,768 115.9 ms 0.6 ms

XMemMapPhysicalPagesScatter in reverse, then unmap, through kernelx. The sparse and plain targets differ only in committed / decommitted. The probe is the one from the #187 review and is not in this PR.

pages scatter, plain scatter, sparse unmap, plain unmap, sparse
16,384 main 4,590 ms 5,020 ms 404 ms 440 ms
16,384 this PR 4,482 ms 4,433 ms 309 ms 362 ms
32,768 main 9,727 ms 10,483 ms 752 ms 1,364 ms
32,768 this PR 8,915 ms 8,924 ms 597 ms 623 ms

With this PR the sparse cost matches the plain one.

This does not remove the whole stall, and probably isn't its main cause. In a release build the old early path would cost only about 1 s even at 100,000 pages, so the debug-build numbers in the #187 review overstated it, and the after-device retain that #203 fixes is the likelier culprit. The scatter itself costs about 0.27 ms per page on plain memory too, linear in the page count (about 27 s at 100,000 pages if that holds). I have not found where that goes. Hoisting page_home out of unmap_away's away.gone.retain saved only about 7%, so it is not in this PR.

Tests

  • early_commits_are_the_pages_a_page_set_holds: 8 seeds × 4,000 random commits and decommits, unaligned ones included. After each step, the pages match a page-set model, and the ranges are page-aligned, disjoint and merged.
  • early_commits_match_the_old_list_for_page_entries: 20,000 random steps against the old list, for what it handled exactly (single-page commits, page-aligned decommits of any length). The resulting pages are the same.
  • early_decommit_of_one_page_keeps_the_rest: the case the old list got wrong.
  • A mutation check passes: dropping the tail re-insert in early_remove fails the first two tests.
  • cargo test --release -p xcl --lib -- --test-threads=4 on Windows 11 with an RTX 5070 Ti (NVIDIA 610.74):
tree passed failed ignored
this branch (83f9c91 on 956a476) 539 0 20
main, the control (plus 3 ignored local probes) 536 0 22
merged onto #187 (8c0346d), clean merge 544 0 22
merged onto #203 (7cbd008), clean merge 539 0 20

The 3 extra passes on this branch are its new tests. The extra ignored test is early_commit_timing.

Not tested:

  • The retail title.
  • The path once the device exists: that is #203's.
  • AMD and Wine.

AI-assisted (Claude Code); synthetic memory only, no game files.

🤖 Generated with Claude Code

**@⁠Kalmarv** on GitHub · 2026-11-23 20:47 UTC · [original](https://github.com/xcl-dev/xcl/pull/204) `Kalmarv:fix/sparse-early-ranges` → `main` · +177 −7 · 1 files · 1 commits · closed without merging Before the D3D12 device exists, `sparse::committed` appended one `(addr, len)` per call to `EARLY`, and `sparse::decommitted` ran `EARLY.retain(..)` over the whole list each time. Moving N pages away therefore cost O(N²). This was found in the #187 review: #187 and #203 put more of the retail title's memory in sparse ranges, which sends it down this path. **Complementary to #203.** #203 fixes the same pattern after the device exists: `decommit_tiles` pruned `to_zero` at every decommit, and #203 prunes it once. This PR is the pre-device path only and does not touch `decommit_tiles`. gw1337x's watchdog stack on #187 (`unmap_away → sparse::decommitted → Vec::retain`) fits either retain, and #203 ties the same watchdog to the post-device one. ## Change `EARLY` is now a `BTreeMap` of disjoint page ranges (start → end), merged with their neighbours. A commit or decommit before the device is O(log n). `init` commits the ranges once the windows exist, as before. A range counts every page it touches, as `committed` / `decommitted` already do once the state exists. A decommit takes out only its own pages. **One behaviour change.** The old list dropped every entry a decommit overlapped. So after `commit_homes` committed several pages in one call and `move_home_away` gave one of them back, `init` left the pages that were still committed as holes. They are now kept. ## Timing Release build, Ryzen 9 9950X, CPU only (no device, so the early path), one run each. `early_commit_timing` (new, ignored) commits 64 KiB pages one at a time, then decommits them one at a time in reverse: | pages | decommit, `main` | decommit, this PR | |---:|---:|---:| | 4,096 | 1.9 ms | 0.1 ms | | 8,192 | 7.4 ms | 0.2 ms | | 16,384 | 34.2 ms | 0.3 ms | | 32,768 | 115.9 ms | 0.6 ms | `XMemMapPhysicalPagesScatter` in reverse, then unmap, through `kernelx`. The sparse and plain targets differ only in `committed` / `decommitted`. The probe is the one from the #187 review and is not in this PR. | pages | | scatter, plain | scatter, sparse | unmap, plain | unmap, sparse | |---:|---|---:|---:|---:|---:| | 16,384 | `main` | 4,590 ms | 5,020 ms | 404 ms | 440 ms | | 16,384 | this PR | 4,482 ms | 4,433 ms | 309 ms | 362 ms | | 32,768 | `main` | 9,727 ms | 10,483 ms | 752 ms | 1,364 ms | | 32,768 | this PR | 8,915 ms | 8,924 ms | 597 ms | 623 ms | With this PR the sparse cost matches the plain one. **This does not remove the whole stall, and probably isn't its main cause.** In a release build the old early path would cost only about 1 s even at 100,000 pages, so the debug-build numbers in the #187 review overstated it, and the after-device retain that #203 fixes is the likelier culprit. The scatter itself costs about 0.27 ms per page on plain memory too, linear in the page count (about 27 s at 100,000 pages if that holds). I have not found where that goes. Hoisting `page_home` out of `unmap_away`'s `away.gone.retain` saved only about 7%, so it is not in this PR. ## Tests - `early_commits_are_the_pages_a_page_set_holds`: 8 seeds × 4,000 random commits and decommits, unaligned ones included. After each step, the pages match a page-set model, and the ranges are page-aligned, disjoint and merged. - `early_commits_match_the_old_list_for_page_entries`: 20,000 random steps against the old list, for what it handled exactly (single-page commits, page-aligned decommits of any length). The resulting pages are the same. - `early_decommit_of_one_page_keeps_the_rest`: the case the old list got wrong. - A mutation check passes: dropping the tail re-insert in `early_remove` fails the first two tests. - `cargo test --release -p xcl --lib -- --test-threads=4` on Windows 11 with an RTX 5070 Ti (NVIDIA 610.74): | tree | passed | failed | ignored | |---|---:|---:|---:| | this branch (`83f9c91` on `956a476`) | 539 | 0 | 20 | | `main`, the control (plus 3 ignored local probes) | 536 | 0 | 22 | | merged onto #187 (`8c0346d`), clean merge | 544 | 0 | 22 | | merged onto #203 (`7cbd008`), clean merge | 539 | 0 | 20 | The 3 extra passes on this branch are its new tests. The extra ignored test is `early_commit_timing`. **Not tested:** - The retail title. - The path once the device exists: that is #203's. - AMD and Wine. AI-assisted (Claude Code); synthetic memory only, no game files. 🤖 Generated with [Claude Code](https://claude.com/claude-code)
Before the device exists, `sparse::committed` appended one entry per call to a list and
`sparse::decommitted` scanned the whole list, so moving N pages away cost O(N^2). The list is
now a BTreeMap of disjoint, merged page ranges: a commit or a decommit is O(log n).

A range counts every page it touches, as `committed` / `decommitted` do once the state
exists, and a decommit now takes out only its pages. The list dropped every entry a decommit
overlapped, so after `commit_homes` (several pages in one call) and `move_home_away` (one of
them), `init` left the still-committed pages as holes.

Tests: random sequences against a page-set model and against the old list (one entry per
page, the case it handled exactly), one page out of a multi-page range, and an ignored timing
probe (`early_commit_timing`).

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 04:15:00 +00:00
github-bot closed this pull request 2026-11-26 04:15:00 +00:00
Author
Owner

@⁠Kalmarv on GitHub · 2026-11-23 20:56 UTC · original

Rebased on main 2abc562 (68a6640). The only conflict was next to the new pool_bytes in sparse.rs; this change is otherwise unchanged. cargo test --release -p xcl --lib sparse::: 9 passed, 1 ignored (the timing probe).

**@⁠Kalmarv** on GitHub · 2026-11-23 20:56 UTC · [original](https://github.com/xcl-dev/xcl/pull/204#issuecomment-7113773887) Rebased on main 2abc562 (68a6640). The only conflict was next to the new `pool_bytes` in `sparse.rs`; this change is otherwise unchanged. `cargo test --release -p xcl --lib sparse::`: 9 passed, 1 ignored (the timing probe).
Author
Owner

@⁠Kalmarv on GitHub · 2026-11-23 21:04 UTC · original

Rebased again, now on main eaa36b6 (8f662ea, no conflicts). cargo test --release -p xcl --lib sparse::: 9 passed, 1 ignored (the timing probe).

Overlaps: #187 also reworks the code around the homes in kernelx.rs and sparse.rs. This PR changes only the pre-device EARLY list and its two callers, so the two stay separable. Main's 38b05b7 (lazy homes, pool release) didn't touch EARLY.

**@⁠Kalmarv** on GitHub · 2026-11-23 21:04 UTC · [original](https://github.com/xcl-dev/xcl/pull/204#issuecomment-7113952100) Rebased again, now on main eaa36b6 (8f662ea, no conflicts). `cargo test --release -p xcl --lib sparse::`: 9 passed, 1 ignored (the timing probe). Overlaps: #187 also reworks the code around the homes in `kernelx.rs` and `sparse.rs`. This PR changes only the pre-device `EARLY` list and its two callers, so the two stay separable. Main's 38b05b7 (lazy homes, pool release) didn't touch `EARLY`.
Author
Owner

@⁠ivall on GitHub · 2026-11-23 21:10 UTC · original

Merged into main (6da33fa) as 68a66402, the head before your last rebase - the same patch (identical patch-id), so GitHub does not show it as merged; closing it by hand. Checked on an RX 9070 (RDNA4) with the 22 PRs merged together: 582 unit tests passed, 0 failed, the integration and e2e tests passed, and the retail game still reaches its first menu (646 frames in 30 s, no device removal). Thanks!

**@⁠ivall** on GitHub · 2026-11-23 21:10 UTC · [original](https://github.com/xcl-dev/xcl/pull/204#issuecomment-7114098439) Merged into main (6da33fa) as 68a66402, the head before your last rebase - the same patch (identical patch-id), so GitHub does not show it as merged; closing it by hand. Checked on an RX 9070 (RDNA4) with the 22 PRs merged together: 582 unit tests passed, 0 failed, the integration and e2e tests passed, and the retail game still reaches its first menu (646 frames in 30 s, no device removal). Thanks!

Pull request closed

This discussion has been locked. Commenting is limited to contributors.
No description provided.