Skip to content

Memory: a scatter or map home of n pages costs O(n), not O(n²) - #254

Merged
ivall merged 1 commit into
ivall:mainfrom
Kalmarv:fix/scatter-cost
Nov 23, 2026
Merged

ivall merged 1 commit into
ivall:mainfrom
Kalmarv:fix/scatter-cost

Conversation

@Kalmarv

@Kalmarv Kalmarv commented Nov 23, 2026 •

Copy link
Copy Markdown
Contributor

Title: Memory: a scatter or map home of n pages costs O(n), not O(n²)

XMemMapPhysicalPagesScatter and MapTitlePhysicalPages asked VirtualQuery whether each page's home was committed (committed_page). The cost of VirtualQuery grows with the run of same-state pages after the address, and on homes committed as a whole each call walked the rest of the allocation. Scattering 16,384 written pages from their home took 29 s on main, and mapping them home again 12.7 s. Both grow with the square of the page count. This is the 0.27 ms/page that #204's description left open.

What is in it
All in kernelx.rs:

  • committed_page asks the working set first (QueryWorkingSetEx, one entry): a valid page is committed. Only pages outside the working set (committed but untouched, or not committed) still go to VirtualQuery.
  • commit_homes asks VirtualQuery once per run of pages (committed_range), not once per page.
  • A map at home tells unmap_away which homes it has just committed, so they are not asked about again.
    • Behaviour change: when another page mapped at one of those homes goes home, that address now stays committed, because it is one of the homes being mapped. Before, it was decommitted, and home_again committed it again later. Without this line the copy would go to decommitted memory: a_page_away_at_another_home_goes_home_with_it crashes (exit 139).
  • Tests:
    • scatter_matches_a_model_of_physical_pages: 4 seeds × 600 random steps over 24 pages and 32 addresses. A step is a scatter of 1–4 pages (page 0: unmap), a write through a mapped address, or a map of the whole allocation at its home. After each step, every mapped address reads its page's last value, every address with no page is uncommitted, and a map at home shows every page's value.
    • a_page_away_at_another_home_goes_home_with_it: guards the behaviour change above. It passes on main too; it fails only when this PR's change is kept without that line.
    • a_home_decommitted_in_the_same_call_is_asked_again: no home's commit state may be kept across a call; an earlier draft that cached it failed this test.
    • scatter_timing: ignored, the timing below.

Release build, Ryzen 9 9950X, CPU only. scatter_timing writes n pages at their home, scatters them into a reservation in one call ("from home"), scatters them again in reverse, and maps them back home. One run each on 7a033c8 (kernelx.rs is the same on 9ab7d76):

pages from home reverse back home
4,096 main 1,656 ms 2,329 ms 821 ms
4,096 this PR 153 ms 184 ms 102 ms
8,192 main 6,258 ms 4,651 ms 3,004 ms
8,192 this PR 227 ms 404 ms 200 ms
16,384 main 28,951 ms 9,973 ms 12,690 ms
16,384 this PR 568 ms 813 ms 428 ms
32,768 this PR 1,247 ms 1,927 ms 1,085 ms

Earlier main heads measured the same: d4f17ca 24.4 / 7.8 / 10.5 s and af20b4b 23.7 / 7.8 / 12.9 s at 16,384 pages.

Related:

Tested

Regression run

main: Pending: no title on our machine; asked testers.

PR: Pending: no title on our machine; asked testers.

Not tested

AI use

  • Claude Code (Claude Opus 5.5): profiled the cost, wrote the change, the tests and this description.

VirtualQuery's cost grows with the run of pages in one state that follows the
address: on homes written as a whole it took 0.25 ms or more a call. map_pages
asked it about each page's home, and the walk was the rest of the allocation, so
scattering 16,384 pages from their home took 24 s, and mapping them home again
10 s.

- committed_page asks the working set first: a valid page is committed, one
  entry. Only a page outside it (committed but untouched, or not committed)
  still goes to VirtualQuery.
- commit_homes asks VirtualQuery once per run, not once per page.
- A map at home tells unmap_away which homes it has just committed. A page away
  at an address among them leaves that address committed: it is one of the
  homes now.

Tests: a model of physical pages (4 seeds x 600 random scatters, writes and
maps home), a page away at another page's home mapped home with it, a home
decommitted inside one call, and an ignored timing test (scatter_timing).

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
@Kalmarv Kalmarv changed the title Physical pages: a scatter or map home of n pages costs O(n), not O(n²) Memory: a scatter or map home of n pages costs O(n), not O(n²) Nov 23, 2026
@Kalmarv

Kalmarv commented Nov 23, 2026

Copy link
Copy Markdown
Contributor Author

Regression run wanted. We have no title on our machine. If you can run scripts/smoke.ps1 -Game <your title folder> twice, once built from main (9ab7d76) and once from this PR's head (2c5fe3f), please paste both xcl-smoke blocks here. I'll copy them into the description with your name and hardware.

🤖 Claude Opus 5.5

@likecrank-dotcom

Copy link
Copy Markdown

Independent Windows/NVIDIA validation of #254 at 2c5fe3ffe418b13c0a9cb1c735dc287db3149efc, against clean main@9ab7d763197c7f420bff10cd9bf1310f07e2a9aa.

Tested

  • Ryzen 5 3600, RTX 5060 Ti (driver 621.14 / 32.0.16.2114), 32 GB RAM, Windows 11 Pro 10.0.26300; Rust 1.100.0, MSVC 19.44, SDK 10.0.26100.0, DXC 1.9.2602.17. Debug builds.
  • Both clean trees: cargo build --workspace exits 0.
  • Both clean trees: cargo test --workspace -- --test-threads=4 --nocapture exits 0.
    • main: 651 passed, 0 failed, 40 ignored (lib: 611/0/20).
    • PR: 654 passed, 0 failed, 41 ignored (lib: 614/0/21).
  • Both include the synthetic-title end-to-end test. The PR's three new correctness tests all pass.

Synthetic timing
The PR author's unchanged scatter_timing benchmark was copied into a separate baseline worktree (test-only addition; main runtime unchanged). XCL_SCATTER_PAGES=4096,8192 cargo test -p xcl --lib scatter_timing -- --ignored --nocapture --test-threads=1 on that control and on the clean PR. One run per row below, debug builds, milliseconds. The baseline rows below were collected after the other build/test jobs finished; these are not gameplay timings.

pages tree from home reverse back home
4096 main + test-only controls 4578.7 6173.3 2296.7
4096 PR 148.3 272.8 112.4
8192 main + test-only controls 18226.1 15037.1 8916.3
8192 PR 316.6 664.9 223.0

Control that did not reproduce the reported crash
I also copied only a_page_away_at_another_home_goes_home_with_it into that baseline runtime and ran it alone, cargo test -p xcl --lib a_page_away_at_another_home_goes_home_with_it -- --nocapture --test-threads=1: 1 passed, exit 0. UMA initialization succeeded (512 MB coherent window). So this particular test is not a failing-before/passing-after control on this Windows host; I cannot confirm the reported pre-fix crash from it. The performance improvement above does reproduce.

SKIPPED / Not tested
Both complete runs reported the same four skips: gpu_geometry_wave32_prefix_count_over_a_mask and gpu_wave32_program_in_wave64_hardware (no wave64); test_title_draws_with_a_root_signature_under_the_debug_layer (no d3d12SDKLayers.dll); uploads_start_from_each_subresources_state (no D3D12 debug layer, 0x887A002D). No XCL_SAMPLE_* variables, no retail title, no retail xcl-smoke blocks, no AMD/Wine testing. This does not satisfy the requested retail regression run and does not establish GTA VI playability. One pre-existing AtomicU32::fetch_update deprecation warning on Rust 1.100.0.

AI use
Codex prepared the local test-only controls, ran the commands and summarized the actual logs. Exact deployed model identifier is not exposed in this session. No production code changed or game data published.

@PHeitor1

Copy link
Copy Markdown

Tested 2c5fe3f merged onto main 9ab7d76 on RTX 5060 Ti 8 GB (Blackwell, 32-lane waves), NVIDIA 621.24, Ryzen 5 5600X (Zen 3), Windows 11 Pro 26300 (native), rustc 1.100.0 MSVC, debug, cargo test --workspace --no-fail-fast -- --test-threads=4, no XCL_SAMPLE_*. Baseline main 9ab7d76: 651 passed, 0 failed, 40 ignored.

Pass: 654 passed, 0 failed, 41 ignored. GPU skips: the same 2 as on main (no wave64).

Run and written with Claude Code (Claude Opus 5.5).

josilo added a commit to josilo/xcl that referenced this pull request Nov 23, 2026
@Kalmarv

Kalmarv commented Nov 23, 2026

Copy link
Copy Markdown
Contributor Author

@likecrank-dotcom thanks for the independent run, and for checking the timing on a second machine.

a_page_away_at_another_home_goes_home_with_it passes on main by design: it is not a before/after control. It guards a line this PR adds (!committed.contains(&at) in unmap_away), and fails only when the PR's change is kept without that line. Measured now on 2c5fe3f: with the line it passes; with only that line taken out, the test process crashes (exit 139).

Your results are credited under Tested in the description.

🤖 Claude Opus 5.5

forbesfields pushed a commit to forbesfields/xcl that referenced this pull request Nov 23, 2026
Handle 16-byte constant-buffer records, upload intro textures from their CPU view, and decode BC1 BCPack streams. Share bounded texture pages, batch upload staging, and tolerate pages decommitted during streaming reads.

Includes work from ivall/xcl ivall#208, ivall#244, ivall#252, ivall#254, ivall#269 and ivall#283. DirectStorage late destination pages are already in the base.

Assisted-by: OpenAI Codex (gpt-6.2-sol)
@ivall
ivall merged commit ef97948 into ivall:main Nov 23, 2026
1 of 5 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants