d3d12/shadow: index carded copies by title range for send_copied (D3D12-SEND-COPIED-RANGE-INDEX) #3493

Code

Clone this repository

Checkout

From your project repository, check out a new branch and test the changes.

git fetch -u  "d3d12/send-copied-range-index:und3rlake-d3d12/send-copied-range-index"
git switch "und3rlake-d3d12/send-copied-range-index"
Open
leonida/mandy
und3rlake wants to merge 1 commit from und3rlake/mandy:d3d12/send-copied-range-index into main

This branch is out-of-date with the base branch

Conversation
Reviewers 0 approvals
  • No reviewers
Details
This pull request is blocked because it's outdated.
This branch is out-of-date with the base branch
You are not authorized to merge this pull request.
View command line instructions

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u 'd3d12/send-copied-range-index:und3rlake-d3d12/send-copied-range-index'
git switch 'und3rlake-d3d12/send-copied-range-index'
First-time contributor

What / why

In steady-state world profiling (LEONIDA_PROFILE on leonida-submit, 12,423 samples), shadow::send_copied accounted for ~4.9% of active submitting-thread time (463 samples: 361 in carded_copies upgrading all ~3,700 Weak<Shadow> entries into a freshly allocated Vec<Arc<Shadow>>, plus 102 in send_copied scanning and dropping them). card_dirty already avoided this full scan via LEONIDA_CARD_QUEUE, while send_copied (called on every CPU title buffer copy when send::own() is on) still called carded_copies().

Change

  • Replace CARDED's bare BTreeMap<usize, Weak<Shadow>> with CardedTable<T> under the same mutex: keeps by_ptr for card_dirty / card_stats / carded_copies, plus a start-sorted Vec (by_start) with per-64-entry block_max_end (CARDED_BLOCK = 64).
  • CardedTable::overlapping(dst, len) binary-searches start < dst_end, skips every 64-entry block with block_max_end <= dst (preserving fast pruning even with huge overlapping windows in early blocks), and upgrades only the overlapping Weak entries.
  • Guard the indexed lookup in shadow::send_copied with LEONIDA_SEND_COPIED_INDEX (crate::toggled!, on by default, nameable in LEONIDA_AB, =0 falls back to carded_copies()), and reuse CardedTable::overlapping in shadow::elide::demote_overlapping.
  • Use saturating end arithmetic (start.saturating_add(len), dst.saturating_add(len)) and sweep both by_ptr and by_start in note_carded and card_dirty.
  • Regenerate d3d12/docs/switches.md (python -I tools/d3d12_switches.py --check d3d12/docs/switches.md).
  • Add unit tests in shadow::carded_table_tests verifying brute-force oracle equivalence across multi-block huge-window overlaps, dead Weaks, duplicate/moved Arc replacements, empty queries, usize::MAX saturation, and post-sweep shrinking.

Verification (NVIDIA RTX 5070 12 GB, Windows 11)

  • cargo test -p leonida-gpu --lib --locked shadow::: 68/68 PASS (0 failed, 2 ignored)
  • python -I tools/d3d12_switches.py --check d3d12/docs/switches.md & python -I scripts/check_no_game_data.py: PASS
  • In-run LEONIDA_AB=4,LEONIDA_SEND_COPIED_INDEX (-SeriesS -NoFrameGen -NoPrecompile -AutoMenu, 200 s in world, max_draws = 5,582, 0 ERROR lines, device-lost.txt = false):
    • tools/d3d12_ab_compare.py phases (25 paired blocks):
      • copies_fills_ms: -1.444 +- 0.150 ms (9.6 sigma)
      • steps_ms: -1.402 +- 0.271 ms (5.2 sigma)
      • median frame time (all 25 pairs, including world-entry transition): -1.624 +- 2.063 ms
    • Steady-state world phases (18 consecutive pairs, draws >= 3,500, frames >= 40):
      • median_ms: 38.75 ms (OFF) -> 37.60 ms (ON), paired -2.344 +- 1.045 ms (2.2 sigma)
      • mean_ms: 39.40 ms (OFF) -> 38.35 ms (ON), paired -2.339 +- 1.064 ms (2.2 sigma)
      • p90_ms: 42.25 ms (OFF) -> 40.90 ms (ON), paired -3.228 +- 1.596 ms (2.0 sigma)
    • scripts/ab_cpu.py (26 pairs): submit_user_ms median 24.408 ms (OFF) -> 22.293 ms (ON), frame_ms median 39.900 ms (OFF) -> 38.800 ms (ON).

AI-assisted.

## What / why In steady-state world profiling (`LEONIDA_PROFILE` on `leonida-submit`, `12,423` samples), `shadow::send_copied` accounted for **~4.9% of active submitting-thread time** (`463` samples: `361` in `carded_copies` upgrading all ~`3,700` `Weak<Shadow>` entries into a freshly allocated `Vec<Arc<Shadow>>`, plus `102` in `send_copied` scanning and dropping them). `card_dirty` already avoided this full scan via `LEONIDA_CARD_QUEUE`, while `send_copied` (called on every CPU title buffer copy when `send::own()` is on) still called `carded_copies()`. ## Change - Replace `CARDED`'s bare `BTreeMap<usize, Weak<Shadow>>` with `CardedTable<T>` under the same mutex: keeps `by_ptr` for `card_dirty` / `card_stats` / `carded_copies`, plus a `start`-sorted `Vec` (`by_start`) with per-64-entry `block_max_end` (`CARDED_BLOCK = 64`). - `CardedTable::overlapping(dst, len)` binary-searches `start < dst_end`, skips every 64-entry block with `block_max_end <= dst` (preserving fast pruning even with huge overlapping windows in early blocks), and upgrades only the overlapping `Weak` entries. - Guard the indexed lookup in `shadow::send_copied` with `LEONIDA_SEND_COPIED_INDEX` (`crate::toggled!`, on by default, nameable in `LEONIDA_AB`, `=0` falls back to `carded_copies()`), and reuse `CardedTable::overlapping` in `shadow::elide::demote_overlapping`. - Use saturating end arithmetic (`start.saturating_add(len)`, `dst.saturating_add(len)`) and sweep both `by_ptr` and `by_start` in `note_carded` and `card_dirty`. - Regenerate `d3d12/docs/switches.md` (`python -I tools/d3d12_switches.py --check d3d12/docs/switches.md`). - Add unit tests in `shadow::carded_table_tests` verifying brute-force oracle equivalence across multi-block huge-window overlaps, dead `Weak`s, duplicate/moved `Arc` replacements, empty queries, `usize::MAX` saturation, and post-sweep shrinking. ## Verification (`NVIDIA RTX 5070 12 GB`, `Windows 11`) - `cargo test -p leonida-gpu --lib --locked shadow::`: **`68/68` PASS** (`0` failed, `2` ignored) - `python -I tools/d3d12_switches.py --check d3d12/docs/switches.md` & `python -I scripts/check_no_game_data.py`: **PASS** - In-run `LEONIDA_AB=4,LEONIDA_SEND_COPIED_INDEX` (`-SeriesS -NoFrameGen -NoPrecompile -AutoMenu`, `200 s` in world, `max_draws = 5,582`, `0` `ERROR` lines, `device-lost.txt = false`): - `tools/d3d12_ab_compare.py phases` (`25` paired blocks): - **`copies_fills_ms`: `-1.444 +- 0.150 ms`** (`9.6 sigma`) - **`steps_ms`: `-1.402 +- 0.271 ms`** (`5.2 sigma`) - `median` frame time (all `25` pairs, including world-entry transition): `-1.624 +- 2.063 ms` - Steady-state world phases (`18` consecutive pairs, `draws >= 3,500`, `frames >= 40`): - `median_ms`: `38.75 ms` (OFF) -> `37.60 ms` (ON), paired **`-2.344 +- 1.045 ms`** (`2.2 sigma`) - `mean_ms`: `39.40 ms` (OFF) -> `38.35 ms` (ON), paired **`-2.339 +- 1.064 ms`** (`2.2 sigma`) - `p90_ms`: `42.25 ms` (OFF) -> `40.90 ms` (ON), paired **`-3.228 +- 1.596 ms`** (`2.0 sigma`) - `scripts/ab_cpu.py` (`26` pairs): `submit_user_ms` median `24.408 ms` (OFF) -> `22.293 ms` (ON), `frame_ms` median `39.900 ms` (OFF) -> `38.800 ms` (ON). AI-assisted.
shadow::send_copied ran on every CPU title buffer copy and called
carded_copies(), locking CARDED, upgrading every Weak<Shadow> (~3,700
copies) into a freshly allocated Vec<Arc<Shadow>>, scanning linearly
for overlaps with [dst, dst + len), and dropping all of them — ~4.9%
of active leonida-submit CPU time in steady-state world profiling.

Keep a start-sorted Vec with per-64-entry block_max_end inside CARDED
(CardedTable) so overlapping(dst, len) skips non-overlapping blocks and
upgrades only the overlapping copies, behind LEONIDA_SEND_COPIED_INDEX
(on by default, nameable in LEONIDA_AB). Reuse the same index in
shadow::elide::demote_overlapping and add unit tests covering
multi-block huge-window overlaps, dead Weaks, replacement, saturation,
and sweeping.
First-time contributor

Independent local verification of this change (I changed no code).

  • Head f5da54a9, merge base 6b0498d3. Both new tests are absent on the base: git grep -F <test> 6b0498d3 -- d3d12/crates/leonida-gpu/src/shadow.rs -> exit 1 for each.
  • On the PR head, with module-qualified names, -- --exact and a private CARGO_TARGET_DIR: shadow::carded_table_tests::overlapping_matches_brute_force_across_blocks_and_huge_windows and shadow::carded_table_tests::edge_cases_empty_replace_saturate_and_shrink -> 1 passed / 851 filtered each. No host().is_none() device gate in the tested source.
  • git diff --check 6b0498d3 f5da54a9 -> clean. python -I scripts/check_no_game_data.py d3d12/crates/leonida-gpu/src/shadow.rs d3d12/crates/leonida-gpu/src/shadow/elide.rs d3d12/docs/switches.md -> clean.

GPUs tested: none (CPU-only brute-force/range-table inputs; no device-gated test counted). Driver: n/a. No game data, assets or extracted shaders were used or attached.

AI-assisted: yes, Hy4 flash (verification only). No approval implied; this is a local test report.

Independent local verification of this change (I changed no code). - Head `f5da54a9`, merge base `6b0498d3`. Both new tests are absent on the base: `git grep -F <test> 6b0498d3 -- d3d12/crates/leonida-gpu/src/shadow.rs` -> exit 1 for each. - On the PR head, with module-qualified names, `-- --exact` and a private `CARGO_TARGET_DIR`: `shadow::carded_table_tests::overlapping_matches_brute_force_across_blocks_and_huge_windows` and `shadow::carded_table_tests::edge_cases_empty_replace_saturate_and_shrink` -> 1 passed / 851 filtered each. No `host().is_none()` device gate in the tested source. - `git diff --check 6b0498d3 f5da54a9` -> clean. `python -I scripts/check_no_game_data.py d3d12/crates/leonida-gpu/src/shadow.rs d3d12/crates/leonida-gpu/src/shadow/elide.rs d3d12/docs/switches.md` -> clean. GPUs tested: none (CPU-only brute-force/range-table inputs; no device-gated test counted). Driver: n/a. No game data, assets or extracted shaders were used or attached. AI-assisted: yes, Hy4 flash (verification only). No approval implied; this is a local test report.
First-time contributor

Independent in-game A/B of this pull request on a second machine, a processor-bound one on the Series X profile (I changed no code).

RUN 4070tis-3493-ab-1 | 2026-12-05 02:38 UTC | 3mptymud | AI-assisted: Claude Code (Claude Fable 5.2)
BASE:        f4b0404e95 + #3493 (head f5da54a986, git merge --no-ff) + none | shaders: the runtime's own translation
MACHINE:     NVIDIA GeForce RTX 4070 Ti SUPER, 16 GB, desktop | 621.24 | Windows 11 Pro 26100 | 32 GB RAM, Ryzen 7 5800X, resizable BAR on
RECIPE:      scripts/play-d3d12.ps1 -Game <folder> -Log -NoFrameGen -PrecompileUsed, the script's defaults otherwise (Series X, the title's own upscaler, no DLSS, output 2560x1440) + DEVIATIONS: LEONIDA_AB=4,LEONIDA_SEND_COPIED_INDEX; menus passed by scripted pad presses; the same save restored before the run, standing where it loads | precompile: yes, 88,875 pipelines made beforehand, 0 bad
OS/DRIVER:   defaults; Developer Mode on (background shader work off by the new default)
TARGET:      copies_fills_ms and steps_ms, on minus off | PREDICTED: lower with the index on, as the author measured
MEASURED:    see the table | 330 s in the world (90 s settling, then 240 s) | 23.5 fps, median frame 42.5 ms, about 6,700 draws a frame
END:         time limit (closed by the runner), no device loss, 0 ERROR lines, read from the log
CACHES:      warm (pipelines precompiled, driver cache warm) | RUN 1 of 1
VERDICT:     prediction held for the counters; frame time: no clear difference

python -I tools/d3d12_ab_compare.py phases <log>: 99 phases of 4 s, 43 pairs of blocks, 6 left out for a draw jump. On minus off, mean and standard error:

Counter on - off
copies_fills_ms -0.810 +- 0.032 ms
steps_ms -0.875 +- 0.104 ms
median frame time -0.172 +- 0.154 ms (tool's verdict: no clear difference)

python -I scripts/ab_cpu.py <log> (95 % intervals, 43 pairs): frame time median 42.9 ms off, 42.6 ms on, paired -0.29 +- 0.42 (inside the interval); submit_kernel_ms -0.46 +- 0.44 (outside the interval); submit_user_ms -0.56 +- 0.82 (inside); p99 51.9 ms off, 51.8 ms on.

Reading: the saving the pull request claims shows on this machine too, at about 0.8 ms of the submitting thread's work a frame (the author's RTX 5070, Series S: 1.4 ms). It does not show as a shorter frame here within the noise of one 330 s run. Nothing got worse: no error, no device loss, the same p99.

Not tested: a cold cache, walking or driving, frame generation, the unit tests (already checked by others above), any other GPU.

tested: #3493 on RTX 4070 Ti SUPER (16 GB), same: play / play.

GPUs tested: NVIDIA GeForce RTX 4070 Ti SUPER 16 GB, driver 621.24, Windows 11 Pro 26100.
AI-assisted: yes. Claude Code (Claude Fable 5.2) merged, built, ran and read the run; no approval implied.

Independent in-game A/B of this pull request on a second machine, a processor-bound one on the Series X profile (I changed no code). ```text RUN 4070tis-3493-ab-1 | 2026-12-05 02:38 UTC | 3mptymud | AI-assisted: Claude Code (Claude Fable 5.2) BASE: f4b0404e95 + #3493 (head f5da54a986, git merge --no-ff) + none | shaders: the runtime's own translation MACHINE: NVIDIA GeForce RTX 4070 Ti SUPER, 16 GB, desktop | 621.24 | Windows 11 Pro 26100 | 32 GB RAM, Ryzen 7 5800X, resizable BAR on RECIPE: scripts/play-d3d12.ps1 -Game <folder> -Log -NoFrameGen -PrecompileUsed, the script's defaults otherwise (Series X, the title's own upscaler, no DLSS, output 2560x1440) + DEVIATIONS: LEONIDA_AB=4,LEONIDA_SEND_COPIED_INDEX; menus passed by scripted pad presses; the same save restored before the run, standing where it loads | precompile: yes, 88,875 pipelines made beforehand, 0 bad OS/DRIVER: defaults; Developer Mode on (background shader work off by the new default) TARGET: copies_fills_ms and steps_ms, on minus off | PREDICTED: lower with the index on, as the author measured MEASURED: see the table | 330 s in the world (90 s settling, then 240 s) | 23.5 fps, median frame 42.5 ms, about 6,700 draws a frame END: time limit (closed by the runner), no device loss, 0 ERROR lines, read from the log CACHES: warm (pipelines precompiled, driver cache warm) | RUN 1 of 1 VERDICT: prediction held for the counters; frame time: no clear difference ``` `python -I tools/d3d12_ab_compare.py phases <log>`: 99 phases of 4 s, 43 pairs of blocks, 6 left out for a draw jump. On minus off, mean and standard error: | Counter | on - off | | --- | --- | | `copies_fills_ms` | -0.810 +- 0.032 ms | | `steps_ms` | -0.875 +- 0.104 ms | | median frame time | -0.172 +- 0.154 ms (tool's verdict: no clear difference) | `python -I scripts/ab_cpu.py <log>` (95 % intervals, 43 pairs): frame time median 42.9 ms off, 42.6 ms on, paired -0.29 +- 0.42 (inside the interval); `submit_kernel_ms` -0.46 +- 0.44 (outside the interval); `submit_user_ms` -0.56 +- 0.82 (inside); p99 51.9 ms off, 51.8 ms on. Reading: the saving the pull request claims shows on this machine too, at about 0.8 ms of the submitting thread's work a frame (the author's RTX 5070, Series S: 1.4 ms). It does not show as a shorter frame here within the noise of one 330 s run. Nothing got worse: no error, no device loss, the same p99. Not tested: a cold cache, walking or driving, frame generation, the unit tests (already checked by others above), any other GPU. tested: #3493 on RTX 4070 Ti SUPER (16 GB), same: play / play. GPUs tested: NVIDIA GeForce RTX 4070 Ti SUPER 16 GB, driver 621.24, Windows 11 Pro 26100. AI-assisted: yes. Claude Code (Claude Fable 5.2) merged, built, ran and read the run; no approval implied.
Sign in to join this conversation.
No description provided.