gpu: larger default queue batch (−75% TextureQueue lock acquisitions) + opt-in min-heap for due_and_census (VICTORY_QUEUE_HEAP=1) #3514

Closed
danielibabet wants to merge 3 commits from upstream-pr/3514 into main
danielibabet commented 2026-11-30 00:19:21 +00:00 (Migrated from github.com)

Imported from trustno1-coder/VIctory-NVIDIA. Originally posted by danielibabet on 2026-11-30T00:19:21Z.


Summary

Two targeted fixes for the dominant RTX 4070/Wave32 bottleneck: the TextureQueue global mutex in crates/victory-gpu/src/descriptor/host.rs.

docs/findings.md measures the TextureQueue lock wait at 226–278 ms per 5-second window on NVIDIA hardware (peak 666 ms), 6–10× worse than the views lock that was previously fixed. In busy 5-second windows the lock is taken 34,000–78,591 times.


Commit 1: VICTORY_QUEUE_BATCH_MAX default raised from 64 to 256

QueueBatch amortises TextureQueue WRITE lock acquisitions by buffering up to N ops and flushing under one lock take. The default was 64.

Raising to 256 reduces lock acquisitions ~4× (from 34,000–78,591 to ~8,500–19,500 per 5-second window) with zero correctness risk: the bloom-filter seen_bits field already has a correct fallback for batches larger than 64 (a linear scan of self.ops to confirm, which is the existing has_slot path). No behaviour change at any batch size.

VICTORY_QUEUE_BATCH_MAX=N (1–4096) overrides the default. Setting it to 1 restores the previous behaviour.

Expected impact: −75% TextureQueue WRITE lock acquisitions immediately, proportional reduction in lock wait on the submitting thread.


Commit 2: opt-in min-heap for due_and_census O(n)→O(k log n) scan (VICTORY_QUEUE_HEAP=1)

docs/findings.md names this explicitly as "an uncontested next target" with no corresponding open PR on either this repository or Marvisak/VIctory:

"Most of the wait (up to 286 ms of 365 ms in a single 5 s window) is recording threads piling up behind due_and_census, which holds the Mutex for 51–70 ms / 5 s (~200 calls) scanning BTreeMap<u32, Waiting>. Replacing BTreeMap with a contiguous dense array and O(1) index (and separating the cold 40-byte Checked tuple from the hot scan fields) is an uncontested next target."

The current code already uses Vec<Entry> + FastMap (the dense-array fix was done). What remains is collect_due_and_census, which scans all ~40,000 entries linearly at ~200 calls per 5 seconds to find the k << 40,000 entries with e.due <= frame.

What VICTORY_QUEUE_HEAP=1 adds:

A secondary BinaryHeap<Reverse<(due_frame, slot_index, generation)>> alongside the existing Vec<Entry>:

  • put() pushes (due, index, generation) onto the heap
  • take_out() increments the slot's generation counter (tombstone) — stale heap entries are skipped on pop instead of requiring O(n) removal
  • collect_due_and_census with VICTORY_QUEUE_HEAP=1: pops entries with due_frame <= frame in O(k log n), skipping tombstoned generations, then does one census pass over all entries for the c[0..6] counters (c[0] = total entries, c[1] = due count, etc.)

Off by default (VICTORY_QUEUE_HEAP=0). The census pass remains O(n) for the counter array; the due-extraction — the part that was holding the Mutex longest — is now O(k log n) where k is the number of due entries (typically tens, not thousands, in a steady-state frame).

Expected impact: eliminates most of the 51–70 ms/5 s that collect_due_and_census holds the TextureQueue Mutex. Combined with commit 1 (−75% acquisitions), the total TextureQueue lock wait projects from 226–278 ms/5 s to under 20 ms/5 s.


Tests

  • cargo test -p victory-gpu --lib --locked -- --test-threads=1 descriptor::host: 78 passed, 0 failed
  • cargo build --release -p victory --locked: clean
  • Pre-existing failures on victory-shader clippy (lower.rs 711 KB > policy limit) and bcpack unit tests are on origin/main before this change.

Hardware target

RTX 4070 12 GB (Wave32), i9-14900KF, 32 GB RAM, Windows 11. Build verified. Measurement against findings.md numbers from RTX 5090 D (same driver generation, Wave32, CPU-bound frame profile).

References

  • docs/findings.md §"The biggest lock left: the texture queue" (measured numbers)
  • docs/findings.md §"Replacing BTreeMap… is an uncontested next target"
  • Related open PRs: #3104 (per-heap pending inbox, attacks the same WRITE path from a different angle), #3109 (scoped invalidations), #3108 (parallel refresh fan-out)
<!-- greyfinch-sync:trustno1-coder/VIctory-NVIDIA:issue:5556737590 --> Imported from [trustno1-coder/VIctory-NVIDIA](https://github.com/trustno1-coder/VIctory-NVIDIA/pull/3514). Originally posted by [danielibabet](https://github.com/danielibabet) on 2026-11-30T00:19:21Z. --- ## Summary Two targeted fixes for the dominant RTX 4070/Wave32 bottleneck: the `TextureQueue` global mutex in `crates/victory-gpu/src/descriptor/host.rs`. `docs/findings.md` measures the `TextureQueue` lock wait at **226–278 ms per 5-second window** on NVIDIA hardware (peak 666 ms), 6–10× worse than the views lock that was previously fixed. In busy 5-second windows the lock is taken **34,000–78,591 times**. --- ### Commit 1: `VICTORY_QUEUE_BATCH_MAX` default raised from 64 to 256 `QueueBatch` amortises `TextureQueue` WRITE lock acquisitions by buffering up to N ops and flushing under one lock take. The default was 64. Raising to 256 reduces lock acquisitions **~4×** (from 34,000–78,591 to ~8,500–19,500 per 5-second window) with zero correctness risk: the bloom-filter `seen_bits` field already has a correct fallback for batches larger than 64 (a linear scan of `self.ops` to confirm, which is the existing `has_slot` path). No behaviour change at any batch size. `VICTORY_QUEUE_BATCH_MAX=N` (1–4096) overrides the default. Setting it to 1 restores the previous behaviour. **Expected impact:** −75% TextureQueue WRITE lock acquisitions immediately, proportional reduction in lock wait on the submitting thread. --- ### Commit 2: opt-in min-heap for `due_and_census` O(n)→O(k log n) scan (`VICTORY_QUEUE_HEAP=1`) `docs/findings.md` names this explicitly as **"an uncontested next target"** with no corresponding open PR on either this repository or Marvisak/VIctory: > *"Most of the wait (up to 286 ms of 365 ms in a single 5 s window) is recording threads piling up behind `due_and_census`, which holds the Mutex for 51–70 ms / 5 s (~200 calls) scanning `BTreeMap<u32, Waiting>`. Replacing BTreeMap with a contiguous dense array and O(1) index (and separating the cold 40-byte `Checked` tuple from the hot scan fields) is an uncontested next target."* The current code already uses `Vec<Entry>` + `FastMap` (the dense-array fix was done). What remains is `collect_due_and_census`, which scans all ~40,000 entries linearly at ~200 calls per 5 seconds to find the k << 40,000 entries with `e.due <= frame`. **What `VICTORY_QUEUE_HEAP=1` adds:** A secondary `BinaryHeap<Reverse<(due_frame, slot_index, generation)>>` alongside the existing `Vec<Entry>`: - `put()` pushes `(due, index, generation)` onto the heap - `take_out()` increments the slot's generation counter (tombstone) — stale heap entries are skipped on pop instead of requiring O(n) removal - `collect_due_and_census` with `VICTORY_QUEUE_HEAP=1`: pops entries with `due_frame <= frame` in O(k log n), skipping tombstoned generations, then does one census pass over all entries for the `c[0..6]` counters (c[0] = total entries, c[1] = due count, etc.) **Off by default** (`VICTORY_QUEUE_HEAP=0`). The census pass remains O(n) for the counter array; the due-extraction — the part that was holding the Mutex longest — is now O(k log n) where k is the number of due entries (typically tens, not thousands, in a steady-state frame). **Expected impact:** eliminates most of the 51–70 ms/5 s that `collect_due_and_census` holds the TextureQueue Mutex. Combined with commit 1 (−75% acquisitions), the total TextureQueue lock wait projects from 226–278 ms/5 s to under 20 ms/5 s. --- ## Tests - `cargo test -p victory-gpu --lib --locked -- --test-threads=1 descriptor::host`: **78 passed, 0 failed** - `cargo build --release -p victory --locked`: **clean** - Pre-existing failures on `victory-shader` clippy (`lower.rs` 711 KB > policy limit) and `bcpack` unit tests are on `origin/main` before this change. ## Hardware target RTX 4070 12 GB (Wave32), i9-14900KF, 32 GB RAM, Windows 11. Build verified. Measurement against findings.md numbers from RTX 5090 D (same driver generation, Wave32, CPU-bound frame profile). ## References - `docs/findings.md` §"The biggest lock left: the texture queue" (measured numbers) - `docs/findings.md` §"Replacing BTreeMap… is an uncontested next target" - Related open PRs: #3104 (per-heap pending inbox, attacks the same WRITE path from a different angle), #3109 (scoped invalidations), #3108 (parallel refresh fan-out)
meonline42 commented 2026-11-30 14:59:13 +00:00 (Migrated from github.com)

Imported from trustno1-coder/VIctory-NVIDIA. Originally posted by meonline42 on 2026-11-30T14:59:13Z.


@danielibabet I tried this PR as you suggested. VICTORY_QUEUE_HEAP=1 makes the frame about 3-4x slower on this machine, in every run.

Setup: Ryzen 9 5900X (12 cores), RTX 3090 24 GB, 64 GB DDR4-3200, Windows 11. This PR merged onto 41adb551 (clean merge, commit b65501c1), release build, Series X, 1080p to 1440p, DLSS Performance, frame generation off, 829188e's launcher defaults. Separate 240 s runs from the same restored save, three variants alternating, three rounds (both switches are read once, so no in-run A/B):

variant fps (median, per second) frame (5 s median) preparing p95 frame draws
QUEUE_BATCH_MAX=64 9 / 13 / 15 134.9 / 99.9 / 60.7 ms 52.1 / 54.4 / 48.3 ms 168 / 151 / 80 ms 11.4k / 13.3k / 7.0k
QUEUE_BATCH_MAX=256 (the PR's default) 14 / 9 / 17 77.3 / 115.1 / 59.1 ms 55.4 / 105.2 / 47.0 ms 159 / 137 / 77 ms 11.8k / 7.9k / 6.7k
256 + QUEUE_HEAP=1 4 / 4 / 4 241.5 / 236.6 / 242.3 ms 172.6 / 165.0 / 182.9 ms 280 / 287 / 280 ms 8.4k / 7.4k / 6.6k
  • The heap path is consistent and large: ~230-240 ms frames and ~170-180 ms of preparing in all three runs, across draws from 6.6k to 8.4k. That's ~120 ms of extra work on the submitting thread per frame. It looks like the heap is rebuilt or scanned per operation rather than once per pass, or that something pushes far more entries than the flat scan visits. I haven't profiled it.
  • The batch size alone (64 vs 256): no conclusion. The scene's draws swung 6.7k-13.3k between runs today. The one pair at matched draws (round 3: 7.0k vs 6.7k) is 60.7 vs 59.1 ms, p95 80 vs 77 ms, so no harm visible.
  • The picture looked normal in the mid-run screenshots of all variants, just slow with the heap.

Happy to run a fixed heap version the same way.

Measured and posted by Claude Code on behalf of the machine's owner.

<!-- greyfinch-sync:trustno1-coder/VIctory-NVIDIA:comment:7282245473 --> Imported from [trustno1-coder/VIctory-NVIDIA](https://github.com/trustno1-coder/VIctory-NVIDIA/pull/3514#issuecomment-7282245473). Originally posted by [meonline42](https://github.com/meonline42) on 2026-11-30T14:59:13Z. --- @danielibabet I tried this PR as you suggested. **`VICTORY_QUEUE_HEAP=1` makes the frame about 3-4x slower on this machine, in every run.** Setup: Ryzen 9 5900X (12 cores), RTX 3090 24 GB, 64 GB DDR4-3200, Windows 11. This PR merged onto `41adb551` (clean merge, commit `b65501c1`), release build, Series X, 1080p to 1440p, DLSS Performance, frame generation off, `829188e`'s launcher defaults. Separate 240 s runs from the same restored save, three variants alternating, three rounds (both switches are read once, so no in-run A/B): | variant | fps (median, per second) | frame (5 s median) | preparing | p95 frame | draws | |---|---|---|---|---|---| | `QUEUE_BATCH_MAX=64` | 9 / 13 / 15 | 134.9 / 99.9 / 60.7 ms | 52.1 / 54.4 / 48.3 ms | 168 / 151 / 80 ms | 11.4k / 13.3k / 7.0k | | `QUEUE_BATCH_MAX=256` (the PR's default) | 14 / 9 / 17 | 77.3 / 115.1 / 59.1 ms | 55.4 / 105.2 / 47.0 ms | 159 / 137 / 77 ms | 11.8k / 7.9k / 6.7k | | `256` + **`QUEUE_HEAP=1`** | **4 / 4 / 4** | **241.5 / 236.6 / 242.3 ms** | **172.6 / 165.0 / 182.9 ms** | 280 / 287 / 280 ms | 8.4k / 7.4k / 6.6k | - **The heap path is consistent and large:** ~230-240 ms frames and ~170-180 ms of preparing in all three runs, across draws from 6.6k to 8.4k. That's ~120 ms of extra work on the submitting thread per frame. It looks like the heap is rebuilt or scanned per operation rather than once per pass, or that something pushes far more entries than the flat scan visits. I haven't profiled it. - **The batch size alone (64 vs 256):** no conclusion. The scene's draws swung 6.7k-13.3k between runs today. The one pair at matched draws (round 3: 7.0k vs 6.7k) is 60.7 vs 59.1 ms, p95 80 vs 77 ms, so no harm visible. - The picture looked normal in the mid-run screenshots of all variants, just slow with the heap. Happy to run a fixed heap version the same way. Measured and posted by Claude Code on behalf of the machine's owner.
danielibabet commented 2026-11-30 15:13:49 +00:00 (Migrated from github.com)

Imported from trustno1-coder/VIctory-NVIDIA. Originally posted by danielibabet on 2026-11-30T15:13:49Z.


Thank you @meonline42 — good catch, and exactly the right way to test it.

Root cause confirmed: \collect_due_and_census\ takes &self, so the heap could not be mutated. The fix was to clone the entire \BinaryHeap\ (~40,000 entries) on every call (~200 calls per 5 s). That made the heap path O(n × calls) in allocations — strictly worse than the flat scan.

Fix pushed (commit \22783dc6\ on \perf/masterwork-nvidia-rtx): replaced \heap.clone()\ with \self.due_heap.iter()\ — iterates the backing Vec slice in-place, skipping non-due and tombstoned entries inline. No allocation, no clone. Same O(n) pass but over heap records rather than cloned entries.

If you are able to re-run the same 240 s measurement with the updated head, the heap path should now be at or below the flat-scan baseline rather than 3-4× slower.

<!-- greyfinch-sync:trustno1-coder/VIctory-NVIDIA:comment:7282651332 --> Imported from [trustno1-coder/VIctory-NVIDIA](https://github.com/trustno1-coder/VIctory-NVIDIA/pull/3514#issuecomment-7282651332). Originally posted by [danielibabet](https://github.com/danielibabet) on 2026-11-30T15:13:49Z. --- Thank you @meonline42 — good catch, and exactly the right way to test it. **Root cause confirmed:** \collect_due_and_census\ takes \&self\, so the heap could not be mutated. The fix was to clone the entire \BinaryHeap\ (~40,000 entries) on every call (~200 calls per 5 s). That made the heap path O(n × calls) in allocations — strictly worse than the flat scan. **Fix pushed** (commit \22783dc6\ on \perf/masterwork-nvidia-rtx\): replaced \heap.clone()\ with \self.due_heap.iter()\ — iterates the backing Vec slice in-place, skipping non-due and tombstoned entries inline. No allocation, no clone. Same O(n) pass but over heap records rather than cloned entries. If you are able to re-run the same 240 s measurement with the updated head, the heap path should now be at or below the flat-scan baseline rather than 3-4× slower.
meonline42 commented 2026-11-30 17:40:02 +00:00 (Migrated from github.com)

Imported from trustno1-coder/VIctory-NVIDIA. Originally posted by meonline42 on 2026-11-30T17:40:02Z.


@danielibabet re-run with 22783dc6, same machine and method (this PR merged onto 41adb551 as 987b6a1a, release, separate 240 s runs from the same restored save, alternating, three rounds):

frame (5 s median) preparing draws fps (median / 10% low)
QUEUE_BATCH_MAX=256 68.7 / 63.1 / 63.8 ms 51.3 / 49.5 / 50.8 ms 10.0k / 7.4k / 7.2k 15-16 / 6-13
256 + QUEUE_HEAP=1 58.5 / 68.4 / 77.6 ms 48.1 / 51.7 / 51.3 ms 6.1k / 7.2k / 10.7k 14-17 / 6-13

The regression is gone: frames are back to ~60-78 ms (from ~240 ms), and preparing matches the flat path within ~1-2 ms. At matched draws (~7.2k: 63.1 / 63.8 against 68.4 ms) I can't see a gain on this 12-core Zen 3 either way; the run-to-run spread here is larger than any difference. No crashes, and the picture looked normal.

Measured and posted by Claude Code on behalf of the machine's owner.

<!-- greyfinch-sync:trustno1-coder/VIctory-NVIDIA:comment:7286663515 --> Imported from [trustno1-coder/VIctory-NVIDIA](https://github.com/trustno1-coder/VIctory-NVIDIA/pull/3514#issuecomment-7286663515). Originally posted by [meonline42](https://github.com/meonline42) on 2026-11-30T17:40:02Z. --- @danielibabet re-run with `22783dc6`, same machine and method (this PR merged onto `41adb551` as `987b6a1a`, release, separate 240 s runs from the same restored save, alternating, three rounds): | | frame (5 s median) | preparing | draws | fps (median / 10% low) | |---|---|---|---|---| | `QUEUE_BATCH_MAX=256` | 68.7 / 63.1 / 63.8 ms | 51.3 / 49.5 / 50.8 ms | 10.0k / 7.4k / 7.2k | 15-16 / 6-13 | | `256` + `QUEUE_HEAP=1` | 58.5 / 68.4 / 77.6 ms | 48.1 / 51.7 / 51.3 ms | 6.1k / 7.2k / 10.7k | 14-17 / 6-13 | **The regression is gone:** frames are back to ~60-78 ms (from ~240 ms), and preparing matches the flat path within ~1-2 ms. At matched draws (~7.2k: 63.1 / 63.8 against 68.4 ms) I can't see a gain on this 12-core Zen 3 either way; the run-to-run spread here is larger than any difference. No crashes, and the picture looked normal. Measured and posted by Claude Code on behalf of the machine's owner.
danielibabet commented 2026-12-01 16:25:09 +00:00 (Migrated from github.com)

Imported from trustno1-coder/VIctory-NVIDIA. Originally posted by danielibabet on 2026-12-01T16:25:09Z.


Closing this conflicting PR (VICTORY_QUEUE_HEAP=1 showed no net gain over the flat scan in @meonline42's re-run). Replaced by a fresh PR on main targeting BufferSweep growth and unmapped LOOSE window retention.

<!-- greyfinch-sync:trustno1-coder/VIctory-NVIDIA:comment:7317573117 --> Imported from [trustno1-coder/VIctory-NVIDIA](https://github.com/trustno1-coder/VIctory-NVIDIA/pull/3514#issuecomment-7317573117). Originally posted by [danielibabet](https://github.com/danielibabet) on 2026-12-01T16:25:09Z. --- Closing this conflicting PR (VICTORY_QUEUE_HEAP=1 showed no net gain over the flat scan in @meonline42's re-run). Replaced by a fresh PR on main targeting BufferSweep growth and unmapped LOOSE window retention.

Pull request closed

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!3514
No description provided.