perf(gpu): replace TextureQueue BTreeMap scan with dense vector, add away_nonempty fast-path, bump Tells::BATCH #1242

Closed
qlfu wants to merge 2 commits from upstream-pr/1242 into main
qlfu commented 2026-11-28 16:15:59 +00:00 (Migrated from github.com)

Imported from trustno1-coder/VIctory-NVIDIA. Originally posted by qlfu on 2026-11-28T16:15:59Z.


Summary

Reduces TextureQueue mutex hold time and lock contention on dense texture-queue scans, avoids taking the HostHeap locks on the common empty-away path, and increases Tells::BATCH flush granularity:

  1. Dense TextureQueue storage (crates/victory-gpu/src/descriptor/host.rs):
    • Replaced TextureQueue.slots: BTreeMap<u32, Waiting> with dense Vec<Waiting> + SlotMeta + HashMap<u32, usize> index using swap_remove on removal, plus held: HashMap<u32, u64> and pre-reserved Due staging vectors.
    • TextureQueue::due() now scans contiguous Vec<Waiting> memory instead of chasing BTreeMap node pointers across tens of thousands of queued slots.
    • Added unit test dense_texture_queue_preserves_pos_and_meta_across_removals_and_transitions.
  2. Lock-free HostHeap::away check (crates/victory-gpu/src/descriptor/host.rs):
    • Added away_nonempty: AtomicBool on HostHeap, allowing texture_slot_here() and texture_slot_gone() to return immediately in the common case when away is empty without locking away / made.
  3. Larger flush batch (crates/victory-gpu/src/draw.rs):
    • Increased Tells::BATCH from 16 to 64 so draw-thread texture notifications flush fewer small batches under contention.

Benchmark (GTA VI, 90s steady gameplay, 1440p Series X, DLSS Quality, Ryzen 7 5700X + RTX 4070 Ti 12 GB)

Paired A/B on main (9ce3cf9) with -FrameGen (VICTORY_TEXTURE_ROOM_PERCENT=80, 90% no-room intervals):

Metric (90s median per 5s window unless noted) main (9ce3cf9) pr/texture-queue-perf (ef3b9ae) Delta
TextureQueue lock hold in due() (held due) 145.5 ms 35.5 ms -75.6% (4.1x faster)
TextureQueue total lock wait (noting part) 629.5 ms (434.5 ms) 251.5 ms (179.5 ms) -60.0%
Frame time (prep / copy back) 63.95 ms (46.95 / 18.4) 59.95 ms (44.85 / 14.95) -6.3%
Title present-wait 19.7 ms 15.4 ms -21.8%
Median FPS 15.65 16.70 +6.7%
Mean FPS 16.06 16.89 +5.2%
Max FPS 21.3 23.2 +8.9%
Gameplay WARN / ERROR count 0 / 0 0 / 0 clean

(Before 64d4931 on df2daaf, the same commit improved non-VRAM-capped -NoFrameGenReady runs from 15.6 -> 18.8 median FPS / 13.64 -> 18.43 mean FPS, and cut lock wait from 202 -> 88 ms.)

Verification

  • cargo clippy -p victory-gpu --all-targets --all-features --locked: 0 warnings/errors in modified files (descriptor/host.rs, draw.rs).
  • cargo test --workspace --exclude victory-gpu --locked: all crate tests passed (xtask failed only on pre-existing upstream bindings.ini allowlist check from 4715a42).
  • cargo test -p victory-gpu --locked -- --test-threads=1: 455 passed, 0 failed, 36 ignored (single-threaded avoids pre-existing multi-thread hang in watch::tests::faults_that_take_no_lock_tell_the_same).
<!-- greyfinch-sync:trustno1-coder/VIctory-NVIDIA:issue:5542167172 --> Imported from [trustno1-coder/VIctory-NVIDIA](https://github.com/trustno1-coder/VIctory-NVIDIA/pull/1242). Originally posted by [qlfu](https://github.com/qlfu) on 2026-11-28T16:15:59Z. --- ### Summary Reduces `TextureQueue` mutex hold time and lock contention on dense texture-queue scans, avoids taking the `HostHeap` locks on the common empty-`away` path, and increases `Tells::BATCH` flush granularity: 1. **Dense `TextureQueue` storage (`crates/victory-gpu/src/descriptor/host.rs`)**: - Replaced `TextureQueue.slots: BTreeMap<u32, Waiting>` with dense `Vec<Waiting>` + `SlotMeta` + `HashMap<u32, usize>` index using `swap_remove` on removal, plus `held: HashMap<u32, u64>` and pre-reserved `Due` staging vectors. - `TextureQueue::due()` now scans contiguous `Vec<Waiting>` memory instead of chasing `BTreeMap` node pointers across tens of thousands of queued slots. - Added unit test `dense_texture_queue_preserves_pos_and_meta_across_removals_and_transitions`. 2. **Lock-free `HostHeap::away` check (`crates/victory-gpu/src/descriptor/host.rs`)**: - Added `away_nonempty: AtomicBool` on `HostHeap`, allowing `texture_slot_here()` and `texture_slot_gone()` to return immediately in the common case when `away` is empty without locking `away` / `made`. 3. **Larger flush batch (`crates/victory-gpu/src/draw.rs`)**: - Increased `Tells::BATCH` from `16` to `64` so draw-thread texture notifications flush fewer small batches under contention. ### Benchmark (GTA VI, 90s steady gameplay, 1440p Series X, DLSS Quality, Ryzen 7 5700X + RTX 4070 Ti 12 GB) Paired A/B on `main` (`9ce3cf9`) with `-FrameGen` (`VICTORY_TEXTURE_ROOM_PERCENT=80`, 90% no-room intervals): | Metric (90s median per 5s window unless noted) | `main` (`9ce3cf9`) | `pr/texture-queue-perf` (`ef3b9ae`) | Delta | |---|---:|---:|---:| | `TextureQueue` lock hold in `due()` (`held due`) | `145.5 ms` | `35.5 ms` | **-75.6% (4.1x faster)** | | `TextureQueue` total lock wait (`noting` part) | `629.5 ms` (`434.5 ms`) | `251.5 ms` (`179.5 ms`) | **-60.0%** | | Frame time (`prep` / `copy back`) | `63.95 ms` (`46.95` / `18.4`) | `59.95 ms` (`44.85` / `14.95`) | **-6.3%** | | Title `present-wait` | `19.7 ms` | `15.4 ms` | **-21.8%** | | Median FPS | `15.65` | `16.70` | **+6.7%** | | Mean FPS | `16.06` | `16.89` | **+5.2%** | | Max FPS | `21.3` | `23.2` | **+8.9%** | | Gameplay `WARN` / `ERROR` count | `0` / `0` | `0` / `0` | clean | *(Before `64d4931` on `df2daaf`, the same commit improved non-VRAM-capped `-NoFrameGenReady` runs from `15.6 -> 18.8` median FPS / `13.64 -> 18.43` mean FPS, and cut lock wait from `202 -> 88 ms`.)* ### Verification - `cargo clippy -p victory-gpu --all-targets --all-features --locked`: 0 warnings/errors in modified files (`descriptor/host.rs`, `draw.rs`). - `cargo test --workspace --exclude victory-gpu --locked`: all crate tests passed (`xtask` failed only on pre-existing upstream `bindings.ini` allowlist check from `4715a42`). - `cargo test -p victory-gpu --locked -- --test-threads=1`: **455 passed, 0 failed, 36 ignored** (single-threaded avoids pre-existing multi-thread hang in `watch::tests::faults_that_take_no_lock_tell_the_same`).
jptorres96 commented 2026-11-28 17:50:47 +00:00 (Migrated from github.com)

Imported from trustno1-coder/VIctory-NVIDIA. Originally posted by jptorres96 on 2026-11-28T17:50:47Z.


Checked at ef3b9ae (base 9ce3cf9), Windows build 26300, rustc 1.98.1.

The queue, differentially. I added a throwaway test, not for commit, that drives a TextureQueue through a seeded random run of its operations: written (seen or not), take_out, later, hold, room, partly, recheck, changed, done, and frames moving on. That is 40 seeds of 4,000 operations over 200 slots. Every 7 steps it prints what callers see: due_and_census(frame) with Due::ordered(usize::MAX), census(frame), and checked, mapped and the mark of a random slot. On the base and on this PR the outputs are byte-identical (45,760 lines). The runs keep about 120 slots queued, with every census state present.

So the dense storage and meta stay in step through swap_remove and every transition. The walk order changes from index order to Vec order, but that doesn't show, because Due::ordered sorts by write number, which is unique.

  • cargo test -p victory-gpu --lib -- --test-threads=1: 455 passed, including dense_texture_queue_preserves_pos_and_meta_across_removals_and_transitions.
  • cargo clippy -p victory-gpu --all-targets [--all-features] --no-deps -- -D warnings: clean apart from main's own manual_is_multiple_of.
  • away_nonempty is only stored under the away lock, so it ends as the map does. The unlocked read in texture_slot_here can only miss an insertion of the same slot made concurrently. Its other caller holds the slot's made lock, as texture_slot_gone does, and the texture pass calls gone and here for a slot one after the other.

One thing to look at: Tells::BATCH goes from 16 to 64. The comment just above it gives the reason for 16: "so that a pass that takes the slots due meanwhile on another thread finds few of these told late". With 64, a concurrent pass can find up to four times as many slots told late, and the differential can't measure that. It may be worth keeping 16, or making the change on its own with an in-game number, since the rest of the PR doesn't depend on it.

No game files.

<!-- greyfinch-sync:trustno1-coder/VIctory-NVIDIA:comment:7243331613 --> Imported from [trustno1-coder/VIctory-NVIDIA](https://github.com/trustno1-coder/VIctory-NVIDIA/pull/1242#issuecomment-7243331613). Originally posted by [jptorres96](https://github.com/jptorres96) on 2026-11-28T17:50:47Z. --- Checked at `ef3b9ae` (base `9ce3cf9`), Windows build 26300, rustc 1.98.1. **The queue, differentially.** I added a throwaway test, not for commit, that drives a `TextureQueue` through a seeded random run of its operations: `written` (seen or not), `take_out`, `later`, `hold`, `room`, `partly`, `recheck`, `changed`, `done`, and frames moving on. That is 40 seeds of 4,000 operations over 200 slots. Every 7 steps it prints what callers see: `due_and_census(frame)` with `Due::ordered(usize::MAX)`, `census(frame)`, and `checked`, `mapped` and the mark of a random slot. On the base and on this PR the outputs are byte-identical (45,760 lines). The runs keep about 120 slots queued, with every census state present. So the dense storage and `meta` stay in step through `swap_remove` and every transition. The walk order changes from index order to `Vec` order, but that doesn't show, because `Due::ordered` sorts by write number, which is unique. - `cargo test -p victory-gpu --lib -- --test-threads=1`: 455 passed, including `dense_texture_queue_preserves_pos_and_meta_across_removals_and_transitions`. - `cargo clippy -p victory-gpu --all-targets [--all-features] --no-deps -- -D warnings`: clean apart from `main`'s own `manual_is_multiple_of`. - `away_nonempty` is only stored under the `away` lock, so it ends as the map does. The unlocked read in `texture_slot_here` can only miss an insertion of the same slot made concurrently. Its other caller holds the slot's `made` lock, as `texture_slot_gone` does, and the texture pass calls gone and here for a slot one after the other. One thing to look at: `Tells::BATCH` goes from 16 to 64. The comment just above it gives the reason for 16: "so that a pass that takes the slots due meanwhile on another thread finds few of these told late". With 64, a concurrent pass can find up to four times as many slots told late, and the differential can't measure that. It may be worth keeping 16, or making the change on its own with an in-game number, since the rest of the PR doesn't depend on it. No game files.
qlfu commented 2026-11-28 18:39:16 +00:00 (Migrated from github.com)

Imported from trustno1-coder/VIctory-NVIDIA. Originally posted by qlfu on 2026-11-28T18:39:16Z.


Thanks for the differential test and thorough audit @jptorres96! Good point on \Tells::BATCH\ — reverted it back to \16\ so this PR stays focused solely on the semantics-preserving dense \TextureQueue\ + \way_nonempty\ changes in \descriptor/host.rs.

<!-- greyfinch-sync:trustno1-coder/VIctory-NVIDIA:comment:7243954940 --> Imported from [trustno1-coder/VIctory-NVIDIA](https://github.com/trustno1-coder/VIctory-NVIDIA/pull/1242#issuecomment-7243954940). Originally posted by [qlfu](https://github.com/qlfu) on 2026-11-28T18:39:16Z. --- Thanks for the differential test and thorough audit @jptorres96! Good point on \Tells::BATCH\ — reverted it back to \16\ so this PR stays focused solely on the semantics-preserving dense \TextureQueue\ + \way_nonempty\ changes in \descriptor/host.rs\.
trustno1-coder commented 2026-11-28 19:23:24 +00:00 (Migrated from github.com)

Imported from trustno1-coder/VIctory-NVIDIA. Originally posted by trustno1-coder on 2026-11-28T19:23:24Z.


Thanks for this. I'm closing it without merging because it conflicts with the current main in several places (main has moved a lot since it was opened, with about 100 pull requests merged today), and I can't resolve that safely on your behalf. If you'd still like it in, please rebase it on the current main and open a new pull request.

<!-- greyfinch-sync:trustno1-coder/VIctory-NVIDIA:comment:7244507522 --> Imported from [trustno1-coder/VIctory-NVIDIA](https://github.com/trustno1-coder/VIctory-NVIDIA/pull/1242#issuecomment-7244507522). Originally posted by [trustno1-coder](https://github.com/trustno1-coder) on 2026-11-28T19:23:24Z. --- Thanks for this. I'm closing it without merging because it conflicts with the current main in several places (main has moved a lot since it was opened, with about 100 pull requests merged today), and I can't resolve that safely on your behalf. If you'd still like it in, please rebase it on the current main and open a new pull request.

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