Skip to content

D3D12.X: a descriptor copy looks its console slots up under one lock - #466

Open
TG0000 wants to merge 1 commit into
ivall:mainfrom
TG0000:perf/d3d12x-hotpath
Open

TG0000 wants to merge 1 commit into
ivall:mainfrom
TG0000:perf/d3d12x-hotpath

Conversation

@TG0000

@TG0000 TG0000 commented Nov 24, 2026

Copy link
Copy Markdown

A device descriptor copy over console heaps (CopyDescriptorsSimple #24 / CopyDescriptors #23) copied the slot words one pair at a time, and looked each side of each pair up with console_heap::slot(). Every lookup locked the heap list and scanned all of it, locked the shared heap to read its geometry (view slots), and copy_words then locked the XCL_TRACE_FRAME resource map once per slot, even with tracing off. That came to about 2n heap-list scans and 3n–4n mutex round-trips for every n-descriptor copy, on a path the title runs every frame.

console_heap::copy_words_run now does the whole run under one lock of the heaps:

  • it tries the heap of the previous pair first (heap_of(.., hint)). Heaps never overlap, so a hit is the same heap the scan would find, and a miss falls back to the scan.
  • it reads the shared heap's (cpu, gpu, inc) once per run, and only when a view slot needs it. These values are fixed once the heap is made, and GLOBAL is never set back to None.
  • copy_words looks up the resource map only when trace_frame() is on, which is when note_resource would use it.

The behaviour is the same as before: the same pairs, the destination looked up before the source for each pair (so shared-heap pages are reserved in the same order), pairs with a side outside the console heaps skipped, and the same words written. slot() now uses the same heap_of / slot_in code.

What is in it

  • crates/xcl/src/hle/d3d12x/console_heap.rs: slot() is split into heap_of + slot_in. Adds copy_words_run, and copy_words skips the resource-map lookup when tracing is off. Adds unit tests and an #[ignore]d release microbenchmark.
  • crates/xcl/src/hle/d3d12x/real.rs: in copy_console_slots, the per-slot loop is replaced by one copy_words_run call (3 lines).

No open PR touches these hunks: #187 edits console_heap.rs only at lines ~27 and ~157–200 (shared_heap), and no open PR edits real.rs near copy_console_slots.

Tested

  • AMD Radeon RX 6800 (driver 32.0.21045.29003), Ryzen 9 5900X, Windows 11 build 26300, rustc 1.100.0 stable-x86_64-pc-windows-msvc
  • New unit tests:
    • a_run_copies_what_each_slot_did: for views and for samplers, it compares copy_words_run with the old per-slot loop on identical heaps. The run starts 2 slots before the end of one heap, goes into a heap right after it, then into memory that belongs to no heap; it also copies whole heaps and copies from outside the console heaps. Image T# are renumbered, V# keep their address, S# word 3 is renumbered. Run 10 times under the parallel test runner: passed every time.
    • a_heap_hint_is_checked: a wrong hint, an out-of-range hint, a correct hint, and an address just past a heap.
  • cargo +stable-x86_64-pc-windows-msvc test -p xcl --lib console_heap: 5 passed, 0 failed, 1 ignored. This includes the existing slots_copy_and_release and large_heaps_get_shared_pages_as_used, which exercise slot() with the shared heap on the real device.
  • cargo +stable-x86_64-pc-windows-msvc test -p xcl --lib: 636 passed, 2 failed, 22 ignored. The 2 failures are vram::tests::d32s8_targets_take_their_own_size_of_video_memory and title_textures::tests::placed_title_textures_share_vram_and_preserve_pixels ("3D 64x64x64 4 mips: subresource 2 mismatch"). They fail the same way on origin/main (a6f5f7a) without this change, on this adapter, and are in files this PR does not touch.
  • cargo +stable-x86_64-pc-windows-msvc clippy -p xcl --all-targets: no new warnings. The only console_heap.rs warnings are the 4 that were already there (# Safety docs on existing unsafe fns). The release build has the same single rustc warning as main.
  • Release microbenchmark, cargo test --release -p xcl --lib copy_run_speed -- --ignored --nocapture. It copies 64 view slots with 48 heaps registered, with the busiest pair registered last:
    copy of 64 view slots, 48 heaps: a lookup per slot 5826 ns, one run 1546 ns (3.8x)
    copy of 64 view slots, 48 heaps: a lookup per slot 6067 ns, one run 1823 ns (3.3x)
    

Regression run

scripts/smoke.ps1 -Seconds 45, one run each, under a 1 s free-RAM watchdog (it never fired):

xcl-smoke v1
commit: 2997c9aca6ebf4577fed34ebdb3cb0a77dd9ec21
gpu: AMD Radeon RX 6800
title: retail-title-1.0.2.1
seconds: 45
exit: 0x0
frames shown: 692 (PresentX 692)
presents per 2 s (last 3): 55 55 56
device removals: 0
crashes: 0
peak commit: 15153 MB
xcl-smoke v1
commit: ed2b685a9b9fea12f31c104c74a656d6ac9e942b
gpu: AMD Radeon RX 6800
title: retail-title-1.0.2.1
seconds: 45
exit: 0x0
frames shown: 828 (PresentX 828)
presents per 2 s (last 3): 58 60 59
device removals: 0
crashes: 0
peak commit: 15154 MB

These are single runs, and frame counts vary from run to run. I don't claim this change caused the difference in frame counts. The point of these runs is that nothing regressed: no device removal, no crash, and the same peak commit.

Not tested

AI use

  • Claude Code with Claude Opus 5.5: found the hot path, wrote the change, the tests and the benchmark, and ran the builds, tests and smoke runs. I reviewed the diff.

🤖 Generated with Claude Code

CopyDescriptorsSimple / CopyDescriptors over console heaps copied the slot
words one pair at a time, each side through console_heap::slot(): a lock of
the heaps and a scan of all of them per descriptor, a lock of the shared
heap for its geometry per view slot, and a lock of the resource map
(XCL_TRACE_FRAME diagnostics) per slot even with tracing off.

copy_words_run does the run under one lock of the heaps, tries the heap of
the previous pair first (heaps do not overlap, so a hit is the scan's
answer), reads the shared heap's geometry once (fixed once made, never
dropped), and copy_words reads the resource map only when it is noted.
Same pairs, same order of page reservation (destination first), same words.

Release microbenchmark (copy_run_speed, 64 view slots, 48 heaps):
a lookup per slot ~5.9 us, one run ~1.6 us (3.3-3.8x).

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

Copy link
Copy Markdown

Tested

Windows 11 build 26300, Ryzen 7 9800X3D, RTX 3080 Ti (driver 621.35), Rust 1.99.1 MSVC, release build. Tested commit ed2b685a

  • cargo test --release --locked -p xcl --lib console_heap -- --test-threads=1 --nocapture — 5 passed, 0 failed, 1 ignored (the benchmark), and 0 reported skips.
  • cargo test --release --locked -p xcl --lib copy_run_speed -- --ignored --nocapture — passed in three separate processes. Across six samples from this revision’s synthetic benchmark (64 descriptors, 48 heaps), per-slot lookup took 3,743–3,921 ns per copy and batched lookup took 1,031–1,052 ns per copy, a 3.6–3.7× speedup in this benchmark.

Not tested

Full workspace suite, AMD, shared-heap page reservation through the batched copy on a real device, retail titles, or per-frame performance.

AI use

Codex with GPT-6.2 Sol ran the tests and drafted this report.

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.

2 participants