perf(gpu/sweep): offload pressure trims off-thread and make slots_showing lock-free #278

Manually merged
github merged 1 commit from gh-pr/278/head into gh-pr/278/base 2026-12-01 20:25:13 +00:00
Owner

@⁠Androtim on GitHub · 2026-12-01 08:01 UTC · original
Androtim:perf/pillar2-async-sweep-and-lockfree-slots → main · +302 −83 · 8 files · 1 commits · merged 2026-12-01 09:22 UTC

Summary

Replaces three synchronous submitting-thread bottlenecks in sweep() / slot_evict() under steady-state host pressure without adding opt-in toggles:

  1. Asynchronous pressure buffer drops with warm working-set floors (host.rs, host/uav_pool.rs, list.rs, shadow.rs):
    • Adds crate::host::drop_aside (a dedicated background reaper thread) so D3D12 ID3D12Resource::Release and large Vec<u8> VirtualFree drops from trim_spare_chunks_with, give_list_uploads, ReadbackPool::trim, uav_pool::account_trimmed, list::trim_on_pressure, shadow::trim_snapshot_pool, and shadow::give_back no longer stall the submitting thread.
    • Retains small working-set floors when host commit is pressured but not critical (< 1.5 GiB): 16 MiB spare scratch chunks, 8 MiB per list-upload pool, 256 idle host command lists (64 on critical), and 4 / 16 MiB CPU snapshot buffers, stopping per-second allocator/list thrashing while keeping full draining under critical commit (trim_spare_chunks still drains to 0).
  2. Lock-free HostHeap::slots_showing + AddressHash (descriptor/host.rs, descriptor.rs, resource.rs):
    • Scans self.kept_raw (AtomicUsize::load(Acquire)) instead of locking 1,000,000+ per-slot Mutexes in self.kept.
    • Passes crate::host::ByCopy<usize> directly into evictable_slots() (dropping the intermediate HashSet copy and default SipHash).
  3. Single-lock host_written_snapshot (texture.rs, resource.rs):
    • Snapshots HOST_WRITTEN once per sweep (before locking TEXTURES) instead of acquiring HOST_WRITTEN's mutex ~43,000+ times per pass, and short-circuits inactive/recent textures in slot_evict on r.host.used first.

Verification

  • cargo +1.98.1-x86_64-pc-windows-gnu xtask policy (317 files ok, host.rs within size cap, 100% title-neutral)
  • Targeted unit tests (--locked --test-threads=1):
    • descriptor::: 81 passed; 0 failed (including slots_showing_from_raws_filters_zero_and_classifies_slots, slots_showing_reflects_keep_ref_updates)
    • resource::: 34 passed; 0 failed
    • list_upload_pool_tests: 5 passed; 0 failed (including list_upload_spare_limit_keeps_warm_floor_until_critical, drop_aside_runs_drop_asynchronously)
    • list::: 17 passed; 0 failed (including pressure_idle_cap_preserves_frame_working_set_until_critical)
    • shadow::: 49 passed; 0 failed (3 ignored; including snapshot_pressure_cap_keeps_warm_floor_until_critical)
  • Dual workspace clippy --workspace --all-targets --locked -- -D warnings (default + --features diagnostics)

Plain --release 60s A/B Benchmark (-PlayParity -Console series-x -DlssMode quality, NO --features diagnostics)

Measured on RTX 4060 Ti 8 GB / i7-13700F / 32 GB RAM:

Metric origin/main (ff6e5925) This PR Delta
Median FPS (frame ms) 13.9 fps (72.0 ms, min 4.5) 14.75 fps (67.85 ms, min 5.4) +0.85 fps (-4.15 ms)
Preparing / GPU / Copyback 32.1 / 0.0 / 13.1 ms 30.7 / 0.0 / 11.7 ms -1.4 / 0.0 / -1.4 ms
ahead.part(14) (heaps & sweep) 6.2–10.6 ms (mean 7.84 ms) 1.5–2.2 ms (mean 1.70 ms) -6.14 ms/frame (-78%)
VRAM max / budget min (over) 6833 / 6724 MB (over=96 MB, 49 blocks) 6695 / 6732 MB (over=0 MB, 45 blocks) -138 MB (over=0 MB)
Pool free / given back 626 MB free / 0 given back 308 MB free / 1 given back -318 MB free waste
Texture queue (uploaded / final_waiting) 38304 / 15453 40641 / 14297 +2337 / -1156
Crashes / Untranslated 0 / 6 0 / 6 Clean
**@⁠Androtim** on GitHub · 2026-12-01 08:01 UTC · [original](https://github.com/Androtim/MiniVI/pull/278) `Androtim:perf/pillar2-async-sweep-and-lockfree-slots` → `main` · +302 −83 · 8 files · 1 commits · merged 2026-12-01 09:22 UTC ## Summary Replaces three synchronous submitting-thread bottlenecks in `sweep()` / `slot_evict()` under steady-state host pressure without adding opt-in toggles: 1. **Asynchronous pressure buffer drops with warm working-set floors (`host.rs`, `host/uav_pool.rs`, `list.rs`, `shadow.rs`):** - Adds `crate::host::drop_aside` (a dedicated background reaper thread) so D3D12 `ID3D12Resource::Release` and large `Vec<u8>` `VirtualFree` drops from `trim_spare_chunks_with`, `give_list_uploads`, `ReadbackPool::trim`, `uav_pool::account_trimmed`, `list::trim_on_pressure`, `shadow::trim_snapshot_pool`, and `shadow::give_back` no longer stall the submitting thread. - Retains small working-set floors when host commit is pressured but not critical (`< 1.5 GiB`): `16 MiB` spare scratch chunks, `8 MiB` per list-upload pool, `256` idle host command lists (`64` on critical), and `4` / `16 MiB` CPU snapshot buffers, stopping per-second allocator/list thrashing while keeping full draining under critical commit (`trim_spare_chunks` still drains to `0`). 2. **Lock-free `HostHeap::slots_showing` + `AddressHash` (`descriptor/host.rs`, `descriptor.rs`, `resource.rs`):** - Scans `self.kept_raw` (`AtomicUsize::load(Acquire)`) instead of locking `1,000,000+` per-slot `Mutex`es in `self.kept`. - Passes `crate::host::ByCopy<usize>` directly into `evictable_slots()` (dropping the intermediate `HashSet` copy and default SipHash). 3. **Single-lock `host_written_snapshot` (`texture.rs`, `resource.rs`):** - Snapshots `HOST_WRITTEN` once per sweep (before locking `TEXTURES`) instead of acquiring `HOST_WRITTEN`'s mutex `~43,000+` times per pass, and short-circuits inactive/recent textures in `slot_evict` on `r.host.used` first. ## Verification - `cargo +1.98.1-x86_64-pc-windows-gnu xtask policy` (`317 files ok`, `host.rs` within size cap, 100% title-neutral) - Targeted unit tests (`--locked --test-threads=1`): - `descriptor::`: `81 passed; 0 failed` (including `slots_showing_from_raws_filters_zero_and_classifies_slots`, `slots_showing_reflects_keep_ref_updates`) - `resource::`: `34 passed; 0 failed` - `list_upload_pool_tests`: `5 passed; 0 failed` (including `list_upload_spare_limit_keeps_warm_floor_until_critical`, `drop_aside_runs_drop_asynchronously`) - `list::`: `17 passed; 0 failed` (including `pressure_idle_cap_preserves_frame_working_set_until_critical`) - `shadow::`: `49 passed; 0 failed` (3 ignored; including `snapshot_pressure_cap_keeps_warm_floor_until_critical`) - Dual workspace `clippy --workspace --all-targets --locked -- -D warnings` (default + `--features diagnostics`) ### Plain `--release` 60s A/B Benchmark (`-PlayParity -Console series-x -DlssMode quality`, NO `--features diagnostics`) Measured on RTX 4060 Ti 8 GB / i7-13700F / 32 GB RAM: | Metric | `origin/main` (`ff6e5925`) | This PR | Delta | |---|---:|---:|---:| | Median FPS (frame ms) | `13.9 fps` (`72.0 ms`, min `4.5`) | **`14.75 fps`** (`67.85 ms`, min `5.4`) | **`+0.85 fps`** (`-4.15 ms`) | | Preparing / GPU / Copyback | `32.1 / 0.0 / 13.1 ms` | **`30.7 / 0.0 / 11.7 ms`** | **`-1.4 / 0.0 / -1.4 ms`** | | `ahead.part(14)` (heaps & sweep) | `6.2–10.6 ms` (mean `7.84 ms`) | **`1.5–2.2 ms`** (mean **`1.70 ms`**) | **`-6.14 ms/frame` (`-78%`)** | | VRAM max / budget min (`over`) | `6833 / 6724 MB` (`over=96 MB`, `49` blocks) | **`6695 / 6732 MB`** (**`over=0 MB`**, `45` blocks) | **`-138 MB` (`over=0 MB`)** | | Pool free / given back | `626 MB` free / `0` given back | **`308 MB` free / `1` given back** | **`-318 MB` free waste** | | Texture queue (`uploaded` / `final_waiting`) | `38304` / `15453` | **`40641` / `14297`** | **`+2337` / `-1156`** | | Crashes / Untranslated | `0` / `6` | **`0` / `6`** | Clean |
github-bot locked as GitHub sync and limited conversation to collaborators 2026-12-01 20:25:09 +00:00
github manually merged commit 15b4351217 into gh-pr/278/base 2026-12-01 20:25:13 +00:00
Author
Owner

@⁠Androtim on GitHub · 2026-12-01 08:04 UTC · original · edited 2026-12-01 09:05 UTC

Automated triage for the maintainers (not an approval).

Verdict: needs-gpu-test · CI (check): passing

Independent review of PR #278 (15b4351217bd). Logic, lock ordering, and unit test coverage look solid, and CI is passing. Because this changes D3D12 host resource release timing (offloading ID3D12Resource drops to a background thread), host/VRAM pressure trim floors, and lock-free descriptor-heap scanning during slot eviction, it warrants a local real-GPU smoke test under memory pressure before merging.

What was verified

  • Async pressure drops & warm floors (crates/victory-gpu/src/host.rs, crates/victory-gpu/src/host/uav_pool.rs, crates/victory-gpu/src/list.rs, crates/victory-gpu/src/shadow.rs):
    • host::drop_aside spawns a single "victory-reaper" background thread via OnceLock and falls back to synchronous drop if thread creation or channel send fails. All pool accounting (ALIVE, UAV_SPARE_STATS, ReadbackStats, IDLE byte totals) is updated synchronously before handing retired objects to drop_aside.
    • sweep() passes residency::critical_commit() into trim_spare_chunks_with, shadow::trim_snapshot_pool, and list::trim_on_pressure, preserving small warm floors under non-critical pressure (16 MiB spare chunks, 8 MiB per list-upload pool, 256 idle lists, 4 / 16 MiB snapshot buffers) while still draining to 0 (64 idle lists) when commit is critical. list_upload_spare_limit matches trim_spare_chunks_with's LIST_UPLOAD_SPARE_MOST / 8 non-critical pressure cap.
    • Note: in shadow::trim_snapshot_pool, the if kept > 0 && total > max_bytes check intentionally retains at least one buffer under non-critical pressure even if that single buffer exceeds 16 MiB, and drains to 0 when critical is true.
  • Lock-free slots_showing + AddressHash (crates/victory-gpu/src/descriptor/host.rs, crates/victory-gpu/src/descriptor.rs, crates/victory-gpu/src/resource.rs):
    • HostHeap::slots_showing_from_raws scans kept_raw with Ordering::Acquire instead of acquiring per-slot kept mutexes, short-circuits on empty raws, and preserves the exact i < count && !sampler && tabled_lately(i) and seen_since(tabled_stamp(i), cutoff, frame) classification.
    • evictable_slots takes &crate::host::ByCopy<usize> directly, avoiding the intermediate HashSet allocation and default SipHash.
  • Single-lock host_written_snapshot (crates/victory-gpu/src/texture.rs, crates/victory-gpu/src/resource.rs):
    • let_go_idle and slot_evict snapshot HOST_WRITTEN before acquiring textures(), avoiding lock nesting. Any texture written by the host concurrently after the snapshot will have a fresh r.host.used timestamp (and slot_evict re-checks long_ago(r.host.used, ...) and Arc::strong_count(host) == 1 under r.host.made), so the snapshot is safe against concurrent writes.
    • Short-circuiting slot_evict on r.host.used first avoids unnecessary descriptor/map lookups for inactive or recently used textures.

Notes for local GPU verification

  • Verify steady-state gameplay under host/VRAM pressure (including VICTORY_SLOT_EVICT=1) confirms no visual corruption, no stuck/leaked host commit from the "victory-reaper" queue, and clean process exit while D3D12 resources are being dropped off-thread.

Generated with Antigravity CLI (agy).

**@⁠Androtim** on GitHub · 2026-12-01 08:04 UTC · [original](https://github.com/Androtim/MiniVI/pull/278#issuecomment-7304842376) · edited 2026-12-01 09:05 UTC <!-- minivi-triage v1 head=15b4351217bde1a47bcd580a8c1e95dcdec167f5 verdict=needs-gpu-test ci=passing at=2026-12-01T09:06:11Z --> Automated triage for the maintainers (not an approval). **Verdict:** `needs-gpu-test` · **CI (`check`):** `passing` Independent review of PR #278 (`15b4351217bd`). Logic, lock ordering, and unit test coverage look solid, and CI is passing. Because this changes D3D12 host resource release timing (offloading `ID3D12Resource` drops to a background thread), host/VRAM pressure trim floors, and lock-free descriptor-heap scanning during slot eviction, it warrants a local real-GPU smoke test under memory pressure before merging. ### What was verified - **Async pressure drops & warm floors (`crates/victory-gpu/src/host.rs`, `crates/victory-gpu/src/host/uav_pool.rs`, `crates/victory-gpu/src/list.rs`, `crates/victory-gpu/src/shadow.rs`):** - `host::drop_aside` spawns a single `"victory-reaper"` background thread via `OnceLock` and falls back to synchronous `drop` if thread creation or channel send fails. All pool accounting (`ALIVE`, `UAV_SPARE_STATS`, `ReadbackStats`, `IDLE` byte totals) is updated synchronously before handing retired objects to `drop_aside`. - `sweep()` passes `residency::critical_commit()` into `trim_spare_chunks_with`, `shadow::trim_snapshot_pool`, and `list::trim_on_pressure`, preserving small warm floors under non-critical pressure (`16 MiB` spare chunks, `8 MiB` per list-upload pool, `256` idle lists, `4` / `16 MiB` snapshot buffers) while still draining to `0` (`64` idle lists) when commit is critical. `list_upload_spare_limit` matches `trim_spare_chunks_with`'s `LIST_UPLOAD_SPARE_MOST / 8` non-critical pressure cap. - Note: in `shadow::trim_snapshot_pool`, the `if kept > 0 && total > max_bytes` check intentionally retains at least one buffer under non-critical pressure even if that single buffer exceeds `16 MiB`, and drains to `0` when `critical` is true. - **Lock-free `slots_showing` + `AddressHash` (`crates/victory-gpu/src/descriptor/host.rs`, `crates/victory-gpu/src/descriptor.rs`, `crates/victory-gpu/src/resource.rs`):** - `HostHeap::slots_showing_from_raws` scans `kept_raw` with `Ordering::Acquire` instead of acquiring per-slot `kept` mutexes, short-circuits on empty `raws`, and preserves the exact `i < count && !sampler && tabled_lately(i)` and `seen_since(tabled_stamp(i), cutoff, frame)` classification. - `evictable_slots` takes `&crate::host::ByCopy<usize>` directly, avoiding the intermediate `HashSet` allocation and default SipHash. - **Single-lock `host_written_snapshot` (`crates/victory-gpu/src/texture.rs`, `crates/victory-gpu/src/resource.rs`):** - `let_go_idle` and `slot_evict` snapshot `HOST_WRITTEN` *before* acquiring `textures()`, avoiding lock nesting. Any texture written by the host concurrently after the snapshot will have a fresh `r.host.used` timestamp (and `slot_evict` re-checks `long_ago(r.host.used, ...)` and `Arc::strong_count(host) == 1` under `r.host.made`), so the snapshot is safe against concurrent writes. - Short-circuiting `slot_evict` on `r.host.used` first avoids unnecessary descriptor/map lookups for inactive or recently used textures. ### Notes for local GPU verification - Verify steady-state gameplay under host/VRAM pressure (including `VICTORY_SLOT_EVICT=1`) confirms no visual corruption, no stuck/leaked host commit from the `"victory-reaper"` queue, and clean process exit while D3D12 resources are being dropped off-thread. --- _Generated with Antigravity CLI (`agy`)._
Author
Owner

@⁠Androtim on GitHub · 2026-12-01 09:22 UTC · original

Architect & Strict-Gate Review (plain --release A/B vs ff6e5925, CI passing, minivi-triage verified on 15b43512):

All strict merge gates pass cleanly with zero new VICTORY_* opt-in toggles and zero visual/shader-path changes:

  • Median FPS: 13.9 -> 14.75 fps (+0.85 fps, -4.15 ms/frame, min 4.5 -> 5.4)
  • Preparing / Copyback: 32.1 -> 30.7 ms (-1.4 ms, ahead.part(14) -78%) / 13.1 -> 11.7 ms (-1.4 ms)
  • VRAM: 6833/6724 MB (over=96 MB) -> 6695/6732 MB (over=0 MB) (-138 MB, room_false 100.0% -> 93.3%, 1 pool block returned, -318 MB free-pool waste)
  • Texture queue: uploaded 38,304 -> 40,641 (+2,337), final_waiting 15,453 -> 14,297 (-1,156)
  • Stability: 0 crashes/DRED, untranslated = 6 -> 6

Squash-merging into main.

**@⁠Androtim** on GitHub · 2026-12-01 09:22 UTC · [original](https://github.com/Androtim/MiniVI/pull/278#issuecomment-7306603372) Architect & Strict-Gate Review (plain `--release` A/B vs `ff6e5925`, CI passing, `minivi-triage` verified on `15b43512`): All strict merge gates pass cleanly with zero new `VICTORY_*` opt-in toggles and zero visual/shader-path changes: - **Median FPS:** `13.9 -> 14.75 fps` (**`+0.85 fps`**, `-4.15 ms/frame`, min `4.5 -> 5.4`) - **Preparing / Copyback:** `32.1 -> 30.7 ms` (**`-1.4 ms`**, `ahead.part(14)` `-78%`) / `13.1 -> 11.7 ms` (**`-1.4 ms`**) - **VRAM:** `6833/6724 MB (over=96 MB)` -> **`6695/6732 MB (over=0 MB)`** (`-138 MB`, `room_false 100.0% -> 93.3%`, `1` pool block returned, `-318 MB` free-pool waste) - **Texture queue:** `uploaded 38,304 -> 40,641` (**`+2,337`**), `final_waiting 15,453 -> 14,297` (**`-1,156`**) - **Stability:** `0` crashes/DRED, `untranslated = 6 -> 6` Squash-merging into `main`.
This discussion has been locked. Commenting is limited to contributors.
No reviewers
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.

Dependencies

No dependencies set

Reference
github/Androtim--MiniVI!278
No description provided.