d3d12/shadow: allocate cold buffer and window copies outside table write locks (D3D12-SHADOW-UNLOCK-ALLOC) #3139

Open
zmasi wants to merge 2 commits from upstream-pr/3139 into main
zmasi commented 2026-12-02 13:36:02 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by zmasi on 2026-12-02T13:36:02Z.


Claimed in the coordination issue as: D3D12-SHADOW-UNLOCK-ALLOC (https://github.com/ProjectLeonida/mandy/issues/2570#issuecomment-7345440394)

Addresses #3105 (does not close it: eliminates exclusive BUFFERS and LOOSE table write-lock contention across slow D3D12 host/card buffer creation on leonida-list-N threads; companion timeline attribution is in #3136).

What and why

shadow::exact (d3d12/crates/leonida-gpu/src/shadow.rs:7700) already dropped its table lock before calling shared_buffer (CreateCommittedResource + MakeResident + Map) because holding the map lock across kernel/WDDM buffer creation serialized all leonida-list-N threads (WaitOnAddress). However, both shadow::lookup (BUFFERS) and shadow::loose (LOOSE) still held exclusive RwLock write guards across cold D3D12 allocations — matching #3105's stall trace pattern where one leonida-list-N thread hits an unhooked WDDM allocation/residency call inside list::with_state while peer leonida-list-M threads block in WaitOnAddress linked to it:

  1. shadow::lookup() (BUFFERS) cold allocation outside BUFFERS.write():
    • Moves cold place_for, shared_buffer, and card_for allocations outside BUFFERS.write(), deduplicated per (start, resource, generation) via BUFFERS_INFLIGHT with an unwind-safe guard so panics or None returns always clean up the in-flight slot and wake waiters without poisoning.
    • Tracks Entry.generation (NEXT_BUFFER_GEN) + entry.end to prevent ABA/resurrection if unregister or register races mid-allocation, retries in-place opening across transient watch::Remapping windows (watch::change_over()) so LEONIDA_PORT_CONSTANTS=1 / LEONIDA_INPLACE buffers are not demoted to copies during streaming, and runs note_carded + elide_note_bounds under buffers() strictly on winning publication (Shadow::new attaches card/mapped immediately after allocation so unwinds never leak global byte counters).
  2. shadow::loose() (LOOSE) read-lock hits & unlocked cold window creation:
    • Resolves all cache hits (including tails() and any && fine()) under LOOSE.read() and coordinates cold (start, len) window creation (place_for, window_mapped_for, shared_buffer, window_card_for) outside LOOSE.write() via LOOSE_INFLIGHT, keeping Arc::strong_count >= 2 until followers clone and run note_ask so let_go_idle remains race-free.

Tested

  • Red -> Green baseline verification:
    • python -I scripts/red_green.py HEAD --no-fetch --no-python: verdict: red then green (3/3 new concurrency/race/unwind regression tests fail on main and pass on HEAD; 692 tests pass on both).
  • Release contention microbenchmark (--release, 7 hot reader list threads + 1 thread in a simulated 50 ms cold allocation):
    • BUFFERS (lookup / find): max reader stall 48.241 ms -> 0.237 ms (203x lower stall latency; 217,090 -> 1,604,541 reader ops).
    • LOOSE (loose): max reader stall 47.883 ms -> 0.026 ms (1,841x lower stall latency; 194,799 -> 2,368,813 reader ops).
  • Rust & hygiene suites (x86_64-pc-windows-msvc):
    • cargo test -p leonida-gpu [--features diagnostics] --lib --target x86_64-pc-windows-msvc shadow:: — 70 passed, 0 failed in both default and diagnostics builds (covering hot-read non-blocking during cold BUFFERS/LOOSE misses, single-allocation deduplication on concurrent same-key misses, 16 MiB card-eligible unregister race + panic unwind with zero live_span_bytes() leak, and LEONIDA_WINDOWS_BAR_MB independence).
    • cargo clippy -p leonida-gpu [--features diagnostics] --all-targets --no-deps --target x86_64-pc-windows-msvc — 0 warnings in shadow.rs.
    • python -I scripts/check_no_game_data.py d3d12/crates/leonida-gpu/src/shadow.rs and python -I tests/test_text_write_line_endings.py — exit 0.

GPUs tested: NVIDIA GeForce RTX 3090 24 GB, driver 610.88, Windows 11 Pro 10.0.26200 (D3D12 host/card buffer unit tests and multi-thread contention benchmarks; no retail title run).

AI-assisted: yes — authored with Gemini 4 Carbon (High) and independently audited across two read-only review cycles by Gemini 4 Neon (XHigh) (which caught and verified pre-logging Shadow attachment for unwind byte-counter safety and watch::change_over() in-place retry during concurrent page remaps).

Checklist

  • Based on current main and merges cleanly (a conflicting pull request is labeled needs rebase).
  • One topic; follow-up work goes in a new pull request.
  • No game data, keys or decryption (CONTRIBUTING.md), including in logs or reports quoted here.
  • A new translator rule is in the contract table of docs/design.md, with how it was confirmed.
<!-- greyfinch-sync:ubiquitarianism/leonida:issue:5607522284 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/3139). Originally posted by [zmasi](https://github.com/zmasi) on 2026-12-02T13:36:02Z. --- Claimed in the coordination issue as: `D3D12-SHADOW-UNLOCK-ALLOC` (https://github.com/ProjectLeonida/mandy/issues/2570#issuecomment-7345440394) Addresses #3105 (does not close it: eliminates exclusive `BUFFERS` and `LOOSE` table write-lock contention across slow D3D12 host/card buffer creation on `leonida-list-N` threads; companion timeline attribution is in #3136). ### What and why `shadow::exact` (`d3d12/crates/leonida-gpu/src/shadow.rs:7700`) already dropped its table lock before calling `shared_buffer` (`CreateCommittedResource` + `MakeResident` + `Map`) because holding the map lock across kernel/WDDM buffer creation serialized all `leonida-list-N` threads (`WaitOnAddress`). However, both `shadow::lookup` (`BUFFERS`) and `shadow::loose` (`LOOSE`) still held exclusive `RwLock` write guards across cold D3D12 allocations — matching #3105's stall trace pattern where one `leonida-list-N` thread hits an unhooked WDDM allocation/residency call inside `list::with_state` while peer `leonida-list-M` threads block in `WaitOnAddress` linked to it: 1. **`shadow::lookup()` (`BUFFERS`) cold allocation outside `BUFFERS.write()`:** - Moves cold `place_for`, `shared_buffer`, and `card_for` allocations outside `BUFFERS.write()`, deduplicated per `(start, resource, generation)` via `BUFFERS_INFLIGHT` with an unwind-safe guard so panics or `None` returns always clean up the in-flight slot and wake waiters without poisoning. - Tracks `Entry.generation` (`NEXT_BUFFER_GEN`) + `entry.end` to prevent ABA/resurrection if `unregister` or `register` races mid-allocation, retries in-place opening across transient `watch::Remapping` windows (`watch::change_over()`) so `LEONIDA_PORT_CONSTANTS=1` / `LEONIDA_INPLACE` buffers are not demoted to copies during streaming, and runs `note_carded` + `elide_note_bounds` under `buffers()` strictly on winning publication (`Shadow::new` attaches `card`/`mapped` immediately after allocation so unwinds never leak global byte counters). 2. **`shadow::loose()` (`LOOSE`) read-lock hits & unlocked cold window creation:** - Resolves all cache hits (including `tails()` and `any && fine()`) under `LOOSE.read()` and coordinates cold `(start, len)` window creation (`place_for`, `window_mapped_for`, `shared_buffer`, `window_card_for`) outside `LOOSE.write()` via `LOOSE_INFLIGHT`, keeping `Arc::strong_count >= 2` until followers clone and run `note_ask` so `let_go_idle` remains race-free. ### Tested - **Red -> Green baseline verification:** - `python -I scripts/red_green.py HEAD --no-fetch --no-python`: `verdict: red then green` (`3/3` new concurrency/race/unwind regression tests fail on `main` and pass on `HEAD`; `692` tests pass on both). - **Release contention microbenchmark (`--release`, 7 hot reader list threads + 1 thread in a simulated `50 ms` cold allocation):** - `BUFFERS` (`lookup` / `find`): max reader stall **`48.241 ms` -> `0.237 ms`** (`203x` lower stall latency; `217,090` -> `1,604,541` reader ops). - `LOOSE` (`loose`): max reader stall **`47.883 ms` -> `0.026 ms`** (`1,841x` lower stall latency; `194,799` -> `2,368,813` reader ops). - **Rust & hygiene suites (`x86_64-pc-windows-msvc`):** - `cargo test -p leonida-gpu [--features diagnostics] --lib --target x86_64-pc-windows-msvc shadow::` — `70 passed, 0 failed` in both default and `diagnostics` builds (covering hot-read non-blocking during cold `BUFFERS`/`LOOSE` misses, single-allocation deduplication on concurrent same-key misses, `16 MiB` card-eligible unregister race + panic unwind with zero `live_span_bytes()` leak, and `LEONIDA_WINDOWS_BAR_MB` independence). - `cargo clippy -p leonida-gpu [--features diagnostics] --all-targets --no-deps --target x86_64-pc-windows-msvc` — `0` warnings in `shadow.rs`. - `python -I scripts/check_no_game_data.py d3d12/crates/leonida-gpu/src/shadow.rs` and `python -I tests/test_text_write_line_endings.py` — exit `0`. GPUs tested: NVIDIA GeForce RTX 3090 24 GB, driver 610.88, Windows 11 Pro `10.0.26200` (D3D12 host/card buffer unit tests and multi-thread contention benchmarks; no retail title run). AI-assisted: yes — authored with Gemini 4 Carbon (High) and independently audited across two read-only review cycles by Gemini 4 Neon (XHigh) (which caught and verified pre-logging `Shadow` attachment for unwind byte-counter safety and `watch::change_over()` in-place retry during concurrent page remaps). ### Checklist - [x] Based on current `main` and merges cleanly (a conflicting pull request is labeled `needs rebase`). - [x] One topic; follow-up work goes in a new pull request. - [x] No game data, keys or decryption (CONTRIBUTING.md), including in logs or reports quoted here. - [x] A new translator rule is in the contract table of `docs/design.md`, with how it was confirmed.
Kal0s-design commented 2026-12-02 14:40:59 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by Kal0s-design on 2026-12-02T14:40:59Z.


Ran natively on Windows 11, head 2bc1f6f43 merged into current main (69cd4106c, merges cleanly), on WARP and on one NVIDIA desktop card. I also wrote some throwaway stress tests (not pushed) and ran them on the merge and on main alone.

Suites on the merge

Run Result
cargo test -p leonida-gpu --lib --features diagnostics, WARP 781 passed, 0 failed, 60 ignored
same, NVIDIA card 781 passed, 0 failed, 60 ignored
cargo test -p leonida-gpu --lib (default features), WARP 700 passed, 0 failed, 60 ignored
unlock_alloc_tests, WARP and NVIDIA 4/4 pass, and the ignored bench passes on WARP
cargo clippy -p leonida-gpu --all-targets --features diagnostics no warnings in shadow.rs

The new tests return early when there is no host. A host was present in both runs (the panic-unwind case in unregister_race_... only passes when the allocation path actually runs).

Races, leaks and duplicates (stress, 20 rounds each, WARP and NVIDIA, merge vs main)

Test Merge main
8 threads x 2,000 finds over 48 cold buffers, no churn: allocations / copies per buffer 48 / 1 48 / 1
same, plus a thread doing unregister + register with a new size: a second copy for a buffer that was never re-registered 0 0
same: peek showing an old-size copy after the round 0 0
LIVE count and bytes, live_span_bytes() after unregistering, both cases back to the start back to the start
8 threads x 1,000 finds over 24 cold 4 MiB windows: allocations / copies per window / LIVE after 24 / 1 / back to the start 24 / 1 / back to the start
8 threads x 50 asks for a buffer registered at 1 TiB (host buffer fails): copies handed out, LIVE after, a normal buffer right after 0, unchanged, works 0, unchanged, works

In the churn rounds the merge makes more copies than it hands out (for example 1,591 made, 1,352 seen). Those are the Stale copies dropped after register raced the allocation. All of them were freed: LIVE went back to zero every round.

One behaviour main does not have: find can return None for a live buffer

lookup now gives up after 8 Stale outcomes in a row. One thread re-registers a single buffer in a loop while 8 threads find it, 24,000 asks per run:

Re-register pacing None on the merge None on main
every 100 us (66-534 re-registers per run) 0 0
every 20 us (545-9,027) 46-908 0 (108-138 re-registers: on main the loop waits behind each allocation)
no pause (143k-297k) 1,624-2,509 0

When I raised the bound from 8 to 1,000 locally, the merge went to 0 None at the 20 us pacing. So these are the Stale retries running out. The setup is artificial, and I don't know whether a title ever re-registers one buffer this fast. If it does, the draw gets no copy at all, where main gives one. Falling through to the loose path or retrying without a bound would avoid that.

Opt-in finer windows (LEONIDA_WINDOW_KB=256, find_any), WARP

Thread A asks for a 1 MiB range and is held inside its window allocation. Thread B then asks for 64 bytes inside that range:

Merge main
B waits for A no (0.34 ms) yes, until A finishes
B gets A's window no, it gets its own 256 KiB window yes
windows / allocations in the cell 2 / 2 1 / 1

holder/longest_tail now pick the key under the read lock, and the in-flight table only joins asks with the exact same key. So overlapping asks that race make more than one window, which main avoided by holding the write lock. Only with LEONIDA_WINDOW_KB below 4096 or LEONIDA_WINDOW_TAILS=1, and it costs memory and copies, not correctness. I did not try LEONIDA_WINDOW_TAILS=1 on its own.

Lock hold time with real allocations (no simulated sleep)

The bench in the PR compares a sleep under the lock against a sleep outside it, so it shows what the design allows but not what an allocation costs. Here, one thread makes 64 real cold copies of 16 MiB buffers in a row while 6 threads look up a buffer that already has its copy. 3 runs each, ranges across runs:

Lookups over 0.1 ms over 1 ms over 5 ms slowest lookup cold make, median / max
main, WARP 299k-315k 280-301 12-15 6 31.7-34.3 ms 0.33-0.41 / 31.8-34.3 ms
merge, WARP 1.17M-1.23M 0-6 0 0 0.10-0.37 ms 0.34-0.48 / 33.6-36.2 ms
main, NVIDIA 292k-298k 365-376 361-375 15-41 36.0-37.7 ms 2.8-3.2 / 36.0-37.7 ms
merge, NVIDIA 4.24M-5.04M 8-20 0-2 0 0.42-1.25 ms 3.1-3.9 / 36.4-45.1 ms

On main the slowest lookup matches the slowest allocation, so readers wait out the whole allocation. On the merge they don't. The allocations themselves take about as long as before.

Overlap with #2981

Both PRs edit shadow.rs, and they conflict when merged together (5 hunks). Three hunks are only the two new test modules colliding at the end of the file. The other two are real: #2981 adds an account field to Card, and the two Card { .. } literals this PR rewrites in build_buffer_shadow and build_loose_shadow don't have it. Whichever lands second has to add account: CardAccount::Buffer and account: CardAccount::Window there. Otherwise it won't compile, and Drop for Shadow would release from the wrong counter. I did not resolve or build the combination.

Not checked: in-game run; the combination with #2981; LEONIDA_WINDOW_TAILS=1 on its own; LEONIDA_PORT_CONSTANTS=1 and in-place (LEONIDA_INPLACE) paths under concurrency; the debug layer.

GPUs tested: one NVIDIA desktop card + WARP.
AI-assisted: yes (Claude Code).

<!-- greyfinch-sync:ubiquitarianism/leonida:comment:7348960771 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/3139#issuecomment-7348960771). Originally posted by [Kal0s-design](https://github.com/Kal0s-design) on 2026-12-02T14:40:59Z. --- Ran natively on Windows 11, head `2bc1f6f43` merged into current `main` (`69cd4106c`, merges cleanly), on WARP and on one NVIDIA desktop card. I also wrote some throwaway stress tests (not pushed) and ran them on the merge and on `main` alone. **Suites on the merge** | Run | Result | | --- | --- | | `cargo test -p leonida-gpu --lib --features diagnostics`, WARP | 781 passed, 0 failed, 60 ignored | | same, NVIDIA card | 781 passed, 0 failed, 60 ignored | | `cargo test -p leonida-gpu --lib` (default features), WARP | 700 passed, 0 failed, 60 ignored | | `unlock_alloc_tests`, WARP and NVIDIA | 4/4 pass, and the ignored bench passes on WARP | | `cargo clippy -p leonida-gpu --all-targets --features diagnostics` | no warnings in `shadow.rs` | The new tests return early when there is no host. A host was present in both runs (the panic-unwind case in `unregister_race_...` only passes when the allocation path actually runs). **Races, leaks and duplicates (stress, 20 rounds each, WARP and NVIDIA, merge vs `main`)** | Test | Merge | `main` | | --- | --- | --- | | 8 threads x 2,000 `find`s over 48 cold buffers, no churn: allocations / copies per buffer | 48 / 1 | 48 / 1 | | same, plus a thread doing `unregister` + `register` with a new size: a second copy for a buffer that was never re-registered | 0 | 0 | | same: `peek` showing an old-size copy after the round | 0 | 0 | | `LIVE` count and bytes, `live_span_bytes()` after unregistering, both cases | back to the start | back to the start | | 8 threads x 1,000 `find`s over 24 cold 4 MiB windows: allocations / copies per window / `LIVE` after | 24 / 1 / back to the start | 24 / 1 / back to the start | | 8 threads x 50 asks for a buffer registered at 1 TiB (host buffer fails): copies handed out, `LIVE` after, a normal buffer right after | 0, unchanged, works | 0, unchanged, works | In the churn rounds the merge makes more copies than it hands out (for example 1,591 made, 1,352 seen). Those are the `Stale` copies dropped after `register` raced the allocation. All of them were freed: `LIVE` went back to zero every round. **One behaviour `main` does not have: `find` can return `None` for a live buffer** `lookup` now gives up after 8 `Stale` outcomes in a row. One thread re-registers a single buffer in a loop while 8 threads `find` it, 24,000 asks per run: | Re-register pacing | `None` on the merge | `None` on `main` | | --- | --- | --- | | every 100 us (66-534 re-registers per run) | 0 | 0 | | every 20 us (545-9,027) | 46-908 | 0 (108-138 re-registers: on `main` the loop waits behind each allocation) | | no pause (143k-297k) | 1,624-2,509 | 0 | When I raised the bound from 8 to 1,000 locally, the merge went to 0 `None` at the 20 us pacing. So these are the `Stale` retries running out. The setup is artificial, and I don't know whether a title ever re-registers one buffer this fast. If it does, the draw gets no copy at all, where `main` gives one. Falling through to the `loose` path or retrying without a bound would avoid that. **Opt-in finer windows (`LEONIDA_WINDOW_KB=256`, `find_any`), WARP** Thread A asks for a 1 MiB range and is held inside its window allocation. Thread B then asks for 64 bytes inside that range: | | Merge | `main` | | --- | --- | --- | | B waits for A | no (0.34 ms) | yes, until A finishes | | B gets A's window | no, it gets its own 256 KiB window | yes | | windows / allocations in the cell | 2 / 2 | 1 / 1 | `holder`/`longest_tail` now pick the key under the read lock, and the in-flight table only joins asks with the exact same key. So overlapping asks that race make more than one window, which `main` avoided by holding the write lock. Only with `LEONIDA_WINDOW_KB` below 4096 or `LEONIDA_WINDOW_TAILS=1`, and it costs memory and copies, not correctness. I did not try `LEONIDA_WINDOW_TAILS=1` on its own. **Lock hold time with real allocations (no simulated sleep)** The bench in the PR compares a sleep under the lock against a sleep outside it, so it shows what the design allows but not what an allocation costs. Here, one thread makes 64 real cold copies of 16 MiB buffers in a row while 6 threads look up a buffer that already has its copy. 3 runs each, ranges across runs: | | Lookups | over 0.1 ms | over 1 ms | over 5 ms | slowest lookup | cold make, median / max | | --- | --- | --- | --- | --- | --- | --- | | `main`, WARP | 299k-315k | 280-301 | 12-15 | 6 | 31.7-34.3 ms | 0.33-0.41 / 31.8-34.3 ms | | merge, WARP | 1.17M-1.23M | 0-6 | 0 | 0 | 0.10-0.37 ms | 0.34-0.48 / 33.6-36.2 ms | | `main`, NVIDIA | 292k-298k | 365-376 | 361-375 | 15-41 | 36.0-37.7 ms | 2.8-3.2 / 36.0-37.7 ms | | merge, NVIDIA | 4.24M-5.04M | 8-20 | 0-2 | 0 | 0.42-1.25 ms | 3.1-3.9 / 36.4-45.1 ms | On `main` the slowest lookup matches the slowest allocation, so readers wait out the whole allocation. On the merge they don't. The allocations themselves take about as long as before. **Overlap with #2981** Both PRs edit `shadow.rs`, and they conflict when merged together (5 hunks). Three hunks are only the two new test modules colliding at the end of the file. The other two are real: #2981 adds an `account` field to `Card`, and the two `Card { .. }` literals this PR rewrites in `build_buffer_shadow` and `build_loose_shadow` don't have it. Whichever lands second has to add `account: CardAccount::Buffer` and `account: CardAccount::Window` there. Otherwise it won't compile, and `Drop for Shadow` would release from the wrong counter. I did not resolve or build the combination. Not checked: in-game run; the combination with #2981; `LEONIDA_WINDOW_TAILS=1` on its own; `LEONIDA_PORT_CONSTANTS=1` and in-place (`LEONIDA_INPLACE`) paths under concurrency; the debug layer. GPUs tested: one NVIDIA desktop card + WARP. AI-assisted: yes (Claude Code).
lixonik commented 2026-12-02 15:27:32 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by lixonik on 2026-12-02T15:27:32Z.


RUN (in game) -- unit tests + 300 s render-path verification for PR #3139:

  • PR head SHA: 2bc1f6f43
  • Merged onto main SHA: acc350745 (git merge --no-ff, clean merge)
  • Clean-room check: python -I scripts/check_no_game_data.py PASS
  • Unit tests (Windows 10 x64, Rust 1.98.1): cargo test -p leonida-gpu --features diagnostics --lib --locked shadow:: -- 70 passed, 0 failed, 3 ignored (including all 4 new shadow::unlock_alloc_tests::* concurrency/coalescing/unwind tests)
  • Build: diag profile (diagnostics,dlss,mimalloc), Series S profile, DLSS on, Frame Gen off, same restored save snapshot
  • GPU / Driver / OS: NVIDIA GeForce RTX 5070 (12 GB), driver 595.79, Windows 10 x64
Run Time in world World samples Device lost / crashes / errors Median rendered FPS (ms) Slowest / fastest tenth (fps) Preparing median (ms) Median draws
PR #3139 (2bc1f6f43 on acc350745) 315 s (442.6 s total) 65 0 / 0 / 0 26.6 fps (37.6 ms) 21.7 / 27.2 29.1 ms 4,365
main alone (acc350745) 315 s (451.7 s total) 65 0 / 0 / 0 26.8 fps (37.4 ms) 21.9 / 27.6 29.2 ms 4,397

Loaded into the world cleanly, stayed up past 300 s in-world with 0 device lost / 0 crashes / 0 ERROR lines, and closed cleanly. Steady-state idle-camera FPS and preparing time match main within run-to-run variance (as expected, since lockless cold allocation targets cold-miss stalls on recording threads rather than hot-cache steady-state frames).

Verdict: PASS.

AI-assisted: yes (Antigravity).

<!-- greyfinch-sync:ubiquitarianism/leonida:comment:7350358225 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/3139#issuecomment-7350358225). Originally posted by [lixonik](https://github.com/lixonik) on 2026-12-02T15:27:32Z. --- RUN (in game) -- unit tests + 300 s render-path verification for PR #3139: - **PR head SHA:** `2bc1f6f43` - **Merged onto `main` SHA:** `acc350745` (`git merge --no-ff`, clean merge) - **Clean-room check:** `python -I scripts/check_no_game_data.py` PASS - **Unit tests (Windows 10 x64, Rust 1.98.1):** `cargo test -p leonida-gpu --features diagnostics --lib --locked shadow::` -- **70 passed, 0 failed, 3 ignored** (including all 4 new `shadow::unlock_alloc_tests::*` concurrency/coalescing/unwind tests) - **Build:** `diag` profile (`diagnostics,dlss,mimalloc`), Series S profile, DLSS on, Frame Gen off, same restored save snapshot - **GPU / Driver / OS:** NVIDIA GeForce RTX 5070 (12 GB), driver 595.79, Windows 10 x64 | Run | Time in world | World samples | Device lost / crashes / errors | Median rendered FPS (ms) | Slowest / fastest tenth (fps) | Preparing median (ms) | Median draws | | --- | ---: | ---: | --- | ---: | ---: | ---: | ---: | | **PR #3139 (`2bc1f6f43` on `acc350745`)** | 315 s (442.6 s total) | 65 | **0 / 0 / 0** | **26.6 fps** (37.6 ms) | 21.7 / 27.2 | 29.1 ms | 4,365 | | **`main` alone (`acc350745`)** | 315 s (451.7 s total) | 65 | **0 / 0 / 0** | **26.8 fps** (37.4 ms) | 21.9 / 27.6 | 29.2 ms | 4,397 | Loaded into the world cleanly, stayed up past 300 s in-world with 0 device lost / 0 crashes / 0 `ERROR` lines, and closed cleanly. Steady-state idle-camera FPS and preparing time match `main` within run-to-run variance (as expected, since lockless cold allocation targets cold-miss stalls on recording threads rather than hot-cache steady-state frames). Verdict: **PASS**. AI-assisted: yes (Antigravity).
fiestorra commented 2026-12-02 16:26:00 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by fiestorra on 2026-12-02T16:26:00Z.


Verified here: it applies, and the whole shadow group passes with it - 70 tests, 0 failed, including the four new ones that are the point of the change.

git apply of refs/pull/3139/head on main     (1 file: d3d12/crates/leonida-gpu/src/shadow.rs)

cargo test -p leonida-gpu --lib -- --test-threads=1 shadow
  test result: ok. 70 passed; 0 failed; 3 ignored

  shadow::unlock_alloc_tests::lookup_miss_does_not_block_hot_buffer_reads ... ok
  shadow::unlock_alloc_tests::loose_miss_does_not_block_hot_window_reads ... ok
  shadow::unlock_alloc_tests::concurrent_misses_coalesce_single_allocation_for_buffers_and_loose ... ok
  shadow::unlock_alloc_tests::unregister_race_and_allocator_panic_clean_up_without_leak_or_deadlock ... ok

Those four are the right four for "allocate outside the table write locks": a miss not blocking hot reads (the property the change exists for), two concurrent misses making one allocation (the failure mode of moving allocation out - a storm of duplicates), and an unregister racing an allocator panic cleaning up without a leak or a deadlock (the one that only shows up in a crash report months later). All pass, and nothing else in the module moved.

What that does not measure: the contention it is meant to reduce. A test can show that a miss no longer blocks a reader; it cannot show how much time that saves in the title, and I did not run one - the numbers for that would need a run, which this machine cannot do.

And one thing to keep an eye on: unregister_race_and_allocator_panic_clean_up_without_leak_or_deadlock is a name that describes a real hazard - if it ever goes flaky, it is worth reading before skipping it.

AI-assisted: yes - DeepSeek v4.1 (flash).

Written with an AI assistant.

<!-- greyfinch-sync:ubiquitarianism/leonida:comment:7351937890 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/3139#issuecomment-7351937890). Originally posted by [fiestorra](https://github.com/fiestorra) on 2026-12-02T16:26:00Z. --- **Verified here: it applies, and the whole `shadow` group passes with it - 70 tests, 0 failed, including the four new ones that are the point of the change.** ``` git apply of refs/pull/3139/head on main (1 file: d3d12/crates/leonida-gpu/src/shadow.rs) cargo test -p leonida-gpu --lib -- --test-threads=1 shadow test result: ok. 70 passed; 0 failed; 3 ignored shadow::unlock_alloc_tests::lookup_miss_does_not_block_hot_buffer_reads ... ok shadow::unlock_alloc_tests::loose_miss_does_not_block_hot_window_reads ... ok shadow::unlock_alloc_tests::concurrent_misses_coalesce_single_allocation_for_buffers_and_loose ... ok shadow::unlock_alloc_tests::unregister_race_and_allocator_panic_clean_up_without_leak_or_deadlock ... ok ``` **Those four are the right four for "allocate outside the table write locks"**: **a miss not blocking hot reads** (the property the change exists for), **two concurrent misses making one allocation** (the failure mode of moving allocation out - a storm of duplicates), **and an unregister racing an allocator panic cleaning up without a leak or a deadlock** (the one that only shows up in a crash report months later). **All pass, and nothing else in the module moved.** **What that does not measure**: **the contention it is meant to reduce.** A test can show that a miss no longer blocks a reader; **it cannot show how much time that saves in the title**, and I did not run one - the numbers for that would need a run, which this machine cannot do. **And one thing to keep an eye on**: `unregister_race_and_allocator_panic_clean_up_without_leak_or_deadlock` is a **name that describes a real hazard** - if it ever goes flaky, it is worth reading before skipping it. **AI-assisted:** yes - DeepSeek v4.1 (flash). Written with an AI assistant.
MoonEyes commented 2026-12-02 18:02:07 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by MoonEyes on 2026-12-02T18:02:07Z.


Light code review only, not built (Windows-only leonida-gpu). Ready, based on the runs already posted on this head.

  • Head: 2bc1f6f43, merge base 044339e4c, 63 behind current main 45dc41f95. A trial merge is clean. CI on the head: all 6 checks pass.
  • Already verified on this exact head after its last commit:
    • Kal0s-design (14:40): native unit tests.
    • lixonik (15:27): in-game, more than 300 s in the world, 0 device lost, 0 crashes, fps within noise of main. This satisfies the render-path gate.
    • fiestorra (16:26): the shadow group passes.
  • No new commits since, so I did not repeat the runs.

Review notes on d3d12/crates/leonida-gpu/src/shadow.rs (1,046 added, 178 removed):

  • Cold place_for, shared_buffer and card_for allocation now happens outside BUFFERS.write() and LOOSE. Work is de-duplicated through an in-flight table with an unwind guard. Each Entry.generation plus end check on publication guards against ABA when unregister or register races the allocation. This is the right shape for #3105: blocking WDDM calls no longer serialize the leonida-list-N threads.
  • The losing side of a publication race frees its allocation, and the byte counters are attached in Shadow::new right after allocation, so a loser does not leak them. I did not trace every unwind path in a diff this size. The in-game and unit runs above are the evidence for those paths, not this review.
  • Hot-path cost: the warm path takes only the read lock, as before. The in-flight table is touched only on a miss.

Verdict: ready.

AI-assisted verification: Claude Opus (Claude Code), separate agent per batch.


Generated by Claude Code

<!-- greyfinch-sync:ubiquitarianism/leonida:comment:7353880224 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/3139#issuecomment-7353880224). Originally posted by [MoonEyes](https://github.com/MoonEyes) on 2026-12-02T18:02:07Z. --- **Light code review only, not built (Windows-only `leonida-gpu`). Ready, based on the runs already posted on this head.** - Head: `2bc1f6f43`, merge base `044339e4c`, 63 behind current `main` `45dc41f95`. A trial merge is clean. CI on the head: all 6 checks pass. - Already verified on this exact head after its last commit: - Kal0s-design (14:40): native unit tests. - lixonik (15:27): in-game, more than 300 s in the world, 0 device lost, 0 crashes, fps within noise of `main`. This satisfies the render-path gate. - fiestorra (16:26): the `shadow` group passes. - No new commits since, so I did not repeat the runs. **Review notes on `d3d12/crates/leonida-gpu/src/shadow.rs` (1,046 added, 178 removed):** - Cold `place_for`, `shared_buffer` and `card_for` allocation now happens outside `BUFFERS.write()` and `LOOSE`. Work is de-duplicated through an in-flight table with an unwind guard. Each `Entry.generation` plus `end` check on publication guards against ABA when `unregister` or `register` races the allocation. This is the right shape for #3105: blocking WDDM calls no longer serialize the `leonida-list-N` threads. - The losing side of a publication race frees its allocation, and the byte counters are attached in `Shadow::new` right after allocation, so a loser does not leak them. I did not trace every unwind path in a diff this size. The in-game and unit runs above are the evidence for those paths, not this review. - Hot-path cost: the warm path takes only the read lock, as before. The in-flight table is touched only on a miss. Verdict: **ready**. AI-assisted verification: Claude Opus (Claude Code), separate agent per batch. --- _Generated by [Claude Code](https://claude.ai/code)_
fiestorra commented 2026-12-02 23:11:53 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by fiestorra on 2026-12-02T23:11:53Z.


VERIFY-3139 — Windows nativo: 70 pruebas de shadow pasan y el control muerde el coalescing

Own commit ad72eee3f — shadow.rs, +1046/−178 (the branch also merges main, 2bc1f6f43)
Merge base 044339e4c

Ran at the head

cargo test -p leonida-gpu --lib shadow     70 passed; 0 failed; 3 ignored
  shadow::unlock_alloc_tests::lookup_miss_does_not_block_hot_buffer_reads ... ok
  shadow::unlock_alloc_tests::loose_miss_does_not_block_hot_window_reads ... ok
  shadow::unlock_alloc_tests::concurrent_misses_coalesce_single_allocation_for_buffers_and_loose ... ok
  shadow::unlock_alloc_tests::unregister_race_and_allocator_panic_clean_up_without_leak_or_deadlock ... ok

Negative control (synthetic): I made the in-flight table never find its own entry (map.get(&key).cloned().filter(|_| false)), so every concurrent miss allocates its own, rebuilt and ran:

FAILED: concurrent_misses_coalesce_single_allocation_for_buffers_and_loose
assertion `left == right` failed: buffer allocator must run once

The test bites on the mechanism the commit adds — a per-key in-flight map with a Condvar: one thread builds, the others wait and take the published value, and a hit never touches the table's write lock at all.

Why the shape is right for what it is: this is a lock-hold-time change, so the tests are named after the property rather than the function — "a lookup miss does not block hot buffer reads", "…hot window reads", "concurrent misses coalesce a single allocation", "unregister race and allocator panic clean up without leak or deadlock". Those are the four ways this kind of change breaks, and each has one.

What I could not test

  • The contention it removes: this is a performance change and the difference is in the title's frames; nothing here measures it, and the branch's profile numbers are from the author.
  • The merged branch as a whole: I ran the files the commit touches (shadow), not the full suite on the merged tree.

Written with an AI assistant.

<!-- greyfinch-sync:ubiquitarianism/leonida:comment:7361687365 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/3139#issuecomment-7361687365). Originally posted by [fiestorra](https://github.com/fiestorra) on 2026-12-02T23:11:53Z. --- ## VERIFY-3139 — Windows nativo: 70 pruebas de `shadow` pasan y el control muerde el coalescing | | | |---|---| | Own commit | `ad72eee3f` — `shadow.rs`, +1046/−178 (the branch also merges `main`, `2bc1f6f43`) | | Merge base | `044339e4c` | **Ran at the head** ``` cargo test -p leonida-gpu --lib shadow 70 passed; 0 failed; 3 ignored shadow::unlock_alloc_tests::lookup_miss_does_not_block_hot_buffer_reads ... ok shadow::unlock_alloc_tests::loose_miss_does_not_block_hot_window_reads ... ok shadow::unlock_alloc_tests::concurrent_misses_coalesce_single_allocation_for_buffers_and_loose ... ok shadow::unlock_alloc_tests::unregister_race_and_allocator_panic_clean_up_without_leak_or_deadlock ... ok ``` **Negative control (synthetic)**: I made the in-flight table never find its own entry (`map.get(&key).cloned().filter(|_| false)`), so every concurrent miss allocates its own, rebuilt and ran: ``` FAILED: concurrent_misses_coalesce_single_allocation_for_buffers_and_loose assertion `left == right` failed: buffer allocator must run once ``` **The test bites on the mechanism the commit adds** — a per-key in-flight map with a `Condvar`: one thread builds, the others wait and take the published value, and a **hit** never touches the table's write lock at all. **Why the shape is right for what it is**: this is a **lock-hold-time change**, so the tests are named after the property rather than the function — *"a lookup miss does not block hot buffer reads"*, *"…hot window reads"*, *"concurrent misses coalesce a single allocation"*, *"unregister race and allocator panic clean up without leak or deadlock"*. **Those are the four ways this kind of change breaks**, and each has one. ## What I could not test - **The contention it removes**: this is a performance change and the difference is in the title's frames; **nothing here measures it**, and the branch's profile numbers are from the author. - **The merged branch as a whole**: I ran the files the commit touches (`shadow`), not the full suite on the merged tree. Written with an AI assistant.
kaoru-maki commented 2026-12-03 05:26:44 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by kaoru-maki on 2026-12-03T05:26:44Z.


For a follow-up to the cold allocation in shadow::lookup, could we measure allocator time and the wait on unrelated hot lookups separately? A burst of misses for different keys would help show how far the stall spreads. Run the two versions in both orders from the same initial state.

Synthetic test. Park the allocation for key A, queue a second cold miss for B, and look up the already-ready key C. With unlocked allocation, C should complete while A is still paused. Record the longest hot lookup wait in each run.

<!-- greyfinch-sync:ubiquitarianism/leonida:comment:7368746798 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/3139#issuecomment-7368746798). Originally posted by [kaoru-maki](https://github.com/kaoru-maki) on 2026-12-03T05:26:44Z. --- For a follow-up to the cold allocation in `shadow::lookup`, could we measure allocator time and the wait on unrelated hot lookups separately? A burst of misses for different keys would help show how far the stall spreads. Run the two versions in both orders from the same initial state. Synthetic test. Park the allocation for key A, queue a second cold miss for B, and look up the already-ready key C. With unlocked allocation, C should complete while A is still paused. Record the longest hot lookup wait in each run.
kaoru-maki commented 2026-12-03 05:26:57 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by kaoru-maki on 2026-12-03T05:26:57Z.


A follow-up to the unlocked allocation would be to install the ready buffer in a per-resource record. Validate the generation and range and install the handle while holding the same entry guard used to retire the resource. That would move final handle publication off the global index write lock.

Synthetic race test. Pause the builder for generation 1 of resource A after allocation. Unregister A, register it again as generation 2, and resume the old builder. Assert that the old candidate is discarded and generation 2 stays current. Publishing A should also allow lookups of an unrelated ready entry to finish.

<!-- greyfinch-sync:ubiquitarianism/leonida:comment:7368750774 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/3139#issuecomment-7368750774). Originally posted by [kaoru-maki](https://github.com/kaoru-maki) on 2026-12-03T05:26:57Z. --- A follow-up to the unlocked allocation would be to install the ready buffer in a per-resource record. Validate the generation and range and install the handle while holding the same entry guard used to retire the resource. That would move final handle publication off the global index write lock. Synthetic race test. Pause the builder for generation 1 of resource A after allocation. Unregister A, register it again as generation 2, and resume the old builder. Assert that the old candidate is discarded and generation 2 stays current. Publishing A should also allow lookups of an unrelated ready entry to finish.
kaoru-maki commented 2026-12-03 08:07:20 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by kaoru-maki on 2026-12-03T08:07:20Z.


Follow-up with four matched A/B pairs using this PR head (2bc1f6f43399) merged locally into clean main (d9b618f10150). I used an RTX 5070 Ti Laptop GPU (12 GB) on Windows 11 with NVIDIA driver 621.47, native 2560×1440 DLAA, no frame generation, and Smooth Motion off. The order was A B B A A B B A after two warm-ups. Each measured run started from the same prepared PSO cache snapshot.

Metric A: main B: main + #3139
Mean FPS, full route 23.30 23.51
Mean per-run P99 105.28 ms 103.95 ms
Longest frame interval 10.17 s 10.16 s
Frames over 250 ms, full route 123 122
Frames over 250 ms, first 40 s 12 6

The paired change in median frame time was −0.43 ms for B versus A (95% CI −0.98 to +0.13 ms). The early window showed fewer long frames. The full-route result was inconclusive.

Run report in #2464.

<!-- greyfinch-sync:ubiquitarianism/leonida:comment:7371849296 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/3139#issuecomment-7371849296). Originally posted by [kaoru-maki](https://github.com/kaoru-maki) on 2026-12-03T08:07:20Z. --- Follow-up with four matched A/B pairs using this PR head (`2bc1f6f43399`) merged locally into clean `main` (`d9b618f10150`). I used an RTX 5070 Ti Laptop GPU (12 GB) on Windows 11 with NVIDIA driver 621.47, native 2560×1440 DLAA, no frame generation, and Smooth Motion off. The order was `A B B A A B B A` after two warm-ups. Each measured run started from the same prepared PSO cache snapshot. | Metric | A: main | B: main + #3139 | | --- | ---: | ---: | | Mean FPS, full route | 23.30 | 23.51 | | Mean per-run P99 | 105.28 ms | 103.95 ms | | Longest frame interval | 10.17 s | 10.16 s | | Frames over 250 ms, full route | 123 | 122 | | Frames over 250 ms, first 40 s | 12 | 6 | The paired change in median frame time was −0.43 ms for B versus A (95% CI −0.98 to +0.13 ms). The early window showed fewer long frames. The full-route result was inconclusive. [Run report in #2464](https://github.com/ProjectLeonida/mandy/issues/2464#issuecomment-7371532538).
merzleckeier commented 2026-12-03 08:18:46 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by merzleckeier on 2026-12-03T08:18:46Z.


I ran the requested two-cold-key control against the actual lookup paths on main d9b618f1 and this head 2bc1f6f4. The same test-only hook parked cold lookup A for 150 ms immediately before its host allocation. Fresh threads then called find for cold key B and for an unrelated ready key C. C had an empty thread-local cache, and the test checked that it returned the existing Arc. The run order was A/B, B/A, A/B.

Result across three runs per side main This PR
C returned while first allocation was parked 0/3 3/3
C lookup time 150.154–150.159 ms 10–17 µs
Cold B reached allocation before A resumed 0/3 3/3
B lookup time 150.218–150.241 ms 6–7 µs

The timings are from the synthetic 150 ms pause in a Windows MSVC build under Wine 11.22. The four existing shadow::unlock_alloc_tests passed, with the separate ignored benchmark left ignored. This isolates the spread of the table-lock wait. AI-assisted with OpenAI Codex.

<!-- greyfinch-sync:ubiquitarianism/leonida:comment:7372101031 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/3139#issuecomment-7372101031). Originally posted by [merzleckeier](https://github.com/merzleckeier) on 2026-12-03T08:18:46Z. --- I ran the requested two-cold-key control against the actual lookup paths on `main` `d9b618f1` and this head `2bc1f6f4`. The same test-only hook parked cold lookup A for 150 ms immediately before its host allocation. Fresh threads then called `find` for cold key B and for an unrelated ready key C. C had an empty thread-local cache, and the test checked that it returned the existing `Arc`. The run order was A/B, B/A, A/B. | Result across three runs per side | `main` | This PR | | --- | --- | --- | | C returned while first allocation was parked | 0/3 | 3/3 | | C lookup time | 150.154–150.159 ms | 10–17 µs | | Cold B reached allocation before A resumed | 0/3 | 3/3 | | B lookup time | 150.218–150.241 ms | 6–7 µs | The timings are from the synthetic 150 ms pause in a Windows MSVC build under Wine 11.22. The four existing `shadow::unlock_alloc_tests` passed, with the separate ignored benchmark left ignored. This isolates the spread of the table-lock wait. AI-assisted with OpenAI Codex.
merzleckeier commented 2026-12-03 10:28:16 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by merzleckeier on 2026-12-03T10:28:16Z.


I checked the #2981 overlap with this exact head (2bc1f6f43) on main d9b618f10. The #2981 head b020637ac still conflicts here in five hunks of shadow.rs. A local resolution kept this PR’s unlocked allocation and publication, set CardAccount::Buffer on the cold buffer constructor and CardAccount::Window on the cold window constructor, and kept both test modules.

The resolved diagnostics build passed the shadow:: suite on AMD Radeon (RADV 26.3.1, Wine 11.22, vkd3d-proton 3.1.0). 73 passed, 0 failed, 3 ignored. A separate synthetic test used LEONIDA_CARD_ALL=1 and LEONIDA_WINDOWS_OWN=1 to exercise both actual constructors. Freeing the buffer returned CARD_BYTES[0] to baseline while leaving the window’s reservation live. Freeing the window then returned WINDOW_CARD_BYTES to baseline. BY_ADDRESS_USED stayed at baseline throughout. git diff --check passed.

AI-assisted verification with OpenAI Codex.

<!-- greyfinch-sync:ubiquitarianism/leonida:comment:7375281022 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/3139#issuecomment-7375281022). Originally posted by [merzleckeier](https://github.com/merzleckeier) on 2026-12-03T10:28:16Z. --- I checked the #2981 overlap with this exact head (`2bc1f6f43`) on `main` `d9b618f10`. The #2981 head `b020637ac` still conflicts here in five hunks of `shadow.rs`. A local resolution kept this PR’s unlocked allocation and publication, set `CardAccount::Buffer` on the cold buffer constructor and `CardAccount::Window` on the cold window constructor, and kept both test modules. The resolved diagnostics build passed the `shadow::` suite on AMD Radeon (RADV 26.3.1, Wine 11.22, vkd3d-proton 3.1.0). **73 passed, 0 failed, 3 ignored.** A separate synthetic test used `LEONIDA_CARD_ALL=1` and `LEONIDA_WINDOWS_OWN=1` to exercise both actual constructors. Freeing the buffer returned `CARD_BYTES[0]` to baseline while leaving the window’s reservation live. Freeing the window then returned `WINDOW_CARD_BYTES` to baseline. `BY_ADDRESS_USED` stayed at baseline throughout. `git diff --check` passed. AI-assisted verification with OpenAI Codex.
This pull request cannot be merged automatically due to conflicts.
Merge manually to resolve the conflicts.
View command line instructions

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u origin 'upstream-pr/3139:upstream-pr/3139'
git switch 'upstream-pr/3139'

Merge

Merge the changes and update on Forgejo.

Warning: The "Autodetect manual merge" setting is not enabled for this repository, you will have to mark this pull request as manually merged afterwards.

git switch 'main'
git merge --no-ff 'upstream-pr/3139'
git switch 'upstream-pr/3139'
git rebase 'main'
git switch 'main'
git merge --ff-only 'upstream-pr/3139'
git switch 'upstream-pr/3139'
git rebase 'main'
git switch 'main'
git merge --no-ff 'upstream-pr/3139'
git switch 'main'
git merge --squash 'upstream-pr/3139'
git switch 'main'
git merge --ff-only 'upstream-pr/3139'
git switch 'main'
git merge 'upstream-pr/3139'
git push origin 'main'
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
ProjectLeonida/mandy!3139
No description provided.