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

Closed
Kalmarv wants to merge 1 commit from upstream-pr/204 into main
Kalmarv commented 2026-11-23 20:47:44 +00:00 (Migrated from github.com)

Imported from ivall/xcl. Originally posted by Kalmarv on 2026-11-23T20:47:44Z.


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

<!-- greyfinch-sync:ivall/xcl:issue:6568517729 --> Imported from [ivall/xcl](https://github.com/xcl-dev/xcl/pull/204). Originally posted by [Kalmarv](https://github.com/Kalmarv) on 2026-11-23T20:47:44Z. --- 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)
upstream-sync closed this pull request 2026-11-24 06:57:38 +00:00
Kalmarv commented 2026-11-24 07:04:18 +00:00 (Migrated from github.com)

Imported from ivall/xcl. Originally posted by Kalmarv on 2026-11-23T20:56:26Z.


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

<!-- greyfinch-sync:ivall/xcl:comment:7113773887 --> Imported from [ivall/xcl](https://github.com/ivall/xcl/pull/204#issuecomment-7113773887). Originally posted by [Kalmarv](https://github.com/Kalmarv) on 2026-11-23T20:56:26Z. --- 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 commented 2026-11-24 07:04:19 +00:00 (Migrated from github.com)

Imported from ivall/xcl. Originally posted by Kalmarv on 2026-11-23T21:04:07Z.


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.

<!-- greyfinch-sync:ivall/xcl:comment:7113952100 --> Imported from [ivall/xcl](https://github.com/ivall/xcl/pull/204#issuecomment-7113952100). Originally posted by [Kalmarv](https://github.com/Kalmarv) on 2026-11-23T21:04:07Z. --- 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`.
ivall commented 2026-11-24 07:04:22 +00:00 (Migrated from github.com)

Imported from ivall/xcl. Originally posted by ivall on 2026-11-23T21:10:10Z.


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!

<!-- greyfinch-sync:ivall/xcl:comment:7114098439 --> Imported from [ivall/xcl](https://github.com/ivall/xcl/pull/204#issuecomment-7114098439). Originally posted by [ivall](https://github.com/ivall) on 2026-11-23T21:10:10Z. --- 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

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
xcl-dev/xcl!201
No description provided.