perf(gpu): split CardShared out of CARD_WORK lock and avoid full-plan flags scan in relay gather #1322

Merged
qlfu merged 1 commit from upstream-pr/1322 into main 2026-11-29 08:16:57 +00:00
qlfu commented 2026-11-28 17:20:30 +00:00 (Migrated from github.com)

Imported from trustno1-coder/VIctory-NVIDIA. Originally posted by qlfu on 2026-11-28T17:20:30Z.


Summary

Eliminates long CARD_WORK mutex holds during Host::card_before and removes the full-plan flags allocation / per-piece binary search loop in Host::relay_card_back:

  1. Split immutable D3D12 comparison state (CardShared) out of CARD_WORK (crates/victory-gpu/src/host.rs):
    • Moved root, pso, and differs (initialized once in make_card_work and read-only afterward) into a OnceLock<CardShared> (CARD_SHARED).
    • Host::relay_card_back now reads CARD_SHARED with zero locking on CARD_WORK.
    • Host::card_before now runs card_make_before, send::take, sync command recording + ExecuteCommandLists, span merging, and check command recording outside CARD_WORK, taking CARD_WORK only briefly to pop pooled command lists and advance taken/out.
    • Previously card_before held CARD_WORK for ~10–13 ms/frame across send::take and D3D12 recording, stalling both relay_card_back (which pieces to fetch) and card_done (read-backs and lists) on the relay landing thread.
  2. Direct flagged-piece gathering & slice lookup in relay_card_back (crates/victory-gpu/src/host.rs, crates/victory-gpu/src/host/torn.rs):
    • Added Suspects::is_empty() and replaced the per-plan flags: Vec<(usize, u32, bool)> allocation (46,000+ entries/frame) + per-piece binary_search loop with collect_flagged_range() (collecting only flagged/wanted pieces directly into a reused Vec<(usize, u32)>, with a bounds-checked contiguous slice fast-path when suspects is empty) and flagged_in_range() (partition_point slice lookup per range).
    • Kept flagged_pieces() under #[cfg(test)] and added unit test collect_flagged_range_and_slice_lookup_match_flagged_pieces.

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 (idle PC, D3D12 VRAM budget ~10,430 MB):

Metric (90s run unless noted) main (9ce3cf9) pr/card-work-lock-and-gather-perf (083a625) Delta
Relay which pieces to fetch (per frame) 6.00 – 7.05 ms 0.40 – 0.51 ms -93% (~15x faster)
Relay read-backs and lists (card_done, per frame) 3.18 – 3.57 ms 1.54 – 1.79 ms -50% (~2x faster)
Median copy-back (back) 16.7 ms 13.7 ms -18.0% (-3.0 ms)
Min / p10 FPS 13.3 / 14.3 14.6 / 14.6 +9.8% min floor
Median / Mean FPS 18.6 / 18.54 18.5 / 18.67 +0.7% mean
GPU busy 71.0% 73.0% +2.0 pp
Gameplay WARN / ERROR count 0 / 0 0 / 0 clean

Verification

  • cargo clippy -p victory-gpu --all-targets --locked and --all-features --locked: 0 warnings/errors in host.rs and host/torn.rs.
  • cargo test -p victory-gpu --lib --locked -- --test-threads=1 --quiet: 455 passed, 0 failed, 36 ignored.
  • cargo build --release --locked -p victory: exit 0.
<!-- greyfinch-sync:trustno1-coder/VIctory-NVIDIA:issue:5542731676 --> Imported from [trustno1-coder/VIctory-NVIDIA](https://github.com/trustno1-coder/VIctory-NVIDIA/pull/1322). Originally posted by [qlfu](https://github.com/qlfu) on 2026-11-28T17:20:30Z. --- ### Summary Eliminates long `CARD_WORK` mutex holds during `Host::card_before` and removes the full-plan `flags` allocation / per-piece binary search loop in `Host::relay_card_back`: 1. **Split immutable D3D12 comparison state (`CardShared`) out of `CARD_WORK` (`crates/victory-gpu/src/host.rs`)**: - Moved `root`, `pso`, and `differs` (initialized once in `make_card_work` and read-only afterward) into a `OnceLock<CardShared>` (`CARD_SHARED`). - `Host::relay_card_back` now reads `CARD_SHARED` with zero locking on `CARD_WORK`. - `Host::card_before` now runs `card_make_before`, `send::take`, `sync` command recording + `ExecuteCommandLists`, span merging, and `check` command recording outside `CARD_WORK`, taking `CARD_WORK` only briefly to pop pooled command lists and advance `taken`/`out`. - Previously `card_before` held `CARD_WORK` for ~10–13 ms/frame across `send::take` and D3D12 recording, stalling both `relay_card_back` (`which pieces to fetch`) and `card_done` (`read-backs and lists`) on the relay landing thread. 2. **Direct flagged-piece gathering & slice lookup in `relay_card_back` (`crates/victory-gpu/src/host.rs`, `crates/victory-gpu/src/host/torn.rs`)**: - Added `Suspects::is_empty()` and replaced the per-plan `flags: Vec<(usize, u32, bool)>` allocation (46,000+ entries/frame) + per-piece `binary_search` loop with `collect_flagged_range()` (collecting only flagged/wanted pieces directly into a reused `Vec<(usize, u32)>`, with a bounds-checked contiguous slice fast-path when `suspects` is empty) and `flagged_in_range()` (`partition_point` slice lookup per range). - Kept `flagged_pieces()` under `#[cfg(test)]` and added unit test `collect_flagged_range_and_slice_lookup_match_flagged_pieces`. ### 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` (idle PC, D3D12 VRAM budget ~10,430 MB): | Metric (90s run unless noted) | `main` (`9ce3cf9`) | `pr/card-work-lock-and-gather-perf` (`083a625`) | Delta | |---|---:|---:|---:| | Relay `which pieces to fetch` (per frame) | `6.00 – 7.05 ms` | `0.40 – 0.51 ms` | **-93% (~15x faster)** | | Relay `read-backs and lists` (`card_done`, per frame) | `3.18 – 3.57 ms` | `1.54 – 1.79 ms` | **-50% (~2x faster)** | | Median `copy-back` (`back`) | `16.7 ms` | `13.7 ms` | **-18.0% (-3.0 ms)** | | Min / `p10` FPS | `13.3` / `14.3` | `14.6` / `14.6` | **+9.8% min floor** | | Median / Mean FPS | `18.6` / `18.54` | `18.5` / `18.67` | `+0.7% mean` | | GPU busy | `71.0%` | `73.0%` | **+2.0 pp** | | Gameplay `WARN` / `ERROR` count | `0` / `0` | `0` / `0` | clean | ### Verification - `cargo clippy -p victory-gpu --all-targets --locked` and `--all-features --locked`: 0 warnings/errors in `host.rs` and `host/torn.rs`. - `cargo test -p victory-gpu --lib --locked -- --test-threads=1 --quiet`: **455 passed, 0 failed, 36 ignored**. - `cargo build --release --locked -p victory`: exit `0`.
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!1322
No description provided.