Keep independent loose-window and exact-view D3D12 allocations outside table locks #238

Manually merged
github merged 1 commit from github-bot/gh-238 into gh-pr/238/base 2026-11-27 12:11:40 +00:00 AGit
Owner

@⁠Vladimir-Human on GitHub · 2026-11-27 11:36 UTC · original
Vladimir-Human:perf/loose-and-exact-allocations-outside-locks → main · +852 −120 · 2 files · 1 commits · merged 2026-11-27 12:09 UTC

Follow-up to #196 (which handles the registered title-buffer table BUFFERS in #199), applying the same outside-lock allocation pattern to the loose-window table (LOOSE) and exact-view table (EXACT) in crates/victory-gpu/src/shadow.rs. This PR is disjoint from #199 and does not change rendering output.

Why

  • Recording threads call shadow::loose() on every draw/view outside registered title buffers (and shadow::exact() for views starting off their copy's element alignment; descriptor/host.rs notes heavy contention under EXACT's mutex when many view copies are created).
  • Previously:
    1. Six read-only LOOSE walks (stale_pages, windows, window_use, memory, spans, window_cover) took LOOSE.write() instead of LOOSE.read().
    2. loose() skipped LOOSE.read() on hits whenever tail mode (VICTORY_WINDOW_TAILS=1) or fine-grid holder lookups (VICTORY_WINDOW_KB + find_any) were active.
    3. On a miss, loose() held LOOSE.write() across D3D12 CreateCommittedResource, MakeResident, residency fence confirmation, and Map (window_mapped_for, shared_buffer, window_card_for), stalling unrelated reads, independent allocations, and idle sweeps.
    4. exact() held a global Mutex across shared_buffer creation and idle-entry eviction drops.

Changes

  • crates/victory-gpu/src/shadow.rs:
    • Switched the six read-only LOOSE callers to LOOSE.read().
    • Routed all loose() hits through LOOSE.read() via loose_key_with, including tail and fine-grid holder lookups.
    • Moved independent D3D12 backing allocations out of LOOSE.write() behind per-key constructor tickets (LOOSE_BUILDING, BTreeMap<(u64, u64), Weak<Mutex<()>>>), re-selecting the live key under LOOSE.write() before publishing and dropping any obsolete candidate outside all locks so Drop for Shadow cleanly restores WINDOW_CARD_BYTES / WINDOW_MAPPED_BYTES / LIVE. Preserves the existing transaction unchanged when optional imported/in-place modes (port::on() / inplace::level() >= 1) are enabled.
    • Converted EXACT to RwLock + EXACT_TICK: AtomicU64 + EXACT_BUILDING per-key constructor tickets, allocating and dropping evicted copies outside EXACT.write() and stamping newly published entries with the publish-time tick (published_now).
  • crates/victory-gpu/src/shadow/loose_allocation_tests.rs:
    • Added 5 D3D12 HW / GBV / WARP regression tests covering unrelated concurrent reads and spans(), parallel allocations of independent keys, same-key deduplication, concurrent wider tail/holder re-selection with card-budget discard release and byte-exact GPU CopyBufferRegion readback, allocation failure retry, and EXACT eviction + publish-time tick freshness.

Verification

All commands ran on Windows (rustc 1.98.1, VICTORY_D3D12_DEBUG=1, --test-threads=1), with synthetic memory and real D3D12 buffers (no runtime FPS claim):

  • cargo clippy -p victory-gpu --all-targets --locked -- -D warnings: 0 warnings
  • cargo clippy -p victory-gpu --all-targets --all-features --locked -- -D warnings: 0 warnings
  • cargo test -p victory-gpu --locked --lib shadow:: -- --test-threads=1: 59 passed; 0 failed; 4 ignored
  • cargo test -p victory-gpu --locked --lib port::tests -- --test-threads=1: 2 passed; 0 failed
  • cargo test -p victory-gpu --all-features --locked --lib shadow:: -- --test-threads=1: 59 passed; 0 failed; 4 ignored
  • VICTORY_D3D12_GBV=1 cargo test -p victory-gpu --all-features --locked --lib shadow::loose_allocation_tests -- --test-threads=1: 5 passed; 0 failed
  • VICTORY_D3D12_WARP=1 cargo test -p victory-gpu --locked --lib shadow::loose_allocation_tests -- --test-threads=1: 5 passed; 0 failed
  • cargo run -p xtask --locked -- policy (311 files ok) and cargo test -p xtask --locked (11 passed; 0 failed)
**@⁠Vladimir-Human** on GitHub · 2026-11-27 11:36 UTC · [original](https://github.com/TaynanGT/Vice-Tay/pull/238) `Vladimir-Human:perf/loose-and-exact-allocations-outside-locks` → `main` · +852 −120 · 2 files · 1 commits · merged 2026-11-27 12:09 UTC Follow-up to #196 (which handles the registered title-buffer table `BUFFERS` in #199), applying the same outside-lock allocation pattern to the loose-window table (`LOOSE`) and exact-view table (`EXACT`) in `crates/victory-gpu/src/shadow.rs`. This PR is disjoint from #199 and does not change rendering output. ### Why - Recording threads call `shadow::loose()` on every draw/view outside registered title buffers (and `shadow::exact()` for views starting off their copy's element alignment; `descriptor/host.rs` notes heavy contention under `EXACT`'s mutex when many view copies are created). - Previously: 1. Six read-only `LOOSE` walks (`stale_pages`, `windows`, `window_use`, `memory`, `spans`, `window_cover`) took `LOOSE.write()` instead of `LOOSE.read()`. 2. `loose()` skipped `LOOSE.read()` on hits whenever tail mode (`VICTORY_WINDOW_TAILS=1`) or fine-grid holder lookups (`VICTORY_WINDOW_KB` + `find_any`) were active. 3. On a miss, `loose()` held `LOOSE.write()` across D3D12 `CreateCommittedResource`, `MakeResident`, residency fence confirmation, and `Map` (`window_mapped_for`, `shared_buffer`, `window_card_for`), stalling unrelated reads, independent allocations, and idle sweeps. 4. `exact()` held a global `Mutex` across `shared_buffer` creation and idle-entry eviction drops. ### Changes - **`crates/victory-gpu/src/shadow.rs`:** - Switched the six read-only `LOOSE` callers to `LOOSE.read()`. - Routed all `loose()` hits through `LOOSE.read()` via `loose_key_with`, including tail and fine-grid holder lookups. - Moved independent D3D12 backing allocations out of `LOOSE.write()` behind per-key constructor tickets (`LOOSE_BUILDING`, `BTreeMap<(u64, u64), Weak<Mutex<()>>>`), re-selecting the live key under `LOOSE.write()` before publishing and dropping any obsolete candidate outside all locks so `Drop for Shadow` cleanly restores `WINDOW_CARD_BYTES` / `WINDOW_MAPPED_BYTES` / `LIVE`. Preserves the existing transaction unchanged when optional imported/in-place modes (`port::on()` / `inplace::level() >= 1`) are enabled. - Converted `EXACT` to `RwLock` + `EXACT_TICK: AtomicU64` + `EXACT_BUILDING` per-key constructor tickets, allocating and dropping evicted copies outside `EXACT.write()` and stamping newly published entries with the publish-time tick (`published_now`). - **`crates/victory-gpu/src/shadow/loose_allocation_tests.rs`:** - Added 5 D3D12 HW / GBV / WARP regression tests covering unrelated concurrent reads and `spans()`, parallel allocations of independent keys, same-key deduplication, concurrent wider tail/holder re-selection with card-budget discard release and byte-exact GPU `CopyBufferRegion` readback, allocation failure retry, and `EXACT` eviction + publish-time tick freshness. ### Verification All commands ran on Windows (`rustc 1.98.1`, `VICTORY_D3D12_DEBUG=1`, `--test-threads=1`), with synthetic memory and real D3D12 buffers (no runtime FPS claim): - `cargo clippy -p victory-gpu --all-targets --locked -- -D warnings`: **0 warnings** - `cargo clippy -p victory-gpu --all-targets --all-features --locked -- -D warnings`: **0 warnings** - `cargo test -p victory-gpu --locked --lib shadow:: -- --test-threads=1`: **59 passed; 0 failed; 4 ignored** - `cargo test -p victory-gpu --locked --lib port::tests -- --test-threads=1`: **2 passed; 0 failed** - `cargo test -p victory-gpu --all-features --locked --lib shadow:: -- --test-threads=1`: **59 passed; 0 failed; 4 ignored** - `VICTORY_D3D12_GBV=1 cargo test -p victory-gpu --all-features --locked --lib shadow::loose_allocation_tests -- --test-threads=1`: **5 passed; 0 failed** - `VICTORY_D3D12_WARP=1 cargo test -p victory-gpu --locked --lib shadow::loose_allocation_tests -- --test-threads=1`: **5 passed; 0 failed** - `cargo run -p xtask --locked -- policy` (`311 files ok`) and `cargo test -p xtask --locked` (**11 passed; 0 failed**)
Follow-up to #196 (which handles the title buffer table in #199): apply the
same outside-lock allocation pattern to the loose-window table (`LOOSE`) and
exact-view table (`EXACT`), and switch read-only `LOOSE` walks from write locks
to shared read locks.

- Switch six read-only `LOOSE` readers (`stale_pages`, `windows`,
  `window_use`, `memory`, `spans`, `window_cover`) from `LOOSE.write()` to
  `LOOSE.read()`.
- Route `loose()` hits through `LOOSE.read()` for all lookup modes, including
  tail and fine-grid holder lookups (`loose_key_with`).
- Move independent D3D12 backing allocations (`window_mapped_for`,
  `shared_buffer`, `window_card_for`) out of `LOOSE.write()` behind per-key
  constructor tickets (`LOOSE_BUILDING`), re-selecting the live key under
  `LOOSE.write()` before publishing and dropping any obsolete candidate
  outside the table lock (preserving `WINDOW_CARD_BYTES` / `WINDOW_MAPPED_BYTES`
  accounting). Keep the existing transaction unchanged when optional
  imported/in-place modes (`port::on()` / `inplace::level() >= 1`) are enabled.
- Convert `EXACT` from a `Mutex` held across `shared_buffer` creation and
  idle eviction drops into an `RwLock` + atomic tick (`EXACT_TICK`) + per-key
  constructor tickets (`EXACT_BUILDING`), allocating and dropping evicted
  copies outside the table lock and stamping new entries with the publish-time
  tick.
- Add D3D12 HW, GBV, and WARP regression tests in
  `crates/victory-gpu/src/shadow/loose_allocation_tests.rs` covering unrelated
  concurrent reads, parallel independent allocations, same-key deduplication,
  concurrent wider tail/holder re-selection with card-budget release and
  byte-exact GPU readback, failure retry, and `EXACT` eviction.
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-27 11:56:35 +00:00
Author
Owner

@⁠TaynanGT on GitHub · 2026-11-27 11:50 UTC · original

@⁠Vladimir-Human The separate constructor tickets for LOOSE and EXACT are the useful part of this follow-up. I’ll check the wider-holder readback and retry cases against current main, especially that discarded candidates return their budget and that the imported and in-place paths keep their existing transaction.

**@⁠TaynanGT** on GitHub · 2026-11-27 11:50 UTC · [original](https://github.com/TaynanGT/Vice-Tay/pull/238#issuecomment-7217533638) @⁠Vladimir-Human The separate constructor tickets for `LOOSE` and `EXACT` are the useful part of this follow-up. I’ll check the wider-holder readback and retry cases against current main, especially that discarded candidates return their budget and that the imported and in-place paths keep their existing transaction.
github-bot changed target branch from main to gh-pr/238/base 2026-11-27 12:11:39 +00:00
github manually merged commit ff1663502b into gh-pr/238/base 2026-11-27 12:11:40 +00:00
Author
Owner

@⁠Vladimir-Human on GitHub · 2026-11-27 12:06 UTC · original

Thanks @⁠TaynanGT — checked against current main (6b3a266, clean git merge-tree with no conflicts, and all CI checks are green). Quick pointers to the three invariants in ff16635:

  1. Wider-holder / tail re-selection, readback, and retry:

    • crates/victory-gpu/src/shadow.rs:6615-6652 (loose_with): acquires per-key LOOSE_BUILDING ticket, re-checks LOOSE.read() via loose_key_with, allocates outside LOOSE (make(key.1)? drops _only cleanly on failure so retries proceed immediately), then re-runs loose_key_with under LOOSE.write(). If a wider tail/holder or the same key published meanwhile, it adopts existing and drops candidate after releasing map and _only; if selected != key, it drops candidate and loops.
    • crates/victory-gpu/src/shadow.rs:6732-6784 (exact_with_limits): same per-key EXACT_BUILDING ticket, ?-based retry on allocation failure, publish-time published_now tick stamping, and out-of-lock evicted drops.
    • Covered in crates/victory-gpu/src/shadow/loose_allocation_tests.rs: loose_reselects_concurrent_wider_tail_or_holder_and_gpu_readback_sees_live_bytes (L273-370, both use_tails and use_holder with Shadow::refresh + CopyBufferRegion readback + host.log_messages().is_none()), loose_allocation_failure_leaves_window_open_to_retry (L373-382), exact_lookups_and_parallel_allocations_proceed_while_pending_with_gpu_readback (L385-471), and exact_eviction_keeps_held_entries_and_uses_publish_time_tick (L474-533).
  2. Discarded candidate budget release outside locks:

    • make_loose_backing (L6457-6467) hands every budget-counted buffer directly into build_loose_shadow (L6469-6511) without pushing to CARDED until publish (L6647-6649).
    • On discard (L6636-6638, L6642-6644, and L6753-6755), map and _only are dropped before drop(candidate), where Drop for Shadow (L3449-3476) subtracts LIVE, WINDOW_MAPPED_BYTES (shadow.mapped), and WINDOW_CARD_BYTES (shadow.window && shadow.card.is_some()).
    • loose_reselects_concurrent_wider_tail_or_holder_and_gpu_readback_sees_live_bytes (L292, L300, L347-351) uses a card-backed candidate (shared_with_card) and asserts WINDOW_CARD_BYTES returns to its exact baseline after both tail and holder discards.
  3. Imported / in-place transaction preserved:

    • loose_with (L6589-6611) bypasses LOOSE_BUILDING whenever crate::port::on() || crate::inplace::level() >= 1 and keeps the original LOOSE.write() transaction (loose_key_with -> port::place_for / inplace::place_for -> fallback allocation + CARDED + insert all under LOOSE.write()). Cache hits under LOOSE.read() (L6578-6586) remain mutually excluded from in_place_let_go (L6035) and let_go_idle (L3868), which both take LOOSE.write().

Happy to rebase or add a mapped-window discard check as well if helpful.

**@⁠Vladimir-Human** on GitHub · 2026-11-27 12:06 UTC · [original](https://github.com/TaynanGT/Vice-Tay/pull/238#issuecomment-7217839116) Thanks @⁠TaynanGT — checked against current `main` (`6b3a266`, clean `git merge-tree` with no conflicts, and all CI checks are green). Quick pointers to the three invariants in `ff16635`: 1. **Wider-holder / tail re-selection, readback, and retry:** - `crates/victory-gpu/src/shadow.rs:6615-6652` (`loose_with`): acquires per-key `LOOSE_BUILDING` ticket, re-checks `LOOSE.read()` via `loose_key_with`, allocates outside `LOOSE` (`make(key.1)?` drops `_only` cleanly on failure so retries proceed immediately), then re-runs `loose_key_with` under `LOOSE.write()`. If a wider tail/holder or the same key published meanwhile, it adopts `existing` and drops `candidate` after releasing `map` and `_only`; if `selected != key`, it drops `candidate` and loops. - `crates/victory-gpu/src/shadow.rs:6732-6784` (`exact_with_limits`): same per-key `EXACT_BUILDING` ticket, `?`-based retry on allocation failure, publish-time `published_now` tick stamping, and out-of-lock `evicted` drops. - Covered in `crates/victory-gpu/src/shadow/loose_allocation_tests.rs`: `loose_reselects_concurrent_wider_tail_or_holder_and_gpu_readback_sees_live_bytes` (`L273-370`, both `use_tails` and `use_holder` with `Shadow::refresh` + `CopyBufferRegion` readback + `host.log_messages().is_none()`), `loose_allocation_failure_leaves_window_open_to_retry` (`L373-382`), `exact_lookups_and_parallel_allocations_proceed_while_pending_with_gpu_readback` (`L385-471`), and `exact_eviction_keeps_held_entries_and_uses_publish_time_tick` (`L474-533`). 2. **Discarded candidate budget release outside locks:** - `make_loose_backing` (`L6457-6467`) hands every budget-counted buffer directly into `build_loose_shadow` (`L6469-6511`) without pushing to `CARDED` until publish (`L6647-6649`). - On discard (`L6636-6638`, `L6642-6644`, and `L6753-6755`), `map` and `_only` are dropped before `drop(candidate)`, where `Drop for Shadow` (`L3449-3476`) subtracts `LIVE`, `WINDOW_MAPPED_BYTES` (`shadow.mapped`), and `WINDOW_CARD_BYTES` (`shadow.window && shadow.card.is_some()`). - `loose_reselects_concurrent_wider_tail_or_holder_and_gpu_readback_sees_live_bytes` (`L292`, `L300`, `L347-351`) uses a card-backed candidate (`shared_with_card`) and asserts `WINDOW_CARD_BYTES` returns to its exact baseline after both tail and holder discards. 3. **Imported / in-place transaction preserved:** - `loose_with` (`L6589-6611`) bypasses `LOOSE_BUILDING` whenever `crate::port::on() || crate::inplace::level() >= 1` and keeps the original `LOOSE.write()` transaction (`loose_key_with` -> `port::place_for` / `inplace::place_for` -> fallback allocation + `CARDED` + insert all under `LOOSE.write()`). Cache hits under `LOOSE.read()` (`L6578-6586`) remain mutually excluded from `in_place_let_go` (`L6035`) and `let_go_idle` (`L3868`), which both take `LOOSE.write()`. Happy to rebase or add a mapped-window discard check as well if helpful.
Author
Owner

@⁠TaynanGT on GitHub · 2026-11-27 12:09 UTC · original

@⁠Vladimir-Human I checked the re-selection and out-of-lock discard paths, including the original transaction for imported and in-place memory. The Windows checks passed and the merge onto current main retained the same allocation patch alongside the existing helper priority change. It’s now in main at 1f69d67. The next gameplay comparison can measure table wait and rendered frame time on the same route.

**@⁠TaynanGT** on GitHub · 2026-11-27 12:09 UTC · [original](https://github.com/TaynanGT/Vice-Tay/pull/238#issuecomment-7217908664) @⁠Vladimir-Human I checked the re-selection and out-of-lock discard paths, including the original transaction for imported and in-place memory. The Windows checks passed and the merge onto current main retained the same allocation patch alongside the existing helper priority change. It’s now in main at `1f69d67`. The next gameplay comparison can measure table wait and rendered frame time on the same route.
Author
Owner

@⁠hamdanpor-ctrl on GitHub · 2026-11-27 16:06 UTC · original

Test report, NVIDIA laptop on Windows: one new test fails now and then.

  • PC: RTX 3050 Laptop GPU 4 GB (driver 32.0.16.1742) with Intel Iris Xe, i7-12700H, 16 GB, Windows 11 Pro
  • Build: Rust 1.98.1, VS 2022 Build Tools
  • Compared: main at 6b3a266, and the same commit with ff16635 merged (git merge --no-ff, clean)
  • Command: cargo test --workspace --locked, tests in parallel (the default)
main main + this PR
Runs 15 5
Runs with a failure 0 3
Clippy, with and without --all-features clean clean

shadow::loose_allocation_tests::exact_eviction_keeps_held_entries_and_uses_publish_time_tick failed in 2 of the 5 runs, both times at loose_allocation_tests.rs:526:

slowly allocated entry must use publish-time tick and survive immediate sweep

Likely cause: the test counts ticks of the global EXACT_TICK (lately = 2, then one more miss), but other tests running beside it also allocate exact entries and advance the same tick, so slow_addr can already look stale at the trigger. Running the loose-allocation tests that touch EXACT one at a time (a test-only lock, like watch::one_asking_test) should make it deterministic.

The third failing run was watch::tests::what_the_kernel_writes_through_is_not_guarded_for_another_view, which also fails now and then on main here and is addressed separately in #246.

Single batch. AI-assisted (Claude Code).

**@⁠hamdanpor-ctrl** on GitHub · 2026-11-27 16:06 UTC · [original](https://github.com/TaynanGT/Vice-Tay/pull/238#issuecomment-7222855943) Test report, NVIDIA laptop on Windows: one new test fails now and then. - PC: RTX 3050 Laptop GPU 4 GB (driver 32.0.16.1742) with Intel Iris Xe, i7-12700H, 16 GB, Windows 11 Pro - Build: Rust 1.98.1, VS 2022 Build Tools - Compared: `main` at `6b3a266`, and the same commit with `ff16635` merged (`git merge --no-ff`, clean) - Command: `cargo test --workspace --locked`, tests in parallel (the default) | | main | main + this PR | |---|---:|---:| | Runs | 15 | 5 | | Runs with a failure | 0 | 3 | | Clippy, with and without `--all-features` | clean | clean | `shadow::loose_allocation_tests::exact_eviction_keeps_held_entries_and_uses_publish_time_tick` failed in 2 of the 5 runs, both times at `loose_allocation_tests.rs:526`: ``` slowly allocated entry must use publish-time tick and survive immediate sweep ``` Likely cause: the test counts ticks of the global `EXACT_TICK` (`lately = 2`, then one more miss), but other tests running beside it also allocate exact entries and advance the same tick, so `slow_addr` can already look stale at the trigger. Running the loose-allocation tests that touch `EXACT` one at a time (a test-only lock, like `watch::one_asking_test`) should make it deterministic. The third failing run was `watch::tests::what_the_kernel_writes_through_is_not_guarded_for_another_view`, which also fails now and then on `main` here and is addressed separately in #246. Single batch. AI-assisted (Claude Code).
Author
Owner

@⁠TaynanGT on GitHub · 2026-11-27 16:17 UTC · original

@⁠hamdanpor-ctrl You found the same shared-clock problem we saw in CI. #300 is on main and checks the published timestamp directly while holding the allocation. Parallel lookups can advance the clock without invalidating that assertion. The current Windows checks pass. Thanks for confirming it under the default parallel test run on your laptop.

**@⁠TaynanGT** on GitHub · 2026-11-27 16:17 UTC · [original](https://github.com/TaynanGT/Vice-Tay/pull/238#issuecomment-7223080672) @⁠hamdanpor-ctrl You found the same shared-clock problem we saw in CI. #300 is on main and checks the published timestamp directly while holding the allocation. Parallel lookups can advance the clock without invalidating that assertion. The current Windows checks pass. Thanks for confirming it under the default parallel test run on your laptop.
This discussion has been locked. Commenting is limited to contributors.
No description provided.