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

Open
GValdezN wants to merge 3 commits from upstream-pr/478 into main
GValdezN commented 2026-12-02 15:29:32 +00:00 (Migrated from github.com)

Imported from readmet3xt-lab/foxhound. Originally posted by GValdezN on 2026-12-02T15:29:32Z.


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

<!-- greyfinch-sync:readmet3xt-lab/foxhound:issue:5609575724 --> Imported from [readmet3xt-lab/foxhound](https://github.com/readmet3xt-lab/foxhound/pull/478). Originally posted by [GValdezN](https://github.com/GValdezN) on 2026-12-02T15:29:32Z. --- ## 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)
tillo88 commented 2026-12-02 21:06:05 +00:00 (Migrated from github.com)

Imported from readmet3xt-lab/foxhound. Originally posted by tillo88 on 2026-12-02T21:06:05Z.


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.

<!-- greyfinch-sync:readmet3xt-lab/foxhound:comment:7358790991 --> Imported from [readmet3xt-lab/foxhound](https://github.com/readmet3xt-lab/foxhound/pull/478#issuecomment-7358790991). Originally posted by [tillo88](https://github.com/tillo88) on 2026-12-02T21:06:05Z. --- **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.
GValdezN commented 2026-12-03 01:36:27 +00:00 (Migrated from github.com)

Imported from readmet3xt-lab/foxhound. Originally posted by GValdezN on 2026-12-03T01:36:27Z.


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.

<!-- greyfinch-sync:readmet3xt-lab/foxhound:comment:7364533390 --> Imported from [readmet3xt-lab/foxhound](https://github.com/readmet3xt-lab/foxhound/pull/478#issuecomment-7364533390). Originally posted by [GValdezN](https://github.com/GValdezN) on 2026-12-03T01:36:27Z. --- 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.
BrenoMazini commented 2026-12-03 02:07:27 +00:00 (Migrated from github.com)

Imported from readmet3xt-lab/foxhound. Originally posted by BrenoMazini on 2026-12-03T02:07:27Z.


Opened it as (https://github.com/readmet3xt-lab/foxhound/pull/527) thanks for offering to review.

<!-- greyfinch-sync:readmet3xt-lab/foxhound:comment:7365111244 --> Imported from [readmet3xt-lab/foxhound](https://github.com/readmet3xt-lab/foxhound/pull/478#issuecomment-7365111244). Originally posted by [BrenoMazini](https://github.com/BrenoMazini) on 2026-12-03T02:07:27Z. --- Opened it as (https://github.com/readmet3xt-lab/foxhound/pull/527) thanks for offering to review.
tillo88 commented 2026-12-03 03:44:42 +00:00 (Migrated from github.com)

Imported from readmet3xt-lab/foxhound. Originally posted by tillo88 on 2026-12-03T03:44:42Z.


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.

<!-- greyfinch-sync:readmet3xt-lab/foxhound:comment:7366889081 --> Imported from [readmet3xt-lab/foxhound](https://github.com/readmet3xt-lab/foxhound/pull/478#issuecomment-7366889081). Originally posted by [tillo88](https://github.com/tillo88) on 2026-12-03T03:44:42Z. --- **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 cannot be merged automatically due to conflicts.
Merge manually to resolve the conflicts.
View command line instructions

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u origin 'upstream-pr/478:upstream-pr/478'
git switch 'upstream-pr/478'

Merge

Merge the changes and update on Forgejo.

Warning: The "Autodetect manual merge" setting is not enabled for this repository, you will have to mark this pull request as manually merged afterwards.

git switch 'main'
git merge --no-ff 'upstream-pr/478'
git switch 'upstream-pr/478'
git rebase 'main'
git switch 'main'
git merge --ff-only 'upstream-pr/478'
git switch 'upstream-pr/478'
git rebase 'main'
git switch 'main'
git merge --no-ff 'upstream-pr/478'
git switch 'main'
git merge --squash 'upstream-pr/478'
git switch 'main'
git merge --ff-only 'upstream-pr/478'
git switch 'main'
git merge 'upstream-pr/478'
git push origin 'main'
Sign in to join this conversation.
No reviewers
No labels
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.

Reference
readmet3xt-lab/foxhound!478
No description provided.