GPU: copy back with NtWriteVirtualMemory, no VirtualQuery before a copy into the title (copy back 1.5 s -> 0.13 s) #1215
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!1215
Loading…
Reference in a new issue
No description provided.
Delete branch "github-bot/gh-1215"
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?
@LooperSalty on GitHub · 2026-11-24 16:26 UTC · original
LooperSalty:perf/nt-write-copy-back→main· +407 −3 · 4 files · 1 commits · openFrom a tester working with Claude (AI-assisted); measured on my own machine (i7-13700KF, RTX 4070 Super). No game data in the diff.
What
write_mapped(the copy back into title memory after a submission) callsNtWriteVirtualMemorydirectly.WriteProcessMemoryqueries the destination region first (NtQueryVirtualMemory), about 2 ms a call in the title's big pools. Falls back toWriteProcessMemoryif the symbol is not found; an unmapped page is reported the same way.copy_mappedno longer runsVirtualQuerybefore a copy into title memory; the check only runs after the first attempt failed.VICTORY_PROFILE=<ms>samples all thread stacks and prints the hottest every 5 s.host.rs(partial write into a half-committed range, copy into not-fully-mapped memory).Measured
World stage, DRED off: copy back of one frame 1.5 s -> 0.13 s. Together with leaving
VICTORY_D3D12_DREDunset (it costs about 2.5x per frame on its own), world went from 0.2 fps to 1.6 fps on this change set alone. Numbers are also in #841.cargo test -p victory-gpu -p victory-shaderis green on this branch (based onorigin/mainffe310e).@TG0000 on GitHub · 2026-11-24 16:57 UTC · original
The direction is right: the
VirtualQuerybefore copies into the title and the one insideWriteProcessMemoryare a large part of the time here too (RX 6800, Windows 11).One behaviour change to check at
6950319, reproduced here: incopy_mapped(host.rs:1306-1327) thecommittedcheck now comes after the failedread. By thenReadProcessMemoryhas written the mapped part of a destination that is not all mapped. Onmainsuch a destination is left alone. Example: a copy of 25 pages over a hole (page 30 of 40):mainwrites nothing,6950319writes the pages before the hole. The new test cannot see it, because its two calls copy the same bytes.Suggested fix: when the destination is title memory, check it before the first read, cheaply: read one byte of each page through the kernel (
ReadProcessMemorywith the title memory as the source), which costs far less thanVirtualQueryin the big pools. I have a test for "written whole or not at all" that passes onmainand fails on this head, and will gladly share it.profile.rs(a sampler behind the diagnostics feature) is unrelated to the change; splitting it out would make this easier to take.Not checked: Wine. I am comparing the speed of
NtWriteVirtualMemorywith other ways in the same launch and will post the number.AI-assisted (Claude Code).
@TG0000 on GitHub · 2026-11-24 17:02 UTC · original
The number I promised, with your two changes ported into my measuring build, RX 6800, Windows 11, one launch, 9 looped world frames of 48 submissions, paired (submissions only, not frame time):
NtWriteVirtualMemoryfor the copy backmain's way of writing back, for scalemain's check before each title copy, for scaleSo no difference found between yours and mine (the noise floor is 9 to 13 ms), and both are far better than
main. I drop my two branches in favour of this PR.For the destination behaviour from my comment above, here is a tested fix, free to take (applied to
6950319:cargo test -p victory-gpu55 passed, 0 failed, including the two tests that fail without it): a destination is checked by reading one byte of each page through the kernel, so it stays written whole or not at all, and a guard page stays one. Its cost is inside the numbers above (my version already did this).patch (host.rs and host/destination_tests.rs)
Also:
cargo clippy -p victory-gpu --all-targets --all-features -- -D warningsfails at6950319in the newprofile.rs(line 211 lacks a SAFETY comment, line 263 wantsis_multiple_of). The patch does not touch that file.AI-assisted (Claude Code).
@Zipettte on GitHub · 2026-11-24 17:59 UTC · original
Tested on AMD Radeon RX 7700 XT (RDNA3, gfx1101), driver 32.0.32029.2010, Windows 11, Rust 1.98.1 MSVC at
6950319:cargo test -p victory-gpupasses, 52 passed / 0 failed. Workspace baseline on main (ffe310e) is green on the same machine (342 passed).Tested with AI assistance (Claude).
@Mumogg on GitHub · 2026-11-24 18:38 UTC · original · edited 2026-11-24 19:09 UTC
A/B under Wine, for the Linux side: no measurable difference here, in either direction, and nothing broke.
Setup: RTX 5070 Ti (driver 615.78.08), Ryzen 5 9600X, Nobara 44, Wine 11.22 Staging, vkd3d-proton from GE-Proton11-14. Tree:
ffe310e+ #454 #177 #259 #233 #646 #578 #334 #493 #724 #725 + the #783 region fallback, with and without this PR (6950319; inwrite_mappedI kept #783's region walk and used this PR'sNtWriteVirtualMemorycall inside it). Diagnostics build, DRED off, 1.0.2.1, network off, warm caches, wave64 mesh programs refused (#578), so about 200 draws per world frame.Four 420 s runs in the order base / PR / PR / base; medians over the frame lines in the world:
The spread between the two runs of one side is as large as the spread between sides. That fits what the description says the gain comes from: Windows querying the destination's region inside
WriteProcessMemory. My guess, not checked in Wine's source, is that itsWriteProcessMemoryhas no such walk to save; the standalone timings I took earlier of these calls under Wine were microseconds each (#400). PR 2 has fewer lines because the title stopped early with the lookup spin described in #774, which is unrelated.So: harmless under Wine as far as these runs show, and the win is a Windows one. I did not try the thread-pool branch or
VICTORY_PROFILE.(AI-assisted, Claude Code; runs on my machine.)
Edit: the Wine version was 11.22 Staging, not 11.21 (the system had updated before these runs).
@EduAzevedo on GitHub · 2026-11-24 21:21 UTC · original
Measured in game on NVIDIA Turing: RTX 2070 SUPER, driver 610.88, Ryzen 7 5700G, Windows 11, retail 1.0.2.1.
Both runs use the same base: main
ffe310e+ #493 + #189 + #206, the 2^18 dynamic reserve from #747 applied locally,VICTORY_SHADER_LOOP_LIMIT=32, and the diag build. The only change between them is this PR. Each run went into the world for about 5 minutes. The figures are medians of the one-secondfpslines while in the world, where frames take 200 ms or more.no dynamic slotsand 0a table is not made;0x80070057texture failures (2560x1440);preparing, mostly the upload copies (~280 ms), then the GPU (~210 ms).Only counts and timings here, no game data. AI-assisted measurement.
@aycow on GitHub · 2026-11-24 21:22 UTC · original
@LooperSalty @avt0m4t48 An independent CPU replay of 4 MiB with 131,072 one-byte runs measured 1,805.628 ms for Win32 checked writes and 114.690 ms for the native write primitive. Metadata replay alone was 2.184 ms. The separate VirtualQuery control took 1,362.622 ms per run set and 10.376 ms when queried once per 4 KiB page. The workload starts at completed change metadata downstream of #1681’s GPU comparison.
On this host both raw write primitives returned zero bytes and consumed PAGE_GUARD on the first attempt. Current write_mapped then retried and wrote one byte, while read_into refused and left the guard intact. The 2.502 ms direct control held a fixture-owned writable mapping. Production still needs an ownership proof for title mappings. This supports evaluating #1215's write backend independently from broad title-copy changes while preserving partial mapping and guard behavior. We are leaving #387 in draft.
AI-assisted using Codex.
@aycow on GitHub · 2026-11-24 22:43 UTC · original
@s2k721 @avt0m4t48 I rechecked the protected bulk copyback control on
70564ae. A 64 KiB span with a protected 4 KiB subrange delivered 61,440 bytes while stock reported 65,536. The local correction preflights contiguous writable spans and leaves a partially delivered tracked page dirty. It deliberately keeps the existing bulk policy and stays separate from exact fragmented replay. AMD and WARP passed the protected/mapped-neighbor and pending-write controls. The earlier exact-write benchmark still leaves roughly 115 ms for 131,072 fragments with a native per-run call. I’m avoiding per-run preflight and direct stores into mappings with no proven lifetime ownership. The GPU mask and lower-overhead checked writer remain complementary.AI-assisted using Codex.
@aycow on GitHub · 2026-11-24 23:17 UTC · original · edited 2026-11-25 01:33 UTC
@LooperSalty @avt0m4t48 I validated a local opt-in exact-run consumer for XMem-managed physical views on
70564ae + #1816. It retains the physical section and page index with the snapshot, verifies them under XMem’s map lock at delivery, and uses your #1215 native checked primitive through the title address. A replaced page receives no stale callback, a protected subrange is skipped, and an overlapping declaration does not retry a delivered or failed byte. The #1681 GPU mask can remain a separate producer.In the generated 4 MiB case with 131,072 changed bytes at stride 32, per-run Win32 writes took a median 492.52 ms and the native writer with one protection walk per 64 KiB piece took 45.50 ms. Both produced the same bytes. This is an isolated writer test, excluding GPU time and title mapping-lock contention. The Windows remap, alias and protected-neighbor controls passed. The default bulk path is unchanged. Computed-address writes and mappings outside XMem still need a separate lifetime guarantee.
AI-assisted using Codex.
Current-main recheck on
ccfb029: the managed-view candidate compares the GPU copy against its submission snapshot and delivers only changed byte runs while the original XMem view is still mapped. The checked kernel copy from upstream #322 reduced the median against the Win32 wrapper from 351.82 to 59.74 ms for the 4 MiB fragmented fixture, and from 214.94 to 80.89 ms with a partially mapped destination. The dense control rose from 2.78 to 3.21 ms. These are CPU exact-writer comparisons. The mapping lease, title workload, and overall copyback time still need matched measurements. The candidate remains local and default-off.@vladbondarenko97 on GitHub · 2026-11-24 23:32 UTC · original
Independent numbers for this PR from an RTX 3080 Ti box (i7-12700KF, Windows 11), from a standalone microbenchmark (one write into a committed
PAGE_READWRITEallocation of our own process; time per call):WriteProcessMemoryVirtualQueryaloneNtWriteVirtualMemorySo all of
WriteProcessMemory's extra cost is its internal region query, which grows with the size of the destination allocation (the title's big pools), andNtWriteVirtualMemoryremoves it — the same conclusion as this PR. A related negative result: aVirtualQueryper region followed by a direct copy (we tried it onffe310e) changed nothing in game, because that query is the slow part.In game on
ffe310e+ #578 / #454 / #206 / #493 (same card), the copy-back time was ≈ 85 % writes (reads 0.5 %, write-watch 1 %) and copy-back was 85–840 ms of a 750–1230 ms world frame (e.g.0.8 fps, 1233 ms/frame (… 838 copying back 27.4 MB)), consistent with your 1.5 s → 0.13 s. I have not run this PR's head in game.AI use: Claude Code (Claude Opus 5.5 and Claude Sonnet 5.5 agents) wrote and ran the benchmark and the profiling build; numbers are from those runs.
@Marvisak on GitHub · 2026-11-25 01:19 UTC · original
Automated triage for the maintainers.
Needs rebase. This pull request conflicts with
main(conflicting:crates/victory-gpu/Cargo.toml,crates/victory-gpu/src/host.rs). It will be reviewed once it is rebased onto the currentmain.Generated by Claude Code
@r4tedx on GitHub · 2026-11-25 07:42 UTC · original
A note for the triage, from reading this PR against
main940d32f: the two speed changes here are inmainsinceb58d198(jsthope, "GPU: copy into title memory without asking for its region first", 24 Nov).VirtualQuerybefore a list's copy into title memory, and stops the copy-back from going throughWriteProcessMemory, for the reason this PR gives: the region query costs about 2 ms a call in the title's big pools.write_mapped's comment onmain(host.rs:2057) says the same.ffe310e, so its 1.5 s to 0.13 s was measured against the code before that commit, and it conflicts withmainin the same functions.What is only here is the diagnostics part,
VICTORY_PROFILE=<ms>sampling of thread stacks. That looks worth keeping as a small PR of its own.So my suggestion: measure the copy-back once on
940d32fwith and withoutNtWriteVirtualMemory. If nothing is left of the gain, close this and send the profiler separately; if something is, a rebase with that number makes the case.Nothing was run for this comment; I read the diff and
main.AI-assisted (Claude Code).
View command line instructions
Manual merge helper
Use this merge commit message when completing the merge manually.
Checkout
From your project repository, check out a new branch and test the changes.Merge
Merge the changes and update on Forgejo.