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

Manually merged
github merged 1 commit from github-bot/gh-797 into main 2026-11-28 19:42:46 +00:00 AGit
Owner

@⁠Capt-Speed on GitHub · 2026-11-28 06:12 UTC · original
Capt-Speed:perf/texture-queue-lock → main · +1073 −139 · 3 files · 1 commits · merged 2026-11-28 19:39 UTC

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 #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.

**@⁠Capt-Speed** on GitHub · 2026-11-28 06:12 UTC · [original](https://github.com/trustno1-coder/VIctory-NVIDIA/pull/797) `Capt-Speed:perf/texture-queue-lock` → `main` · +1073 −139 · 3 files · 1 commits · merged 2026-11-28 19:39 UTC ## 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 #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.
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-28 06:18:05 +00:00
Author
Owner

@⁠nick0451 on GitHub · 2026-11-28 09:21 UTC · original · edited 2026-11-28 10:00 UTC

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.

**@⁠nick0451** on GitHub · 2026-11-28 09:21 UTC · [original](https://github.com/trustno1-coder/VIctory-NVIDIA/pull/797#issuecomment-7237188681) · edited 2026-11-28 10:00 UTC 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.
github-bot referenced this pull request from a commit 2026-11-28 19:41:56 +00:00
github manually merged commit c0fb0be151 into main 2026-11-28 19:42:46 +00:00
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/trustno1-coder--VIctory-NVIDIA!797
No description provided.