perf(victory-gpu): enable thin-list state cache under VICTORY_STATE_CACHE and optimize thin/rootcache hot paths #7

Manually merged
github merged 2 commits from github-bot/gh-7 into mega-upgrade-v3 2026-12-04 20:20:04 +00:00 AGit
Owner

@⁠MahanYTT on GitHub · 2026-12-04 17:49 UTC · original
MahanYTT:perf/thin-state-cache → mega-upgrade-v3 · +727 −123 · 2 files · 2 commits · merged 2026-12-04 20:18 UTC

Root Causes Found

  1. VICTORY_STATE_CACHE=1 never enabled thin-list state elision (repeated state calls left out 0 on all 538 log lines):
    • thin::coalesce() was gated solely behind an off-by-default VICTORY_THIN_COALESCE=1 env var and ignored VICTORY_STATE_CACHE (crate::rootcache::on()).
  2. thin::repeats() only caught immediately-adjacent identical plain calls in the same un-handed-over chunk:
    • Only checked op <= Plain::SET_PIPELINE against self.last, missing non-adjacent state re-binds across draws, calls spanning hand_over() piece splits (~303 pieces/frame), SAMPLE_MASK, and all put_with state calls (VIEWPORTS, SCISSORS, BLEND_FACTOR, HEAPS, COMPUTE_CONSTANTS, GRAPHICS_CONSTANTS, INDEX_BUFFER, VERTEX_BUFFERS).
  3. Additional hot-path CPU bottlenecks in thin.rs and rootcache.rs:
    • Global REPEATS.fetch_add(1, Relaxed) on every elided call would bounce a shared cache line across title threads; now batched per Front and flushed on hand_over() / run_here() / leave().
    • run_plain used match guards (n if n == Plain::$op as u64), preventing LLVM jump-table generation across 45k+ calls/frame; replaced with plain_op::* constants.
    • binds_only(op) did a 20-element linear array scan on every command when VICTORY_WAIT_SHARE=1; replaced with O(1) range checks.
    • Recording::drop allocated a temporary Vec<Vec<u64>> on every dropped recording to call give_back; changed give_back to impl IntoIterator<Item = Vec<u64>>.
    • rootcache::Side::clear() memset 1,024 bytes ([Given; 64]) on every root signature / heap / reset invalidation; replaced with a given_known: u64 bitmask (O(1) clear).
    • rootcache::Cache only flushed issued/skipped stats on Drop, so pooled command lists in BACKS never reported live telemetry; now flushes on clear() as well as Drop without double-counting.

Changes (Exclusive Scope)

  • crates/victory-gpu/src/list/thin.rs
    • Wired coalesce() to honor explicit VICTORY_THIN_COALESCE and otherwise follow crate::rootcache::on() (VICTORY_STATE_CACHE), with runtime toggle sync (Front::sync_state_cache).
    • Added per-Front FrontState guest-state shadow cache covering SET_PIPELINE, TOPOLOGY, STENCIL_REF, DEPTH_BOUNDS, SAMPLE_MASK, graphics/compute root tables, root views, single/multi-dword root constants, VIEWPORTS, SCISSORS, BLEND_FACTOR, HEAPS, INDEX_BUFFER, and VERTEX_BUFFERS.
    • Preserved exact invalidation on RESET, CLEAR_STATE, MIRROR_FRAME (invalidates cached pipeline so mirror-frame binds always emit), Front::leave(), and dynamic switch flips, while persisting across hand_over() piece splits.
    • Batched REPEATS updates per Front, converted run_plain to jump-table plain_op consts, converted binds_only to O(1) range checks, and removed the temporary Vec in Recording::drop.
  • crates/victory-gpu/src/rootcache.rs
    • Added given_known: u64 bitmask to Side so Side::clear() is O(1) instead of zeroing 1 KB per side.
    • Added Cache::flush_stats() called from both clear() and Drop so pooled command lists report live state-cache telemetry without double-counting.

Tests

  • Updated/added unit tests in crates/victory-gpu/src/list/thin.rs (repeat_tests): non-adjacent & cross-draw elision, work commands never elided, persistence across piece splits, invalidation on CLEAR_STATE/RESET, and FrontState root constants/views/IA/viewport/scissor/blend/heap semantics.
  • Added clear_flushes_stats_without_double_counting_on_drop and given_known assertions in crates/victory-gpu/src/rootcache.rs.
  • Verified rootcache.rs and FrontState/binds_only/try_slice_of logic locally with cargo test (14 passed, 0 failed) and cargo clippy --all-targets -- -D warnings (0 warnings); full Windows CI build/tests will run on this PR.
**@⁠MahanYTT** on GitHub · 2026-12-04 17:49 UTC · [original](https://github.com/MahanYTT/VIctory-MA/pull/7) `MahanYTT:perf/thin-state-cache` → `mega-upgrade-v3` · +727 −123 · 2 files · 2 commits · merged 2026-12-04 20:18 UTC ## Root Causes Found 1. **`VICTORY_STATE_CACHE=1` never enabled thin-list state elision (`repeated state calls left out 0` on all 538 log lines):** - `thin::coalesce()` was gated solely behind an off-by-default `VICTORY_THIN_COALESCE=1` env var and ignored `VICTORY_STATE_CACHE` (`crate::rootcache::on()`). 2. **`thin::repeats()` only caught immediately-adjacent identical plain calls in the same un-handed-over chunk:** - Only checked `op <= Plain::SET_PIPELINE` against `self.last`, missing non-adjacent state re-binds across draws, calls spanning `hand_over()` piece splits (~303 pieces/frame), `SAMPLE_MASK`, and all `put_with` state calls (`VIEWPORTS`, `SCISSORS`, `BLEND_FACTOR`, `HEAPS`, `COMPUTE_CONSTANTS`, `GRAPHICS_CONSTANTS`, `INDEX_BUFFER`, `VERTEX_BUFFERS`). 3. **Additional hot-path CPU bottlenecks in `thin.rs` and `rootcache.rs`:** - Global `REPEATS.fetch_add(1, Relaxed)` on every elided call would bounce a shared cache line across title threads; now batched per `Front` and flushed on `hand_over()` / `run_here()` / `leave()`. - `run_plain` used match guards (`n if n == Plain::$op as u64`), preventing LLVM jump-table generation across 45k+ calls/frame; replaced with `plain_op::*` constants. - `binds_only(op)` did a 20-element linear array scan on every command when `VICTORY_WAIT_SHARE=1`; replaced with O(1) range checks. - `Recording::drop` allocated a temporary `Vec<Vec<u64>>` on every dropped recording to call `give_back`; changed `give_back` to `impl IntoIterator<Item = Vec<u64>>`. - `rootcache::Side::clear()` memset 1,024 bytes (`[Given; 64]`) on every root signature / heap / reset invalidation; replaced with a `given_known: u64` bitmask (O(1) clear). - `rootcache::Cache` only flushed `issued`/`skipped` stats on `Drop`, so pooled command lists in `BACKS` never reported live telemetry; now flushes on `clear()` as well as `Drop` without double-counting. ## Changes (Exclusive Scope) - `crates/victory-gpu/src/list/thin.rs` - Wired `coalesce()` to honor explicit `VICTORY_THIN_COALESCE` and otherwise follow `crate::rootcache::on()` (`VICTORY_STATE_CACHE`), with runtime toggle sync (`Front::sync_state_cache`). - Added per-`Front` `FrontState` guest-state shadow cache covering `SET_PIPELINE`, `TOPOLOGY`, `STENCIL_REF`, `DEPTH_BOUNDS`, `SAMPLE_MASK`, graphics/compute root tables, root views, single/multi-dword root constants, `VIEWPORTS`, `SCISSORS`, `BLEND_FACTOR`, `HEAPS`, `INDEX_BUFFER`, and `VERTEX_BUFFERS`. - Preserved exact invalidation on `RESET`, `CLEAR_STATE`, `MIRROR_FRAME` (invalidates cached pipeline so mirror-frame binds always emit), `Front::leave()`, and dynamic switch flips, while persisting across `hand_over()` piece splits. - Batched `REPEATS` updates per `Front`, converted `run_plain` to jump-table `plain_op` consts, converted `binds_only` to O(1) range checks, and removed the temporary `Vec` in `Recording::drop`. - `crates/victory-gpu/src/rootcache.rs` - Added `given_known: u64` bitmask to `Side` so `Side::clear()` is O(1) instead of zeroing 1 KB per side. - Added `Cache::flush_stats()` called from both `clear()` and `Drop` so pooled command lists report live state-cache telemetry without double-counting. ## Tests - Updated/added unit tests in `crates/victory-gpu/src/list/thin.rs` (`repeat_tests`): non-adjacent & cross-draw elision, work commands never elided, persistence across piece splits, invalidation on `CLEAR_STATE`/`RESET`, and `FrontState` root constants/views/IA/viewport/scissor/blend/heap semantics. - Added `clear_flushes_stats_without_double_counting_on_drop` and `given_known` assertions in `crates/victory-gpu/src/rootcache.rs`. - Verified `rootcache.rs` and `FrontState`/`binds_only`/`try_slice_of` logic locally with `cargo test` (14 passed, 0 failed) and `cargo clippy --all-targets -- -D warnings` (0 warnings); full Windows CI build/tests will run on this PR.
github-bot locked as GitHub sync and limited conversation to collaborators 2026-12-04 17:49:58 +00:00
github manually merged commit 4ba58378c8 into mega-upgrade-v3 2026-12-04 20:20:02 +00:00
This discussion has been locked. Commenting is limited to contributors.
No description provided.