perf(gpu): single-pass ask_once, binary-searched ahead ranges, Batch add caches #1278

Closed
github-bot wants to merge 1 commit from github-bot/gh-1278 into main AGit
Owner

@⁠qlfu on GitHub · 2026-11-28 16:52 UTC · original
qlfu:pr/watch-and-batch-perf → main · +249 −39 · 2 files · 1 commits · closed without merging

Summary

Reduces write-watch lock contention and redundant per-flush/per-refresh work on the submitting thread and its worker threads:

  1. Skip ask_ahead / ask_ahead_done work & JOURNAL write lock while ask_once is active (crates/victory-gpu/src/watch.rs, crates/victory-gpu/src/host.rs):
    • Added an AtomicBool mirror (KEPT / ask_once_active()) synchronized with Journal::kept under journal_mut().
    • When ask_once stands (the default), Batch::refresh_front and Batch::flush_all now skip allocating ranges and calling ask_ahead/ask_ahead_done (~70–90 times/frame). Previously ask_ahead ran published() + collected/sorted pieces before checking journal().kept, and ask_ahead_done took the global JOURNAL write lock on every flush only to no-op.
    • ask_once_end() and wrote() also fast-return via ask_once_active() without taking journal_mut() when no submission asking is active.
  2. Single-pass ask_once + sorted/merged Journal::ahead with binary search (crates/victory-gpu/src/watch.rs):
    • Unified ask_once() into one published() scan, one Asking::hold_all(), one journal_mut() write lock, and deduplicated ask_leftover() calls instead of running ask_ahead() + ask_rest() back-to-back.
    • Kept Journal::ahead sorted and merged via join_page_ranges() and replaced linear .iter().any(...) scans in asked(), writes(), and nothing_new() with O(log n) covered_by() (partition_point).
    • Kept ask_rest() under #[cfg(test)] calling the shared ask_leftover() helper so the_rest_of_the_one_asking_has_a_number_of_its_own continues to test it directly.
  3. Fast-path nothing_new() & single-page journal lookup (crates/victory-gpu/src/watch.rs):
    • Reordered nothing_new() to check copied slice bounds and ranged == 0 && taken.contains(&0) before touching published() or acquiring JOURNAL.read().
    • Added written_in() / direct BTreeMap::get(&first) fast-path for single-page (last == first + 1) lookups in nothing_new(), asked(), and writes().
    • Added unit tests join_page_ranges_merges_and_covered_by_matches_linear and ask_once_single_pass_tracks_active_and_nothing_new.
  4. 1-entry MRU caches in Batch::add (crates/victory-gpu/src/host.rs):
    • Added last_ahead and last_index caches on Batch (invalidated in give_back, flush_one, title_written, refresh_front, refresh_ahead, and flush) to avoid repeated hash lookups across consecutive Step::Refresh entries for the same Shadow copy.

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:

Metric (90s median per 5s window unless noted) main (9ce3cf9) pr/watch-and-batch-perf (c834762) Delta
Median FPS 15.65 17.75 +13.4%
Mean FPS 16.06 17.46 +8.7%
Max FPS (p90) 21.3 (19.2) 24.4 (22.7) +14.6% (+18.2%)
Frame time (prep / copy back) 63.95 ms (46.95 / 18.4) 56.35 ms (41.75 / 15.45) -11.9% (prep -11.1%)
ASYNC_SUBMIT worked 61.1 ms 54.45 ms -10.9%
Title present-wait 19.7 ms 14.8 ms -24.9%
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 watch.rs and host.rs.
  • cargo test -p victory-gpu --lib --locked -- --test-threads=1 --quiet: 456 passed, 0 failed, 36 ignored.
  • cargo build --release --locked -p victory: exit 0.
**@⁠qlfu** on GitHub · 2026-11-28 16:52 UTC · [original](https://github.com/trustno1-coder/VIctory-NVIDIA/pull/1278) `qlfu:pr/watch-and-batch-perf` → `main` · +249 −39 · 2 files · 1 commits · closed without merging ### Summary Reduces write-watch lock contention and redundant per-flush/per-refresh work on the submitting thread and its worker threads: 1. **Skip `ask_ahead` / `ask_ahead_done` work & `JOURNAL` write lock while `ask_once` is active (`crates/victory-gpu/src/watch.rs`, `crates/victory-gpu/src/host.rs`)**: - Added an `AtomicBool` mirror (`KEPT` / `ask_once_active()`) synchronized with `Journal::kept` under `journal_mut()`. - When `ask_once` stands (the default), `Batch::refresh_front` and `Batch::flush_all` now skip allocating `ranges` and calling `ask_ahead`/`ask_ahead_done` (~70–90 times/frame). Previously `ask_ahead` ran `published()` + collected/sorted `pieces` before checking `journal().kept`, and `ask_ahead_done` took the global `JOURNAL` write lock on every flush only to no-op. - `ask_once_end()` and `wrote()` also fast-return via `ask_once_active()` without taking `journal_mut()` when no submission asking is active. 2. **Single-pass `ask_once` + sorted/merged `Journal::ahead` with binary search (`crates/victory-gpu/src/watch.rs`)**: - Unified `ask_once()` into one `published()` scan, one `Asking::hold_all()`, one `journal_mut()` write lock, and deduplicated `ask_leftover()` calls instead of running `ask_ahead()` + `ask_rest()` back-to-back. - Kept `Journal::ahead` sorted and merged via `join_page_ranges()` and replaced linear `.iter().any(...)` scans in `asked()`, `writes()`, and `nothing_new()` with `O(log n)` `covered_by()` (`partition_point`). - Kept `ask_rest()` under `#[cfg(test)]` calling the shared `ask_leftover()` helper so `the_rest_of_the_one_asking_has_a_number_of_its_own` continues to test it directly. 3. **Fast-path `nothing_new()` & single-page journal lookup (`crates/victory-gpu/src/watch.rs`)**: - Reordered `nothing_new()` to check `copied` slice bounds and `ranged == 0 && taken.contains(&0)` before touching `published()` or acquiring `JOURNAL.read()`. - Added `written_in()` / direct `BTreeMap::get(&first)` fast-path for single-page (`last == first + 1`) lookups in `nothing_new()`, `asked()`, and `writes()`. - Added unit tests `join_page_ranges_merges_and_covered_by_matches_linear` and `ask_once_single_pass_tracks_active_and_nothing_new`. 4. **1-entry MRU caches in `Batch::add` (`crates/victory-gpu/src/host.rs`)**: - Added `last_ahead` and `last_index` caches on `Batch` (invalidated in `give_back`, `flush_one`, `title_written`, `refresh_front`, `refresh_ahead`, and `flush`) to avoid repeated hash lookups across consecutive `Step::Refresh` entries for the same `Shadow` copy. ### 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`: | Metric (90s median per 5s window unless noted) | `main` (`9ce3cf9`) | `pr/watch-and-batch-perf` (`c834762`) | Delta | |---|---:|---:|---:| | Median FPS | `15.65` | `17.75` | **+13.4%** | | Mean FPS | `16.06` | `17.46` | **+8.7%** | | Max FPS (`p90`) | `21.3` (`19.2`) | `24.4` (`22.7`) | **+14.6%** (**+18.2%**) | | Frame time (`prep` / `copy back`) | `63.95 ms` (`46.95` / `18.4`) | `56.35 ms` (`41.75` / `15.45`) | **-11.9%** (`prep -11.1%`) | | `ASYNC_SUBMIT` worked | `61.1 ms` | `54.45 ms` | **-10.9%** | | Title `present-wait` | `19.7 ms` | `14.8 ms` | **-24.9%** | | 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 `watch.rs` and `host.rs`. - `cargo test -p victory-gpu --lib --locked -- --test-threads=1 --quiet`: **456 passed, 0 failed, 36 ignored**. - `cargo build --release --locked -p victory`: exit `0`.
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-28 16:52:55 +00:00
github-bot closed this pull request 2026-11-28 20:57:17 +00:00
Author
Owner

@⁠qlfu on GitHub · 2026-11-28 20:53 UTC · original

Closing as superseded by \3055afd\ (per-range \Askings\ in \watch.rs, which already improved \watch\ below this PR on current \main).

**@⁠qlfu** on GitHub · 2026-11-28 20:53 UTC · [original](https://github.com/trustno1-coder/VIctory-NVIDIA/pull/1278#issuecomment-7245623652) Closing as superseded by \3055afd\ (per-range \Askings\ in \watch.rs\, which already improved \watch\ below this PR on current \main\).

Pull request closed

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