GPU: append only newly covered bytes to write snapshots #3211
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!3211
Loading…
Reference in a new issue
No description provided.
Delete branch "github-bot/gh-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?
@yumaste on GitHub · 2026-11-26 13:01 UTC · original
yumaste:perf/shadow-snapshot-new-bytes→main· +235 −69 · 1 files · 2 commits · openProblem
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).
@Dukestar69 on GitHub · 2026-11-26 13:07 UTC · original · approved these changes
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).
@AurumCarlos on GitHub · 2026-11-26 13:08 UTC · original · reviewed
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.@yumaste on GitHub · 2026-11-26 13:12 UTC · original · edited 2026-11-26 13:15 UTC
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.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.