perf(victory-gpu): coalesce heap_twin write-watch queries, batch ahead syncs, and defer heap_marks floor sampling #12

Manually merged
github merged 1 commit from github-bot/gh-12 into mega-upgrade-v3 2026-12-04 20:20:05 +00:00 AGit
Owner

@⁠MahanYTT on GitHub · 2026-12-04 18:27 UTC · original
MahanYTT:perf/heap-twin-watch → mega-upgrade-v3 · +395 −104 · 2 files · 1 commits · merged 2026-12-04 20:18 UTC

Root Cause

From victory-ma-play-20261204-165800-18832.log: heap_twin: 1.0 syncs (16.0 left out, covered), 2,229 parts of 4 KiB copied (8.7 MB), 0.09 ms copy (1.88 ms asking the write watch!); ahead: 8.1 syncs, 2.49 ms.

  1. Fragmented GetWriteWatch calls: pieces() split on every unused 2 MiB chunk and sync() forced 32 MiB (PIECE = 16) splits whenever needed != 0 or ours (and can_join() checked all chunks including unused 0-epoch chunks), multiplying GetWriteWatch(WRITE_WATCH_FLAG_RESET) kernel calls (30–70 µs fixed cost each) and contending on Windows kernel address-space locks.
  2. Unbatched ahead thread: ahead() woke and ran a full-heap write-watch scan on every title_submits burst (8.1 syncs/frame, 2.49 ms/frame), did not clear OURS under SYNC_LOCK, and title_submits locked WAKE.0 on every title submission.
  3. Hot-path atomic RMWs: note_used() executed an unconditional used.swap(true, SeqCst) on every CBV/draw lookup even when chunks were already marked used.
  4. Redundant heap_marks lock acquisitions: heap_marks::prep_wrote() called descriptor::round_mark() (locking SYNC_ROUNDS.0) and PREP_FLOOR.fetch_max() on every Step::Placed / Step::Run / flush_copy during submission prep.

Changes

  • crates/victory-gpu/src/heap_twin.rs:
    • Bridge small unused gaps (GAP_CHUNKS = 4, 8 MiB, matching watch::AHEAD_GAP) in coalesced_pieces() and query full coalesced runs with automatic fallback to (Some(PIECE), 0) if the kernel refuses a wide range.
    • Filter returned (page, write_epoch) entries per chunk (filter_written() + copy_runs_used() + advance_used()) so bridged unused gaps keep after == 0 and mixed-epoch chunks never replay older CPU writes over shader data, while skipping parts_written() and extra allocations when no pages changed.
    • Coalesce rapid title_submits bursts in ahead() (AHEAD_BATCH = 4, AHEAD_COALESCE_US = 1500, tunable via VICTORY_TWIN_AHEAD_BATCH / VICTORY_TWIN_AHEAD_COALESCE_US), handle/clear OURS under SYNC_LOCK, and gate WAKE.0 locking in title_submits via AHEAD_WAKE_SEQ.
    • Avoid hot-path atomic RMWs in note_used() with a read-only Relaxed check before swap(true, SeqCst).
  • crates/victory-gpu/src/heap_marks.rs:
    • Replace immediate round_mark() lock acquisition in prep_wrote() with a PREP_DIRTY flag flushed once at set_mark(0) (after all submission writes complete) or needed(), avoiding repeated SYNC_ROUNDS mutex locks per submission.
    • Make round_after overflow-safe (saturating_sub / saturating_add).

Verification

  • clippy-driver --edition=2024 --crate-type=lib -D warnings and --test -D warnings: 0 warnings, 0 errors.
  • Unit tests: 14 passed, 0 failed (including new tests for coalesced_pieces, filter_written, advance_used, ahead_action, and prep_dirty_coalesces_floor_samples).
**@⁠MahanYTT** on GitHub · 2026-12-04 18:27 UTC · [original](https://github.com/MahanYTT/VIctory-MA/pull/12) `MahanYTT:perf/heap-twin-watch` → `mega-upgrade-v3` · +395 −104 · 2 files · 1 commits · merged 2026-12-04 20:18 UTC ## Root Cause From `victory-ma-play-20261204-165800-18832.log`: `heap_twin: 1.0 syncs (16.0 left out, covered), 2,229 parts of 4 KiB copied (8.7 MB), 0.09 ms copy (1.88 ms asking the write watch!); ahead: 8.1 syncs, 2.49 ms`. 1. **Fragmented `GetWriteWatch` calls**: `pieces()` split on every unused 2 MiB chunk and `sync()` forced 32 MiB (`PIECE = 16`) splits whenever `needed != 0` or `ours` (and `can_join()` checked all chunks including unused `0`-epoch chunks), multiplying `GetWriteWatch(WRITE_WATCH_FLAG_RESET)` kernel calls (30–70 µs fixed cost each) and contending on Windows kernel address-space locks. 2. **Unbatched `ahead` thread**: `ahead()` woke and ran a full-heap write-watch scan on every `title_submits` burst (8.1 syncs/frame, 2.49 ms/frame), did not clear `OURS` under `SYNC_LOCK`, and `title_submits` locked `WAKE.0` on every title submission. 3. **Hot-path atomic RMWs**: `note_used()` executed an unconditional `used.swap(true, SeqCst)` on every CBV/draw lookup even when chunks were already marked used. 4. **Redundant `heap_marks` lock acquisitions**: `heap_marks::prep_wrote()` called `descriptor::round_mark()` (locking `SYNC_ROUNDS.0`) and `PREP_FLOOR.fetch_max()` on every `Step::Placed` / `Step::Run` / `flush_copy` during submission prep. ## Changes - **`crates/victory-gpu/src/heap_twin.rs`**: - Bridge small unused gaps (`GAP_CHUNKS = 4`, 8 MiB, matching `watch::AHEAD_GAP`) in `coalesced_pieces()` and query full coalesced runs with automatic fallback to `(Some(PIECE), 0)` if the kernel refuses a wide range. - Filter returned `(page, write_epoch)` entries per chunk (`filter_written()` + `copy_runs_used()` + `advance_used()`) so bridged unused gaps keep `after == 0` and mixed-epoch chunks never replay older CPU writes over shader data, while skipping `parts_written()` and extra allocations when no pages changed. - Coalesce rapid `title_submits` bursts in `ahead()` (`AHEAD_BATCH = 4`, `AHEAD_COALESCE_US = 1500`, tunable via `VICTORY_TWIN_AHEAD_BATCH` / `VICTORY_TWIN_AHEAD_COALESCE_US`), handle/clear `OURS` under `SYNC_LOCK`, and gate `WAKE.0` locking in `title_submits` via `AHEAD_WAKE_SEQ`. - Avoid hot-path atomic RMWs in `note_used()` with a read-only `Relaxed` check before `swap(true, SeqCst)`. - **`crates/victory-gpu/src/heap_marks.rs`**: - Replace immediate `round_mark()` lock acquisition in `prep_wrote()` with a `PREP_DIRTY` flag flushed once at `set_mark(0)` (after all submission writes complete) or `needed()`, avoiding repeated `SYNC_ROUNDS` mutex locks per submission. - Make `round_after` overflow-safe (`saturating_sub` / `saturating_add`). ## Verification - `clippy-driver --edition=2024 --crate-type=lib -D warnings` and `--test -D warnings`: 0 warnings, 0 errors. - Unit tests: 14 passed, 0 failed (including new tests for `coalesced_pieces`, `filter_written`, `advance_used`, `ahead_action`, and `prep_dirty_coalesces_floor_samples`).
github-bot locked as GitHub sync and limited conversation to collaborators 2026-12-04 18:27:27 +00:00
github manually merged commit 4ba58378c8 into mega-upgrade-v3 2026-12-04 20:20:04 +00:00
This discussion has been locked. Commenting is limited to contributors.
No description provided.