Guards: blocks made read-only again outside the guards' and journal's locks (VICTORY_GUARD_OUTSIDE, off; #1083) #1312

Closed
KainanP wants to merge 2 commits from upstream-pr/1315 into main
KainanP commented 2026-11-26 13:55:52 +00:00 (Migrated from github.com)

Imported from fxceless/victory-fork. Originally posted by KainanP on 2026-11-26T13:55:52Z.


For #1083. MKmajster and CuteBooty found that spreading a submission's refreshes over more threads did not shorten the frame, because the threads queue on the guards' lock. This PR looks at what is done while that lock is held and moves the costly part out. It is off by default. Turn it on with VICTORY_GUARD_OUTSIDE=1, or with alt to switch it on and off every 20 s; each switch is logged with the lock counters.

What is serialized. For view memory, Shadow::update → watch::writes → guard_at holds the journal's lock and then the guards' lock. While it holds them, it makes every block it found written read-only again: one VirtualProtect per block, because the title's views are mapped block by block and a call cannot span two views. main (9a9091d) still does this under both locks. Three things wait for those locks:

  • the title's threads, at every write fault (the handler takes the guards' lock);
  • the copy-back relay and kernel writes (writing);
  • every other refresh, including those on the helper threads (VICTORY_WIDE_REFRESH), which line up at the journal's lock.

Windows itself can run these protection changes in parallel. In protect_scaling (below), 4,096 touched blocks take 48 ms on 1 thread, 21 ms on 4 and 15 ms on 8. So the serialization comes from our locks, not from the kernel.

The change. With the switch, guard_at only records the blocks to re-arm while it holds the lock. It adds them to protected and clean, plus a new arming set. writes_as stamps the journal, releases both locks, and then makes the blocks read-only (Arming::finish) before it returns, so before the caller copies them. The answers stay the same:

  • Another asking whose range holds an arming block waits (condvar) until those blocks are read-only. Without that wait, it could take a block as clean while it is still writable, and a write landing in that gap would be in nobody's copy. The test below catches this: with the wait removed, it failed in 3 of 8 runs.
  • An opening during the window (write fault, writing, title_memory_written, views_mapped, title_memory_remapped) also opens arming blocks and records them as disturbed. finish then makes them writable again. A fault on such a block before finish is let through like any other opening.
  • Light-refresh bits. publish skips arming blocks. finish sets their bits afterwards, taking the journal's lock first and then the guards' lock, after releasing its own hold on the guards' lock.
  • Other views. A run goes outside the lock when protect would change only its own blocks: each page is shown at one view, or at more than MANY_VIEWS (inplace::shown_alone, the same test as protect's own loop). A run with a page shown at 2 to MANY_VIEWS views is still guarded together with those views, under the lock, as before. A view mapped after that answer opens the blocks (views_mapped → open_here), so they end writable.
  • Failures. A block that cannot be made read-only is handled as in protect_here.
  • Drop safety. If an Arming is dropped without being finished, for example because of a panic, it is finished on drop, so no later asking can wait forever.

The first version deferred a run only when views_between found no view at all. CuteBooty pointed out that this also finds the run's own view, and the runtime registers every view it maps. So in the game no run took the new path. The benchmark did not show it, because its views were never registered. Fixed in the second commit; the numbers below are with views registered as the runtime does it.

Counters (always on, cheap).

  • watch::WAITS counts lock waits; the clock is read only when a lock is found taken.
  • watch::GUARD_WORK counts how long the guards' lock is held and the protection changes.
  • watch::GUARD_LATER counts runs made read-only outside the lock, and runs kept under it for other views.

The alt log line prints all three, so a run shows what share took the new path.

Benchmark (shadow::guard_bench::guard_contention, #[ignore]):

  • Setup. 256 MB of title memory, mapped as 4,096 pagefile views of one block each (a placeholder, as xmem does). 128 host copies of 2 MB each. BENCH_VIEWS=1 registers the views through title_memory_view, as the runtime does (VICTORY_VIEW_GUARD on, the default).
  • Each round. About 1,400 random blocks are dirtied first, about 89 MB as in #1083. Then a flush refreshes every copy while 4 writer threads keep writing random blocks (BENCH_GAP_US=150: one write every 150 µs each).
  • Numbers. Median of 11 rounds, VICTORY_COPY_PLAIN=1, on main 9a9091d, on the i7-14650HX of the RTX 4060 Laptop machine. Cells are repetitions.

Views registered (the game's case), 3 repetitions:

flush switch flush time (ms) lock waits (ms): flush thread / writers / all threads guards' lock held (ms) a writer's write (µs)
on the submitting thread off 16.6 / 16.5 / 19.3 0.0 / 5.6–6.7 / 5.7–7.0 7.0–8.3 18–23
on the submitting thread on 16.5 / 16.1 / 19.8 0.0 / 0.1–0.2 / 0.1–0.3 0.7–0.8 5–6
VICTORY_WIDE_REFRESH-style, helper threads off 10.0 / 12.0 / 12.0 7.1–8.7 / 8.3–11.6 / 57–70 8.0–9.4 49–56
VICTORY_WIDE_REFRESH-style, helper threads on 5.2 / 6.0 / 6.3 0.6–0.8 / 0.6–1.0 / 4.8–6.5 1.4–1.7 13–18

With the first commit alone, registered views made no difference: the helper-thread flush took 10.6 / 10.9 / 12.4 ms with the switch on, against 10.6 / 10.3 / 13.4 off.

Views not registered, 2 repetitions:

flush switch flush time (ms) lock waits (ms): flush thread / writers / all threads guards' lock held (ms) a writer's write (µs)
on the submitting thread off 17.9 / 17.8 0.0 / 4.7–4.8 / 4.8 7.2–7.3 16
on the submitting thread on 17.9 / 17.8 0.0 / 0.1 / 0.1 0.5 5
VICTORY_WIDE_REFRESH-style, helper threads off 11.0 / 11.0 7.6–8.1 / 9.1–9.4 / 62–63 8.0–8.3 47–49
VICTORY_WIDE_REFRESH-style, helper threads on 5.8 / 5.7 0.3–0.5 / 0.3–0.4 / 3.0–3.6 0.8–0.9 12–13

So:

  • Submitting thread alone (the default). The flush costs the same, because it still makes the same ~2,000 protection changes itself. But the title's threads no longer wait for the lock at their faults: a write that faults takes 5 µs instead of about 20.
  • Helper threads. The flush is about half as long, because the protection changes now run side by side. "All threads 57–70 ms" is the queueing CuteBooty measured (about 190 thread-ms a frame in town).
  • Registered views. Checking whether a run's pages have other views costs about 0.3 ms more lock hold per flush here (0.75 against 0.45 ms on the submitting thread).

Correctness. shadow::guard_bench::flushes_under_writes_lose_nothing (not ignored, about 1.1 s) runs 3 writer threads against 100 flushes. It covers one thread and the helper threads, the switch off and on, and copies that overlap each other's edges. It runs twice: over unregistered views, and over registered views where 16 blocks' pages are also shown at a second view. After each flush it checks every word:

  • no write made before the flush began is missing;
  • no value is newer than any that was written;
  • no copy goes back to an older value.

After the writers stop, one flush must make every copy byte-equal to title memory. With the switch on, it also asserts that runs went outside the lock. Over registered views, it asserts that runs with a second view stayed under it. With the first commit's condition, it fails with "Told { aliased: 16 } wide false: no run made read-only outside the lock".

On 9a9091d it passed 10 of 10 runs in release and 10 of 10 in debug. It also passed with VICTORY_LIGHT_REFRESH=1 and alt, with VICTORY_ABSENT_ONCE=1 VICTORY_ABSENT_MS=told, with VICTORY_VIEW_KNOWN=1, and with VICTORY_VIEW_GUARD=0. (An earlier version of the test found a self-deadlock in my first version of the light-bits step, which is fixed here.)

Suite (9a9091d).

  • cargo test -p victory-gpu --lib, switch unset and with VICTORY_GUARD_OUTSIDE=1: 243 passed on the RTX 4060 Laptop.
  • On the Intel UHD (8086:a78b): 243 passed. Some runs fail inplace::tests::warp_a_title_buffer_in_place_shows_its_bytes_at_its_offset, or the parallel run crashes. Unmodified main does both on this machine too (3 failures and 1 crash in 8 runs).
  • watch::tests share title addresses when run in parallel, and fail now and then on unmodified main too (7 of 40 runs of watch:: alone; 2 of 40 with this PR).
  • WARP (--features diagnostics --test-threads=1, release): 260 passed. The 3 profile::tests fail, and they fail on main too.

The GPU does not matter for this path: the benchmark's copies are host memory, and its D3D12 buffer is never used, so the timings are CPU-only.

Lints. clippy -D warnings passes with and without --all-features. rustfmt was applied only to the lines this PR changes; the new guard_bench.rs is formatted whole.

Rebase onto 9a9091d. main's new one-block fast path in open_here is kept for a block that is not being armed. A range holding an arming block takes the general path, which notes the block disturbed.

Measured in the game by CarverXI (RTX 5090, first commit only, on ceb9e1f): about +9% fps with VICTORY_WIDE_REFRESH=1 VICTORY_GUARD_OUTSIDE=alt. With the first commit, only runs over memory with no registered view could take the new path, so that gain came from those runs alone. With the second commit, runs over registered views can take it too.

@MKmajster, @CuteBooty, @CarverXI: could one of you run an alt A/B in your scene, with VICTORY_WIDE_REFRESH=1 VICTORY_GUARD_OUTSIDE=alt and the rest of your usual switches? Please compare batch_flush / the "copies" counter and the frame median between the ON and off phases. The switch lines in the log now also give the runs made read-only outside the lock and the runs kept under it.

AI use: Claude Code (Claude Opus 5.5) analysed the code, wrote the change, the benchmark and the tests, and ran them on the machine above.

🤖 Generated with Claude Code

<!-- greyfinch-sync:fxceless/victory-fork:issue:5511321221 --> Imported from [fxceless/victory-fork](https://github.com/fxceless/victory-fork/pull/1315). Originally posted by [KainanP](https://github.com/KainanP) on 2026-11-26T13:55:52Z. --- For #1083. MKmajster and CuteBooty found that spreading a submission's refreshes over more threads did not shorten the frame, because the threads queue on the guards' lock. This PR looks at what is done while that lock is held and moves the costly part out. It is off by default. Turn it on with `VICTORY_GUARD_OUTSIDE=1`, or with `alt` to switch it on and off every 20 s; each switch is logged with the lock counters. **What is serialized.** For view memory, `Shadow::update` → `watch::writes` → `guard_at` holds the **journal's** lock and then the **guards'** lock. While it holds them, it makes every block it found written read-only again: one `VirtualProtect` per block, because the title's views are mapped block by block and a call cannot span two views. `main` (9a9091d) still does this under both locks. Three things wait for those locks: - the title's threads, at every write fault (the handler takes the guards' lock); - the copy-back relay and kernel writes (`writing`); - every other refresh, including those on the helper threads (`VICTORY_WIDE_REFRESH`), which line up at the journal's lock. Windows itself can run these protection changes in parallel. In `protect_scaling` (below), 4,096 touched blocks take 48 ms on 1 thread, 21 ms on 4 and 15 ms on 8. So the serialization comes from our locks, not from the kernel. **The change.** With the switch, `guard_at` only records the blocks to re-arm while it holds the lock. It adds them to `protected` and `clean`, plus a new `arming` set. `writes_as` stamps the journal, releases both locks, and then makes the blocks read-only (`Arming::finish`) before it returns, so before the caller copies them. The answers stay the same: - **Another asking whose range holds an arming block waits** (condvar) until those blocks are read-only. Without that wait, it could take a block as clean while it is still writable, and a write landing in that gap would be in nobody's copy. The test below catches this: with the wait removed, it failed in 3 of 8 runs. - **An opening during the window** (write fault, `writing`, `title_memory_written`, `views_mapped`, `title_memory_remapped`) also opens arming blocks and records them as `disturbed`. `finish` then makes them writable again. A fault on such a block before `finish` is let through like any other opening. - **Light-refresh bits.** `publish` skips arming blocks. `finish` sets their bits afterwards, taking the journal's lock first and then the guards' lock, after releasing its own hold on the guards' lock. - **Other views.** A run goes outside the lock when `protect` would change only its own blocks: each page is shown at one view, or at more than `MANY_VIEWS` (`inplace::shown_alone`, the same test as `protect`'s own loop). A run with a page shown at 2 to `MANY_VIEWS` views is still guarded together with those views, under the lock, as before. A view mapped after that answer opens the blocks (`views_mapped` → `open_here`), so they end writable. - **Failures.** A block that cannot be made read-only is handled as in `protect_here`. - **Drop safety.** If an `Arming` is dropped without being finished, for example because of a panic, it is finished on drop, so no later asking can wait forever. The first version deferred a run only when `views_between` found no view at all. CuteBooty pointed out that this also finds the run's own view, and the runtime registers every view it maps. So in the game no run took the new path. The benchmark did not show it, because its views were never registered. Fixed in the second commit; the numbers below are with views registered as the runtime does it. **Counters (always on, cheap).** - `watch::WAITS` counts lock waits; the clock is read only when a lock is found taken. - `watch::GUARD_WORK` counts how long the guards' lock is held and the protection changes. - `watch::GUARD_LATER` counts runs made read-only outside the lock, and runs kept under it for other views. The `alt` log line prints all three, so a run shows what share took the new path. **Benchmark** (`shadow::guard_bench::guard_contention`, `#[ignore]`): - **Setup.** 256 MB of title memory, mapped as 4,096 pagefile views of one block each (a placeholder, as xmem does). 128 host copies of 2 MB each. `BENCH_VIEWS=1` registers the views through `title_memory_view`, as the runtime does (`VICTORY_VIEW_GUARD` on, the default). - **Each round.** About 1,400 random blocks are dirtied first, about 89 MB as in #1083. Then a flush refreshes every copy while 4 writer threads keep writing random blocks (`BENCH_GAP_US=150`: one write every 150 µs each). - **Numbers.** Median of 11 rounds, `VICTORY_COPY_PLAIN=1`, on `main` 9a9091d, on the i7-14650HX of the RTX 4060 Laptop machine. Cells are repetitions. Views registered (the game's case), 3 repetitions: | flush | switch | flush time (ms) | lock waits (ms): flush thread / writers / all threads | guards' lock held (ms) | a writer's write (µs) | |---|---|---|---|---|---| | on the submitting thread | off | 16.6 / 16.5 / 19.3 | 0.0 / 5.6–6.7 / 5.7–7.0 | 7.0–8.3 | 18–23 | | on the submitting thread | **on** | 16.5 / 16.1 / 19.8 | 0.0 / 0.1–0.2 / 0.1–0.3 | 0.7–0.8 | **5–6** | | `VICTORY_WIDE_REFRESH`-style, helper threads | off | 10.0 / 12.0 / 12.0 | 7.1–8.7 / 8.3–11.6 / 57–70 | 8.0–9.4 | 49–56 | | `VICTORY_WIDE_REFRESH`-style, helper threads | **on** | **5.2 / 6.0 / 6.3** | 0.6–0.8 / 0.6–1.0 / 4.8–6.5 | 1.4–1.7 | 13–18 | With the first commit alone, registered views made no difference: the helper-thread flush took 10.6 / 10.9 / 12.4 ms with the switch on, against 10.6 / 10.3 / 13.4 off. Views not registered, 2 repetitions: | flush | switch | flush time (ms) | lock waits (ms): flush thread / writers / all threads | guards' lock held (ms) | a writer's write (µs) | |---|---|---|---|---|---| | on the submitting thread | off | 17.9 / 17.8 | 0.0 / 4.7–4.8 / 4.8 | 7.2–7.3 | 16 | | on the submitting thread | **on** | 17.9 / 17.8 | 0.0 / 0.1 / 0.1 | 0.5 | **5** | | `VICTORY_WIDE_REFRESH`-style, helper threads | off | 11.0 / 11.0 | 7.6–8.1 / 9.1–9.4 / 62–63 | 8.0–8.3 | 47–49 | | `VICTORY_WIDE_REFRESH`-style, helper threads | **on** | **5.8 / 5.7** | 0.3–0.5 / 0.3–0.4 / 3.0–3.6 | 0.8–0.9 | 12–13 | So: - **Submitting thread alone (the default).** The flush costs the same, because it still makes the same ~2,000 protection changes itself. But the title's threads no longer wait for the lock at their faults: a write that faults takes 5 µs instead of about 20. - **Helper threads.** The flush is about half as long, because the protection changes now run side by side. "All threads 57–70 ms" is the queueing CuteBooty measured (about 190 thread-ms a frame in town). - **Registered views.** Checking whether a run's pages have other views costs about 0.3 ms more lock hold per flush here (0.75 against 0.45 ms on the submitting thread). **Correctness.** `shadow::guard_bench::flushes_under_writes_lose_nothing` (not ignored, about 1.1 s) runs 3 writer threads against 100 flushes. It covers one thread and the helper threads, the switch off and on, and copies that overlap each other's edges. It runs twice: over unregistered views, and over registered views where 16 blocks' pages are also shown at a second view. After each flush it checks every word: - no write made before the flush began is missing; - no value is newer than any that was written; - no copy goes back to an older value. After the writers stop, one flush must make every copy byte-equal to title memory. With the switch on, it also asserts that runs went outside the lock. Over registered views, it asserts that runs with a second view stayed under it. With the first commit's condition, it fails with "Told { aliased: 16 } wide false: no run made read-only outside the lock". On 9a9091d it passed 10 of 10 runs in release and 10 of 10 in debug. It also passed with `VICTORY_LIGHT_REFRESH=1` and `alt`, with `VICTORY_ABSENT_ONCE=1 VICTORY_ABSENT_MS=told`, with `VICTORY_VIEW_KNOWN=1`, and with `VICTORY_VIEW_GUARD=0`. (An earlier version of the test found a self-deadlock in my first version of the light-bits step, which is fixed here.) **Suite (9a9091d).** - `cargo test -p victory-gpu --lib`, switch unset and with `VICTORY_GUARD_OUTSIDE=1`: 243 passed on the RTX 4060 Laptop. - On the Intel UHD (8086:a78b): 243 passed. Some runs fail `inplace::tests::warp_a_title_buffer_in_place_shows_its_bytes_at_its_offset`, or the parallel run crashes. Unmodified `main` does both on this machine too (3 failures and 1 crash in 8 runs). - `watch::tests` share title addresses when run in parallel, and fail now and then on unmodified `main` too (7 of 40 runs of `watch::` alone; 2 of 40 with this PR). - WARP (`--features diagnostics --test-threads=1`, release): 260 passed. The 3 `profile::tests` fail, and they fail on `main` too. The GPU does not matter for this path: the benchmark's copies are host memory, and its D3D12 buffer is never used, so the timings are CPU-only. **Lints.** `clippy -D warnings` passes with and without `--all-features`. rustfmt was applied only to the lines this PR changes; the new `guard_bench.rs` is formatted whole. **Rebase onto 9a9091d.** `main`'s new one-block fast path in `open_here` is kept for a block that is not being armed. A range holding an arming block takes the general path, which notes the block disturbed. **Measured in the game by CarverXI** (RTX 5090, first commit only, on ceb9e1f): about +9% fps with `VICTORY_WIDE_REFRESH=1 VICTORY_GUARD_OUTSIDE=alt`. With the first commit, only runs over memory with no registered view could take the new path, so that gain came from those runs alone. With the second commit, runs over registered views can take it too. @MKmajster, @CuteBooty, @CarverXI: could one of you run an `alt` A/B in your scene, with **`VICTORY_WIDE_REFRESH=1 VICTORY_GUARD_OUTSIDE=alt`** and the rest of your usual switches? Please compare `batch_flush` / the "copies" counter and the frame median between the ON and off phases. The switch lines in the log now also give the runs made read-only outside the lock and the runs kept under it. _AI use: Claude Code (Claude Opus 5.5) analysed the code, wrote the change, the benchmark and the tests, and ran them on the machine above._ 🤖 Generated with [Claude Code](https://claude.com/claude-code)
CuteBooty commented 2026-11-26 16:04:28 +00:00 (Migrated from github.com)

Imported from fxceless/victory-fork. Originally posted by CuteBooty on 2026-11-26T16:04:28Z.


Read of 33c2b7f3: with view guarding on, runs over registered view memory seem never to take the outside path

The follow-up I promised on #1083. This is from reading the code (two independent reads), not from an in-game run: my tree's watch.rs has diverged too far for the PR to apply, so I cannot give you the A/B from the 3070. Please correct me if I misread.

  • The test. A run defers only if later && (!view_guard() || views_between(from * BLOCK, block * BLOCK).is_empty()) (watch.rs:1494-1506). views_between also returns the run's own view (inplace.rs:349-363), so one registered view over the run keeps it under the locks.
  • The game path registers. Each successful physical-page mapping in xmem::map calls title_memory_view (xmem.rs:812-845), and VICTORY_VIEW_GUARD defaults on. So with the default, only runs over memory with no registered view at all can defer.
  • The benchmark does not register. guard_bench maps Windows views and calls title_memory_remapped, which removes registry entries (guard_bench.rs:115-130, 274; watch.rs:2192), so every run there qualifies. The 11 -> 5.5 ms and 61-66 -> 3 ms would then describe that case.
  • No way to see it in a run. Both paths add to the same GUARD_WORK[2] count (watch.rs:1565, 1858). A separate count for deferred runs would let any tester say what share took the new path.

What neither read measured is how many runs in a real frame are over unregistered memory; the share may not be zero.

One possible small change: defer a run when every block passes the existing alias-skip test (shown.len() < 2 || shown.len() > MANY_VIEWS, watch.rs:1784-1789), with a new alias mapped during the deferred protection invalidating the pending guard.

A related number from my tree, for what it is worth: cutting the guards' lock hold of the submit worker from about 34 to about 14 ms a frame did not change the frame time on this machine, so here the lock is not what limits the frame.

<!-- greyfinch-sync:fxceless/victory-fork:comment:7197144796 --> Imported from [fxceless/victory-fork](https://github.com/fxceless/victory-fork/pull/1315#issuecomment-7197144796). Originally posted by [CuteBooty](https://github.com/CuteBooty) on 2026-11-26T16:04:28Z. --- **Read of `33c2b7f3`: with view guarding on, runs over registered view memory seem never to take the outside path** The follow-up I promised on #1083. This is from reading the code (two independent reads), not from an in-game run: my tree's `watch.rs` has diverged too far for the PR to apply, so I cannot give you the A/B from the 3070. Please correct me if I misread. - **The test.** A run defers only if `later && (!view_guard() || views_between(from * BLOCK, block * BLOCK).is_empty())` (`watch.rs:1494-1506`). `views_between` also returns the run's own view (`inplace.rs:349-363`), so one registered view over the run keeps it under the locks. - **The game path registers.** Each successful physical-page mapping in `xmem::map` calls `title_memory_view` (`xmem.rs:812-845`), and `VICTORY_VIEW_GUARD` defaults on. So with the default, only runs over memory with no registered view at all can defer. - **The benchmark does not register.** `guard_bench` maps Windows views and calls `title_memory_remapped`, which removes registry entries (`guard_bench.rs:115-130, 274`; `watch.rs:2192`), so every run there qualifies. The 11 -> 5.5 ms and 61-66 -> 3 ms would then describe that case. - **No way to see it in a run.** Both paths add to the same `GUARD_WORK[2]` count (`watch.rs:1565, 1858`). A separate count for deferred runs would let any tester say what share took the new path. What neither read measured is how many runs in a real frame are over unregistered memory; the share may not be zero. One possible small change: defer a run when every block passes the existing alias-skip test (`shown.len() < 2 || shown.len() > MANY_VIEWS`, `watch.rs:1784-1789`), with a new alias mapped during the deferred protection invalidating the pending guard. A related number from my tree, for what it is worth: cutting the guards' lock hold of the submit worker from about 34 to about 14 ms a frame did not change the frame time on this machine, so here the lock is not what limits the frame.
CarverXI commented 2026-11-26 17:10:20 +00:00 (Migrated from github.com)

Imported from fxceless/victory-fork. Originally posted by CarverXI on 2026-11-26T17:10:20Z.


Measured in the game on an RTX 5090 (i9-14900K, 128 GB, Windows 11 26300, driver 621.01), this PR merged onto ceb9e1f, Series X profile, the opening cutscene from one save (2,000 to 5,000 draws a frame), 60 s to 220 s after the world starts loading. The set is the one in #1253 plus VICTORY_SETTLE_ROOM=1 VICTORY_PARALLEL_REFRESH=1, so VICTORY_WIDE_REFRESH=1 is on: the helper-thread case of your benchmark.

The switch itself, VICTORY_GUARD_OUTSIDE=alt, frames over time per phase, the first 4 s after each change dropped:

launch off on
phases as they are 10.02 fps 11.05 +10.4 %
phases swapped (a local switch, see below) 10.32 11.12 +7.7 %

About +9 %. The picture is as before and each run logs the same known lines as without it. cargo test -p victory-gpu --lib passes here with the switch unset and set (232, one profiler test skipped that fails on main on this machine).

What it unblocks. A profile of this scene before the PR (100 s, the cutscene only) had the helper threads waiting 46 s for the journal's lock inside watch::nothing_noted against 8 s running it, and 15 s waiting in watch::writes against 10 s running; the title's recording threads waited 12 s for the guards' lock. Two switches of main that put work beside the submitting thread were nulls here before this PR and are not on top of it:

alt, with this PR on phases as they are swapped mean
VICTORY_SYNC_BESIDE +11.3 % +0.9 % +6 %
VICTORY_REAPER +14.4 % +1.9 % +8 %

(VICTORY_SYNC_BESIDE before: 10.80 / 10.61 / 10.67 fps without, 10.26 / 10.92 / 10.93 with, three launches a side.)

Together, plain launches, two a side, alternating: 10.90 / 11.90 fps without any of the three, 12.87 / 13.09 with VICTORY_GUARD_OUTSIDE=1 VICTORY_SYNC_BESIDE=1 VICTORY_REAPER=1 (+14 %).

Two things to hold against these numbers:

  • In the SYNC_BESIDE, REAPER and "together" launches a local change of ours was also on: the askings that only read the journal (nothing_noted, the asked-ahead and known-by-bits answers of writes_as) hold it shared, through an RwLock. I have not yet measured what it adds on top of this PR, so part of those three rows may be its. The first table is this PR alone.
  • The two columns differ because alt phases start with the process and this scene is scripted: "with" falls on the same shots every launch. Swapping the halves and taking the mean takes the scene out. I'll send that small switch separately.

A second profile with the journal shared still had readers queueing behind the askings that write it, which is what this PR shortens: the two look complementary rather than the same fix twice.

<!-- greyfinch-sync:fxceless/victory-fork:comment:7198600452 --> Imported from [fxceless/victory-fork](https://github.com/fxceless/victory-fork/pull/1315#issuecomment-7198600452). Originally posted by [CarverXI](https://github.com/CarverXI) on 2026-11-26T17:10:20Z. --- Measured in the game on an RTX 5090 (i9-14900K, 128 GB, Windows 11 26300, driver 621.01), this PR merged onto `ceb9e1f`, Series X profile, the opening cutscene from one save (2,000 to 5,000 draws a frame), 60 s to 220 s after the world starts loading. The set is the one in #1253 plus `VICTORY_SETTLE_ROOM=1 VICTORY_PARALLEL_REFRESH=1`, so `VICTORY_WIDE_REFRESH=1` is on: the helper-thread case of your benchmark. **The switch itself, `VICTORY_GUARD_OUTSIDE=alt`**, frames over time per phase, the first 4 s after each change dropped: | launch | off | on | | |---|---|---|---| | phases as they are | 10.02 fps | 11.05 | +10.4 % | | phases swapped (a local switch, see below) | 10.32 | 11.12 | +7.7 % | About +9 %. The picture is as before and each run logs the same known lines as without it. `cargo test -p victory-gpu --lib` passes here with the switch unset and set (232, one profiler test skipped that fails on `main` on this machine). **What it unblocks.** A profile of this scene before the PR (100 s, the cutscene only) had the helper threads waiting 46 s for the journal's lock inside `watch::nothing_noted` against 8 s running it, and 15 s waiting in `watch::writes` against 10 s running; the title's recording threads waited 12 s for the guards' lock. Two switches of `main` that put work beside the submitting thread were nulls here before this PR and are not on top of it: | `alt`, with this PR on | phases as they are | swapped | mean | |---|---|---|---| | `VICTORY_SYNC_BESIDE` | +11.3 % | +0.9 % | +6 % | | `VICTORY_REAPER` | +14.4 % | +1.9 % | +8 % | (`VICTORY_SYNC_BESIDE` before: 10.80 / 10.61 / 10.67 fps without, 10.26 / 10.92 / 10.93 with, three launches a side.) **Together**, plain launches, two a side, alternating: 10.90 / 11.90 fps without any of the three, 12.87 / 13.09 with `VICTORY_GUARD_OUTSIDE=1 VICTORY_SYNC_BESIDE=1 VICTORY_REAPER=1` (+14 %). Two things to hold against these numbers: - In the `SYNC_BESIDE`, `REAPER` and "together" launches a local change of ours was also on: the askings that only read the journal (`nothing_noted`, the asked-ahead and known-by-bits answers of `writes_as`) hold it shared, through an `RwLock`. I have not yet measured what it adds on top of this PR, so part of those three rows may be its. The first table is this PR alone. - The two columns differ because `alt` phases start with the process and this scene is scripted: "with" falls on the same shots every launch. Swapping the halves and taking the mean takes the scene out. I'll send that small switch separately. A second profile with the journal shared still had readers queueing behind the askings that write it, which is what this PR shortens: the two look complementary rather than the same fix twice.
KainanP commented 2026-11-27 11:33:53 +00:00 (Migrated from github.com)

Imported from fxceless/victory-fork. Originally posted by KainanP on 2026-11-27T11:33:53Z.


Rebased on main 9a9091d (now a17cdf2), plus a second commit for CuteBooty's finding.

Is it still needed? Yes. main still makes the written blocks read-only one VirtualProtect at a time, under both the journal's lock and the guards' lock. The only conflict was main's new one-block fast path in open_here. It is kept for blocks that are not being armed; a range holding an arming block takes the general path, which notes the block disturbed.

@CuteBooty, your read was right. views_between also returns the run's own view, and the runtime registers every view it maps. So with VICTORY_VIEW_GUARD on, no run over registered memory ever took the outside path. The benchmark missed this because its views were never registered.

The second commit changes the test. A run now goes outside the lock when protect would change only its own blocks: every page shown at one view, or at more than MANY_VIEWS. That is the same test as protect's own loop, now done under one take of the views' lock (inplace::shown_alone). If a view is mapped meanwhile, the blocks are opened, and they end writable. The new GUARD_LATER counter, which the alt line prints, counts the runs made read-only outside the lock and the runs kept under it, as you suggested.

Benchmark with the views registered (helper-thread flush, median of 11, 3 repetitions):

switch off switch on
first commit only 10.6 / 10.3 / 13.4 ms 10.6 / 10.9 / 12.4 ms
with the second commit 10.0 / 12.0 / 12.0 ms 5.2 / 6.0 / 6.3 ms

On the submitting thread alone, the flush time is unchanged, but a writer's faulting write takes about 5 µs instead of about 20.

Tests.

  • flushes_under_writes_lose_nothing now also runs over registered views, with 16 blocks shown at a second view. With the first commit's condition, it fails.
  • It passed 10/10 in release and 10/10 in debug, plus the env variants.
  • The victory-gpu lib suite on RTX and Intel (switch unset and =1) and on WARP matches main, including the known Intel inplace flake, the parallel watch::tests flakes, and the 3 WARP profile failures.
  • clippy -D warnings is clean with and without --all-features.

The description has the new tables. @CarverXI, your +9% was measured with the first commit, so it came only from runs over memory with no registered view. A rerun with this head would show what the registered views add.

AI use: Claude Code (Claude Opus 5.5).

<!-- greyfinch-sync:fxceless/victory-fork:comment:7217219263 --> Imported from [fxceless/victory-fork](https://github.com/fxceless/victory-fork/pull/1315#issuecomment-7217219263). Originally posted by [KainanP](https://github.com/KainanP) on 2026-11-27T11:33:53Z. --- Rebased on `main` 9a9091d (now a17cdf2), plus a second commit for CuteBooty's finding. **Is it still needed? Yes.** `main` still makes the written blocks read-only one `VirtualProtect` at a time, under both the journal's lock and the guards' lock. The only conflict was `main`'s new one-block fast path in `open_here`. It is kept for blocks that are not being armed; a range holding an arming block takes the general path, which notes the block disturbed. **@CuteBooty, your read was right.** `views_between` also returns the run's own view, and the runtime registers every view it maps. So with `VICTORY_VIEW_GUARD` on, no run over registered memory ever took the outside path. The benchmark missed this because its views were never registered. The second commit changes the test. A run now goes outside the lock when `protect` would change only its own blocks: every page shown at one view, or at more than `MANY_VIEWS`. That is the same test as `protect`'s own loop, now done under one take of the views' lock (`inplace::shown_alone`). If a view is mapped meanwhile, the blocks are opened, and they end writable. The new `GUARD_LATER` counter, which the `alt` line prints, counts the runs made read-only outside the lock and the runs kept under it, as you suggested. **Benchmark with the views registered** (helper-thread flush, median of 11, 3 repetitions): | | switch off | switch on | |---|---|---| | first commit only | 10.6 / 10.3 / 13.4 ms | 10.6 / 10.9 / 12.4 ms | | with the second commit | 10.0 / 12.0 / 12.0 ms | **5.2 / 6.0 / 6.3 ms** | On the submitting thread alone, the flush time is unchanged, but a writer's faulting write takes about 5 µs instead of about 20. **Tests.** - `flushes_under_writes_lose_nothing` now also runs over registered views, with 16 blocks shown at a second view. With the first commit's condition, it fails. - It passed 10/10 in release and 10/10 in debug, plus the env variants. - The victory-gpu lib suite on RTX and Intel (switch unset and `=1`) and on WARP matches `main`, including the known Intel `inplace` flake, the parallel `watch::tests` flakes, and the 3 WARP `profile` failures. - clippy `-D warnings` is clean with and without `--all-features`. The description has the new tables. @CarverXI, your +9% was measured with the first commit, so it came only from runs over memory with no registered view. A rerun with this head would show what the registered views add. _AI use: Claude Code (Claude Opus 5.5)._
KainanP commented 2026-11-27 15:35:24 +00:00 (Migrated from github.com)

Imported from fxceless/victory-fork. Originally posted by KainanP on 2026-11-27T15:35:24Z.


Closing this. main 5dbf5ce (the VIctory-NVIDIA tree) has a different watch.rs, and it already takes apart the lock chain this PR shortened:

  • The journal's lock is no longer held while guarding. JOURNAL is an RwLock. writes holds only its own range (Asking::hold) while guard runs, so other refreshes no longer queue behind a guard at the journal. That queueing was most of what @CarverXI's profile showed (46 s waiting in nothing_noted).
  • Faults can skip the guards' lock. VICTORY_FAULT_UNLOCKED and open_unlocked are off by default. guard also releases the guards' lock between runs of blocks (here).

What is not covered is the VirtualProtect of each run. It still happens under the guards' lock (protect_here), so two refreshes guarding at once still take turns. This PR's code cannot be carried over: the fault paths (OPENING/TOLD/BESIDE) are different. If a profile on the new tree shows refreshes waiting on the guards' lock, I'll open a fresh, measured PR for that part. Thanks @CarverXI and @CuteBooty for the measurements and the careful read.

AI use: Claude Code (Claude Opus 5.5).

<!-- greyfinch-sync:fxceless/victory-fork:comment:7222241191 --> Imported from [fxceless/victory-fork](https://github.com/fxceless/victory-fork/pull/1315#issuecomment-7222241191). Originally posted by [KainanP](https://github.com/KainanP) on 2026-11-27T15:35:24Z. --- Closing this. `main` 5dbf5ce (the VIctory-NVIDIA tree) has a different `watch.rs`, and it already takes apart the lock chain this PR shortened: - **The journal's lock is no longer held while guarding.** [`JOURNAL`](https://github.com/fxceless/victory-fork/blob/5dbf5ce226690bb0f1b2949dc8ef822617ae81ea/crates/victory-gpu/src/watch.rs#L145) is an `RwLock`. `writes` holds only its own range ([`Asking::hold`](https://github.com/fxceless/victory-fork/blob/5dbf5ce226690bb0f1b2949dc8ef822617ae81ea/crates/victory-gpu/src/watch.rs#L700)) while `guard` runs, so other refreshes no longer queue behind a guard at the journal. That queueing was most of what @CarverXI's profile showed (46 s waiting in `nothing_noted`). - **Faults can skip the guards' lock.** [`VICTORY_FAULT_UNLOCKED`](https://github.com/fxceless/victory-fork/blob/5dbf5ce226690bb0f1b2949dc8ef822617ae81ea/crates/victory-gpu/src/watch.rs#L1072-L1088) and [`open_unlocked`](https://github.com/fxceless/victory-fork/blob/5dbf5ce226690bb0f1b2949dc8ef822617ae81ea/crates/victory-gpu/src/watch.rs#L1113) are off by default. `guard` also releases the guards' lock between runs of blocks ([here](https://github.com/fxceless/victory-fork/blob/5dbf5ce226690bb0f1b2949dc8ef822617ae81ea/crates/victory-gpu/src/watch.rs#L2034-L2045)). What is not covered is the `VirtualProtect` of each run. It still happens under the guards' lock ([`protect_here`](https://github.com/fxceless/victory-fork/blob/5dbf5ce226690bb0f1b2949dc8ef822617ae81ea/crates/victory-gpu/src/watch.rs#L2252)), so two refreshes guarding at once still take turns. This PR's code cannot be carried over: the fault paths (`OPENING`/`TOLD`/`BESIDE`) are different. If a profile on the new tree shows refreshes waiting on the guards' lock, I'll open a fresh, measured PR for that part. Thanks @CarverXI and @CuteBooty for the measurements and the careful read. _AI use: Claude Code (Claude Opus 5.5)._

Pull request closed

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
fxceless/victory-fork!1312
No description provided.