Skip to content

victory-gpu: open write-faulted guarded blocks without the guards' lock by default, and build soft runs once per guard pass - #516

Merged
trustno1-coder merged 4 commits into
trustno1-coder:mainfrom
henriazz10:perf/cpu-copies-rtx50
Nov 28, 2026
Merged

trustno1-coder merged 4 commits into
trustno1-coder:mainfrom
henriazz10:perf/cpu-copies-rtx50

Conversation

@henriazz10

Copy link
Copy Markdown
Contributor

Summary

  • Root-cause profiling (copystat diagnostics build on RTX 5080):
    • In the 12-minute user log (victory-nvidia-play-20261127-220810-11640.log), the last 60s ran at 4.0–5.7 fps (175.4–246.9 ms/frame) with CPU copies at 56.3–61.9 ms (116.4–157.8 MB) and refreshes at 51.7 ms, while GPU busy time was 12–21% and live shader/pipeline compilation had mostly subsided (72 shader translated, 3 host pipeline shaders=).
    • A copystat run (victory-nvidia-play-20261127-224637-55336.log) showed guards holding GUARDS.lock() for 49.5 ms/frame (450 watches 53.65 ms, 474 blocks made read-only) while write-fault handling (VICTORY_FAULT_UNLOCKED off by default) spent 43.63 ms per frame waiting on GUARDS.lock() across threads (429 faults/frame).
  • Change (crates/victory-gpu/src/watch.rs, commit f5bde6b (rebased on main 64d4931)):
    • Enabled VICTORY_FAULT_UNLOCKED by default (unlocked_from_env returns true unless VICTORY_FAULT_UNLOCKED=0).
    • Hoisted soft_runs(first * BLOCK, (last - first) * BLOCK) out of GUARDS.lock() in guard(), passing &soft into protect() / protect_here().
    • Added unit test watch::many_askers_tests::unlocked_defaults_on_unless_zero.
  • Negative control:
    • Temporarily switching unlocked_from_env back to val.is_some_and(|v| v == "1") caused cargo test -p victory-gpu --lib --locked unlocked_defaults_on_unless_zero to fail (exit code 1):
      thread 'watch::many_askers_tests::unlocked_defaults_on_unless_zero' (31036) panicked at crates\victory-gpu\src\watch.rs:5294:9: assertion failed: super::unlocked_from_env(None)

Before / After Measurements (Plain Release Build, 130s VICTORY_WARMUP_RESTART=0)

  • Baseline 1 (victory-nvidia-play-20261127-225157-24736.log): 12.6 fps, 79.5 ms/frame (66.0 preparing: copies 34.3 ms 42.3 MB, textures 3.1 ms 24.0 MB; 0.0 on the GPU; 9.5 copying back 0.5 MB), 3576 draws/frame; refreshes 27.1–36.1 ms.
  • Baseline 2 (victory-nvidia-play-20261127-225454-59896.log): 8.3 fps, 120.0 ms/frame (80.3 preparing: copies 44.4 ms 82.1 MB, textures 4.1 ms 57.4 MB; 2.8 on the GPU; 9.3 copying back 0.7 MB), 3862 draws/frame; refreshes 20.0–50.7 ms.
  • After 1 (victory-nvidia-play-20261127-231559-54768.log): 11.9 fps, 84.2 ms/frame (66.9 preparing: copies 32.2 ms 61.0 MB, textures 18.1 ms 40.1 MB; 2.7 on the GPU; 6.3 copying back 0.5 MB), 3743 draws/frame; refreshes 16.1–26.0 ms.
  • After 2 (victory-nvidia-play-20261127-231853-57360.log): 11.1 fps, 90.2 ms/frame (73.8 preparing: copies 46.4 ms 69.7 MB, textures 3.9 ms 29.0 MB; 0.8 on the GPU; 8.4 copying back 0.5 MB), 3161 draws/frame; refreshes 25.1–37.3 ms.
  • Note: refreshes narrowed from 20.0–50.7 ms to 16.1–37.3 ms, while CPU copies remained 32.2–46.4 ms because remaining contention/copy cost is dominated by GUARDS.lock() across 8 GPU worker threads in guard() (620 waits, 175.03 ms in copystat) and soft_read (43.47–46.58 ms).

Reviewer notes (Claude Code)

  • Rebased on main 64d4931. On that same main, without this change, the CI commands fail the same way on this Windows checkout: cargo fmt --check (CRLF from core.autocrlf=true), clippy (clippy::precedence in victory-formats/src/texture.rs, missing_transmute_annotations in victory-loader/src/write_sites.rs) and the same 2 tests in victory-gpu::indirect::tests. This PR adds no new failure.
  • The 130 s before/after runs above are within run-to-run noise; the numbers do not prove a frame-rate gain by themselves.
  • Hand test by the user on the RTX 5080, about 10 minutes of play (prison, streets, police chase, boat): called playable; the window title showed roughly 5-20 fps, versus 3-5 fps and a near-black picture on the first try with df2daaf. That first try had no warm shader cache, so part of that difference is the cache, not this change.
  • Behaviour change to review: VICTORY_FAULT_UNLOCKED is now on by default (VICTORY_FAULT_UNLOCKED=0 restores the old path).

Checks

  • cargo test -p victory-gpu --lib --locked watch::: exit code 0 (48 passed; 0 failed)
  • cargo fmt --all -- --check: exit code 1 (pre-existing formatting diffs across untouched files; zero diffs in our added/modified lines in crates/victory-gpu/src/watch.rs)
  • cargo clippy --workspace --all-targets --locked -- -D warnings: exit code 101 (pre-existing clippy::precedence error in crates/victory-formats/src/texture.rs:1346)
  • cargo clippy --workspace --all-targets --all-features --locked -- -D warnings: exit code 101 (same pre-existing clippy::precedence error in crates/victory-formats/src/texture.rs:1346)
  • cargo test --workspace --locked: exit code 101 (victory: 23 passed, census: 2 passed, victory-core: 13 passed, victory-formats: 42 passed; 5 ignored, victory-game: 3 passed, victory-gpu: 423 passed; 2 failed; 35 ignored — indirect::tests::host_list_budget_starts_empty_at_each_submission and indirect::tests::warp_batch_charges_every_execution also fail identically with VICTORY_FAULT_UNLOCKED=0)
  • git diff --check: exit code 0

Environment & Untested Scope

  • Environment: Windows 11 build 26300, NVIDIA RTX 5080, driver 616.92.
  • Not tested: Extended open-world driving past the 130s startup/prologue sequence; the 4 out-of-scope picture issues are left for separate pull requests.

AI-assisted. Gemini 4 Hydrogen (Antigravity CLI) wrote the change and ran the checks; Claude Code reviewed it.

🤖 Generated with Claude Code

@Machad3x

Copy link
Copy Markdown

Tested merged on top of main (RTX 3080 Ti, Ryzen 7 5700X, 1440p DLSS Quality, title ray tracing on): 300 s alive. Together with #498, it cut the frame from 90-97 ms to 63-67 ms in the cell scene. The submitting thread went from 89.6 to 59.7 ms.

One race found in review: the guard pass now reads the soft-view runs from the runtime's books before taking the GUARDS lock. If a view is made real (title_memory_made_real) in between, it was opened before it was noted. The pass then still treats the block as guarded and clean, with no protection on it, so writes through it go unseen.

Our fix: a counter bumped under the lock in title_memory_made_real. The guard pass compares it after taking the lock and re-reads the runs if it changed. We also kept the write-fault switch going through toggled!, so it stays on by default and can still be flipped by VICTORY_AB.

… toggled!

Review of trustno1-coder#516 (Machad3x): guard() read the soft-view runs before taking
GUARDS. A view made real in between (title_memory_made_real opens it under
that lock) was still taken as soft, so the block stayed guarded and clean
with no protection on it, and writes through it went unseen. The runs are
now read once, after the lock is taken (as protect_here did before trustno1-coder#516,
under the same lock).

VICTORY_FAULT_UNLOCKED goes through crate::toggled! again: same rule (on
unless "0"), and VICTORY_AB can flip it in diagnostics builds. The helper
unlocked_from_env and its test, both added in trustno1-coder#516, go with it.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@henriazz10

Copy link
Copy Markdown
Contributor Author

Thanks for testing and for catching the race. Fixed in e29efec:

Checks: cargo test -p victory-gpu --locked gives 434 passed and the 2 indirect::tests failures that are already on main. git diff --check is clean. No new clippy findings in the touched lines. There is no deterministic test for the race itself.

🤖 Generated with Claude Code

@POMXARK

POMXARK commented Nov 28, 2026 •

Copy link
Copy Markdown

Testing feedback on AMD Ryzen 7 5700X (8C/16T), NVIDIA RTX 5080 16 GB, 32 GB RAM, Windows 11, Series X 1440p + DLSS Quality + DLSS MFG 4× (4 MiB windows, VICTORY_GPU_BCPACK=0, VICTORY_WAIT_RESIDENT=0, VICTORY_TEXTURE_ROOM_PERCENT=70, VICTORY_LAND_SKIP=1, VICTORY_LAZY_COMPARE_MB=64, VICTORY_LAZY_NARROW=1).

Full disclosure: tested on a fork branch based on v2026.11.27.7 (with local GPU patches, x86-64-v3/ThinLTO, and PR #501 + hot_within fix), applying the updated PR #516 commit (e29efec, soft_runs read under GUARDS).

Result — 12-minute (720 s) 3D run

Bucket (3D only, fps < 20, draws >= 100) R1 (no #501/#516) R2 (+ #501 fixed) R3 (+ #516 e29efec)
ALL 3D 11.2 p50 / 11.55 mean (89.1 ms) 10.6 p50 / 10.12 mean (94.6 ms) 12.9 p50 / 12.40 mean (77.4 ms, Prep 63.4, copies 34.4)
Mid-draw 3D (4k–7k draws) 11.70 p50 (85.2 ms) 11.20 p50 (89.5 ms) 13.10 p50 / 12.97 mean (76.6 ms)
High-draw 3D (7k–10k draws) 9.30 p50 / 9.37 mean (107.4 ms) 10.30 p50 / 9.79 mean (96.6 ms) 9.90 p50 / 10.25 mean (101.0 ms, back 12.3 ms)
Very-high-draw 3D (>=10k draws) 7.80 p50 (127.6 ms, Prep 92.8) 8.10 p50 (123.9 ms, Prep 92.1) 8.20 p50 (122.1 ms, Prep 84.4, copies 54.9)
600..760 s 10.6 p50 (94.5 ms) 10.4 p50 (95.4 ms) 12.8 p50 (78.1 ms, max 118.2 ms, 0 frames >=250 ms)
tex refreshes / write watch p50 15.8 / 2.3 ms 17.6 / 2.6 ms 14.2 / 2.0 ms
  • On the submitting thread, preparing dropped from 54.1–56.8 ms (refreshes 23.2–23.8 ms) down to 32.7–42.3 ms (refreshes 8.8–12.2 ms).
  • Clean win on 8C/16T + RTX 5080 — directly addresses the GUARDS.lock() contention previously measured at 30–96 ms/frame across threads in a diagnostics capture.

@henriazz10

Copy link
Copy Markdown
Contributor Author

Thanks a lot for the long run and the careful A/B, @POMXARK. It's good to see e29efec hold for 12 minutes with no errors, and to see the refresh time on the submitting thread roughly halved on the same CPU/GPU class we develop on.

It also matches what we see in the heavy open world: the frame there is still bound by CPU work around textures. We're now going through those costs one by one, and any results will come as separate PRs.

🤖 Generated with Claude Code

@CuteBooty

Copy link
Copy Markdown
Contributor

A data point from an RTX 3070 8 GB for making VICTORY_FAULT_UNLOCKED the default. To be clear about what this is: the switch itself, flipped by the in-run A/B (VICTORY_AB=30,VICTORY_FAULT_UNLOCKED) on main 63b902fe1. This PR's own commits (the hoisted soft-view read, e29efec) were not in the build.

switch off switch on
phases / frames 13 / 5,954 12 / 5,423
frame time, frame-weighted mean 63.4 ms 64.2 ms
p90, mean of the phases 78.4 ms 78.0 ms
at 4-6k draws (10 / 8 phases) 63.1 ms 65.9 ms
at 6-8k draws (1 / 2 phases) 69.8 ms 64.5 ms
at 8-10k draws (1 / 1 phase) 78.7 ms 74.2 ms
write faults under the guards' lock, a frame 479: 40.0 ms waiting, 4.6 ms holding 21: 1.5 ms waiting
write faults without the lock, a frame 21 (0.5 ms) 474 (12.2 ms), 6.7 going the locked way after all
"guards" in the lock-wait line, all threads 96.6 ms 34.9 ms
"ranges held", median 0.97 ms 0.51 ms

(The lock-wait line is printed every 5 s and the phases last 30 s, so each side carries a little of the other.)

So the switch does exactly what it says here: the wait for the guards' lock in write faults goes from about 40 ms to about 1.5 ms a frame across threads. But the frame time does not move outside noise on this PC: +1.3 % overall, and over the 13 neighbouring phase pairs whose draw counts are within 15 % of each other, 66.6 ms off against 67.8 ms on. These frames look bound by the submitting thread (the "preparing per frame, the submitting thread" line has a median of 51 ms in this session), not by the title's threads waiting in faults. That is a different picture from the gains reported above, so it may simply be a different bottleneck on this setup (VICTORY_THIN=1, 60-75 ms frames).

Stability: no error lines and no device loss; the switch changed 33 times in the session and the player reported no stuck or flickering geometry.

One line from each side, as logged:

waits for the write trackers' locks per frame, all threads (number, time): ranges held 25 1.47 ms, journal read 229 1.31 ms, journal noted in 84 0.54 ms, guards 645 99.62 ms; write faults opened under the guards' lock 538 (44.14 ms waiting for it, 4.91 ms holding it), without it 0 (0.00 ms), going the locked way after all 0 (VICTORY_FAULT_UNLOCKED)
waits for the write trackers' locks per frame, all threads (number, time): ranges held 21 0.63 ms, journal read 286 2.03 ms, journal noted in 92 0.85 ms, guards 234 26.78 ms; write faults opened under the guards' lock 3 (0.11 ms waiting for it, 0.46 ms holding it), without it 517 (16.15 ms), going the locked way after all 7 (VICTORY_FAULT_UNLOCKED)

One session on one PC. I can repeat it with this PR's commits built in if that is more useful than the bare switch.

Measurements and this report were prepared with an AI assistant (Claude Code); the run was played on my machine.

@yeedgrthfgv

Copy link
Copy Markdown
Contributor

One thing to keep in mind if this turns the switch on by default: #817 (found by Machad3x after the review above) is a separate race in the same path. A late lock-free fault can undo a guard taken since, and the title's next write to that block is an access violation. The fix is in #834. As far as I can see it is not in this PR, so #834 should probably land first or together with it.

Another data point for the switch itself, like CuteBooty's: in-run A/B (VICTORY_AB=30,VICTORY_FAULT_UNLOCKED) on 63b902f with the #834 fix, Ryzen 5 9600X (6 cores), RTX 5080, Series X 4K output, DLSS Performance, frame generation 2x, THIN on, prison scene, about 4,800 draws a frame. Guards waits went from 43 to 50 ms a frame to 7.5 to 12.5 ms, and faults waiting for the lock from 23 to 26 ms to under 0.1 ms. The frame did not change: 42.2 ms both ways. No crash in 13 flips. Details in #834.

So on this PC, as on CuteBooty's 3070, the waiting goes away but the frame is set elsewhere. Machad3x's bigger gain (with #498 and title ray tracing on) may depend on those.

A write fault that checked PROTECTED, and reached open_unlocked only after
another fault had opened the block and that was settled, found the block
writable already but queued its opening anyway. A refresh that had looked at
the block before guarded it again (read-only, protected, clean); the next
settle then took the stale entry, removed the block from the guards and
cleared PROTECTED while the memory stayed read-only. The title's next write
there was an access violation no handler took.

open_unlocked records took = !(done && old == PAGE_READWRITE): whether that
fault's own VirtualProtect made the block writable. settle leaves an entry
that did not take the block alone, apart from its OPENING and TOLD bits, so
the guard taken since survives. Test a_late_fault_leaves_the_guard_taken_since.

Reported in trustno1-coder#817 by @Machad3x, who published the fix as commit 3175c5dc in
Machad3x/VIctory-nvidia-RTX. Ported onto current main with a longer test.

(cherry picked from commit 48afe3e)
The regression test queued a stale entry with no `others`, so the
`TOLD.clear(other)` branch added by the fix was never exercised. Let the
entry tell the other view as written, as `open_unlocked` does, and assert
that after settling TOLD and OPENING are gone while both views keep their
guard notes and PROTECTED bit. On the unfixed code the other view's notes
are removed, so this also detects the bug on that path.

(cherry picked from commit fd31efd)
@henriazz10

Copy link
Copy Markdown
Contributor Author

Thanks @CuteBooty and @yeedgrthfgv, both reports are very useful.

@yeedgrthfgv, good catch. Since this PR turns VICTORY_FAULT_UNLOCKED on by default, it now includes the #817 fix from #834 by @GRG-DA, as two cherry-picks with authorship kept (843f9de, 694b97d on top of e29efec). With them, cargo test -p victory-gpu --lib watch passes (48 tests, including a_late_fault_leaves_the_guard_taken_since). The land / ask / refresh tests where the segfault showed up passed 8 runs out of 8. If #834 lands first, I'll drop the two commits here.

On the frame rate: your two runs and CuteBooty's agree. The switch removes the lock waits (CuteBooty's guards wait went from about 97 to 35 ms a frame across threads) but leaves the frame where it is, when the submitting thread sets the frame (51 ms "preparing" on the 5800X3D, 42 ms on the 9600X). POMXARK's gain on the 5700X came mostly from the refresh time on the submitting thread itself (54-57 down to 33-42 ms). That fits the same picture. So I'd describe this PR as removing contention, with an fps gain only where faults sit on the submitting thread's path. Next we're looking at the submitting thread itself, starting from #1066.

🤖 Generated with Claude Code

@hazben

hazben commented Nov 28, 2026

Copy link
Copy Markdown

A measurement of #516 on its own (head e29efec), with the counters the PR describes. Short version: on this PC and route the title's write faults waiting on the guards' lock go from 18.9 ms a frame to 0.02, and the frame does not change (17.15 fps without #516, 17.30 with, one run with). The waiting goes away and the frame is set elsewhere, as the two reports on the bare switch above found (RTX 3070; RTX 5080 with #834).

Setup: RTX 4070 SUPER, 32 GB, DLSS Quality, frame generation off. Main 63b902f plus a local patch stack (24 commits, not upstream), plus #614, #501 (its switches off) and #834 in every arm. A stick-steered walk of 250-320 s through one district with 13 marks, 3,500-11,400 draws per frame (median per leg), the same save and path in every run. Arms: Y = without #516, run twice; X = Y + #516 at e29efec, nothing else changed, run once; Yfu = Y with VICTORY_FAULT_UNLOCKED=1, main's switch alone without #516's soft runs under the guards' lock, run once. A first run of X is not counted: the scripted walk was blocked at a door and cut at its time limit; that log has no device loss and no new error line.

Frames (from 60 s after the world loads):

Y run 1 Y run 2 X (#516) Yfu (switch only)
fps 17.14 17.16 17.30 16.12
median frame ms 54.4 55.0 55.0 57.8
p95 / p99 ms 88.5 / 119.3 88.3 / 115.9 81.7 / 107.5 96.6 / 121.9
frames over 100 ms per min 29.0 26.3 16.4 40.6
dense stretch (marks 6-11, about 9,000 draws), fps 15.58 15.61 16.30 15.29
draws per frame, median 7,942 7,780 7,899 8,410
frame ms above what the draw count predicts +0.1 -0.1 -1.9 +1.2

The last row: a line ms = 38.0 + 0.0028 x draws fitted on the two Y runs' 5 s stretches (123 of them, r = 0.88); each cell is that run's mean distance from the line. The two Y runs agree to 0.02 fps, which is closer than the noise, not a measure of it: five runs of the same build as X without #834 (63b902f + stack + #516 + #614) span 16.41-17.56 fps (1.15 fps, 6.8 %), and their draw-adjusted frame ms span 5.7 ms. X against Y (+0.16 fps, -1.9 ms) and Yfu against Y (-1.0 fps, +1.2 ms, with 7 % more draws) are both inside that. I read this as no difference from #516 on this route, not as no gain anywhere.

Counters (medians of the 5 s lines inside the route, per frame, all threads unless said):

Y run 1 Y run 2 X (#516) Yfu
write faults opened under the guards' lock: number, ms waiting for it 375, 18.70 375, 19.15 3, 0.02 3, 0.02
write faults opened without it: number, ms 0 0 377, 12.8 391, 13.4
time in write faults, all three kinds, ms 23.4 24.2 13.0 13.6
the guards' lock: acquisitions, ms 355, 45.8 354, 53.1 113, 26.9 112, 23.1
"ms waiting for the guards", title-memory line 18.5 19.1 18.1 17.9
submitting thread, total ms 50.8 52.9 50.7 53.7
... refreshes, ms 16.5 16.1 17.0 17.0
... lists' steps, ms 25.3 24.2 25.8 25.9
refreshes counted 14,416 14,254 14,247 14,808
page faults 1,504 1,604 1,403 1,468

What it says. The waits are large in Y, not already small: 375 faults a frame spent 18.7-19.2 ms waiting for the lock and 4.9 ms holding it. #516 and the bare switch both remove them (3 faults, 0.02 ms) and cut the lock's acquisitions from 355 to 113 a frame; the time in write faults over all threads falls from 23.4-24.2 to 13.0-13.6 ms, the rest being the faults now taken without the lock. The submitting thread does not move (50.7 against 50.8 and 52.9 ms) and its refresh time does not halve here (17.0 against 16.5 and 16.1 ms), so the title's threads that waited were not what set the frame on this route. Likely, not tested: the frame here is set by the submitting thread (about 51 of 55 ms). The "ms waiting for the guards" number on the title-memory line (18.1 against 18.5 and 19.1) is a different wait and does not change with #516 here. Yfu is the one run below the others; it also had the most draws, and the draw-adjusted gap (+1.2 ms) is a fifth of the noise, so I take it as no sign that the switch alone is worse.

How this fits the combined-stack result. An earlier batch on main 8fc2254 (same local stack) had #516 + #614 together against neither: 15.47 and 16.02 fps for neither, 17.43 with both (+10.7 % on the mean of two), and the write-fault waits went from 15 ms to 0 there too. Two more runs of that same with-both build gave 16.89 and 16.31 fps, so its spread is 1.1 fps and the +10.7 % is nearer +4 to +11 %. Y here has #614 and not #516 and runs at 17.15, inside the range of every build that has both (16.31-17.56 over eight runs). So the step from 15.7 to about 17 fps did not need #516. Likely: it is #614 or something else in the move to 63b902f (the counted refreshes were 38.7k a frame without #614 and are 14.3k in Y and 14.2k in X). Not tested: #516 without #614, and "neither" on 63b902f, so I cannot separate those.

Memory and stability: process private peak 24.58 / 24.59 GB (Y), 24.63 GB (X), 24.64 GB (Yfu); dedicated VRAM peak 8.39 / 8.63, 8.54, 8.53 GB. All four counted runs reached the end of the route with no crash, hang or device error; every run, Y included, has the same one error line (a Streamline line about 55-60 s in). #834 is in all arms, so I cannot say anything about the race it fixes; no crash in the three runs that had the lock-free path (both X runs and Yfu).

Not measured: #516 without #614; a second run of X; title ray tracing with #498 (the largest gain reported above had it on) or frame generation on; another card or CPU. Unverified: that the local stack does not change these numbers.

@henriazz10

Copy link
Copy Markdown
Contributor Author

Thanks @hazben, this is the cleanest measurement of #516 so far. The A/A pair, the draw-adjusted column and the five-run spread make the result easy to trust.

It agrees with what we see here and with the two earlier reports. #516 removes the write-fault waits on the guards' lock (375 faults and about 19 ms a frame down to 3 faults and 0.02 ms on your route). The frame stays where the submitting thread puts it (about 51 of 55 ms, unchanged at 50.7 ms with #516). Your note that the 15.7 to 17 fps step came without #516 also fits: on our side most of the refresh time on the submitting thread went with #614-style lock-free no-op refreshes, which #814 now carries.

So I'll keep describing #516 as a contention fix: fewer lock acquisitions, less time in write faults on the title's threads, and fewer frames over 100 ms in your X run (16.4 against 26-29 a minute, one run). It isn't an fps change while the submitting thread sets the frame. That thread is what we're working on next, starting from #1066.

🤖 Generated with Claude Code

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

8 participants