Skip to content

GPU: a host copy nothing wrote is not asked for at submission - #2136

Open
r4tedx wants to merge 2 commits into
Marvisak:mainfrom
r4tedx:gpu/skip-unchanged-copies
Open

r4tedx wants to merge 2 commits into
Marvisak:mainfrom
r4tedx:gpu/skip-unchanged-copies

Conversation

@r4tedx

@r4tedx r4tedx commented Nov 25, 2026 •

Copy link
Copy Markdown
Contributor

A world submission asks for every host copy of title memory many times over, and nearly every time nothing was written there. The title run on 940d32f in #400 (comment) counted 254,139 change checks a frame over 92.8 GB, 99.7 % of it unchanged: lists whose shaders use title memory name every copy there is (about 4,100 in the world, memory::prepare), and every title copy makes them all due again (Batch::title_written).

Each of those checks does real work to find nothing: Batch::add goes through two hash maps and queues a refresh, the flush calls Shadow::update, which locks the copy's declared ranges and then the tracker's set of changed pages, and the range is recorded as current in a map again. In a synthetic frame of the published volumes (#2134) that is 332–354 ns a check, 274,094 times: half of the time spent preparing.

The change

A tracker keeps the number of its changed pages beside the set (watch::Changed): written while the set's lock is held, by every path that changes the set (a take, taken, the fault handler, a reported write, a remap), and read without the lock by Tracked::unchanged. Batch::add leaves out a copy whose tracker says nothing changed: one atomic load, no lock, no map.

The bytes the host copies get are the same as before. A refresh of a tracked copy with no changed page copied nothing and guarded nothing, so leaving it out changes no state. A write that lands while the number is read counts as landing after it, which is what a take that ran a moment earlier already meant. Copies of memory that is not tracked are copied whole as before, and a copy with a page it cannot guard, or one it took only in part, keeps a changed page and is asked for as before.

The copies per frame line counts the copies left out in its skipped fields, which nothing used; checked and unchanged now cover only the copies that had something changed.

Numbers (synthetic)

The benchmark of #2134 on this branch and on main, run back to back in the same job, twice. WARP, Ryzen 7 7800X3D confined to 6 logical processors, diag profile with diagnostics, median of 5 frames:

per frame main this branch
preparing 225.1 ms / 168.1 ms 100.8 ms / 81.9 ms
of it: refresh batches 155.8 / 114.6 ms 45.8 / 38.7 ms
of it: naming every copy for title memory 22.0 / 16.5 ms 6.6 / 4.9 ms
change checks 274,094 1,325
checked 102.7 GB 3.5 GB
changed and copied 159.1 MB 159.1 MB
protect calls 7,813 7,813
guard faults 3,638 3,638
marked pages compared 942 942
a check of an unchanged copy through a batch 332–354 ns 6 ns

A second job later, with the machine busier and so less even, gave 721.7 / 306.1 ms on main and 122.7 / 106.2 ms on this branch.

The bytes copied, the pages guarded and the pages written back are the same in both; only the checks that found nothing are gone. At the 254,139 checks of the title run, the measured cost per check comes to 84–90 ms a frame before and 1.5 ms after.

This is the wall time of a made-up pattern on an idle process, not a frame rate. In the title the same volumes cost six times what they cost in the benchmark, so I do not know how much of the title's 1.2 s this takes off: a tester's copies per frame line before and after would say.

Not a duplicate of

None of them skips a copy without visiting it. The idea of answering "nothing changed" without going to the page tables is the one the forks fxceless/victory-fork and Zettrox/ztrx had for the older write-watch design, where they batched the GetWriteWatch queries of a submission into one and answered later checks from its journal. Credit to them for the idea; none of their code is used, and on main's guard design the answer is already in the tracker, so it needs no query at all.

Tested

  • New watch::tests::a_tracker_says_when_nothing_changed: the number follows the set through takes, a guard fault, a reported write, taken and a remap; partial_pages_stay_changed also checks it.
  • New host::batch::a_copy_nothing_wrote_is_not_asked_for: an unchanged copy queues no refresh in this or a later batch, also after title_written; a write the guard takes or one that is reported brings back the page written.
  • The victory-gpu suite on WARP (diagnostics build, VICTORY_D3D12_WARP=1, one thread): 148 passed, 0 failed, 2 ignored.
  • cargo clippy -p victory-gpu --all-targets --locked -- -D warnings, with and without --features diagnostics: clean.

Not run: the title; any hardware GPU (the suite and the benchmark ran on the software adapter only); cargo test --workspace. The title was not used in these tests.

AI-assisted

🤖 Generated with Claude Code

Lists whose shaders use title memory ask for every host copy there is
at each submission (about 4,100 in the world), and again after every
title copy. A title run on 940d32f published 254,139 change checks a
frame over 92.8 GB, 99.7 % of it unchanged (Marvisak#400). Each of those checks
went through the refresh batch's maps and locked the copy's set of
changed pages to find it empty: about 330 ns each in a synthetic frame
of the same volumes, where they were half of the preparing time.

A tracker now keeps the number of its changed pages beside the set,
written under the set's lock and read without it, and the refresh batch
leaves out a copy whose number is zero. The refresh of such a copy
copied nothing before, so the host copies get the same bytes as before;
a write that lands while the number is read counts as landing after, as
it does with a take. Memory that is not tracked is copied whole as
before. The diagnostics line counts the copies left out as skipped.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
RecompLive added a commit to RecompLive/victory-fork that referenced this pull request Nov 25, 2026
A world submission asks for every host copy of title memory many times
over (lists whose shaders use title memory name every copy there is,
about 4,100), and nearly every time nothing was written there. Each ask
did real work to find nothing: a hash-map lookup, a queued refresh, a
Shadow::update that locked the copy's declared ranges and the tracker's
changed pages.

Tracked now keeps the number of its changed pages in an atomic, written
under the set's lock by a guard around every use of it; Shadow::unchanged
is one load. Batch::add returns on it before any bookkeeping, so such a
copy queues nothing and is remembered nowhere. Memory copied whole each
time is never unchanged and keeps its old path.

Adapted from Marvisak/VIctory#2136 (GPL-3.0-or-later), by r4tedx; the
tests are theirs, ported to our batch (which keeps ranges per copy and
the fx maps).

PortWatch: frame preparation work removed, not a visual change.

@DenRakEiw DenRakEiw left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Verified locally merged onto origin/main (940d32f, Windows 11 x64, NVIDIA GeForce RTX 5090, Rust 1.98.1 MSVC):

  • cargo test -p victory-gpu --lib a_copy_nothing_wrote_is_not_asked_for — passed on RTX 5090
  • cargo clippy -p victory-gpu --all-targets — 0 warnings
  • cargo xtask policy — ok (183 files ok)

Clean unchanged() fast-path skipping submission refresh copies when nothing wrote to the tracked range. Looks great.

AI-assisted (Gemini 4 Neon).

@yualk

yualk commented Nov 25, 2026

Copy link
Copy Markdown

Independent locked MSVC diag comparison of 940d32f69 vs 937f329a4 on Windows / RTX 5070 Ti 10de:2c05 and WARP 1414:008c. An owned production Host::execute control records actual GPU buffer-to-readback copies: cold partial + later full refresh, untouched, watched CPU store, ordered deferred title copy followed by refresh, and rearmed CPU store all return the exact expected words on both pins and both adapters; the independent same-shaped decoy is unchanged. In the untouched arm the two watches drop to zero with two skips.

With the exact #2134 workload and the same test-only counters in both trees, 2 warm-up + 5 scored frames/process in A–B–B–A order: RTX paired preparing medians 147.6→51.1 ms and 123.6→46.2 ms; explicit WARP 129.2→55.5 and 133.7→53.4 ms. Median checks 274,094→1,325 in both adapters. At each scored frame the checked-minus-unchanged bytes, requested bytes/count, guard/protect/open counts, snapshot bytes, marked pages/returned bytes, and title-copy count were equal. Same-pin RTX drift was visible, so these are directional synthetic results, not a precise speedup estimate or gameplay FPS. No title, textures, real shader workload or title threads writing concurrently with preparation in this replay.

@catminou4 catminou4 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Static review only: victory-gpu is Windows-only (D3D12/windows_sys) — nothing compiles or runs on macOS; source review of the diff.

Host copies nothing wrote not requested at submission — avoids redundant copies; check the dirty-tracking covers all write paths (#1963's write-scope tracking related).

r4tedx added a commit to r4tedx/VIctory that referenced this pull request Nov 25, 2026
…nothing wrote is not asked for at submission, head 937f329)

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
r4tedx added a commit to r4tedx/VIctory that referenced this pull request Nov 25, 2026
…sak#2115 in; Marvisak#2150 left out for now

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

r4tedx commented Nov 25, 2026

Copy link
Copy Markdown
Contributor Author

@catminou4 on whether the tracking covers every write path: the skip reads the same thing the refresh read. Tracked::unchanged is the size of the very set Tracked::take takes its pages from, kept under that set's lock, so a copy is left out exactly when its refresh would have found no page to copy. A write the trackers do not see (one that neither faults into the guard handler nor is reported through writing / title_memory_remapped) was not copied before this change either; this PR neither widens nor narrows that. Memory that is not tracked is never skipped.

#1963 is the other direction (what shaders wrote going back into title memory); it does not change which title writes a refresh sees.

Thanks to @yualk for the independent run above: its read-back control (cold, untouched, a watched CPU store, a title copy followed by a refresh, a re-armed store returning the expected words on both builds) is a check I did not have, and its counters match mine (274,094 → 1,325 checks, everything else equal).

Nothing new was run for this comment. The title was not used in these tests.

AI-assisted

@nathanbzrr

Copy link
Copy Markdown
Contributor

Verified head 937f329a4987918f38890ff9fe010c40710e6437 merged with #2134 (efb35b56) onto main (940d32f69b) on Windows 11 x64 / AMD Radeon RX 7600:

Hardware Benchmark (world_frame_preparing, 2 trials/config)

Variant preparing, ms (T1 / T2) copying back, ms (T1 / T2) change checks checked, MB changed (copied), MB Unchanged check (batch)
940d32f + #2134 (RX 7600) 244.1 / 227.2 181.9 / 173.3 274,094 102,722.0 159.1 437 ns
940d32f + #2134 + #2136 (RX 7600) 126.9 / 108.8 208.4 / 179.7 1,325 3,510.1 159.1 9–10 ns
940d32f + #2134 + #2136 + #2147 (RX 7600) 125.5 / 112.1 304.7 / 285.1 1,325 3,510.1 159.1 9 ns
940d32f + #2134 + #2136 + #2147 (WARP) 104.2 240.9 1,325 3,510.1 159.1 10 ns

All 145 victory-gpu lib tests pass (--test-threads=1). PR #2136's own AtomicUsize count + RAII Drop update is sound (Pages::drop updates AtomicUsize with Release while MutexGuard is still held).

Pre-Existing watch.rs Alias Bugs Exposed / Amplified by unchanged()

Two live synthetic repros against 940d32f + #2136 fail as predicted:

  1. Tracked::drop strips PAGE_READONLY from live sibling aliases (watch.rs:255-275): Dropping tracker ta over view a sees empty holders[a] and opens a to PAGE_READWRITE, ignoring live alias tracker tb over b. Writes via a stop faulting -> tb.unchanged() == true and tb.take(b) == [] (repro dropping_one_alias_tracker_keeps_live_alias_guarded panicked).
  2. writing(a) vs tb.take(b) marks b clean (watch.rs:199-202, 323-335): guard_aliases ignores protect failure when a is busy, and tb.take(b) still clears b -> tb.changed(b/PAGE) == false and tb.unchanged() == true while a stays unguarded (repro taking_alias_while_other_mapping_is_busy_keeps_page_changed panicked).
  3. (Source-level) changed_here (watch.rs:480-492) calls open() (PAGE_READWRITE) before c.pages().insert(page) (which updates Changed.2) and c.save(page), leaving a concurrency window where another CPU thread writes without faulting while unchanged() reads 0.

(Proposed / uncompiled fix direction:) keep a guarded in Tracked::drop whenever any alias still has holders; propagate protect success from aliases to Changed; mark dirty & save before open() in changed_here.

AI-assisted; synthetic tests only, no game files, dumps, or captures used.

…e opening

Three ways a write could go unseen by a tracker of another mapping of the
same pages (reported on Marvisak#2136, whose lock-free unchanged() shows them; the
first two fail on main 940d32f too):

- Dropping the last tracker of one mapping opened its pages although a
  tracker of another mapping still held them. They stay guarded until the
  last tracker of every mapping is gone, and are opened then.
- Taking a page whose other mapping is being written unseen (writing())
  could not guard it there, and still cleared the page. It stays changed.
- A fault opened the page before it marked the trackers (and kept the
  bytes): another thread could write it meanwhile with no fault and find
  unchanged() true, or the saved bytes already overwritten. All trackers,
  those of other mappings too, are marked first, then the pages opened.

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

r4tedx commented Nov 25, 2026

Copy link
Copy Markdown
Contributor Author

@nathanbzrr thank you, all three findings hold. Pushed as f85763e0 on top of 937f329 (fast-forward, no rebase). Only crates/victory-gpu/src/watch.rs changes.

Bugs 1 and 2 are already on main 940d32f. #2136's unchanged() makes them easier to see but did not cause them. On 940d32f, with only the unchanged() asserts removed, both of your repros still fail at tb.changed(b / PAGE).

  1. Tracked::drop: a page is opened only when no tracker holds it on its own mapping or on any other mapping of the same memory. When the last tracker is gone, all the mappings are opened.
  2. guard_aliases: it now returns the pages whose other mapping could not be guarded (busy, or protect failed). take() and taken() leave those pages changed, as they already did for a busy page on the tracker's own mapping.
  3. changed(): every tracker, including trackers of the other mappings, is marked and its bytes are saved while the pages are still read-only. Only then are the pages opened. The marking moved into a new helper mark(), which replaces changed_here. Lock order is unchanged.

Both of your repros are now tests (dropping_one_alias_tracker_keeps_live_alias_guarded, which also checks that both mappings are opened at the end, and taking_alias_while_other_mapping_is_busy_keeps_page_changed). They fail on 937f329 and pass on f85763e. There is no test for the ordering in 3, because a single-threaded test can't hit that race.

Verified:

  • cargo test -p victory-gpu --features diagnostics --lib -- --test-threads=1 on WARP: 150 passed, 0 failed, 2 ignored.
  • Clippy on victory-gpu, all targets: no warnings.
  • A separate review of the diff found no deadlock, no page left guarded without a holder, and no page opened while a tracker still needs it.

Still open, not new in this PR: alias links are one hop only. A third mapping linked only to a would not be marked by a write through b. This assumes the runtime reports every other mapping, which is what it does today.

AI-assisted; synthetic tests only, no game files, dumps, or captures used.

r4tedx pushed a commit to r4tedx/VIctory that referenced this pull request Nov 25, 2026
@r4tedx

r4tedx commented Nov 25, 2026

Copy link
Copy Markdown
Contributor Author

In-game counters from one PC: RTX 5070 (driver 616.92), Ryzen 7 7800X3D (max processor state 99 %), Windows 11, diagnostics builds, VICTORY_LOG=info VICTORY_D3D12_DRED=1. The title was run locally from my own copy. Only counts and timings are given here; no logs or title data are shared.

This is not a same-frame A/B:

The scenes are different moments of the opening. Only the counters this PR controls mean much; the frame times can't be attributed to it.

copies per frame:, a late world frame of each run:

before after, run A after, run B
refreshes skipped 0 281,414 (163,786 MB) 404,892
watches 129,062 (36.3 ms) 2,689 (11.6 ms) 4,474 (18.2 ms)
MB checked (unchanged) 48,020 (47,703) 4,229 (4,122) 4,860
refreshes made 4,730 (6,246 MB) 10,084 (12,192 MB) 27,767 (18,299 MB)
page faults 108,501 9,842 830
  • Skips and checks: as the PR intends, unchanged host copies are skipped in large numbers (0 on main), and watch work and MB checked fall by about 10-50x.
  • Refreshes made went up in both "after" runs, along with the draw count (about 1,000-1,900 before, 2,100-4,300 after). I can't separate the scene from the build there.
  • Preparing, as the whole build's frame line reports it: 300-700 ms before, 74-270 ms after.

AI-assisted

khayem487 pushed a commit to khayem487/VIctory that referenced this pull request Nov 25, 2026
…e opening

Three ways a write could go unseen by a tracker of another mapping of the
same pages (reported on Marvisak#2136, whose lock-free unchanged() shows them; the
first two fail on main 940d32f too):

- Dropping the last tracker of one mapping opened its pages although a
  tracker of another mapping still held them. They stay guarded until the
  last tracker of every mapping is gone, and are opened then.
- Taking a page whose other mapping is being written unseen (writing())
  could not guard it there, and still cleared the page. It stays changed.
- A fault opened the page before it marked the trackers (and kept the
  bytes): another thread could write it meanwhile with no fault and find
  unchanged() true, or the saved bytes already overwritten. All trackers,
  those of other mappings too, are marked first, then the pages opened.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
(cherry picked from commit f85763e)

Copy link
Copy Markdown
Owner

Automated triage for the maintainers.

Merge candidate. A tracker now keeps a count of its changed pages beside the set (watch::Changed, kept current by the Pages guard while the lock is held). Batch::add skips a copy whose tracker says nothing changed (Shadow::unchanged). For tracked memory, Shadow::update would have copied nothing in that case, so the bytes copied stay the same.

The second commit fixes the alias issues nathanbzrr reported:

  • Tracked::drop no longer opens a page that a tracker of another mapping still holds.
  • guard_aliases leaves pages changed when the other mapping could not be guarded.
  • changed() marks every tracker before it opens any page.

There are tests for each fix. I read the watch and batch changes and found no gap. CI passes. The counters from the author's in-game run fall as described, but that build carried other PRs, so the frame times cannot be credited to this PR.


Generated by Claude Code

Machad3x pushed a commit to Machad3x/VIctory-nvidia-3080ti that referenced this pull request Nov 25, 2026
…e opening

Three ways a write could go unseen by a tracker of another mapping of the
same pages (reported on Marvisak#2136, whose lock-free unchanged() shows them; the
first two fail on main 940d32f too):

- Dropping the last tracker of one mapping opened its pages although a
  tracker of another mapping still held them. They stay guarded until the
  last tracker of every mapping is gone, and are opened then.
- Taking a page whose other mapping is being written unseen (writing())
  could not guard it there, and still cleared the page. It stays changed.
- A fault opened the page before it marked the trackers (and kept the
  bytes): another thread could write it meanwhile with no fault and find
  unchanged() true, or the saved bytes already overwritten. All trackers,
  those of other mappings too, are marked first, then the pages opened.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants