Reduce allocations and prefetch in fill and write-watch hot paths #478

Open
github-bot wants to merge 3 commits from github-bot/gh-478 into main AGit
Owner

@⁠GValdezN on GitHub · 2026-12-02 15:29 UTC · original
GValdezN:perf-quick-wins → main · +1129 −296 · 13 files · 3 commits · open

Reduce allocations and prefetch in fill and write-watch hot paths; CCD pinning, journal sharding, async copy queue and enhanced barriers

Three commits on the submitting thread's critical path:

Commit 1: Prefetch + Vec::with_capacity

Prefetch in Batch::fill(): the fill loop prefetches the next entry's source and destination addresses with _mm_prefetch(..., _MM_HINT_T0) one iteration ahead, in both the single-threaded path (< 96 fills) and the wide path (>= 96, dispatched to victory-wide-N workers via wide_each). The loop uses indexed access (self.fills[i]) with .clear() at the end instead of .drain(..) to avoid iterator overhead.

Vec::with_capacity on seven hot-path allocations in watch.rs: each vector is sized to the element count known at construction — kernel_written, ask_ahead runs, ask_rest written/extra, writes, written_ranges_after, guard_locked.

SPLIT[7] (copies and fills) accounts for 4–8 ms/frame; SPLIT[0] (write-watch asking) accounts for 5–10 ms. Complements #460 (skip redundant guard_ahead).

Commit 2: Four gated features + batch unguard

VICTORY_CCD_PIN=1: pins the submitting thread and its helpers (victory-submit, victory-send, victory-prelude, victory-ask, victory-wide) to the last-level cache group (CCD) they start on, so they share one L3 on dual-CCD Ryzen processors.

VICTORY_JOURNAL_SHARDS=1: splits the JOURNAL's written, written_fast and parts maps into 16 RwLock-guarded shards (page % 16), with a separate RwLock for the global ahead/kept state. Readers lock one shard per page instead of the whole journal. Complements the summary bitmap (#472) and the striped VirtualProtect (#434).

VICTORY_COPY_AHEAD=1: records CopyBufferRegion calls on a dedicated D3D12_COMMAND_LIST_TYPE_COPY queue, so the GPU's copy DMA engine runs in parallel with the previous frame's rendering. The DIRECT queue does a GPU-side Wait on the copy fence before barriers and scatter dispatches.

VICTORY_ENHANCED_BARRIERS=1: on GPUs that report EnhancedBarriersSupported, emits D3D12_BUFFER_BARRIER with precise SyncBefore/SyncAfter scopes instead of legacy transitions. Phase 1 covers buffer-only sites in card_steps.rs and host.rs.

Batch unguard (always on): contiguous blocks in unguard() are merged into single VirtualProtect calls with per-block fallback when the range spans two VirtualAlloc regions.

Commit 3: Enhanced barriers phase 2

Mechanically migrates 25 remaining single-barrier buffer transition sites across 8 files to buffer_barrier(). Net −45 lines. The 15 remaining ResourceBarrier sites are all textures (back buffers, render targets, depth stencils) — left for phase 3.

Compatibility

  • #434 (striped VirtualProtect): complementary — different locks (VP_STRIPE vs JOURNAL shards), both touch unguard() but reduce different costs (syscall count vs lock hold time). Will need merge reconciliation.
  • #433 (retirement shards): no conflict — different data structures, different files.
  • #457 (protect/open_unlocked race fix): no direct conflict — we don't touch protect_here/protect_alias/open_unlocked.
  • #460 (skip redundant guard_ahead): complementary — both reduce submit-thread overhead on different phases.
  • #472 (summary bitmap): complementary — bitmap is the first-level skip, sharding reduces contention on the remaining lookups.
  • #473 (merge fills): complementary — merged fills + prefetch together reduce SPLIT[7].

cargo test -p victory-gpu: 620 passed, 0 failed.

🤖 Generated with Claude Code

**@⁠GValdezN** on GitHub · 2026-12-02 15:29 UTC · [original](https://github.com/readmet3xt-lab/foxhound/pull/478) `GValdezN:perf-quick-wins` → `main` · +1129 −296 · 13 files · 3 commits · open ## Reduce allocations and prefetch in fill and write-watch hot paths; CCD pinning, journal sharding, async copy queue and enhanced barriers Three commits on the submitting thread's critical path: ### Commit 1: Prefetch + Vec::with_capacity **Prefetch in `Batch::fill()`:** the fill loop prefetches the next entry's source and destination addresses with `_mm_prefetch(..., _MM_HINT_T0)` one iteration ahead, in both the single-threaded path (< 96 fills) and the wide path (>= 96, dispatched to `victory-wide-N` workers via `wide_each`). The loop uses indexed access (`self.fills[i]`) with `.clear()` at the end instead of `.drain(..)` to avoid iterator overhead. **`Vec::with_capacity` on seven hot-path allocations in `watch.rs`:** each vector is sized to the element count known at construction — `kernel_written`, `ask_ahead` runs, `ask_rest` written/extra, `writes`, `written_ranges_after`, `guard_locked`. SPLIT[7] (copies and fills) accounts for 4–8 ms/frame; SPLIT[0] (write-watch asking) accounts for 5–10 ms. Complements #460 (skip redundant `guard_ahead`). ### Commit 2: Four gated features + batch unguard **`VICTORY_CCD_PIN=1`:** pins the submitting thread and its helpers (`victory-submit`, `victory-send`, `victory-prelude`, `victory-ask`, `victory-wide`) to the last-level cache group (CCD) they start on, so they share one L3 on dual-CCD Ryzen processors. **`VICTORY_JOURNAL_SHARDS=1`:** splits the JOURNAL's `written`, `written_fast` and `parts` maps into 16 `RwLock`-guarded shards (`page % 16`), with a separate `RwLock` for the global `ahead`/`kept` state. Readers lock one shard per page instead of the whole journal. Complements the summary bitmap (#472) and the striped VirtualProtect (#434). **`VICTORY_COPY_AHEAD=1`:** records `CopyBufferRegion` calls on a dedicated `D3D12_COMMAND_LIST_TYPE_COPY` queue, so the GPU's copy DMA engine runs in parallel with the previous frame's rendering. The DIRECT queue does a GPU-side Wait on the copy fence before barriers and scatter dispatches. **`VICTORY_ENHANCED_BARRIERS=1`:** on GPUs that report `EnhancedBarriersSupported`, emits `D3D12_BUFFER_BARRIER` with precise `SyncBefore`/`SyncAfter` scopes instead of legacy transitions. Phase 1 covers buffer-only sites in `card_steps.rs` and `host.rs`. **Batch unguard (always on):** contiguous blocks in `unguard()` are merged into single `VirtualProtect` calls with per-block fallback when the range spans two `VirtualAlloc` regions. ### Commit 3: Enhanced barriers phase 2 Mechanically migrates 25 remaining single-barrier buffer transition sites across 8 files to `buffer_barrier()`. Net −45 lines. The 15 remaining `ResourceBarrier` sites are all textures (back buffers, render targets, depth stencils) — left for phase 3. ### Compatibility - **#434** (striped VirtualProtect): complementary — different locks (VP_STRIPE vs JOURNAL shards), both touch `unguard()` but reduce different costs (syscall count vs lock hold time). Will need merge reconciliation. - **#433** (retirement shards): no conflict — different data structures, different files. - **#457** (protect/open_unlocked race fix): no direct conflict — we don't touch `protect_here`/`protect_alias`/`open_unlocked`. - **#460** (skip redundant guard_ahead): complementary — both reduce submit-thread overhead on different phases. - **#472** (summary bitmap): complementary — bitmap is the first-level skip, sharding reduces contention on the remaining lookups. - **#473** (merge fills): complementary — merged fills + prefetch together reduce SPLIT[7]. `cargo test -p victory-gpu`: 620 passed, 0 failed. 🤖 Generated with [Claude Code](https://claude.com/claude-code)
Software prefetch of the next fill's source and destination in Batch::fill()
to hide cache-miss latency across the thousands of small scattered copies each
frame. Vec::with_capacity in seven hot-path allocations in watch.rs (kernel_written,
ask_ahead, ask_rest, writes, written_ranges_after, guard_locked) sized to the
known element count, avoiding re-allocation during the frame.

Co-Authored-By: Claude Opus 4.6 <[email protected]>
github-bot locked as GitHub sync and limited conversation to collaborators 2026-12-02 15:33:23 +00:00
Four gated features, all off by default:

VICTORY_CCD_PIN=1: pins the submitting thread and its helpers (victory-submit,
victory-send, victory-prelude, victory-ask, victory-wide) to the last-level
cache group (CCD) they start on, so they share one L3 on dual-CCD Ryzen
processors instead of bouncing lines between two. The infrastructure
(CpuSet with LastLevelCacheIndex, ccd_groups, pin_to_ccd) and unit tests
are added to cores.rs; the five wiring calls go beside the existing
keep_on_fast_cores / keep_helper_on_fast_cores calls.

VICTORY_JOURNAL_SHARDS=1: splits the JOURNAL's written, written_fast and
parts maps into 16 RwLock-guarded shards (page % 16), with a separate
RwLock for the global ahead/kept state. Readers that check nothing_new
lock one shard per page instead of the whole journal. Writers (note) lock
only the target shard. The sharded path runs beside the original single-lock
path, chosen once at startup. Complements the summary bitmap (#472) and
the striped VirtualProtect (#434).

VICTORY_COPY_AHEAD=1: records CopyBufferRegion calls on a dedicated
D3D12_COMMAND_LIST_TYPE_COPY queue with its own fence, so the GPU's copy
DMA engine runs in parallel with the previous frame's rendering. The
DIRECT queue does a GPU-side Wait on the copy fence before barriers and
scatter dispatches. Staging buffer lifetime is unchanged.

VICTORY_ENHANCED_BARRIERS=1: on GPUs that report EnhancedBarriersSupported,
emits D3D12_BUFFER_BARRIER with precise SyncBefore/SyncAfter scopes instead
of legacy D3D12_RESOURCE_BARRIER transitions. Phase 1 covers the buffer-only
barrier sites in card_steps.rs and the scratch-buffer transitions in host.rs.
Texture barriers are left for a later phase.

Also batches contiguous blocks in unguard() into single VirtualProtect calls
with per-block fallback when the range spans two VirtualAlloc regions.

cargo test -p victory-gpu: 620 passed, 0 failed.

Co-Authored-By: Claude Opus 4.6 <[email protected]>
Mechanically replaces list.ResourceBarrier(&[transition(resource, A, B)])
with buffer_barrier(list, resource, A, B) at every remaining single-barrier
buffer site:

  host.rs           7 sites (scratch, between, staging transitions)
  indirect.rs       9 sites (compute scratch, indirect argument buffers)
  indirect/bench.rs 2 sites (benchmark scratch transitions)
  host/card_steps.rs 1 site (between buffer)
  host/panel.rs     1 site (panel buffer) + removed unused transition import
  host/place.rs     2 sites (placement scratch)
  host/upcheck.rs   2 sites (upcheck scratch)
  tiles.rs          1 site (tile scratch)

buffer_barrier() already handles the enhanced/legacy branch: on
VICTORY_ENHANCED_BARRIERS=1 it emits D3D12_BUFFER_BARRIER with precise sync
scopes, otherwise it falls back to the legacy transition.

The 15 remaining ResourceBarrier sites are all textures (back buffers, render
targets, depth stencils) or the buffer_barrier legacy fallback itself — left
for phase 3.

Net -45 lines: each migration replaces a 3–4 line unsafe block with a
single call.

620 tests passed, 0 failed.

Co-Authored-By: Claude Opus 4.6 <[email protected]>
Author
Owner

@⁠tillo88 on GitHub · 2026-12-02 21:06 UTC · original

Replay-bench measurement: no measurable difference on v0.8.3.3

Measured: PR head 06376bf merged onto release v0.8.3.3 (same code as main 7dbcdcb), 2 measured runs (after a warm-up run that is not counted), against 3 runs of v0.8.3.3 without the PR, same route (~6.5 min).

run fps CPU prep ms median ms p95 ms p99 ms frames > 100 ms
release run 1 36.54 19.08 29.00 50.42 60.01 32
release run 2 36.45 20.15 28.10 38.59 45.48 13
release run 3 36.15 19.30 29.35 38.74 46.08 10
PR #478 run 1 36.93 19.77 28.20 38.26 45.42 17
PR #478 run 2 36.68 18.79 28.70 37.90 45.21 15

Mean fps: release 36.38, PR 36.80 (+1.2%). That is below the 2-3% we treat as a real change, so: no measurable difference on this route. Only 3 release runs so far, so the release's own spread is not established yet.

Crashes: none in the PR runs and none in the release runs (no host GPU failure, no device lost).

Note: two earlier release runs the same evening, with identical switches, were much slower for most of the route (6-9 fps average); the cause is not known yet and they are left out of the comparison above. No PR run fell into that state.

Config: RTX 5070 Ti 16 GB, Ryzen 7 9800X3D, 32 GB, Windows 11; default play-foxhound.ps1 settings (Series X profile, 1440p, DLSS balanced, no frame generation), fullscreen, no frame cap.

Measured unattended on a replay bench (same save, recorded controller input); numbers and text prepared with Claude.

**@⁠tillo88** on GitHub · 2026-12-02 21:06 UTC · [original](https://github.com/readmet3xt-lab/foxhound/pull/478#issuecomment-7358790991) **Replay-bench measurement: no measurable difference on v0.8.3.3** Measured: PR head `06376bf` merged onto release **v0.8.3.3** (same code as main `7dbcdcb`), 2 measured runs (after a warm-up run that is not counted), against 3 runs of v0.8.3.3 without the PR, same route (~6.5 min). | run | fps | CPU prep ms | median ms | p95 ms | p99 ms | frames > 100 ms | |---|---|---|---|---|---|---| | release run 1 | 36.54 | 19.08 | 29.00 | 50.42 | 60.01 | 32 | | release run 2 | 36.45 | 20.15 | 28.10 | 38.59 | 45.48 | 13 | | release run 3 | 36.15 | 19.30 | 29.35 | 38.74 | 46.08 | 10 | | PR #478 run 1 | 36.93 | 19.77 | 28.20 | 38.26 | 45.42 | 17 | | PR #478 run 2 | 36.68 | 18.79 | 28.70 | 37.90 | 45.21 | 15 | Mean fps: release 36.38, PR 36.80 (+1.2%). That is below the 2-3% we treat as a real change, so: **no measurable difference** on this route. Only 3 release runs so far, so the release's own spread is not established yet. Crashes: none in the PR runs and none in the release runs (no host GPU failure, no device lost). Note: two earlier release runs the same evening, with identical switches, were much slower for most of the route (6-9 fps average); the cause is not known yet and they are left out of the comparison above. No PR run fell into that state. Config: RTX 5070 Ti 16 GB, Ryzen 7 9800X3D, 32 GB, Windows 11; default `play-foxhound.ps1` settings (Series X profile, 1440p, DLSS balanced, no frame generation), fullscreen, no frame cap. Measured unattended on a replay bench (same save, recorded controller input); numbers and text prepared with Claude.
Author
Owner

@⁠GValdezN on GitHub · 2026-12-03 01:36 UTC · original

Thanks for the replay-bench run, @⁠tillo88! The +1.2% is the closest to the noise threshold of the three PRs you tested — not enough to call a real change on this route, but the allocation reduction and prefetch work targets per-frame overhead that shows up more under sustained CPU pressure (slower CPUs, Wine/Proton, or heavier scenes with more copy spans) than on a 9800X3D at steady state.

@⁠BrenoMazini's analysis on #472 confirms the same pattern: the submitting thread's time on fast hardware is dominated by kernel write-watch and copies, not the allocation/prefetch overhead these changes address. Still worth having as a zero-regression cleanup that trims the constant factors.

**@⁠GValdezN** on GitHub · 2026-12-03 01:36 UTC · [original](https://github.com/readmet3xt-lab/foxhound/pull/478#issuecomment-7364533390) Thanks for the replay-bench run, @⁠tillo88! The +1.2% is the closest to the noise threshold of the three PRs you tested — not enough to call a real change on this route, but the allocation reduction and prefetch work targets per-frame overhead that shows up more under sustained CPU pressure (slower CPUs, Wine/Proton, or heavier scenes with more copy spans) than on a 9800X3D at steady state. @⁠BrenoMazini's analysis on #472 confirms the same pattern: the submitting thread's time on fast hardware is dominated by kernel write-watch and copies, not the allocation/prefetch overhead these changes address. Still worth having as a zero-regression cleanup that trims the constant factors.
Author
Owner

@⁠BrenoMazini on GitHub · 2026-12-03 02:07 UTC · original

Opened it as (#527) thanks for offering to review.

**@⁠BrenoMazini** on GitHub · 2026-12-03 02:07 UTC · [original](https://github.com/readmet3xt-lab/foxhound/pull/478#issuecomment-7365111244) Opened it as (https://git.gtahub.st/github/readmet3xt-lab--foxhound/pulls/527) thanks for offering to review.
Author
Owner

@⁠tillo88 on GitHub · 2026-12-03 03:44 UTC · original

Replay-bench measurement with VICTORY_JOURNAL_SHARDS=1: no measurable fps difference on v0.8.3.3; CPU prep higher in both runs

Follow-up to the earlier measurement of this PR with default switches. Measured: PR head 06376bf merged onto release v0.8.3.3 (same code as main 7dbcdcb), with VICTORY_JOURNAL_SHARDS=1, 2 measured runs (after a warm-up run that is not counted), against 8 runs of v0.8.3.3 without the PR, same route (~6.5 min).

run fps CPU prep ms median ms p95 ms p99 ms frames > 100 ms
release (8 runs, mean; fps range 36.15-37.12) 36.60 19.57 28.45 39.70 47.35 14.9
PR #478 + VICTORY_JOURNAL_SHARDS=1 run 1 36.26 21.87 28.80 37.63 45.85 16
PR #478 + VICTORY_JOURNAL_SHARDS=1 run 2 35.80 21.91 28.70 38.32 46.00 20

Mean fps: release 36.60, PR 36.03 (-1.6%), one run inside the release's range and one below it: no measurable difference in frame rate. The CPU prep time per frame was higher in both runs (21.87 / 21.91 ms) than in any of the 8 release runs (19.08-20.15 ms); with only 2 runs this is an observation, not a settled result.

Crashes: none in the PR runs and none in the release runs (no host GPU failure, no device lost).

Config: RTX 5070 Ti 16 GB, Ryzen 7 9800X3D, 32 GB, Windows 11; default play-foxhound.ps1 settings (Series X profile, 1440p, DLSS balanced, no frame generation), fullscreen, no frame cap.

Measured unattended on a replay bench (same save, recorded controller input); numbers and text prepared with Claude.

**@⁠tillo88** on GitHub · 2026-12-03 03:44 UTC · [original](https://github.com/readmet3xt-lab/foxhound/pull/478#issuecomment-7366889081) **Replay-bench measurement with `VICTORY_JOURNAL_SHARDS=1`: no measurable fps difference on v0.8.3.3; CPU prep higher in both runs** Follow-up to the earlier measurement of this PR with default switches. Measured: PR head `06376bf` merged onto release **v0.8.3.3** (same code as main `7dbcdcb`), with `VICTORY_JOURNAL_SHARDS=1`, 2 measured runs (after a warm-up run that is not counted), against 8 runs of v0.8.3.3 without the PR, same route (~6.5 min). | run | fps | CPU prep ms | median ms | p95 ms | p99 ms | frames > 100 ms | |---|---|---|---|---|---|---| | release (8 runs, mean; fps range 36.15-37.12) | 36.60 | 19.57 | 28.45 | 39.70 | 47.35 | 14.9 | | PR #478 + VICTORY_JOURNAL_SHARDS=1 run 1 | 36.26 | 21.87 | 28.80 | 37.63 | 45.85 | 16 | | PR #478 + VICTORY_JOURNAL_SHARDS=1 run 2 | 35.80 | 21.91 | 28.70 | 38.32 | 46.00 | 20 | Mean fps: release 36.60, PR 36.03 (-1.6%), one run inside the release's range and one below it: **no measurable difference** in frame rate. The CPU prep time per frame was higher in both runs (21.87 / 21.91 ms) than in any of the 8 release runs (19.08-20.15 ms); with only 2 runs this is an observation, not a settled result. Crashes: none in the PR runs and none in the release runs (no host GPU failure, no device lost). Config: RTX 5070 Ti 16 GB, Ryzen 7 9800X3D, 32 GB, Windows 11; default `play-foxhound.ps1` settings (Series X profile, 1440p, DLSS balanced, no frame generation), fullscreen, no frame cap. Measured unattended on a replay bench (same save, recorded controller input); numbers and text prepared with Claude.
This pull request has changes conflicting with the target branch.
  • crates/victory-gpu/src/host.rs
  • crates/victory-gpu/src/tiles.rs
  • crates/victory-gpu/src/watch.rs
View command line instructions

Manual merge helper

Use this merge commit message when completing the merge manually.

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u origin '+refs/pull/478/head:github-bot/gh-478'
git switch 'github-bot/gh-478'

Merge

Merge the changes and update on Forgejo.
git switch 'main'
git merge --no-ff 'github-bot/gh-478'
git switch 'github-bot/gh-478'
git rebase 'main'
git switch 'main'
git merge --ff-only 'github-bot/gh-478'
git switch 'github-bot/gh-478'
git rebase 'main'
git switch 'main'
git merge --no-ff 'github-bot/gh-478'
git switch 'main'
git merge --squash 'github-bot/gh-478'
git switch 'main'
git merge --ff-only 'github-bot/gh-478'
git switch 'main'
git merge 'github-bot/gh-478'
git push origin 'main'
This discussion has been locked. Commenting is limited to contributors.
No description provided.