GPU: append only newly covered bytes to write snapshots #3209
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/3211"
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?
Imported from Marvisak/VIctory. Originally posted by yumaste on 2026-11-26T13:01:55Z.
Problem
A submission can declare overlapping writes to the same large GPU shadow. Whenever the covered range grows,
Shadow::writingallocates or takes another buffer and copies the previous snapshot into it along with the new bytes. Repeated extensions recopy the same data before any GPU work runs.Change
Keep the captured bytes in one pooled buffer and index disjoint ranges into it. Each declaration appends only bytes that have not been captured. Growth reuses a suitable touched pool buffer when available and returns the old one. The first checkout uses the shadow's last captured size as a preference, so repeated submissions can start in a warm buffer that already fits. Refreshes still update the baseline where the CPU has supplied newer values before submission. During copy-back, contiguous pieces are compared in place. A piece stored in several fragments is assembled only when the GPU changed it.
Synthetic benchmark
Windows x64, Core Ultra 9 275HX, Rust 1.98.1 with
-C opt-level=3. Captured from a synthetic 128 MiB buffer. Timed the capture and comparison kernels frommainand this branch with independent warmed pools, alternating run order and reusing the snapshot across modeled submissions. Medians of 16 runs per variant. Comparison skips duplicate full pieces as the copy-back loop does. These figures exclude D3D12 queue execution, synchronization, and the actual writes to guest memory.For the first case, the declarations are
[(32-i) MiB, (64+3*i) MiB)fori=0..=16. The old capture builds 1,088 MiB of snapshots to keep 96 MiB. On the warmed pool this change appends the 96 MiB once. The fragmented, modified case shows the cost of gathering the old bytes for a changed page.Pool growth after review
This capture-only check creates a fresh snapshot per sample. The mixed pool starts with touched 32 B, 4 KiB, 8 MiB and 64 MiB buffers. The growing-overlap test seeds additional 32 MiB and 128 MiB buffers. Medians of nine interleaved runs, with process page faults measured during capture.
SnapshotBytesand the per-shadow diagnostics now count newly captured bytes. Before, they counted the whole rebuilt span, including earlier bytes copied again. These counters exclude pool transfers and should be compared only within the same implementation.Validation
cargo test --locked -p victory-gpu --lib --features diagnostics -- --test-threads=1under WARP: 247 passed, 1 ignored.cargo xtask policypassed.Coordination claim. No game files used. AI-assisted (Codex).
Imported from Marvisak/VIctory. Originally posted by Dukestar69 on 2026-11-26T13:07:21Z.
Review: APPROVED
Verified on RTX 5090 & WARP against
origin/main(63bcca4):cargo test -p victory-gpu --lib shadow:: -- --test-threads=1: 11/11 passed on RTX 5090 and WARP (includingoverlapping_snapshots_keep_old_bytes_until_a_refresh).cargo clippy -p victory-gpu --all-targets -- -D warnings: clean.cargo xtask policy: 191 files ok.Cleanly appends only newly covered byte ranges to write snapshots while keeping prior bytes intact until refresh.
Synthetic tests only (no game files used). AI-assisted (Gemini 4 Neon).
Imported from Marvisak/VIctory. Originally posted by AurumCarlos on 2026-11-26T13:08:10Z.
Review: COMMENTED
Nice change. Capturing each byte once, with spans pointing into one buffer, is a much better shape than rebuilding the union on every extension, and the 62 → 6.6 ms on the growing-overlap case is the pattern that matters. I went through
capture,covers,changed_pieceandrefresh:capturewalks the gaps correctly: it skips covered spans, inserts or extends ati, and only merges into the previous span when it is adjacent both in address and in the buffer, so a span's bytes always stay contiguous. The total stored is exactly the union of the declared ranges.changed_piecerelies oncovershaving been checked (it indexesself.spans[first]directly).copy_back_or_notdoes check it first, so that holds. Adebug_assert!(self.covers(start, end))at the top would document it.refreshis equivalent to the oldsync_beforeloop.One concern, about the pool:
Growing the capture buffer bypasses the pool.
capturetakes a pooled buffer only whencapacity() == 0, sized for the first declaration (pooled(host.len())).pooledreturns the smallest pooled buffer that fits. So when a small range is declared first and a large one later (32-byte ranges then their 64 MiB span, in your fragmented case), the laterextend_from_slicemakes theVecreallocate through the global allocator. That copies everything captured so far into fresh pages, and frees the old buffer instead of returning it to the pool. Fresh pages are exactly whatPOOLexists to avoid ("a page fault per 4 KiB touched", pergive_back's comment). The old code asked the pool for the full union size on every growth.The benchmark probably doesn't show this because the pools were warmed with large buffers, so even a 32-byte request got a big one. In a real frame, the pool also holds the smaller snapshots that earlier shadows gave back. Something like this would keep growth on pooled memory:
This would replace the
capacity() == 0check, and it costs one copy per growth, as before. A benchmark row with a cold-ish pool of mixed sizes (or a first declaration much smaller than the later ones) would show whether it matters.A minor note:
SnapshotBytes/copystat::snapshotnow count only newly captured bytes, not the bytes copied to rebuild a segment. That's the more useful number, but diagnostics totals before and after this change aren't comparable, which might be worth a line in the description.Imported from Marvisak/VIctory. Originally posted by yumaste on 2026-11-26T13:12:56Z.
The mixed-pool case reproduced the regression. With a touched 64 MiB buffer available, the first version still incurred 16,417 process page faults when a 32-byte declaration was followed by a 64 MiB one.
ebd01cdgrows from the pool when possible and brings that back to zero on the same input (16.64 -> 3.37 ms). It uses the last captured size as the next submission's initial checkout preference. I added the debug assertion and updated the PR with warmed/mixed/empty-pool results and the change in diagnostic counters. All 247 GPU tests pass under WARP.Imported from Marvisak/VIctory. Originally posted by rezioliver-collab on 2026-11-27T02:59:14Z.
I extended the fragmented 64 MiB case on an Apple M1. I captured 1,024 out-of-order 32-byte ranges followed by the enclosing range, then flipped one byte on each selected 64 KiB page. Both revisions found exactly the selected pages and recovered every original byte on those pages.
These are medians for capture plus comparison of the enclosing range, with independent warmed pools and 12 alternating pairs (base
63bcca43, PRebd01cdd). At 50% the PR won 6/12 pairs at either offset. At 100% with the early byte it won 0/12, then 2/12 in a separate repeat. Gathering each changed page can outweigh the capture savings in that layout. The timing excludes guest writes and separate copy-backs of the small declarations.AI-assisted with Codex.
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.Warning: The "Autodetect manual merge" setting is not enabled for this repository, you will have to mark this pull request as manually merged afterwards.