victory-gpu: skip redundant guard_ahead when ask_first already ran #460

Open
kwaadrat wants to merge 1 commit from upstream-pr/460 into main
kwaadrat commented 2026-12-02 12:54:04 +00:00 (Migrated from github.com)

Imported from readmet3xt-lab/foxhound. Originally posted by kwaadrat on 2026-12-02T12:54:04Z.


When VICTORY_ASK_FIRST=1 and VICTORY_GUARD_FIRST=1 (both on by default), ask_first already runs guard_ahead(&guarded) on a background thread in parallel with ask_once.

Previously, after first.wait(), Host::submit unconditionally called guard_ahead(&batch.guarded) a second time on the submitting thread, even when batch.copy_spans was not re-run and no flights were awaited (!waited_for_flights && !settled_for_sweep).

Change

  • Track guarded_from in crates/victory-gpu/src/host.rs:
    • When copy_walk_once is enabled, asked_first.is_some(), batch.spans_steps == others, guard_first() is true, and !waited_for_flights && !settled_for_sweep, only guard any newly appended entries from batch.copy_spans_first(own.list()) (&batch.guarded[guarded_from..]), skipping the redundant full guard_ahead call when nothing new was added.

Verification

Measured on Linux (Proton Experimental / vkd3d-proton, NVIDIA RTX 3060 12 GB, Intel i5-9600KF 6c/6t, Series S profile, 1080p, VICTORY_GUARD_PAGES=1, VICTORY_JOURNAL_SHORT=1, 0 being made steady state):

  • asking the write watch: 37.5–65.3 ms/frame -> 11.6–11.9 ms/frame
  • preparing total on submitting thread: 125–186 ms/frame -> 49.2 ms/frame
<!-- greyfinch-sync:readmet3xt-lab/foxhound:issue:5606808287 --> Imported from [readmet3xt-lab/foxhound](https://github.com/readmet3xt-lab/foxhound/pull/460). Originally posted by [kwaadrat](https://github.com/kwaadrat) on 2026-12-02T12:54:04Z. --- When `VICTORY_ASK_FIRST=1` and `VICTORY_GUARD_FIRST=1` (both on by default), `ask_first` already runs `guard_ahead(&guarded)` on a background thread in parallel with `ask_once`. Previously, after `first.wait()`, `Host::submit` unconditionally called `guard_ahead(&batch.guarded)` a second time on the submitting thread, even when `batch.copy_spans` was not re-run and no flights were awaited (`!waited_for_flights && !settled_for_sweep`). ### Change - Track `guarded_from` in `crates/victory-gpu/src/host.rs`: - When `copy_walk_once` is enabled, `asked_first.is_some()`, `batch.spans_steps == others`, `guard_first()` is true, and `!waited_for_flights && !settled_for_sweep`, only guard any newly appended entries from `batch.copy_spans_first(own.list())` (`&batch.guarded[guarded_from..]`), skipping the redundant full `guard_ahead` call when nothing new was added. ### Verification Measured on Linux (Proton Experimental / `vkd3d-proton`, NVIDIA RTX 3060 12 GB, Intel i5-9600KF 6c/6t, Series S profile, 1080p, `VICTORY_GUARD_PAGES=1`, `VICTORY_JOURNAL_SHORT=1`, `0 being made` steady state): - `asking the write watch`: `37.5–65.3 ms/frame` -> **`11.6–11.9 ms/frame`** - `preparing` total on submitting thread: `125–186 ms/frame` -> **`49.2 ms/frame`**
GValdezN commented 2026-12-02 16:50:57 +00:00 (Migrated from github.com)

Imported from readmet3xt-lab/foxhound. Originally posted by GValdezN on 2026-12-02T16:50:57Z.


Clean approach — capturing guarded_from = batch.guarded.len() before copy_spans_first and slicing batch.guarded[guarded_from..] avoids the redundant guard_ahead without changing the fallback path. The conditions (guard_first() && !waited_for_flights && !settled_for_sweep) correctly identify when ask_first already covered the full set.

One thing to verify: batch.guarded is only appended to between the two hunks (no truncation/reorder), which looks correct from the surrounding code but worth confirming since guarded_from is a positional index.

This complements our prefetch + Vec::with_capacity work in #478 — both reduce submit-thread overhead on different phases. No conflict expected.

<!-- greyfinch-sync:readmet3xt-lab/foxhound:comment:7352544233 --> Imported from [readmet3xt-lab/foxhound](https://github.com/readmet3xt-lab/foxhound/pull/460#issuecomment-7352544233). Originally posted by [GValdezN](https://github.com/GValdezN) on 2026-12-02T16:50:57Z. --- Clean approach — capturing `guarded_from = batch.guarded.len()` before `copy_spans_first` and slicing `batch.guarded[guarded_from..]` avoids the redundant `guard_ahead` without changing the fallback path. The conditions (`guard_first() && !waited_for_flights && !settled_for_sweep`) correctly identify when `ask_first` already covered the full set. One thing to verify: `batch.guarded` is only appended to between the two hunks (no truncation/reorder), which looks correct from the surrounding code but worth confirming since `guarded_from` is a positional index. This complements our prefetch + Vec::with_capacity work in #478 — both reduce submit-thread overhead on different phases. No conflict expected.
kwaadrat commented 2026-12-02 20:33:10 +00:00 (Migrated from github.com)

Imported from readmet3xt-lab/foxhound. Originally posted by kwaadrat on 2026-12-02T20:33:10Z.


Thanks for reviewing! Confirmed in crates/victory-gpu/src/host.rs:

  1. copy_spans_first -> copy_spans_first_steps -> copy_spans_steps only calls self.guarded.push((from, to)) (it never clears, truncates, or reorders self.guarded; self.guarded.clear() is only called in Batch::put_back and Batch::copy_spans).
  2. Between capturing guarded_from and crate::watch::guard_ahead(&batch.guarded[guarded_from..]), batch.guarded is not touched at all (only batch.ranges is updated and ask_once/ask_more are called).

So guarded_from remains a valid positional slice start for the newly appended entries from own.list().

<!-- greyfinch-sync:readmet3xt-lab/foxhound:comment:7357920729 --> Imported from [readmet3xt-lab/foxhound](https://github.com/readmet3xt-lab/foxhound/pull/460#issuecomment-7357920729). Originally posted by [kwaadrat](https://github.com/kwaadrat) on 2026-12-02T20:33:10Z. --- Thanks for reviewing! Confirmed in `crates/victory-gpu/src/host.rs`: 1. `copy_spans_first` -> `copy_spans_first_steps` -> `copy_spans_steps` only calls `self.guarded.push((from, to))` (it never clears, truncates, or reorders `self.guarded`; `self.guarded.clear()` is only called in `Batch::put_back` and `Batch::copy_spans`). 2. Between capturing `guarded_from` and `crate::watch::guard_ahead(&batch.guarded[guarded_from..])`, `batch.guarded` is not touched at all (only `batch.ranges` is updated and `ask_once`/`ask_more` are called). So `guarded_from` remains a valid positional slice start for the newly appended entries from `own.list()`.
tillo88 commented 2026-12-02 21:05:57 +00:00 (Migrated from github.com)

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


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

Measured: PR head ab905fb 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 #460 run 1 36.40 19.66 28.70 38.96 46.34 16
PR #460 run 2 36.46 19.86 29.05 38.72 46.50 21

Mean fps: release 36.38, PR 36.43 (+0.1%). 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:7358787753 --> Imported from [readmet3xt-lab/foxhound](https://github.com/readmet3xt-lab/foxhound/pull/460#issuecomment-7358787753). Originally posted by [tillo88](https://github.com/tillo88) on 2026-12-02T21:05:57Z. --- **Replay-bench measurement: no measurable difference on v0.8.3.3** Measured: PR head `ab905fb` 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 #460 run 1 | 36.40 | 19.66 | 28.70 | 38.96 | 46.34 | 16 | | PR #460 run 2 | 36.46 | 19.86 | 29.05 | 38.72 | 46.50 | 21 | Mean fps: release 36.38, PR 36.43 (+0.1%). 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.
kwaadrat commented 2026-12-02 21:16:34 +00:00 (Migrated from github.com)

Imported from readmet3xt-lab/foxhound. Originally posted by kwaadrat on 2026-12-02T21:16:34Z.


Thanks a lot for running the replay-bench measurement, @tillo88!

That makes total sense on a 9800X3D + native Windows 11 — when guard_ahead hits already-guarded pages on native NT, the syscall cost is small enough to sit within run-to-run noise. On slower CPUs and especially under Wine/Proton on Linux (where VirtualProtect crosses the Wine/kernel boundary), skipping the redundant pass has a much more noticeable impact, while still keeping the Windows hot path slightly leaner with zero regression.

<!-- greyfinch-sync:readmet3xt-lab/foxhound:comment:7359047314 --> Imported from [readmet3xt-lab/foxhound](https://github.com/readmet3xt-lab/foxhound/pull/460#issuecomment-7359047314). Originally posted by [kwaadrat](https://github.com/kwaadrat) on 2026-12-02T21:16:34Z. --- Thanks a lot for running the replay-bench measurement, @tillo88! That makes total sense on a 9800X3D + native Windows 11 — when `guard_ahead` hits already-guarded pages on native NT, the syscall cost is small enough to sit within run-to-run noise. On slower CPUs and especially under Wine/Proton on Linux (where `VirtualProtect` crosses the Wine/kernel boundary), skipping the redundant pass has a much more noticeable impact, while still keeping the Windows hot path slightly leaner with zero regression.
GValdezN commented 2026-12-02 21:20:21 +00:00 (Migrated from github.com)

Imported from readmet3xt-lab/foxhound. Originally posted by GValdezN on 2026-12-02T21:20:21Z.


Why the replay-bench shows no measurable difference (hypothesis)

The optimization in this PR activates when all five conditions hold together:

  1. copy_walk_once() = true
  2. asked_first.is_some() (the ask_first thread ran)
  3. batch.spans_steps == others (steps didn't change)
  4. guard_first() = true
  5. !waited_for_flights && !settled_for_sweep

When all are met, before this PR guard_ahead was called on the full batch.guarded set redundantly (the ask_first thread had already done it). The PR changes it to only guard newly appended entries (guarded[guarded_from..]).

But look at what guard_ahead does internally (watch.rs:3679–3694):

  • Filters out ranges already published()
  • Filters out ranges already in the journal (ahead_contains)
  • If fewer than 2 pieces remain, early returns

Most likely explanation: On tillo88's config (RTX 5070 Ti, 1440p, Series X), by the time the submitting thread reaches the second guard_ahead call, the ask_first thread has already finished and marked everything as published. So guard_ahead hits the early return on the published() filter — the redundant call is cheap because there's no real work left.

On the author's config (RTX 3060, 1080p, Series S), the GPU is slower, there's more CPU pressure, and the ask_first thread likely hasn't finished when the submitting thread reaches the second guard_ahead — so the pages aren't published yet, the filter doesn't discard them, and guard_ahead does real work: materialize, install, hold askings, etc. That's where the 37–65 ms cost comes from.

In short: the difference between the two configs isn't that the optimization doesn't work — it's that on faster hardware the redundancy was already "free" because guard_ahead self-short-circuits when the background thread has already published everything. On slower hardware there's a timing window where both calls do real work.

<!-- greyfinch-sync:readmet3xt-lab/foxhound:comment:7359137804 --> Imported from [readmet3xt-lab/foxhound](https://github.com/readmet3xt-lab/foxhound/pull/460#issuecomment-7359137804). Originally posted by [GValdezN](https://github.com/GValdezN) on 2026-12-02T21:20:21Z. --- **Why the replay-bench shows no measurable difference (hypothesis)** The optimization in this PR activates when all five conditions hold together: 1. `copy_walk_once()` = true 2. `asked_first.is_some()` (the ask_first thread ran) 3. `batch.spans_steps == others` (steps didn't change) 4. `guard_first()` = true 5. `!waited_for_flights && !settled_for_sweep` When all are met, before this PR `guard_ahead` was called on the full `batch.guarded` set redundantly (the ask_first thread had already done it). The PR changes it to only guard newly appended entries (`guarded[guarded_from..]`). But look at what `guard_ahead` does internally (watch.rs:3679–3694): - Filters out ranges already `published()` - Filters out ranges already in the journal (`ahead_contains`) - If fewer than 2 pieces remain, **early returns** **Most likely explanation:** On tillo88's config (RTX 5070 Ti, 1440p, Series X), by the time the submitting thread reaches the second `guard_ahead` call, the ask_first thread has **already finished** and marked everything as published. So `guard_ahead` hits the early return on the `published()` filter — the redundant call is cheap because there's no real work left. On the author's config (RTX 3060, 1080p, Series S), the GPU is slower, there's more CPU pressure, and the ask_first thread likely **hasn't finished** when the submitting thread reaches the second `guard_ahead` — so the pages aren't published yet, the filter doesn't discard them, and `guard_ahead` does real work: materialize, install, hold askings, etc. That's where the 37–65 ms cost comes from. **In short:** the difference between the two configs isn't that the optimization doesn't work — it's that on faster hardware the redundancy was already "free" because `guard_ahead` self-short-circuits when the background thread has already published everything. On slower hardware there's a timing window where both calls do real work.
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/460:upstream-pr/460'
git switch 'upstream-pr/460'

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/460'
git switch 'upstream-pr/460'
git rebase 'main'
git switch 'main'
git merge --ff-only 'upstream-pr/460'
git switch 'upstream-pr/460'
git rebase 'main'
git switch 'main'
git merge --no-ff 'upstream-pr/460'
git switch 'main'
git merge --squash 'upstream-pr/460'
git switch 'main'
git merge --ff-only 'upstream-pr/460'
git switch 'main'
git merge 'upstream-pr/460'
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!460
No description provided.