perf(gpu): cache watch::ahead snapshot by generation across flushes and flush_one #4533

Closed
github-bot wants to merge 2 commits from github-bot/gh-4532 into main AGit
Owner

@⁠YounesAH15 on GitHub · 2026-11-30 22:01 UTC · original
YounesAH15:perf/cpu-wall-refresh-flushes → main · +88 −16 · 3 files · 2 commits · closed without merging

Summary

Part of #2415 (CPU wall): eliminates redundant JOURNAL RwLock acquisitions and Journal::ahead vector copies across submission flushes and one-at-a-time Batch::flush_one checks.

Problem

  1. Batch::refresh_front_spans, Batch::refresh_written_before, and Batch::flush_all called watch::ahead_snapshot_into(&mut self.ahead_snap) on every flush (~40-47 flushes/frame), acquiring the global JOURNAL read lock and cloning Journal::ahead even when KEPT was active and Journal::ahead had not changed all submission.
  2. Batch::flush_one (~330-350 calls/frame before shader writes) called shadow.nothing_new(...) -> watch::nothing_new_single(...), acquiring JOURNAL on each non-published range instead of reusing Batch::ahead_snap, and did not increment FLUSHES[2] when skipping unchanged spans.

Changes

  • crates/victory-gpu/src/watch.rs:
    • Added monotonic AHEAD_GEN bumped under journal_mut() at all 5 Journal::ahead mutation sites (ask_ahead, ask_ahead_done, ask_once, ask_more, ask_once_end).
    • Added ahead_snapshot_cached(out, cached_gen) which returns in a single Acquire load (zero locks, zero copies) when Journal::ahead is unchanged.
    • Added unit test ahead_snapshot_cached_reuses_until_gen_bumps.
  • crates/victory-gpu/src/host.rs:
    • Added ahead_gen: u64 and Batch::ensure_ahead_snap().
    • Wired ensure_ahead_snap() into Batch::flush_one, Batch::refresh_front_spans, Batch::refresh_written_before, and Batch::flush_all (simplifying away the local snapped bool).
    • Updated Batch::flush_one to use shadow.nothing_new_with(&self.ahead_snap, ...) and count skipped no-ops in FLUSHES[2].
  • crates/victory-gpu/src/shadow.rs:
    • Marked Shadow::nothing_new with #[cfg_attr(not(test), allow(dead_code))] so release builds stay warning-free.

Verification

  • Unit tests:
    • watch::blocks_all_tests::ahead_snapshot_cached_reuses_until_gen_bumps — ok
    • host::refresh_tests::chunked_nothing_new_with_matches_individual — ok
  • In-game (RTX 3050 8 GB, Series S, DLSS Performance, FSR 3 2x, release build, 2,199-2,467 draws/frame):
    • Batch::flush_one (one at a time in ~335 before shader writes): 0.3-0.8 ms/frame (down from 1.8-2.4 ms/frame on prior ~346-352 call runs).
    • Refresh flushes: 8.7-11.2 ms/frame across 40-41 flushes (6,417-7,984 spans, 5,632-7,194 skipped as no-ops); total submitting thread preparing 33.7-40.3 ms/frame.
    • Visual & correctness check: clean in-world rendering, VRAM 4.5/7.1 GB, binds dropped: 0.00/frame, raw view clamp: [0, 0, 0, 0], buffer views left null: 0.
**@⁠YounesAH15** on GitHub · 2026-11-30 22:01 UTC · [original](https://github.com/trustno1-coder/VIctory-NVIDIA/pull/4532) `YounesAH15:perf/cpu-wall-refresh-flushes` → `main` · +88 −16 · 3 files · 2 commits · closed without merging ### Summary Part of #2415 (CPU wall): eliminates redundant `JOURNAL` RwLock acquisitions and `Journal::ahead` vector copies across submission flushes and one-at-a-time `Batch::flush_one` checks. ### Problem 1. `Batch::refresh_front_spans`, `Batch::refresh_written_before`, and `Batch::flush_all` called `watch::ahead_snapshot_into(&mut self.ahead_snap)` on every flush (~40-47 flushes/frame), acquiring the global `JOURNAL` read lock and cloning `Journal::ahead` even when `KEPT` was active and `Journal::ahead` had not changed all submission. 2. `Batch::flush_one` (~330-350 calls/frame before shader writes) called `shadow.nothing_new(...)` -> `watch::nothing_new_single(...)`, acquiring `JOURNAL` on each non-published range instead of reusing `Batch::ahead_snap`, and did not increment `FLUSHES[2]` when skipping unchanged spans. ### Changes - **`crates/victory-gpu/src/watch.rs`**: - Added monotonic `AHEAD_GEN` bumped under `journal_mut()` at all 5 `Journal::ahead` mutation sites (`ask_ahead`, `ask_ahead_done`, `ask_once`, `ask_more`, `ask_once_end`). - Added `ahead_snapshot_cached(out, cached_gen)` which returns in a single `Acquire` load (zero locks, zero copies) when `Journal::ahead` is unchanged. - Added unit test `ahead_snapshot_cached_reuses_until_gen_bumps`. - **`crates/victory-gpu/src/host.rs`**: - Added `ahead_gen: u64` and `Batch::ensure_ahead_snap()`. - Wired `ensure_ahead_snap()` into `Batch::flush_one`, `Batch::refresh_front_spans`, `Batch::refresh_written_before`, and `Batch::flush_all` (simplifying away the local `snapped` bool). - Updated `Batch::flush_one` to use `shadow.nothing_new_with(&self.ahead_snap, ...)` and count skipped no-ops in `FLUSHES[2]`. - **`crates/victory-gpu/src/shadow.rs`**: - Marked `Shadow::nothing_new` with `#[cfg_attr(not(test), allow(dead_code))]` so release builds stay warning-free. ### Verification - **Unit tests:** - `watch::blocks_all_tests::ahead_snapshot_cached_reuses_until_gen_bumps` — ok - `host::refresh_tests::chunked_nothing_new_with_matches_individual` — ok - **In-game (RTX 3050 8 GB, Series S, DLSS Performance, FSR 3 2x, release build, 2,199-2,467 draws/frame):** - `Batch::flush_one` (`one at a time in ~335 before shader writes`): **0.3-0.8 ms/frame** (down from 1.8-2.4 ms/frame on prior ~346-352 call runs). - Refresh flushes: **8.7-11.2 ms/frame** across 40-41 flushes (6,417-7,984 spans, 5,632-7,194 skipped as no-ops); total submitting thread preparing **33.7-40.3 ms/frame**. - Visual & correctness check: clean in-world rendering, `VRAM 4.5/7.1 GB`, `binds dropped: 0.00/frame`, `raw view clamp: [0, 0, 0, 0]`, `buffer views left null: 0`.
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-30 22:02:47 +00:00
Author
Owner

@⁠IRennie74 on GitHub · 2026-11-30 22:39 UTC · original

Relevant for CPU-bound PCs: on a Ryzen 5 5600X + RTX 3060 (Series S, DLSS Performance) the submitting thread is busy 44-54 ms of a 55-58 ms frame, refreshes are 11-15 ms of that, and the title waits on that thread at its presents. A lock and a copy saved per flush counts there.

Two small things:

  • flush_one (crates/victory-gpu/src/host.rs:14036) used to call Shadow::nothing_new, which honours VICTORY_PART_EXACT (watch::part_exact() → nothing_new_single(.., exact)). nothing_new_with always takes the non-exact path. That is still correct (only more conservative), but with PART_EXACT=1 fewer flush_one calls are skipped than before. Worth a line in the description, or a fallback to the old call when part_exact() is on.
  • ahead_snapshot_cached_reuses_until_gen_bumps asserts that AHEAD_GEN does not move between two calls, but AHEAD_GEN is process-global, and any other test in the same binary that calls ask_ahead/ask_once/ask_once_end (the refresh_tests do) can bump it in between. The test could flake under the default parallel runner. Testing ahead_snapshot_cached against a local generation counter, or serialising with the watch tests' lock, would avoid that.

It overlaps textually with #4423 and #4489 in Batch::flush_all (the snapped logic). #4423's bitset sift_by also reads self.ahead_snap, so whichever lands second needs ensure_ahead_snap() in front of it.

**@⁠IRennie74** on GitHub · 2026-11-30 22:39 UTC · [original](https://github.com/trustno1-coder/VIctory-NVIDIA/pull/4532#issuecomment-7294129256) Relevant for CPU-bound PCs: on a Ryzen 5 5600X + RTX 3060 (Series S, DLSS Performance) the submitting thread is busy 44-54 ms of a 55-58 ms frame, refreshes are 11-15 ms of that, and the title waits on that thread at its presents. A lock and a copy saved per flush counts there. Two small things: - `flush_one` (`crates/victory-gpu/src/host.rs:14036`) used to call `Shadow::nothing_new`, which honours `VICTORY_PART_EXACT` (`watch::part_exact()` → `nothing_new_single(.., exact)`). `nothing_new_with` always takes the non-exact path. That is still correct (only more conservative), but with `PART_EXACT=1` fewer `flush_one` calls are skipped than before. Worth a line in the description, or a fallback to the old call when `part_exact()` is on. - `ahead_snapshot_cached_reuses_until_gen_bumps` asserts that `AHEAD_GEN` does not move between two calls, but `AHEAD_GEN` is process-global, and any other test in the same binary that calls `ask_ahead`/`ask_once`/`ask_once_end` (the `refresh_tests` do) can bump it in between. The test could flake under the default parallel runner. Testing `ahead_snapshot_cached` against a local generation counter, or serialising with the watch tests' lock, would avoid that. It overlaps textually with #4423 and #4489 in `Batch::flush_all` (the `snapped` logic). #4423's bitset `sift_by` also reads `self.ahead_snap`, so whichever lands second needs `ensure_ahead_snap()` in front of it.
Author
Owner

@⁠YounesAH15 on GitHub · 2026-12-01 03:10 UTC · original

Thanks for the review @⁠IRennie74!

  1. Test isolation: Good catch on AHEAD_GEN being process-global under parallel cargo test. Pushed a commit introducing ahead_snapshot_with_gen(gen_counter, out, cached_gen) and updating ahead_snapshot_cached_reuses_until_gen_bumps to run against a local AtomicU64 (re-verified with cargo test -p victory-gpu ahead_snapshot_cached: 1 passed, 0 failed).
  2. VICTORY_PART_EXACT: On current main (e1251c31), Shadow::nothing_new and watch::nothing_new_single do not use VICTORY_PART_EXACT — both call WRITTEN_EPOCHS.all_within(...), differing only in borrowing &journal().ahead vs reusing &self.ahead_snap.
  3. Overlap note: Acknowledged on #4422 / #4488 — happy to rebase and place self.ensure_ahead_snap() in front of sift_by if those land first.
**@⁠YounesAH15** on GitHub · 2026-12-01 03:10 UTC · [original](https://github.com/trustno1-coder/VIctory-NVIDIA/pull/4532#issuecomment-7299409648) Thanks for the review @⁠IRennie74! 1. **Test isolation:** Good catch on `AHEAD_GEN` being process-global under parallel `cargo test`. Pushed a commit introducing `ahead_snapshot_with_gen(gen_counter, out, cached_gen)` and updating `ahead_snapshot_cached_reuses_until_gen_bumps` to run against a local `AtomicU64` (re-verified with `cargo test -p victory-gpu ahead_snapshot_cached`: 1 passed, 0 failed). 2. **`VICTORY_PART_EXACT`:** On current `main` (`e1251c31`), `Shadow::nothing_new` and `watch::nothing_new_single` do not use `VICTORY_PART_EXACT` — both call `WRITTEN_EPOCHS.all_within(...)`, differing only in borrowing `&journal().ahead` vs reusing `&self.ahead_snap`. 3. **Overlap note:** Acknowledged on `#4422` / `#4488` — happy to rebase and place `self.ensure_ahead_snap()` in front of `sift_by` if those land first.
github-bot force-pushed github-bot/gh-4532 from 37d2ca73d2 to bc9a337cd9 2026-12-01 07:48:10 +00:00 Compare
github-bot closed this pull request 2026-12-01 18:43:08 +00:00
Author
Owner

@⁠trustno1-coder on GitHub · 2026-12-01 18:38 UTC · original

Closing this as part of moving main to the texture and video-memory rework (v2026.12.01.01-test). A lot of the code changed, and this pull request no longer applies cleanly to it; it would need to be rewritten against the new main rather than rebased. Thank you for the work. If the change is still needed, please open a new pull request based on the current main.

**@⁠trustno1-coder** on GitHub · 2026-12-01 18:38 UTC · [original](https://github.com/trustno1-coder/VIctory-NVIDIA/pull/4532#issuecomment-7321255740) Closing this as part of moving main to the texture and video-memory rework (v2026.12.01.01-test). A lot of the code changed, and this pull request no longer applies cleanly to it; it would need to be rewritten against the new main rather than rebased. Thank you for the work. If the change is still needed, please open a new pull request based on the 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!4533
No description provided.