perf(gpu): speed up copy_runs, pieces::take, write-watch journal, and preparing hot paths #306

Manually merged
github merged 1 commit from github-bot/gh-306 into gh-pr/306/base 2026-12-06 00:59:25 +00:00 AGit
Owner

@⁠talfour on GitHub · 2026-12-05 22:54 UTC · original
talfour:perf/copy-runs-and-preparing → main · +566 −149 · 5 files · 1 commits · merged 2026-12-05 23:10 UTC

Summary

Follow-up CPU overhead optimizations on top of #185 targeting Host::submit / preparing hot paths in larp-gpu (no new dependencies, no shader-cache invalidation, zero extra RAM):

  1. Sort-and-cluster fast path for copy_runs + generation-tagged run_map + zero-alloc Spans::add (crates/larp-gpu/src/host.rs)

    • Why: Per-stage submission profiling on top of #185 showed copy_runs() taking 3.0–4.1 ms/frame at 7k–11k draws/frame (~50% of all preparing time — more than 2× slower than the actual D3D12 CopyBufferRegion + ExecuteCommandLists work at ~1.5 ms/frame) due to rebuilding and destroying two BTreeMap-backed HashMaps and allocating Vec<u64> inside Spans::add for thousands of mostly disjoint copies.
    • What: Collects (to_ptr, at, end, idx) into a reused thread-local buffer, sorts once by (to_ptr, at), and partitions into connected overlap components:
      • Singleton (disjoint) copies bypass Spans and run_map completely in both passes.
      • 2-element clusters are resolved with 2 integer comparisons.
      • Larger clusters reuse a single Spans without hash lookups, and pass 2 tracks only surviving clustered copies using a u32 generation tag instead of clearing/freeing BTreeMaps on every run split.
      • Eliminates the intermediate places Vec in copy_send_refs.
    • Includes a 2,000-case randomized fuzz equivalence test + edge cases (host::copy_runs_tests::copy_runs_matches_reference_fuzz) verifying bit-for-bit identical output against the reference implementation.
  2. Lock-free fast path in pieces::take() + off-lock/reaper drops in pieces::State::settle() (crates/larp-gpu/src/pieces.rs, host.rs)

    • Mirrors copies non-empty (HAS_COPIES) and earliest resting submission (NEXT_REST) in atomics so pieces::take() skips the pieces::STATE mutex (contended with streaming new_buffer D3D12 allocations) when idle.
    • Moves settled Buffer / Span drops out of the STATE lock and offloads them via host::reap_now to larp-reaper when LARP_OPT_RELEASE_HELD is active.
    • Switches by_system and by_object to FastMap.
  3. Write-watch journal contention & redundant ask_ahead elimination (crates/larp-gpu/src/watch.rs, host.rs)

    • Switches watch::JOURNAL from Mutex to RwLock so parallel larp-wide refresh threads take shared read locks on the ask_once fast path.
    • Mirrors Journal::kept in an atomic KEPT so Batch::flush / ask_ahead / ask_ahead_done skip allocating ranges/pieces, locking GUARDS, and sorting when ask_once already covers the submission.
    • Deduplicates consecutive 4 KiB kernel pages mapping to the same 64 KiB PAGE before BTreeMap::insert, and removes the temporary Vec in watch::writes.
  4. FastMap/FastSet and thread-local buffer reuse in preparing (host.rs, shadow.rs, texevents.rs)

    • Replaces default SipHash HashMap/HashSet with FastMap/FastSet on integer/pointer keys in PLANNED, gpu_writes, ASK_SCRATCH, Batch::index, shadow::Pending/Flights, and texevents::Objects.
    • Reuses PLANNED, ASK_SCRATCH, and Batch allocations (bounded by KEEP_BYTES), removes per-Planned vec! allocations in ask_once, and short-circuits Planned::Wait scanning per list when none exist.

Measured In-Game Results (RX 9070 XT, 1440p, Series X profile, on top of #185)

Scene (draws/frame) Before (#185) After this PR Delta
~2,070 draws/f 46–49 fps, prep 3.3–3.6 ms, copy_runs 0.57–1.07 ms 56.3–56.8 fps, prep 2.8 ms, copy_runs 0.05–0.06 ms 10–15× faster copy_runs
~6,900 draws/f 35.4 fps, prep 6.4 ms, copy_runs 3.25 ms 36.7–37.8 fps, prep 3.9–4.0 ms, copy_runs 0.11–0.45 ms -2.5 ms prep
~8,500–8,800 draws/f ~29–30 fps, prep 8.7–9.3 ms 35.2–36.0 fps, prep 3.8–4.2 ms, copy_runs 0.45–0.54 ms -4.8 ms prep
~10,800–11,350 draws/f prep 7.9–9.3 ms, copy_runs 4.05–4.07 ms prep 4.2–4.6 ms, copy_runs 0.42–0.50 ms 8–10× faster copy_runs, -3.6 ms prep

pieces::take() measured 0.00–0.03 ms/f and watch::ask_once 0.01–0.03 ms/f.

Verification

  • cargo test --lib --locked -p larp-gpu — 19 passed, 0 failed (including 2,000-case fuzz test host::copy_runs_tests::copy_runs_matches_reference_fuzz)
  • cargo check --release --locked -p larp — passed with zero new warnings
  • cargo check --release --locked -p larp-gpu --features diagnostics — passed with zero new warnings
  • No changes to crates/larp-shader or shader_cache.rs (existing shader/pipeline caches stay valid)
**@⁠talfour** on GitHub · 2026-12-05 22:54 UTC · [original](https://github.com/gamingwithhomie/larp/pull/306) `talfour:perf/copy-runs-and-preparing` → `main` · +566 −149 · 5 files · 1 commits · merged 2026-12-05 23:10 UTC ## Summary Follow-up CPU overhead optimizations on top of #185 targeting `Host::submit` / `preparing` hot paths in `larp-gpu` (no new dependencies, no shader-cache invalidation, zero extra RAM): 1. **Sort-and-cluster fast path for `copy_runs` + generation-tagged `run_map` + zero-alloc `Spans::add` (`crates/larp-gpu/src/host.rs`)** - *Why:* Per-stage submission profiling on top of #185 showed `copy_runs()` taking **3.0–4.1 ms/frame** at 7k–11k draws/frame (~50% of all `preparing` time — more than 2× slower than the actual D3D12 `CopyBufferRegion` + `ExecuteCommandLists` work at ~1.5 ms/frame) due to rebuilding and destroying two `BTreeMap`-backed `HashMap`s and allocating `Vec<u64>` inside `Spans::add` for thousands of mostly disjoint copies. - *What:* Collects `(to_ptr, at, end, idx)` into a reused thread-local buffer, sorts once by `(to_ptr, at)`, and partitions into connected overlap components: - Singleton (disjoint) copies bypass `Spans` and `run_map` completely in both passes. - 2-element clusters are resolved with 2 integer comparisons. - Larger clusters reuse a single `Spans` without hash lookups, and pass 2 tracks only surviving clustered copies using a `u32` generation tag instead of clearing/freeing `BTreeMap`s on every run split. - Eliminates the intermediate `places` `Vec` in `copy_send_refs`. - Includes a 2,000-case randomized fuzz equivalence test + edge cases (`host::copy_runs_tests::copy_runs_matches_reference_fuzz`) verifying bit-for-bit identical output against the reference implementation. 2. **Lock-free fast path in `pieces::take()` + off-lock/reaper drops in `pieces::State::settle()` (`crates/larp-gpu/src/pieces.rs`, `host.rs`)** - Mirrors `copies` non-empty (`HAS_COPIES`) and earliest `resting` submission (`NEXT_REST`) in atomics so `pieces::take()` skips the `pieces::STATE` mutex (contended with streaming `new_buffer` D3D12 allocations) when idle. - Moves settled `Buffer` / `Span` drops out of the `STATE` lock and offloads them via `host::reap_now` to `larp-reaper` when `LARP_OPT_RELEASE_HELD` is active. - Switches `by_system` and `by_object` to `FastMap`. 3. **Write-watch journal contention & redundant `ask_ahead` elimination (`crates/larp-gpu/src/watch.rs`, `host.rs`)** - Switches `watch::JOURNAL` from `Mutex` to `RwLock` so parallel `larp-wide` refresh threads take shared read locks on the `ask_once` fast path. - Mirrors `Journal::kept` in an atomic `KEPT` so `Batch::flush` / `ask_ahead` / `ask_ahead_done` skip allocating `ranges`/`pieces`, locking `GUARDS`, and sorting when `ask_once` already covers the submission. - Deduplicates consecutive 4 KiB kernel pages mapping to the same 64 KiB `PAGE` before `BTreeMap::insert`, and removes the temporary `Vec` in `watch::writes`. 4. **`FastMap`/`FastSet` and thread-local buffer reuse in `preparing` (`host.rs`, `shadow.rs`, `texevents.rs`)** - Replaces default SipHash `HashMap`/`HashSet` with `FastMap`/`FastSet` on integer/pointer keys in `PLANNED`, `gpu_writes`, `ASK_SCRATCH`, `Batch::index`, `shadow::Pending`/`Flights`, and `texevents::Objects`. - Reuses `PLANNED`, `ASK_SCRATCH`, and `Batch` allocations (bounded by `KEEP_BYTES`), removes per-`Planned` `vec!` allocations in `ask_once`, and short-circuits `Planned::Wait` scanning per list when none exist. ## Measured In-Game Results (RX 9070 XT, 1440p, Series X profile, on top of #185) | Scene (draws/frame) | Before (#185) | After this PR | Delta | |---|---|---|---| | ~2,070 draws/f | 46–49 fps, `prep` 3.3–3.6 ms, `copy_runs` 0.57–1.07 ms | **56.3–56.8 fps**, `prep` **2.8 ms**, `copy_runs` **0.05–0.06 ms** | **10–15× faster `copy_runs`** | | ~6,900 draws/f | 35.4 fps, `prep` 6.4 ms, `copy_runs` 3.25 ms | **36.7–37.8 fps**, `prep` **3.9–4.0 ms**, `copy_runs` **0.11–0.45 ms** | **-2.5 ms `prep`** | | ~8,500–8,800 draws/f | ~29–30 fps, `prep` 8.7–9.3 ms | **35.2–36.0 fps**, `prep` **3.8–4.2 ms**, `copy_runs` **0.45–0.54 ms** | **-4.8 ms `prep`** | | ~10,800–11,350 draws/f | `prep` 7.9–9.3 ms, `copy_runs` 4.05–4.07 ms | `prep` **4.2–4.6 ms**, `copy_runs` **0.42–0.50 ms** | **8–10× faster `copy_runs`, -3.6 ms `prep`** | `pieces::take()` measured **0.00–0.03 ms/f** and `watch::ask_once` **0.01–0.03 ms/f**. ## Verification - `cargo test --lib --locked -p larp-gpu` — **19 passed, 0 failed** (including 2,000-case fuzz test `host::copy_runs_tests::copy_runs_matches_reference_fuzz`) - `cargo check --release --locked -p larp` — passed with zero new warnings - `cargo check --release --locked -p larp-gpu --features diagnostics` — passed with zero new warnings - No changes to `crates/larp-shader` or `shader_cache.rs` (existing shader/pipeline caches stay valid)
- Rewrite copy_runs with a sort-and-cluster fast path (thread-local scratch, generation-tagged run map, zero-alloc Spans::add) plus 2,000-case fuzz test against the reference implementation, cutting copy_runs from ~3.0-4.1 ms/f to ~0.05-0.50 ms/f.
- Add lock-free HAS_COPIES/NEXT_REST fast path to pieces::take and offload settled D3D12 buffer/span drops outside STATE via host::reap_now.
- Switch watch::JOURNAL to RwLock with atomic KEPT short-circuit for redundant ask_ahead/ask_ahead_done calls and 4 KiB -> 64 KiB page deduplication.
- Replace SipHash HashMap/HashSet with FastMap/FastSet and reuse thread-local scratch containers across host::preparing, shadow, pieces, and texevents.
github-bot locked as GitHub sync and limited conversation to collaborators 2026-12-05 23:02:46 +00:00
github-bot changed target branch from main to gh-pr/306/base 2026-12-05 23:13:09 +00:00
github-bot referenced this pull request from a commit 2026-12-06 00:57:58 +00:00
github manually merged commit 7d96eac33d into gh-pr/306/base 2026-12-06 00:59:25 +00:00
This discussion has been locked. Commenting is limited to contributors.
No description provided.