victory-gpu: batch heap sync and loose Execute slots #127

Manually merged
github merged 3 commits from github-bot/gh-127 into gh-pr/127/base 2026-12-01 17:04:59 +00:00 AGit
Owner

@⁠olafix52 on GitHub · 2026-12-01 16:50 UTC · original
olafix52:perf/heap-sync-batch → main · +269 −180 · 4 files · 3 commits · merged 2026-12-01 17:03 UTC

Targets docs/TASKS.md item "The serial frame chain" (title:heap-sync).

Problem

On placed shader-visible heaps, sync_heaps (crates/victory-gpu/src/descriptor/sync.rs) spent ~6.9 ms/frame on average in its "making" phase:

  • A second spread::each_view pass after page comparison, scanning a combined Vec<u32> on every victory-records worker thread.
  • Acquiring a per-slot mutex (index % 256), running atomic fetch_or/fetch_and RMWs, updating shared global atomics (SENT, LOOSE_READS) on every slot (~91k slots/frame), and building unused [u8; 20] payloads + GATHERED TLS vectors (record.rs).
  • Emitting ~83k loose slot updates per frame as individual MEMORY events before Execute, then re-sorting even when indices are already strictly monotonic.

Changes

  • D3D12 counterpart (rule 12): Matches HostHeap::mirror / sync_heaps (descriptor.rs, descriptor/sent.rs): only slots holding VIEW_TAG are sent, a previously sent slot that loses its view is sent once more to null it, and concurrent updates between descriptor::mirror (SentSlots::send) and sync_heaps (SentSlots::send_batch) preserve last-read ordering under the slot's stripe lock.
  • SentSlots block striping & send_batch (crates/victory-gpu/src/descriptor/sent.rs):
    • Group stripe mutexes into cache-line-aligned blocks of 1,024 slots (STRIPE_SHIFT = 10, STRIPES = 256). Because 1,024 is a multiple of 64, each u64 word in sent belongs exclusively to one stripe lock, allowing note to use load(Relaxed) + conditional store(Relaxed) without atomic RMWs.
    • Added SentSlots::send_batch to lock once per contiguous block run, reserve one contiguous LOOSE_READS sequence range while holding the stripe lock, and accumulate local SENT counters before flushing once at the end.
  • Single-pass sync_heaps (crates/victory-gpu/src/descriptor/sync.rs):
    • Collapsed the two spread::each_view passes into one, running sink.send_batch directly while each chunk's changed slots are warm in cache, then appending chunks to LOOSE under a single lock via record::extend_loose.
  • Recorder & decoder packing (crates/victory-gpu/src/record.rs, greenfield/vkback/src/live/liststage.rs):
    • Removed eager [u8; 20] stack formatting outside OPEN.depth > 0 and removed GATHERED TLS (loose_together); unified 20-byte slot encoding into append_slot.
    • Replaced latest_by_handle + per-slot MEMORY emission with pack_loose (adds an O(n) no-sort fast path when handles are strictly increasing) emitting a single SLOTS event before Execute, matching Decoder::event in liststage.rs with self.pending.reserve(payload.len() / 20).

Measurement (interleaved A/B/B/A, RX 9070 XT, 5ad3422 vs candidate, standard 0..140 s window, VKB_LAT + frames.py --no-perf)

Metric A1 (5ad3422) B1 (heap-sync-batch) B2 (heap-sync-batch) A2 (5ad3422) Mean delta
FPS 25.59 27.98 27.86 25.26 +2.50 fps (+9.8%)
Mean frame 39.1 ms 35.7 ms 35.9 ms 39.6 ms −3.55 ms (−9.0%)
p50 / p90 / p99 38.0 / 54.7 / 67.2 35.1 / 42.8 / 52.9 35.5 / 43.4 / 54.1 39.9 / 52.3 / 64.3 −3.65 / −10.40 / −12.25 ms
Time > 33.3 ms 28,085 ms 14,780 ms 15,909 ms 28,835 ms −13,116 ms (−46.1%)
title:heap-sync (>33.3 ms) 455 fr (10,346 ms) 227 fr (2,226 ms) 356 fr (3,356 ms) 948 fr (15,342 ms) −78% ms
sync_heaps total (log.txt) 10.97 ms/f 5.08 ms/f 6.15 ms/f 11.10 ms/f −5.42 ms/f (−49%)
making (log.txt) 6.91 ms/f 0.58 ms/f 1.00 ms/f 6.89 ms/f −6.11 ms/f (−89%)
Renderer / GPU busy / title wait 26.1 / 26.9 / 10.6 ms 25.3 / 26.2 / 8.9 ms 24.8 / 26.5 / 9.4 ms 25.2 / 27.4 / 12.1 ms No refill loop

Verification

  • Clippy (-D warnings):
    • cargo xwin clippy --workspace --all-targets --all-features --target x86_64-pc-windows-msvc --locked -- -D warnings: pass (verified after rebase onto 3465541).
    • cargo xwin clippy --workspace --all-targets --target x86_64-pc-windows-msvc --locked -- -D warnings: pass (verified without diagnostics after #119).
    • Native crates (victory-shader, victory-formats, victory-core, xtask) and greenfield: pass.
  • Tests:
    • cargo xtask policy: 521 files OK.
    • vkback --lib (155 passed, 3 ignored) and vkback --test policy (6 passed).
    • Windows PE victory-gpu unit tests under Wine (descriptor::sent::tests, record::loose_tests, descriptor::copied::changed_records_tests): 6/6 passed.
  • Loud errors (rule 9f): Checked loud.txt / ERROR rule32 on all four runs — zero new errors (only existing known items already tracked in docs/TASKS.md).
  • Vulkan validation run (rule 30): Not run — changes are confined to PE CPU heap-sync/slot packing and a single Vec::reserve in liststage.rs; Vulkan API calls and emitted SPIR-V are unchanged.
**@⁠olafix52** on GitHub · 2026-12-01 16:50 UTC · [original](https://github.com/vktory6/VKtory/pull/127) `olafix52:perf/heap-sync-batch` → `main` · +269 −180 · 4 files · 3 commits · merged 2026-12-01 17:03 UTC Targets `docs/TASKS.md` item **"The serial frame chain"** (`title:heap-sync`). ### Problem On placed shader-visible heaps, `sync_heaps` (`crates/victory-gpu/src/descriptor/sync.rs`) spent ~6.9 ms/frame on average in its `"making"` phase: - A second `spread::each_view` pass after page comparison, scanning a combined `Vec<u32>` on every `victory-records` worker thread. - Acquiring a per-slot mutex (`index % 256`), running atomic `fetch_or`/`fetch_and` RMWs, updating shared global atomics (`SENT`, `LOOSE_READS`) on every slot (~91k slots/frame), and building unused `[u8; 20]` payloads + `GATHERED` TLS vectors (`record.rs`). - Emitting ~83k loose slot updates per frame as individual `MEMORY` events before `Execute`, then re-sorting even when indices are already strictly monotonic. ### Changes - **D3D12 counterpart (rule 12):** Matches `HostHeap::mirror` / `sync_heaps` (`descriptor.rs`, `descriptor/sent.rs`): only slots holding `VIEW_TAG` are sent, a previously sent slot that loses its view is sent once more to null it, and concurrent updates between `descriptor::mirror` (`SentSlots::send`) and `sync_heaps` (`SentSlots::send_batch`) preserve last-read ordering under the slot's stripe lock. - **`SentSlots` block striping & `send_batch` (`crates/victory-gpu/src/descriptor/sent.rs`):** - Group stripe mutexes into cache-line-aligned blocks of 1,024 slots (`STRIPE_SHIFT = 10`, `STRIPES = 256`). Because 1,024 is a multiple of 64, each `u64` word in `sent` belongs exclusively to one stripe lock, allowing `note` to use `load(Relaxed)` + conditional `store(Relaxed)` without atomic RMWs. - Added `SentSlots::send_batch` to lock once per contiguous block run, reserve one contiguous `LOOSE_READS` sequence range while holding the stripe lock, and accumulate local `SENT` counters before flushing once at the end. - **Single-pass `sync_heaps` (`crates/victory-gpu/src/descriptor/sync.rs`):** - Collapsed the two `spread::each_view` passes into one, running `sink.send_batch` directly while each chunk's changed slots are warm in cache, then appending chunks to `LOOSE` under a single lock via `record::extend_loose`. - **Recorder & decoder packing (`crates/victory-gpu/src/record.rs`, `greenfield/vkback/src/live/liststage.rs`):** - Removed eager `[u8; 20]` stack formatting outside `OPEN.depth > 0` and removed `GATHERED` TLS (`loose_together`); unified 20-byte slot encoding into `append_slot`. - Replaced `latest_by_handle` + per-slot `MEMORY` emission with `pack_loose` (adds an O(n) no-sort fast path when handles are strictly increasing) emitting a single `SLOTS` event before `Execute`, matching `Decoder::event` in `liststage.rs` with `self.pending.reserve(payload.len() / 20)`. ### Measurement (interleaved A/B/B/A, RX 9070 XT, `5ad3422` vs candidate, standard `0..140 s` window, `VKB_LAT` + `frames.py --no-perf`) | Metric | A1 (`5ad3422`) | B1 (`heap-sync-batch`) | B2 (`heap-sync-batch`) | A2 (`5ad3422`) | Mean delta | | --- | ---: | ---: | ---: | ---: | ---: | | **FPS** | 25.59 | **27.98** | **27.86** | 25.26 | **+2.50 fps (+9.8%)** | | **Mean frame** | 39.1 ms | **35.7 ms** | **35.9 ms** | 39.6 ms | **−3.55 ms (−9.0%)** | | **p50 / p90 / p99** | 38.0 / 54.7 / 67.2 | **35.1 / 42.8 / 52.9** | **35.5 / 43.4 / 54.1** | 39.9 / 52.3 / 64.3 | **−3.65 / −10.40 / −12.25 ms** | | **Time > 33.3 ms** | 28,085 ms | **14,780 ms** | **15,909 ms** | 28,835 ms | **−13,116 ms (−46.1%)** | | **`title:heap-sync` (>33.3 ms)** | 455 fr (10,346 ms) | **227 fr (2,226 ms)** | **356 fr (3,356 ms)** | 948 fr (15,342 ms) | **−78% ms** | | **`sync_heaps` total (`log.txt`)** | 10.97 ms/f | **5.08 ms/f** | **6.15 ms/f** | 11.10 ms/f | **−5.42 ms/f (−49%)** | | **`making` (`log.txt`)** | 6.91 ms/f | **0.58 ms/f** | **1.00 ms/f** | 6.89 ms/f | **−6.11 ms/f (−89%)** | | **Renderer / GPU busy / title wait** | 26.1 / 26.9 / 10.6 ms | **25.3 / 26.2 / 8.9 ms** | **24.8 / 26.5 / 9.4 ms** | 25.2 / 27.4 / 12.1 ms | No refill loop | ### Verification - **Clippy (`-D warnings`):** - `cargo xwin clippy --workspace --all-targets --all-features --target x86_64-pc-windows-msvc --locked -- -D warnings`: pass (verified after rebase onto `3465541`). - `cargo xwin clippy --workspace --all-targets --target x86_64-pc-windows-msvc --locked -- -D warnings`: pass (verified without `diagnostics` after `#119`). - Native crates (`victory-shader`, `victory-formats`, `victory-core`, `xtask`) and `greenfield`: pass. - **Tests:** - `cargo xtask policy`: 521 files OK. - `vkback --lib` (155 passed, 3 ignored) and `vkback --test policy` (6 passed). - Windows PE `victory-gpu` unit tests under Wine (`descriptor::sent::tests`, `record::loose_tests`, `descriptor::copied::changed_records_tests`): 6/6 passed. - **Loud errors (rule 9f):** Checked `loud.txt` / `ERROR rule32` on all four runs — zero new errors (only existing known items already tracked in `docs/TASKS.md`). - **Vulkan validation run (rule 30):** Not run — changes are confined to PE CPU heap-sync/slot packing and a single `Vec::reserve` in `liststage.rs`; Vulkan API calls and emitted SPIR-V are unchanged.
On placed shader-visible heaps, `sync_heaps` spent ~6.9 ms/frame in its
"making" pass: a second `spread::each_view` job pass locking 256 slot
stripes (`index % 256`), re-reading title memory, running per-slot atomic
RMWs and `SENT`/`LOOSE_READS` atomic increments across ~91k slots/frame, and
emitting tens of thousands of separate `MEMORY` events before `Execute`.

Matches the D3D12 counterpart (`HostHeap::mirror` / `sync_heaps`: send
live view writes and once when a previously sent view goes null, preserving
last-read order against concurrent `descriptor::mirror`):
- `SentSlots` (`crates/victory-gpu/src/descriptor/sent.rs`): replace per-slot
  `index % 256` mutexes with cache-line-aligned block stripes of 1,024 slots
  (`STRIPE_SHIFT = 10`, a multiple of 64 so each `sent` `u64` word belongs
  exclusively to one stripe and `note` needs only `load` + conditional `store`
  without `fetch_or`/`fetch_and`), and add `SentSlots::send_batch` to lock
  once, reserve a contiguous `LOOSE_READS` sequence range under the stripe lock,
  and tally counters locally per contiguous block run.
- `sync_heaps` (`crates/victory-gpu/src/descriptor/sync.rs`): collapse the two
  `spread::each_view` passes into one, running `send_batch` immediately while
  the page's changed indices are hot in cache and merging batches into `LOOSE`
  with one lock per heap via `record::extend_loose`.
- `record` (`crates/victory-gpu/src/record.rs`) & `liststage`
  (`greenfield/vkback/src/live/liststage.rs`): drop unused per-slot `[u8; 20]`
  encoding outside `OPEN.depth > 0` and the `GATHERED` TLS (`loose_together`),
  unify 20-byte encoding in `append_slot`, add an O(n) no-sort fast path in
  `pack_loose` when handles are strictly increasing, emit `Execute` loose slots
  as one `SLOTS` event, and pre-reserve `self.pending` in `Decoder::event`.

Measured on main (5ad3422, interleaved A/B/B/A, 0..140 s):
- Mean frame time: 39.1 / 39.6 ms (25.59 / 25.26 fps) -> 35.7 / 35.9 ms
  (27.98 / 27.86 fps), +9.8% fps; p90 54.7 / 52.3 -> 42.8 / 43.4 ms;
  time above 33.3 ms: 28,085 / 28,835 ms -> 14,780 / 15,909 ms (-46%).
- `sync_heaps` (`log.txt`): 10.97 / 11.10 -> 5.08 / 6.15 ms/frame
  (`making` 6.91 / 6.89 -> 0.58 / 1.00 ms/frame).
- `loud.txt` / `ERROR rule32`: no new errors. Vulkan validation run not needed
  (rule 30): PE CPU path + `Vec::reserve` in decoder only; Vulkan calls and
  SPIR-V are unchanged.
github-bot locked as GitHub sync and limited conversation to collaborators 2026-12-01 16:54:17 +00:00
SYNCED slots 4-6 and SYNCED_KINDS (table/wait counts, the mirrored
kinds, the submission wait) were never written on main either; the
sync line printed them as zeros. They go; SYNCED keeps the four that
sync_heaps fills.
github-bot changed target branch from main to gh-pr/127/base 2026-12-01 17:04:57 +00:00
github manually merged commit 3901c2c5b3 into gh-pr/127/base 2026-12-01 17:04:58 +00:00
Author
Owner

@⁠vktory6 on GitHub · 2026-12-01 17:03 UTC · original

Merged as 63a18bf, thanks! The A/B/B/A table is very convincing: half of sync_heaps gone, -46 % time over 33.3 ms.

What I checked:

  • Order: sequence numbers are reserved under the same stripe lock as send, so the latest read still wins across the title's thread and the sync.
  • Fast path: pack_loose only skips the sort for strictly increasing handles.
  • Event format: vkback decodes the SLOTS event exactly like the old 20-byte MEMORY events (both go to pending for the Execute).
  • Runs: our check passed with the validation run (no new VUIDs) and a counter comparison against a main run (only streaming-timing noise).

I pushed one commit before merging: the sync line printed four counters that were never filled (SYNCED slots 4-6 and SYNCED_KINDS; already dead on main, your change made it visible). They're gone, and SYNCED keeps the four sync_heaps fills.

**@⁠vktory6** on GitHub · 2026-12-01 17:03 UTC · [original](https://github.com/vktory6/VKtory/pull/127#issuecomment-7318681395) Merged as 63a18bf, thanks! The A/B/B/A table is very convincing: half of `sync_heaps` gone, -46 % time over 33.3 ms. What I checked: - **Order:** sequence numbers are reserved under the same stripe lock as `send`, so the latest read still wins across the title's thread and the sync. - **Fast path:** `pack_loose` only skips the sort for strictly increasing handles. - **Event format:** vkback decodes the `SLOTS` event exactly like the old 20-byte `MEMORY` events (both go to `pending` for the Execute). - **Runs:** our check passed with the validation run (no new VUIDs) and a counter comparison against a main run (only streaming-timing noise). I pushed one commit before merging: the sync line printed four counters that were never filled (`SYNCED` slots 4-6 and `SYNCED_KINDS`; already dead on main, your change made it visible). They're gone, and `SYNCED` keeps the four `sync_heaps` fills.
This discussion has been locked. Commenting is limited to contributors.
No description provided.