GPU: open guarded pages in runs, not one VirtualProtect at a time #2849
No reviewers
Labels
No labels
accessibility
area: build
area: formats
area: gpu
area: loader
area: runtime
area: shader
bug
documentation
duplicate
enhancement
good first issue
gpu: amd
gpu: intel
gpu: nvidia
help wanted
host: wine
invalid
out of scope
question
request
run report
triage: confirmed
triage: has patch
triage: merge candidate
triage: needs info
triage: needs rebase
triage: needs work
triage: proposed close
wontfix
No milestone
No assignees
1 participant
Notifications
Due date
No due date set.
Dependencies
No dependencies set
Reference
github/Marvisak--VIctory!2849
Loading…
Reference in a new issue
No description provided.
Delete branch "github-bot/gh-2849"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
@Therecalloftheonion on GitHub · 2026-11-25 22:14 UTC · original
Therecalloftheonion:perf/guard-open-runs→main· +182 −13 · 1 files · 2 commits · openProblem
Issue #400 puts about 600 ms a frame in the guard lock on an RTX 2080, with ~3,800
VirtualProtectcalls a frame. Two hot paths opened guarded pages one call at a time:changed_here(a title write over a range, e.g. amemsetor a file read) collected the protected pages of the range and calledVirtualProtectper page.Tracked::dropdid the same for every page freed when the last tracker let go of it.The pages are already in sorted order in both callers.
Change
open_pagesgroups consecutive guarded pages andopen_rangemakes oneVirtualProtectcall per run. Only runs where every page is guarded are opened; unguarded pages are skipped exactly as before, and the guard set, changed marks and saved snapshots are unchanged.This is not #1884: that sorts/deduplicates alias pages and replaces active-writer scans; this batches the write-access restore calls themselves. It does not touch the copy path or the listed copy PRs.
Benchmark
cargo test --release -p victory-gpu benchmark_guard_open_runs -- --ignored --test-threads=1 --nocapture, 512 pages, 20 rounds, RX 6700 XT, release:The test-only switch runs both paths on the same protected pages, so this is a direct before/after. It measures the write-access restore step only, not game FPS; no title data was available here.
Tests
watch::tests::a_bulk_write_opens_guarded_pages_in_one_call: an 8-page write opens the run with one call and every page is read-write again.cargo test -p victory-gpu --lib: 227 passed, 2 ignored. Two unrelated upstream tests are filtered because they fail onmainon this machine (the 2^21 view-heap assertion and the copy-pair placement test).cargo clippy -p victory-gpu --all-targets -- -D warnings: clean.rustfmt --checkon the file reports pre-existing differences (first at line 100, unrelated); the new code follows the surrounding style.Regressions
open_pagesrelies on the sorted page lists both callers already provide; a future unsorted caller would lose batching but not correctness (unguarded pages are skipped).All Windows D3D12 vendors benefit equally; it is host CPU/kernel work, not GPU-specific.
@fuzeofficlal on GitHub · 2026-11-25 22:34 UTC · original
Tested
7be4f20don Windows 11 (RTX 5090 + WARP) and benchmarked--release, but caught a real WindowsVirtualProtectboundary bug inopen_range:Bug:
VirtualProtectfails across adjacentVirtualAllocregions without fallbackopen_rangeremoves[first, last)fromg.protectedand issues oneVirtualProtect((first * PAGE) as ..., (last - first) * PAGE, PAGE_READWRITE, &mut old)while ignoring theBOOLreturn value.VirtualProtectfails withERROR_INVALID_ADDRESS(487) and leaves all pages untouched whenever the range crosses two separateVirtualAllocreservations/allocations (even if they are contiguous and all present ing.protected) or contains a freed/uncommitted page.VirtualAlloc(..., PAGE_READONLY)regions: a single 128 KiBVirtualProtect(..., PAGE_READWRITE)returnedok=0, err=487, while per-page calls succeeded (ok=1).g.protectedalready removed those pages, the next write hits a still-PAGE_READONLYpage outside the guard set and crashes with an unhandled0xc0000005.VirtualProtect(...) != 0and falling back to per-pageVirtualProtectwhenlast - first > 1(matchingprotect_notinginwatch.rs) avoids this cleanly.Local test & benchmark results
cargo xtask policy(190 ok), clippy default &diagnostics0 warnings;watch::tests16/16 &shadow::tests9/9 passed on RTX 5090 and WARP.--releasebenchmark_guard_open_runs:4.5307 ms(10,240 calls) ->1.2228 ms(20 calls, ~3.71x faster).2851648) incrates/victory-gpu/src/watch.rs.@Therecalloftheonion on GitHub · 2026-11-25 22:52 UTC · original
Thanks @fuzeofficlal. Confirmed and accepted:
VirtualProtectcan fail across two separateVirtualAllocregions, and myopen_rangeremoved the pages fromprotectedbefore ignoring the return. A later write then faults on a still-read-only page outside the guard set.I also see #2848 item 6 implements the same open-runs optimization. This PR is a duplicate of that item, so I am closing it rather than carrying a second copy. The failure and the fallback belong on #2848; I have left the detail there. No credit claimed here.
@Therecalloftheonion on GitHub · 2026-11-25 23:02 UTC · original
Reopened with the fix for the
ERROR_INVALID_ADDRESS(487) failure found by @fuzeofficlal.open_rangenow checks the batchVirtualProtectresult and, when it is 0, restores the run one page at a time (the pages were already taken out ofg.protected; the per-page calls put them back toPAGE_READWRITEand a page that is gone still fails harmlessly, as the old path did).Added
open_runs_falls_back_across_separate_allocations: two adjacent pages allocated separately, both guarded, thenopen_pages; both must bePAGE_READWRITEand no longer inprotected. On this machine the two-page batch is the only run, so the test exercises the fallback path.Benchmark unchanged when the batch succeeds: 512 pages, 20 rounds, release: 4.13 ms (10,240 calls) vs 1.61 ms (20 calls). The fix adds no work to the success path.
Still overlaps #2848 item 6 (
2851648), which has the same batching and the same missing fallback; the fix applies there too. Maintainers can keep either; I am not claiming a separate contribution for the same optimization.@Machad3x on GitHub · 2026-11-25 23:18 UTC · original · edited 2026-11-25 23:26 UTC
Hardware result at head
7be4f20d, cherry-picked onto a fork stack that opens a changed page's aliases too, RTX 3080 Ti, driver 621.24.VirtualProtectfails (a call cannot span allocations), the pages are already removed from the guard set, and the next write faults for ever (access violation in a two-mapping tracker test the stack carries). Opening page by page when the call returns 0 fixed the test. Onmainthis needs two adjacent views both in one run.Ran with AI assistance (Claude Code).
@liandraraiane on GitHub · 2026-11-26 02:42 UTC · original
Measured on a Ryzen 7 5700G (16 threads) + RTX 3060, Windows 11, release, against their base
f3578e3, with a guard-lock contention benchmark (writer threads faulting on guarded pages while a submission thread arms and takes every tracker in rounds, plus a kernel-writer thread opening 8-page ranges withwritingevery 200 µs;guard_lock_contention_benchon branchliandraraiane/VIctory:perf/guard-lock-contention). Median of 5 rotated runs:writingp50 µsWith writers pausing 50 µs between writes the picture is the same. Under contention the time is waiting for the guard lock, not the open calls, so batching them does not show here. Even with one writer, an 8-page
writingtakes ~270 µs, almost all queued behind takes. Both pass a 12-thread stress test (writes vs takes and copies, no write lost, no stray fault) 20/20. Not tested: the title; ranges longer than 8 pages; #2848's other six changes.AI use: Claude Code ran the builds and benchmarks on this machine and drafted this comment.
@Marvisak on GitHub · 2026-11-26 03:23 UTC · original
Automated triage for the maintainers.
Verdict: needs work (small). The batching in
open_pages/open_range(crates/victory-gpu/src/watch.rs) looks correct. Runs are opened only when every page is guarded. The per-page fallback when the batchedVirtualProtectreturns 0 covers the cross-allocation failure fuzeofficlal found. The pages leaveprotectedas before, andOpenPagesstill counts pages. What needs fixing before a merge:writes_mark_every_tracker_of_their_page("Everything is changed at first; …"). Please restore it.open_pageslinks to[open], which this PR removes.For the maintainers: #2848 (item 6) makes the same batching change in
watch.rswithout the fallback, and the two conflict. If that item is kept, it needs the same fallback. The microbenchmark shows the restore step about 2.4x faster. liandraraiane's contention benchmark and Machad3x's title run show no measured effect, so the gain in the title is unproven.CI: passing (
check).Generated by Claude Code
@MeMako on GitHub · 2026-11-26 06:28 UTC · original
Verified
#2849(7b1316a) on Windows 11 (10.0.26200, Ryzen 9 7950X, RX 9070 XT, driver32.0.32029.2010, 32 GB RAM).Tests & Microbenchmark
cargo clippy -p victory-gpu --all-targets --locked -- -D warnings: clean.victory-gpu: 272/272 passed (includinga_bulk_write_opens_guarded_pages_in_one_call).benchmark_guard_open_runs(--release, 512 pages × 20 rounds):VirtualProtectcalls): 5.120 msVirtualProtectcalls): 1.336 ms (3.83x faster)Live Title Verification & Integration Note
main63bcca4+#2051+#2472+#2918+#1010+#2130with VRAM upload flags off +#3055+#2914), adding#2985+#2849+#3035together in the same live run (n=54world frames /n=38heavy frames vs.n=51/n=38baseline).6,186draws vs.5,290draws in baseline):prep → deferred: 92.7 → 71.8 ms (-20.9 ms / -22.5%)copy_back: 99.2 → 75.5 ms (-23.7 ms / -23.9%)watch.rstimers reflect#2849and#3035combined;#2985affects texture detile/upload separately.)#2985+#2849+#3035together): steady-tail frame time dropped 453.3 → 423.6 ms (2.20 → 2.40 fps), even with +17% more draws.#3035: Since#2849replaces single-pageopen()withopen_range(),#3035'sopened(page).fetch_add(1, Ordering::Relaxed)bump simply moves insideopen_range()'sif g.protected.remove(&page)loop (the single removal site) so both PRs compose cleanly.Hardware runs were performed by me on my 7950X / RX 9070 XT; comment drafted with Antigravity.
@kotekompotek on GitHub · 2026-11-25 22:51 UTC · original · reviewed
Correct batching: the debug_assert that a run is fully guarded is exactly the safety property needed — one VirtualProtect over a partial run would silently change protection of pages that aren't ours or that another tracker watches. Collecting to_open first keeps the same semantics as the per-page loop. LGTM.
@crashbashash on GitHub · 2026-11-26 10:10 UTC · original
On "the gain in the title is unproven": here is an in-game A/B from Linux, where every
VirtualProtectis a Wine callplus an
mprotect, so the per-call cost the batching saves is larger than on Windows. RX 6800 XT, RADV,GE-Proton11-14 (with a newer vkd3d-proton and the wineserver view tree),
diagbuild ofmain219878aplus ourLinux commits, with and without this PR (
7b1316a, merges cleanly), a save in the prison, 120 s in the world, idlemachine, runs interleaved.
No crash or change in behaviour in those runs, and the crate's tests pass under Wine, the fallback test included
(Wine also refuses a
VirtualProtectwhose range is not inside one view). Two runs a side is not a precise figure,but both are well apart.
AI-assisted (Claude Code). No game data attached.
@RobertAndreiGit on GitHub · 2026-11-26 13:59 UTC · original
RTX 3060 Ti / Proton: batching avoided 380
VirtualProtectcalls, but we saw no world-frame improvement in a downstream A/B. We adapted7be4f20and the per-block fallback in7b1316atocool_fox; this is not a test of the upstream branch unchanged. This fork first usesVirtualQueryto check that a run fits in one committed, uniformly read-only region. The 380 counts avoided protection calls, not net syscalls; 577 candidate runs took the per-block path.Same release executable (SHA-256 starts
d798074e88b7), same save and initial pipeline cache; onlyVICTORY_BATCH_OPEN=1changed. Series S, host temporal 720p → 1080p, 24 ms pipeline wait; Ryzen 5 5600, 32 GB RAM, RTX 3060 Ti 8 GB, NVIDIA 615.78.08, CachyOS Proton / vkd3d-proton 3.2.0. The focused Proton test for uniform and mixed protections passed.The player preferred the control and found the batching run visibly slower. One non-replayed run per setting, with overlapping frame times; this does not contradict the PR’s isolated open-step benchmark. A useful discriminator would be timing
VirtualQuery, successful batches and fallbacks separately during world frames.View command line instructions
Checkout
From your project repository, check out a new branch and test the changes.Merge
Merge the changes and update on Forgejo.