Skip to content

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

Open
Capt-Speed wants to merge 1 commit into
trustno1-coder:mainfrom
Capt-Speed:pr-view-shapes
Open

Capt-Speed wants to merge 1 commit into
trustno1-coder:mainfrom
Capt-Speed:pr-view-shapes

Conversation

@Capt-Speed

Copy link
Copy Markdown
Contributor

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.

LJGhost pushed a commit to LJGhost/VIctory-NVIDIA that referenced this pull request Nov 28, 2026
…-probe-target-dir

Loader tests: build the probe fixture under CARGO_TARGET_TMPDIR
genzsekiro pushed a commit to genzsekiro/VIctory-NVIDIA-RT that referenced this pull request Nov 28, 2026
…-probe-target-dir

Loader tests: build the probe fixture under CARGO_TARGET_TMPDIR
genzsekiro pushed a commit to genzsekiro/VIctory-NVIDIA-RT that referenced this pull request Nov 28, 2026
…-probe-target-dir

Loader tests: build the probe fixture under CARGO_TARGET_TMPDIR

@Dukestar69 Dukestar69 left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

POMXARK commented Nov 28, 2026

Copy link
Copy Markdown

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

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.

MystifiedSky added a commit to MystifiedSky/VIctory-NVIDIA-Improved that referenced this pull request Nov 28, 2026
whoalin1 added a commit to whoalin1/victory-altracing-prs that referenced this pull request Nov 28, 2026
…s, PRs trustno1-coder#697/trustno1-coder#614/trustno1-coder#700/trustno1-coder#666

- upload.rs: set default VICTORY_LOW_EARLY_MB=0 (restores bright bedroom & cuts stray faults while retaining explicit override + trustno1-coder#88 arena backoff)

- host/panel.rs & shadow::register: port Spaghetih/victory-sley-fork 6d813b4 (panel COPY_SOURCE->COPY_DEST barrier via uploaded flag; retire replaced shadow entry)

- descriptor.rs: port PR trustno1-coder#666 (FxHasher FastMap, split VIEW_SHAPES RwLock, forget early-out)

- watch.rs/host.rs/shadow.rs: port PR trustno1-coder#697 (per-range ask holds), PR trustno1-coder#614 (lock-free atomic shadow copied + ReadJournal + wide_chunks + word_runs + deferred coalescing), PR trustno1-coder#700 (keep 4 MiB chunk in scratch_any_size)

- shadow::land_tests: drop JournalRead before calling watch::nothing_new to prevent Windows SRWLock recursive read deadlock
trustno1-coder pushed a commit that referenced this pull request Nov 28, 2026
krolyxon pushed a commit to krolyxon/VIctory-NVIDIA-proton that referenced this pull request Nov 28, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants