Refreshes: a submission guards the view memory it refreshes in one go #80

Manually merged
github merged 2 commits from github-bot/gh-80 into main 2026-11-30 08:00:55 +00:00 AGit
Owner

@⁠mgrein on GitHub · 2026-11-30 06:22 UTC · original
mgrein:batched-guards → main · +160 −12 · 3 files · 2 commits · merged 2026-11-30 07:57 UTC

What's slow

Each refresh of a host copy of view memory guards its own range (watch::guard from writes_into): 3 rounds of the guards' lock (GUARDS), plus one more to publish. This runs on the submitting thread and on the refresh helpers beside it, and they mostly wait for each other on that lock.

Counted on a Ryzen 5 5600X with an RTX 4070 (Series S profile, DLSS, frame generation 2x), per frame:

  • ~156 guard calls, 154 of which find written blocks (~5 each)
  • 20 ms of guarding and 5 ms of publishing, all threads summed
  • 6–10 % of the submitting thread's samples in Mutex::lock_contended under watch::guards

The fix

After its one asking, a submission guards every range its steps refresh of guarded copies in one go, on its own thread (watch::guard_ahead, called from Host::submit right after ask_once):

  1. Guard: the same body as guard (now split into guard + guard_locked, which takes the held lock and hands it back), over the merged ranges, with them held (Askings::hold_or_all).
  2. Note: the written pages are noted in the journal at a fresh asking.
  3. Publish: they are published (publish).

The refreshes then find those ranges published and answer from the journal without a lock. A block written after the batch (a title fault, our own write, a remap) is no longer published, so its range goes through its own writes → guard as before. Nothing can be seen later than before, and the batch only does early what each refresh would have done.

The ranges are the exact refresh ranges of guarded copies (guarded_refreshes: the Step::Refreshes whose copy is GUARDED), not the copies' whole spans, so no extra blocks get protected.

VICTORY_GUARD_BATCH=0 turns it off.

A second, small commit caches wide_threads(): it called available_parallelism (a question to Windows) several times per batch of refreshes, about 0.6 % of the submitting thread.

Same idea as upstream VIctory's #3585 (one round of the guard lock per refresh batch), written for this tree's writes / guard path.

Measured

Ryzen 5 5600X (6C/12T), RTX 4070 12 GB, 32 GB DDR4-3600, Windows 11. victory run --execute --console series-s with the same switches (DLSS Quality, frame generation 2x), the same save and scripted input, 330 s; numbers over the last 3 minutes. Release builds, then the dist profile with -C target-cpu=x86-64-v3:

frame median p95 longest submitting thread list calls a frame
main (fafa3adf), release 51.7 ms 62.5 65.6 49.6 ms 28 k
this PR, release 49.9 ms 61.8 64.1 48.6 ms 46 k (heavier scene)
this PR, dist + x86-64-v3 49.1 ms 59.3 62.0 47.6 ms 28 k

Submitting thread profile (1 ms samples, 40 s), main → this PR:

main this PR
lock_contended 9.6 % 4.8 %
watch::writes 16.6 % 7.6 %
Shadow::update_free 20.1 % 11.9 %
guard_ahead (new) — 6.5 %

The same change on trustno1-coder/VIctory-NVIDIA took guards by refreshes from 156 to 60 a frame (20 → 7 ms), publishing from 5 to 1 ms, and frame p95 from 70.0 to 66.3 ms. A run there with VICTORY_REFRESH_CHECK=1 found no stale copy.

Tests

  • New test: watch::many_askers_tests::no_write_to_views_is_lost_with_batched_guards. The existing views stress test (8 writer threads over a pool and several views of the same pages, writes beside the guards, our own writes, faults that take no lock) runs with a batch of guards (guard_ahead of the asked range and another) before half the askings, for 1 and 3 views, resting and not.
  • Mutation check: a batch that guards without noting its written pages in the journal fails this test within a few rounds.
  • Suite: cargo test -p victory-gpu --lib passes (468 passed, 37 ignored). cargo clippy -p victory-gpu --all-targets -- -D warnings is clean, with and without --all-features.
  • In the game: the pictures look the same.

AI-assisted (Claude), tested on the hardware above with my own copy of the title.

🤖 Generated with Claude Code

**@⁠mgrein** on GitHub · 2026-11-30 06:22 UTC · [original](https://github.com/readmet3xt-lab/foxhound/pull/80) `mgrein:batched-guards` → `main` · +160 −12 · 3 files · 2 commits · merged 2026-11-30 07:57 UTC ## What's slow Each refresh of a host copy of view memory guards its own range (`watch::guard` from `writes_into`): 3 rounds of the guards' lock (`GUARDS`), plus one more to `publish`. This runs on the submitting thread and on the refresh helpers beside it, and they mostly wait for each other on that lock. Counted on a Ryzen 5 5600X with an RTX 4070 (Series S profile, DLSS, frame generation 2x), per frame: - ~156 guard calls, 154 of which find written blocks (~5 each) - 20 ms of guarding and 5 ms of publishing, all threads summed - 6–10 % of the submitting thread's samples in `Mutex::lock_contended` under `watch::guards` ## The fix After its one asking, a submission guards every range its steps refresh of guarded copies in one go, on its own thread (`watch::guard_ahead`, called from `Host::submit` right after `ask_once`): 1. **Guard:** the same body as `guard` (now split into `guard` + `guard_locked`, which takes the held lock and hands it back), over the merged ranges, with them held (`Askings::hold_or_all`). 2. **Note:** the written pages are noted in the journal at a fresh asking. 3. **Publish:** they are published (`publish`). The refreshes then find those ranges published and answer from the journal without a lock. A block written after the batch (a title fault, our own write, a remap) is no longer published, so its range goes through its own `writes` → `guard` as before. Nothing can be seen later than before, and the batch only does early what each refresh would have done. The ranges are the exact refresh ranges of guarded copies (`guarded_refreshes`: the `Step::Refresh`es whose copy is `GUARDED`), not the copies' whole spans, so no extra blocks get protected. `VICTORY_GUARD_BATCH=0` turns it off. A second, small commit caches `wide_threads()`: it called `available_parallelism` (a question to Windows) several times per batch of refreshes, about 0.6 % of the submitting thread. Same idea as upstream VIctory's #3585 (one round of the guard lock per refresh batch), written for this tree's `writes` / `guard` path. ## Measured Ryzen 5 5600X (6C/12T), RTX 4070 12 GB, 32 GB DDR4-3600, Windows 11. `victory run --execute --console series-s` with the same switches (DLSS Quality, frame generation 2x), the same save and scripted input, 330 s; numbers over the last 3 minutes. Release builds, then the `dist` profile with `-C target-cpu=x86-64-v3`: | | frame median | p95 | longest | submitting thread | list calls a frame | |---|---:|---:|---:|---:|---:| | main (fafa3adf), release | 51.7 ms | 62.5 | 65.6 | 49.6 ms | 28 k | | this PR, release | 49.9 ms | 61.8 | 64.1 | 48.6 ms | 46 k (heavier scene) | | this PR, dist + x86-64-v3 | 49.1 ms | 59.3 | 62.0 | 47.6 ms | 28 k | Submitting thread profile (1 ms samples, 40 s), main → this PR: | | main | this PR | |---|---:|---:| | `lock_contended` | 9.6 % | 4.8 % | | `watch::writes` | 16.6 % | 7.6 % | | `Shadow::update_free` | 20.1 % | 11.9 % | | `guard_ahead` (new) | — | 6.5 % | The same change on trustno1-coder/VIctory-NVIDIA took guards by refreshes from 156 to 60 a frame (20 → 7 ms), publishing from 5 to 1 ms, and frame p95 from 70.0 to 66.3 ms. A run there with `VICTORY_REFRESH_CHECK=1` found no stale copy. ## Tests - **New test:** `watch::many_askers_tests::no_write_to_views_is_lost_with_batched_guards`. The existing views stress test (8 writer threads over a pool and several views of the same pages, writes beside the guards, our own writes, faults that take no lock) runs with a batch of guards (`guard_ahead` of the asked range and another) before half the askings, for 1 and 3 views, resting and not. - **Mutation check:** a batch that guards without noting its written pages in the journal fails this test within a few rounds. - **Suite:** `cargo test -p victory-gpu --lib` passes (468 passed, 37 ignored). `cargo clippy -p victory-gpu --all-targets -- -D warnings` is clean, with and without `--all-features`. - **In the game:** the pictures look the same. AI-assisted (Claude), tested on the hardware above with my own copy of the title. 🤖 Generated with [Claude Code](https://claude.com/claude-code)
wide_threads() called available_parallelism (a question to Windows) every time, several times a
batch of refreshes: 0.6 % of the submitting thread (RTX 4070, Ryzen 5 5600X).

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Ported from our work on trustno1-coder/VIctory-NVIDIA (a6567e07 there). Each refresh of a copy of
view memory guarded its own range (`watch::guard`) in rounds of the guards' lock and one more to
publish, on the submitting thread and the refresh threads beside it, which mostly waited for each
other: some 156 guards a frame, 20 ms of guarding and 5 of publishing a frame, all threads summed
(RTX 4070, Ryzen 5 5600X, Series S).

After its one asking, a submission now guards the ranges its steps refresh of guarded copies
(`watch::guard_ahead`, the same body as `guard` over all of them: `guard_locked`), notes their
written pages in the journal and publishes them; the refreshes then find them published and read
the journal with no lock. A block written after that is not published, and its range is guarded
by its own refresh as before. VICTORY_GUARD_BATCH=0: as before.

There it took guards by refreshes from 156 to 60 a frame (20 -> 7 ms), publishing 5 -> 1 ms, and
the guards' lock waits on the submitting thread from 6.2 % to 2.6 % of its time; a run with
VICTORY_REFRESH_CHECK=1 found no stale copy. New test: the views stress test with batches before
half the askings (fails within a few rounds if a batch does not note what it guarded).

Co-Authored-By: Claude Opus 5.5 <[email protected]>
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-30 06:26:21 +00:00
github-bot referenced this pull request from a commit 2026-11-30 08:00:53 +00:00
github manually merged commit 87e8837e9c into main 2026-11-30 08:00:55 +00:00
github-bot referenced this pull request from a commit 2026-12-01 01:10:15 +00:00
github-bot referenced this pull request from a commit 2026-12-02 07:54:13 +00:00
This discussion has been locked. Commenting is limited to contributors.
No description provided.