perf(gpu): split VIEW_SHAPES lock, O(1) FxHashMap, and forget() read-check in descriptor.rs #666

Merged
Capt-Speed merged 1 commit from upstream-pr/666 into main 2026-11-29 08:16:59 +00:00
Capt-Speed commented 2026-11-28 03:07:42 +00:00 (Migrated from github.com)

Imported from trustno1-coder/VIctory-NVIDIA. Originally posted by Capt-Speed on 2026-11-28T03:07:42Z.


Summary

Single-file lock-contention and lookup optimization in crates/victory-gpu/src/descriptor.rs (zero new crate dependencies, zero shader-cache impact):

  1. Split VIEW_SHAPES out of the global VIEWS RwLock: Placed-view shapes (shapes + shape_index) are append-only and read heavily across concurrent recording threads. Moving them to their own VIEW_SHAPES RwLock stops frequent PLACED lookups from blocking behind VIEWS insertions and forget(resource) writes.
  2. Replace BTreeMap with a zero-dependency 64-bit FxHasher HashMap (FastMap) for Views::index, Views::by_resource, and ViewShapes::shape_index, reducing lock hold times from O(log n) comparisons over large view structs to O(1).
  3. Add a read-lock fast path in forget(resource): check views_read().by_resource.contains_key(&resource) before taking the exclusive write lock, avoiding unnecessary write-lock stalls when releasing resources that had no registered views.

All existing #58 placed-view padding clearing and ownerless cap behavior are preserved unchanged.

Why

Even after #58 eliminated placed-view padding leaks, VIEWS still guarded both placed-shape lookups and resource view registration/release under one process-wide RwLock backed by BTreeMap. In heavy outdoor scenes (7,000–10,000+ draws/frame), the views' lock wait still averaged ~38–40 ms per 5-second interval (with spikes up to 147 ms).

Measurements

Tested on Ryzen 7 9800X3D + NVIDIA RTX 5090 D (32 GB), Windows 11, --release, Series X 4K (3840x2160), DLSS Balanced (Preset K), DLSS Frame Generation 4x, Jason's House outdoor save (#573), built from a4d05e7 (which already includes #58):

  • All 51 VICTORY_* switches identical between baseline (main) and patched (pr-view-shapes).
  • Both binaries copied onto the same victory.exe path before launch (shared NVIDIA DXCache; 4,010 gameplay pipelines pre-warmed via victory warm --used + 25,758 shaders pre-translated).
  • Golden save snapshot restored before every run; identical 120 s deterministic VICTORY_KEYS_FILE camera/walk sequence.

1. Direct lock contention (the views' lock)

Window Baseline (main a4d05e7) Patched (VIEW_SHAPES + FastMap) Delta
Full gameplay (36×5s vs 33×5s) 40.5 ms / 5s (total 1,458 ms, max 147 ms) 20.0 ms / 5s (total 661 ms, max 83 ms) -50.6%
Pure-warm tail (last 20×5s = 100 s) 38.3 ms / 5s (total 766 ms, max 96 ms) 18.4 ms / 5s (total 369 ms, max 83 ms) -52.0%

(Control check: a 3rd run on the same warm 36 MB host-pipelines.bin without this patch still showed 29.9 ms / 5s warm / 35.2 ms / 5s full views' lock wait, confirming the ~50% lock reduction is from this change rather than cache warmth.)

2. Gameplay frame time & native FPS (strictly filtered clean ticks)

Transparency note: Baseline ran first while host-pipelines.bin grew from 17 KB to 35.5 MB, whereas the patched run loaded the saved 35.5 MB library on start. To avoid attributing any first-run library-save overhead to this patch, the table below skips the first 5 ticks and excludes every tick within ±10 s of a host-pipelines.bin save or with gpu >= 1.0 ms (both runs had 0 pipeline waits >= 50 ms).

Strict clean bucket (gpu < 1 ms, no library save within ±10 s) Baseline (main) Patched (VIEW_SHAPES + FastMap) Delta
All clean ticks (avg 7,513 vs 7,517 draws; n=15 vs n=19) 20.15 FPS / 51.1 ms (med 18.60) 21.78 FPS / 47.7 ms (med 19.70) +8.1% FPS / -3.4 ms (-6.7%)
Heavy draws >= 9,000 (avg 9,642 vs 9,542; n=8 vs n=11) 18.16 FPS / 55.3 ms (max 62.6 ms) 19.06 FPS / 52.5 ms (max 57.4 ms) +5.0% FPS / -2.8 ms (-5.1%)
Medium-heavy 7,000–8,999 (n=2 vs n=3) 17.60 FPS / 56.9 ms 18.77 FPS / 53.4 ms +6.6% FPS / -3.5 ms (-6.2%)

Validation

  • Rebased cleanly onto main (8fc2254): 1 file changed (crates/victory-gpu/src/descriptor.rs, +132/-34).
  • cargo test --lib -p victory-gpu descriptor: 62 passed; 0 failed; 1 ignored (including ownerless_entries_are_capped, buffer_views_differing_in_padding_are_one_shape_and_offsets_stay_apart, views_at_new_addresses_add_no_entries, views_die_with_their_resource).
  • Zero clippy warnings in changed code in both default and --features diagnostics builds.
<!-- greyfinch-sync:trustno1-coder/VIctory-NVIDIA:issue:5536082439 --> Imported from [trustno1-coder/VIctory-NVIDIA](https://github.com/trustno1-coder/VIctory-NVIDIA/pull/666). Originally posted by [Capt-Speed](https://github.com/Capt-Speed) on 2026-11-28T03:07:42Z. --- ## Summary Single-file lock-contention and lookup optimization in `crates/victory-gpu/src/descriptor.rs` (zero new crate dependencies, zero shader-cache impact): 1. **Split `VIEW_SHAPES` out of the global `VIEWS` `RwLock`**: Placed-view shapes (`shapes` + `shape_index`) are append-only and read heavily across concurrent recording threads. Moving them to their own `VIEW_SHAPES` `RwLock` stops frequent `PLACED` lookups from blocking behind `VIEWS` insertions and `forget(resource)` writes. 2. **Replace `BTreeMap` with a zero-dependency 64-bit `FxHasher` `HashMap` (`FastMap`)** for `Views::index`, `Views::by_resource`, and `ViewShapes::shape_index`, reducing lock hold times from `O(log n)` comparisons over large view structs to `O(1)`. 3. **Add a read-lock fast path in `forget(resource)`**: check `views_read().by_resource.contains_key(&resource)` before taking the exclusive write lock, avoiding unnecessary write-lock stalls when releasing resources that had no registered views. All existing `#58` placed-view padding clearing and `ownerless` cap behavior are preserved unchanged. ## Why Even after `#58` eliminated placed-view padding leaks, `VIEWS` still guarded both placed-shape lookups and resource view registration/release under one process-wide `RwLock` backed by `BTreeMap`. In heavy outdoor scenes (`7,000–10,000+` draws/frame), `the views' lock` wait still averaged ~`38–40 ms` per 5-second interval (with spikes up to `147 ms`). ## Measurements Tested on **Ryzen 7 9800X3D + NVIDIA RTX 5090 D (32 GB)**, Windows 11, `--release`, Series X 4K (`3840x2160`), DLSS Balanced (Preset K), DLSS Frame Generation 4x, **Jason's House outdoor save (`#573`)**, built from `a4d05e7` (which already includes `#58`): - All **51 `VICTORY_*` switches** identical between baseline (`main`) and patched (`pr-view-shapes`). - Both binaries copied onto the same `victory.exe` path before launch (shared NVIDIA DXCache; `4,010` gameplay pipelines pre-warmed via `victory warm --used` + `25,758` shaders pre-translated). - Golden save snapshot restored before every run; identical 120 s deterministic `VICTORY_KEYS_FILE` camera/walk sequence. ### 1. Direct lock contention (`the views' lock`) | Window | Baseline (`main` `a4d05e7`) | Patched (`VIEW_SHAPES` + `FastMap`) | Delta | | --- | ---: | ---: | ---: | | Full gameplay (`36×5s` vs `33×5s`) | `40.5 ms / 5s` (total `1,458 ms`, max `147 ms`) | **`20.0 ms / 5s`** (total `661 ms`, max `83 ms`) | **`-50.6%`** | | Pure-warm tail (last `20×5s` = 100 s) | `38.3 ms / 5s` (total `766 ms`, max `96 ms`) | **`18.4 ms / 5s`** (total `369 ms`, max `83 ms`) | **`-52.0%`** | *(Control check: a 3rd run on the same warm `36 MB` `host-pipelines.bin` without this patch still showed `29.9 ms / 5s` warm / `35.2 ms / 5s` full `views' lock` wait, confirming the ~50% lock reduction is from this change rather than cache warmth.)* ### 2. Gameplay frame time & native FPS (strictly filtered clean ticks) > **Transparency note:** Baseline ran first while `host-pipelines.bin` grew from `17 KB` to `35.5 MB`, whereas the patched run loaded the saved `35.5 MB` library on start. To avoid attributing any first-run library-save overhead to this patch, the table below skips the first 5 ticks and excludes every tick within `±10 s` of a `host-pipelines.bin` save or with `gpu >= 1.0 ms` (both runs had `0` pipeline waits `>= 50 ms`). | Strict clean bucket (`gpu < 1 ms`, no library save within `±10 s`) | Baseline (`main`) | Patched (`VIEW_SHAPES` + `FastMap`) | Delta | | --- | ---: | ---: | ---: | | All clean ticks (avg `7,513` vs `7,517` draws; `n=15` vs `n=19`) | `20.15 FPS` / `51.1 ms` (med `18.60`) | **`21.78 FPS` / `47.7 ms`** (med `19.70`) | **`+8.1% FPS` / `-3.4 ms` (`-6.7%`)** | | Heavy `draws >= 9,000` (avg `9,642` vs `9,542`; `n=8` vs `n=11`) | `18.16 FPS` / `55.3 ms` (max `62.6 ms`) | **`19.06 FPS` / `52.5 ms`** (max `57.4 ms`) | **`+5.0% FPS` / `-2.8 ms` (`-5.1%`)** | | Medium-heavy `7,000–8,999` (`n=2` vs `n=3`) | `17.60 FPS` / `56.9 ms` | **`18.77 FPS` / `53.4 ms`** | **`+6.6% FPS` / `-3.5 ms` (`-6.2%`)** | ## Validation - Rebased cleanly onto `main` (`8fc2254`): 1 file changed (`crates/victory-gpu/src/descriptor.rs`, `+132/-34`). - `cargo test --lib -p victory-gpu descriptor`: `62 passed; 0 failed; 1 ignored` (including `ownerless_entries_are_capped`, `buffer_views_differing_in_padding_are_one_shape_and_offsets_stay_apart`, `views_at_new_addresses_add_no_entries`, `views_die_with_their_resource`). - Zero clippy warnings in changed code in both default and `--features diagnostics` builds.
Dukestar69 commented 2026-11-28 05:37:44 +00:00 (Migrated from github.com)

Imported from trustno1-coder/VIctory-NVIDIA. Originally posted by Dukestar69 on 2026-11-28T05:37:44Z.

Review: APPROVED


LGTM — verified on Windows (RTX 5090):

  • cargo test -p victory-gpu --lib descriptor:: passed (62/62) on RTX 5090
  • 0 new compiler warnings in touched files
  • cargo xtask policy passed (294 files ok)

Synthetic tests only (no game files used). AI-assisted (Gemini 4 Neon).

<!-- greyfinch-sync:trustno1-coder/VIctory-NVIDIA:review:6096081908 --> Imported from [trustno1-coder/VIctory-NVIDIA](https://github.com/trustno1-coder/VIctory-NVIDIA/pull/666#pullrequestreview-6096081908). Originally posted by [Dukestar69](https://github.com/Dukestar69) on 2026-11-28T05:37:44Z. **Review:** APPROVED --- LGTM — verified on Windows (RTX 5090): - `cargo test -p victory-gpu --lib descriptor::` passed (62/62) on RTX 5090 - 0 new compiler warnings in touched files - `cargo xtask policy` passed (`294 files ok`) Synthetic tests only (no game files used). AI-assisted (Gemini 4 Neon).
POMXARK commented 2026-11-28 06:14:33 +00:00 (Migrated from github.com)

Imported from trustno1-coder/VIctory-NVIDIA. Originally posted by POMXARK on 2026-11-28T06:14:33Z.


Tested this PR in a controlled 20-minute 3D A/B run on Windows 11 — PASS (clean stability + lower Prep / heap time on >=4k draw workloads, 0 regressions).

Setup

  • Hardware: AMD Ryzen 7 5700X (8C/16T), NVIDIA RTX 5080 16 GB, 32 GB RAM, Windows 11.
  • Profile / flags: Series X 1440p, DLSS Quality + DLSS MFG 4x (tearing=true), default 4 MiB windows, VICTORY_GPU_BCPACK=0, VICTORY_WAIT_RESIDENT=0, VICTORY_TEXTURE_ROOM_PERCENT=70, VICTORY_LAND_SKIP=1.
  • Base: Local fork based on v2026.11.27.10 (8fc2254) + AVX2/ThinLTO + PR #516 + PR #340 + PR #81 (d354ec4). PR #666 applied cleanly with zero conflicts.
  • Unit tests: cargo test --locked -p victory-gpu --lib -- descriptor:: --test-threads=1 (VICTORY_D3D12_WARP=1) — 64/64 passed (0.84 s).

20-Minute 3D A/B Results (Strict 3D-only, Draw-Count-Matched)

Metric Baseline (d354ec4, 20m) + PR #666 (20m) Delta
Errors / DevRem / Exceptions 0 / 0 / 0 0 / 0 / 0 Clean
Responding (hw.csv) 325 / 327 (99.4%) 316 / 316 (100.0%) 100%
GPU>=50ms / F>=250ms / F>=500ms 1 / 1 / 1 0 / 2 / 0 0 spikes >=500ms
Worst 3D frame (20m max) 2789.9 ms 397.9 ms -85.7%
ALL 3D (n=162 / 172) 13.7 p50 / 13.68 mean (Prep 59.5 ms) 13.4 p50 / 13.67 mean (Prep 57.9 ms) Flat FPS, Prep -1.6 ms
3D frame_times (1s lines) 65.0 med / 86.1 p95 / 98.3 p99 ms 65.4 med / 87.5 p95 / 101.0 p99 ms Flat (~0.6%)
1k–4k draws/frame (n=103 / 105) 17.70 p50 / 17.20 mean (Prep 44.7) 17.00 p50 / 17.03 mean (Prep 47.6) Within noise
4k–7k draws/frame (n=57 / 63) 12.70 p50 / 12.95 mean (Prep 64.0, heap 14.5) 12.50 p50 / 13.34 mean (Prep 58.2, heap 12.5) Mean +3.0%, Prep -5.8 ms
7k–10k draws/frame (n=24 / 26) 10.75 p50 (Prep 75.9, heap 14.7) 10.25 p50 (Prep 74.6, heap 10.8) heap -3.9 ms (copies slightly higher in late streaming)
>=10k draws/frame (n=3 / 3) 8.80 p50 (Prep 96.0 ms) 9.30 p50 (Prep 81.8 ms) +5.7%, Prep -14.2 ms

Takeaways

  • Lock-ordering audit looks safe (views_kept() takes views_read() then shapes_read(); no other path nests both locks).
  • Clean 20-minute run with 0 rendering anomalies, 100% window responsiveness, 0 frames >=500 ms, and consistent Prep / heap reductions on medium-to-heavy draw-count workloads (4k+ draws/frame). Overall FPS stays mostly flat because CPU copy work (copies 30–52 ms) remains the dominant bottleneck on this 8C/16T CPU.
<!-- greyfinch-sync:trustno1-coder/VIctory-NVIDIA:comment:7235170181 --> Imported from [trustno1-coder/VIctory-NVIDIA](https://github.com/trustno1-coder/VIctory-NVIDIA/pull/666#issuecomment-7235170181). Originally posted by [POMXARK](https://github.com/POMXARK) on 2026-11-28T06:14:33Z. --- Tested this PR in a controlled 20-minute 3D A/B run on Windows 11 — **PASS (clean stability + lower `Prep` / `heap` time on >=4k draw workloads, 0 regressions)**. ### Setup - **Hardware:** AMD Ryzen 7 5700X (8C/16T), NVIDIA RTX 5080 16 GB, 32 GB RAM, Windows 11. - **Profile / flags:** Series X 1440p, DLSS Quality + DLSS MFG 4x (`tearing=true`), default `4 MiB` windows, `VICTORY_GPU_BCPACK=0`, `VICTORY_WAIT_RESIDENT=0`, `VICTORY_TEXTURE_ROOM_PERCENT=70`, `VICTORY_LAND_SKIP=1`. - **Base:** Local fork based on `v2026.11.27.10` (`8fc2254`) + AVX2/ThinLTO + PR #516 + PR #340 + PR #81 (`d354ec4`). PR #666 applied cleanly with zero conflicts. - **Unit tests:** `cargo test --locked -p victory-gpu --lib -- descriptor:: --test-threads=1` (`VICTORY_D3D12_WARP=1`) — **64/64 passed** (`0.84 s`). ### 20-Minute 3D A/B Results (Strict 3D-only, Draw-Count-Matched) | Metric | Baseline (`d354ec4`, 20m) | + PR #666 (20m) | Delta | | :--- | ---: | ---: | ---: | | **Errors / DevRem / Exceptions** | `0 / 0 / 0` | `0 / 0 / 0` | Clean | | **Responding (`hw.csv`)** | `325 / 327` (`99.4%`) | `316 / 316` (`100.0%`) | `100%` | | **GPU>=50ms / F>=250ms / F>=500ms** | `1 / 1 / 1` | `0 / 2 / 0` | `0` spikes >=500ms | | **Worst 3D frame (20m max)** | `2789.9 ms` | `397.9 ms` | **-85.7%** | | **ALL 3D** (`n=162 / 172`) | `13.7` p50 / `13.68` mean (`Prep 59.5 ms`) | `13.4` p50 / `13.67` mean (`Prep 57.9 ms`) | Flat FPS, **`Prep -1.6 ms`** | | **3D `frame_times` (1s lines)** | `65.0` med / `86.1` p95 / `98.3` p99 ms | `65.4` med / `87.5` p95 / `101.0` p99 ms | Flat (~0.6%) | | **1k–4k draws/frame** (`n=103 / 105`) | `17.70` p50 / `17.20` mean (`Prep 44.7`) | `17.00` p50 / `17.03` mean (`Prep 47.6`) | Within noise | | **4k–7k draws/frame** (`n=57 / 63`) | `12.70` p50 / `12.95` mean (`Prep 64.0`, `heap 14.5`) | `12.50` p50 / `13.34` mean (`Prep 58.2`, `heap 12.5`) | Mean `+3.0%`, **`Prep -5.8 ms`** | | **7k–10k draws/frame** (`n=24 / 26`) | `10.75` p50 (`Prep 75.9`, `heap 14.7`) | `10.25` p50 (`Prep 74.6`, `heap 10.8`) | **`heap -3.9 ms`** (`copies` slightly higher in late streaming) | | **>=10k draws/frame** (`n=3 / 3`) | `8.80` p50 (`Prep 96.0 ms`) | `9.30` p50 (`Prep 81.8 ms`) | **`+5.7%`**, `Prep -14.2 ms` | ### Takeaways - Lock-ordering audit looks safe (`views_kept()` takes `views_read()` then `shapes_read()`; no other path nests both locks). - Clean 20-minute run with `0` rendering anomalies, `100%` window responsiveness, `0` frames >=500 ms, and consistent `Prep` / `heap` reductions on medium-to-heavy draw-count workloads (`4k+` draws/frame). Overall FPS stays mostly flat because CPU copy work (`copies 30–52 ms`) remains the dominant bottleneck on this 8C/16T CPU.
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
trustno1-coder/VIctory-NVIDIA!666
No description provided.