Skip to content

perf(gpu): reduce TextureQueue lock hold time, TELL fallbacks, and zero-work locks - #797

Merged
trustno1-coder merged 1 commit into
trustno1-coder:mainfrom
Capt-Speed:perf/texture-queue-lock
Nov 28, 2026
Merged

trustno1-coder merged 1 commit into
trustno1-coder:mainfrom
Capt-Speed:perf/texture-queue-lock

Conversation

@Capt-Speed

Copy link
Copy Markdown
Contributor

Motivation

As recorded in #766 (docs/findings.md), the texture queue's lock (TextureQueue in crates/victory-gpu/src/descriptor/host.rs) is the largest remaining lock in outdoor 4K runs (~214-278 ms waited / 5 s, peaks up to 594-666 ms):

  1. due_and_census walks a pointer-chasing BTreeMap<u32, Waiting> of 80-byte entries (including the cold 40-byte Checked tuple) and grows Due vectors from capacity 0 while holding HostHeap::textures's Mutex (41-51 ms held / 5 s, peaks 70-81 ms).
  2. In draw.rs, when concurrent early and frame texture passes snapshot the same (index, write) due list and one finishes a slot (Told::Done(None) -> marks[index] = 0), the sibling pass sees texture_slot_write(index) != Some(write) and falls back to host.texture_slot_checked(index) under queue_site::TELL (~73-84% of all TELL lock acquisitions, up to ~41k locks / 5 s) before executing the entire redundant slot body whose Told is then dropped by TextureQueue::same.
  3. texture_news(), texture_slots_room(), and empty MADE_DUE calls acquire the queue Mutex even when nothing changed or no slots remain after tabled_lately filtering.
  4. descriptor.rs::mirror acquires heap.pending.lock() on every descriptor write even after heap.host (OnceLock) is already published, and copy_simple re-acquires SHADER_HEAPS.read() per slot.

Changes

  • crates/victory-gpu/src/descriptor/host.rs:
    • Replace BTreeMap<u32, Waiting> (80 B/entry) and held: BTreeMap<u32, u64> with a cache-dense Vec<Entry> (32 B/entry, swap-remove on take_out, in-place overwrite on put), O(1) by_slot index reusing existing crate::host::AddressHash (zero new dependencies), and cold side maps checked / mapped / held.
    • Pre-allocate compact Due (again: Vec<(u32, u64, u32)>, fresh: Vec<(u32, u64)>, checked: Vec<Checked>) outside the DUE lock from due_hints; fast-path max == 0 via census; use select_nth_unstable_by_key in Due::ordered to avoid sorting surplus again / fresh entries while preserving exact output order.
    • Extend QueueMarks with lock-free writes (AtomicU64) and has_held (AtomicBool) so texture_news() and texture_slots_room() skip the Mutex in the common case (respecting VICTORY_QUEUE_MARKS=0), and skip empty MADE_DUE locks after tabled_lately filtering. Keep texture_slots_all_due sorted by slot index outside the lock and preserve atomic PAGE_WRITES[3] counting.
    • Add QueueBatch / QueueFlusher::new_unpublished(&mut HostHeap) for initial unpublished Heap::host() pending replay (compile-time restricted to &mut HostHeap so published heaps always commit per-slot under Made::lock and can never resurrect a table-read or stale slot).
  • crates/victory-gpu/src/descriptor.rs:
    • Check heap.host.get() lock-free before heap.pending.lock() in mirror (safe because Heap::host() drains pending before OnceLock publishes).
    • Add mirror_range for copy_simple to hoist SHADER_HEAPS.read() when the copied range overlaps a single heap that fully contains it (falling back to per-slot mirror whenever multiple placed heaps overlap, preserving priority order).
  • crates/victory-gpu/src/draw.rs:
    • When queue_batch() is on and !baking && sync.is_none(), skip slots whose texture_slot_write(index) is already Some(0) (left the queue) before TAKEN, and avoid host.texture_slot_checked(index) on Some(0) in all modes.
    • Increase Tells::BATCH from 16 to 64.
  • Tests:
    • Added 3 TextureQueue / Due::ordered / QueueBatch unit tests (dense_texture_queue_invariants_and_cold_maps, due_ordered_truncation_matches_full_sort, queue_batch_remove_if_never_clobbers_newer_write_and_matches_unbatched).
    • Added 3 regression tests (audit_range_matches_individual_with_aliases, audit_shared_copy_table, audit_shared_copy_stale) covering overlapping placed-heap priority and concurrent table-read / stale-view races during range mirroring.

Verification

Automated

  • cargo test -p victory-gpu --lib --locked audit_regressions -- --test-threads=1: 3 passed, 0 failed
  • cargo test -p victory-gpu --lib --locked descriptor -- --test-threads=1: 69 passed, 0 failed
  • cargo test -p victory-gpu --lib --locked draw -- --test-threads=1: all passed
  • cargo test -p victory-gpu --features diagnostics --lib --locked descriptor::host:: -- --test-threads=1: 55 passed, 0 failed
  • cargo test -p xtask repository_passes_policy --locked: 1 passed, 0 failed
  • cargo clippy (default and --features diagnostics): 0 warnings in changed/added code and tests
  • git apply --check against PR perf(gpu): split VIEW_SHAPES lock, O(1) FxHashMap, and forget() read-check in descriptor.rs #666: clean, no conflicts; Cargo.lock unchanged (--locked).

Real-hardware measurement (RTX 5090 D 32 GB, Ryzen 7 9800X3D, 4K Series X, a4d05e7 baseline)

5-minute outdoor run (identical save snapshot, 120 s VICTORY_KEYS_FILE sequence, identical 51 VICTORY_* switches, 0 errors, normal outdoor textures):

Warm tail (last 20 x 5 s windows) Baseline A (a4d05e7) With this patch (D) Delta
TELL lock acquisitions / 5 s 15,085 912 -94.0%
DUE hold time (due_and_census) / 5 s 41.6 ms (max 70) 13.7 ms (max 32) -67.1%
MADE_DUE lock acquisitions / wait / 5 s 337 / 11.4 ms 91 / 1.6 ms -73.0% / -86.0%
Total TextureQueue lock wait / 5 s 213.6 ms (max 530) 124.2 ms (max 302) -41.9%
Texture-pass slots_taken / 5 s 59,225 41,249 -30.4%

Disclosure on FPS: This D run was taken with background processes leaving ~27 GB free RAM and triggered host memory pressure (physical_free_mib=2651) mid-run, slowing unrelated subsystems (views' lock +21%, copying back +3-4 ms) relative to the fresh-reboot A run; therefore no net FPS delta is claimed here (only the lock-contention and duplicate-work telemetry above). I can follow up with a clean post-reboot A/D frame-time comparison in a comment.

465d6a0eac added a commit to 465d6a0eac/VIctory-NVIDIA-Experiment that referenced this pull request Nov 28, 2026
…HADER_OPT=127 measured in the open world; about 13 fps is the 3070 Ti's spot today

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01SGw6DM2fQu3WKmCUhFxrrM
henriazz10 added a commit to henriazz10/VIctory-NVIDIA that referenced this pull request Nov 28, 2026
@nick0451

nick0451 commented Nov 28, 2026 •

Copy link
Copy Markdown

Tested in the game: RTX 3090 24 GB (driver 610.88), i9-14900K, Windows 11. Release builds, each of #797, #801, #814 and #816 merged on its own onto main at 63b902f.

How: the same frozen save, outdoors on a balcony by the water, about 5,900-6,100 draws a frame (corrected: I first wrote "an interior"). Series S, DLSS Performance, frame generation off, standing still. Each build is started once first as a warm-up (a new build's first start is slow), then the five builds are run in turn, three rounds. Each run is 40 s to settle, then 60 s measured, and the figures are medians of the frame lines. An A/A run of main against itself the same way differed by 2-3%, so smaller differences are noise.

Build fps, 3 runs median frame preparing card busy
main 63b902f 25.5, 24.7, 25.1 25.1 39.8 ms 25.6 ms 95%
#814 25.3, 25.6, 26.4 25.6 39.1 ms 25.1 ms 97%
#801 25.4, 25.8, 25.2 25.4 39.5 ms 25.8 ms 95%
#797 25.3, 25.2, 23.9 25.2 39.7 ms 26.8 ms 95%
#816 24.0, 25.4, 24.2 24.2 41.3 ms 25.0 ms 94%

For this PR: no change in fps here. Preparing is 1.2 ms higher, within noise. The lock you target is the one you measured outdoors at 4K, which this scene, a balcony by the water, does not stress the way a 4K street does.

In this scene the card is about 95% busy, so the frame is the card's. Work saved on the CPU side would show in "preparing" more than in fps.

No crashes, device losses or ERROR lines in any build: about 7 minutes in the world each, warm-up included.

Tested with an AI assistant (Claude Code); results are from this PC.

henriazz10 added a commit to henriazz10/VIctory-NVIDIA that referenced this pull request Nov 28, 2026
An AtomicBool beside HostHeap::away, set and cleared under its lock, lets
texture_slot_here return at once in the usual case of an empty map.

Based on trustno1-coder#1242 by @qlfu (its other two parts,
the dense texture queue and Tells batches of 64, are already in this branch
through trustno1-coder#797).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@trustno1-coder
trustno1-coder merged commit c0fb0be into trustno1-coder:main Nov 28, 2026
krolyxon pushed a commit to krolyxon/VIctory-NVIDIA-proton 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
An AtomicBool beside HostHeap::away, set and cleared under its lock, lets
texture_slot_here return at once in the usual case of an empty map.

Based on trustno1-coder#1242 by @qlfu (its other two parts,
the dense texture queue and Tells batches of 64, are already in this branch
through trustno1-coder#797).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
(cherry picked from commit c92ddc2)
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