GPU: append only newly covered bytes to write snapshots #3211

Open
github-bot wants to merge 2 commits from github-bot/gh-3211 into main AGit
Owner

@⁠yumaste on GitHub · 2026-11-26 13:01 UTC · original
yumaste:perf/shadow-snapshot-new-bytes → main · +235 −69 · 1 files · 2 commits · open

Problem

A submission can declare overlapping writes to the same large GPU shadow. Whenever the covered range grows, Shadow::writing allocates 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 from main and 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.

Ranges Capture before -> after Comparison before -> after
17 overlapping ranges growing from 32 to 96 MiB 56.93 -> 5.91 ms 11.40 -> 10.80 ms
8 disjoint 8 MiB ranges 3.67 -> 3.71 ms 7.67 -> 7.54 ms
One 112 MiB range 8.11 -> 7.79 ms 13.97 -> 13.70 ms
1,024 out-of-order 32-byte ranges followed by their full 64 MiB span 10.33 -> 8.57 ms 7.61 -> 7.03 ms
Same fragmented case, with changes in 25% of pages 8.24 -> 7.53 ms 4.78 -> 6.21 ms

For the first case, the declarations are [(32-i) MiB, (64+3*i) MiB) for i=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.

Initial pool and input Main First PR commit With pooled growth
Mixed, 32 B followed by an enclosing 64 MiB range 3.47 ms / 0 faults 16.64 ms / 16,417 faults 3.37 ms / 0 faults
Empty, same input 15.46 ms / 16,417 faults 16.67 ms / 16,417 faults 17.53 ms / 16,417 faults
Mixed, growing overlap 118.19 ms / 82,084 faults 35.10 ms / 41,045 faults 12.07 ms / 0 faults
Mixed, one 64 MiB range 3.82 ms / 0 faults 3.85 ms / 0 faults 3.81 ms / 0 faults

SnapshotBytes and 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

  • New test covers an overlap that fills a hole, preserves previously captured bytes, handles fragmented comparison, and updates the baseline on refresh.
  • WARP copy-back test covers a changed page split across captures and checks that a full page goes back once.
  • cargo test --locked -p victory-gpu --lib --features diagnostics -- --test-threads=1 under WARP: 247 passed, 1 ignored.
  • Clippy with all targets and features and warnings denied passed. cargo xtask policy passed.

Coordination claim. No game files used. AI-assisted (Codex).

**@⁠yumaste** on GitHub · 2026-11-26 13:01 UTC · [original](https://github.com/Marvisak/VIctory/pull/3211) `yumaste:perf/shadow-snapshot-new-bytes` → `main` · +235 −69 · 1 files · 2 commits · open ## Problem A submission can declare overlapping writes to the same large GPU shadow. Whenever the covered range grows, `Shadow::writing` allocates 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 from `main` and 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. | Ranges | Capture before -> after | Comparison before -> after | | --- | ---: | ---: | | 17 overlapping ranges growing from 32 to 96 MiB | 56.93 -> 5.91 ms | 11.40 -> 10.80 ms | | 8 disjoint 8 MiB ranges | 3.67 -> 3.71 ms | 7.67 -> 7.54 ms | | One 112 MiB range | 8.11 -> 7.79 ms | 13.97 -> 13.70 ms | | 1,024 out-of-order 32-byte ranges followed by their full 64 MiB span | 10.33 -> 8.57 ms | 7.61 -> 7.03 ms | | Same fragmented case, with changes in 25% of pages | 8.24 -> 7.53 ms | 4.78 -> 6.21 ms | For the first case, the declarations are `[(32-i) MiB, (64+3*i) MiB)` for `i=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. | Initial pool and input | Main | First PR commit | With pooled growth | | --- | ---: | ---: | ---: | | Mixed, 32 B followed by an enclosing 64 MiB range | 3.47 ms / 0 faults | 16.64 ms / 16,417 faults | 3.37 ms / 0 faults | | Empty, same input | 15.46 ms / 16,417 faults | 16.67 ms / 16,417 faults | 17.53 ms / 16,417 faults | | Mixed, growing overlap | 118.19 ms / 82,084 faults | 35.10 ms / 41,045 faults | 12.07 ms / 0 faults | | Mixed, one 64 MiB range | 3.82 ms / 0 faults | 3.85 ms / 0 faults | 3.81 ms / 0 faults | `SnapshotBytes` and 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 - New test covers an overlap that fills a hole, preserves previously captured bytes, handles fragmented comparison, and updates the baseline on refresh. - WARP copy-back test covers a changed page split across captures and checks that a full page goes back once. - `cargo test --locked -p victory-gpu --lib --features diagnostics -- --test-threads=1` under WARP: 247 passed, 1 ignored. - Clippy with all targets and features and warnings denied passed. `cargo xtask policy` passed. [Coordination claim](https://git.gtahub.st/github/Marvisak--VIctory/issues/1633). No game files used. AI-assisted (Codex).
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 13:07:37 +00:00
Author
Owner

@⁠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 (including overlapping_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).

**@⁠Dukestar69** on GitHub · 2026-11-26 13:07 UTC · [original](https://github.com/Marvisak/VIctory/pull/3211#pullrequestreview-6074650361) · **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 (including `overlapping_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).
Author
Owner

@⁠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_piece and refresh:

  • capture walks the gaps correctly: it skips covered spans, inserts or extends at i, 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_piece relies on covers having been checked (it indexes self.spans[first] directly). copy_back_or_not does check it first, so that holds. A debug_assert!(self.covers(start, end)) at the top would document it.
  • refresh is equivalent to the old sync_before loop.

One concern, about the pool:

Growing the capture buffer bypasses the pool. capture takes a pooled buffer only when capacity() == 0, sized for the first declaration (pooled(host.len())). pooled returns 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 later extend_from_slice makes the Vec reallocate 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 what POOL exists to avoid ("a page fault per 4 KiB touched", per give_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:

let need = host.len(); // an upper bound on what this call appends
if self.bytes.capacity() - self.bytes.len() < need {
    let mut grown = pooled(self.bytes.len() + need);
    grown.extend_from_slice(&self.bytes);
    let old = std::mem::replace(&mut self.bytes, grown);
    if old.capacity() != 0 {
        give_back(old);
    }
}

This would replace the capacity() == 0 check, 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::snapshot now 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.

**@⁠AurumCarlos** on GitHub · 2026-11-26 13:08 UTC · [original](https://github.com/Marvisak/VIctory/pull/3211#pullrequestreview-6074661193) · **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_piece` and `refresh`: - `capture` walks the gaps correctly: it skips covered spans, inserts or extends at `i`, 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_piece` relies on `covers` having been checked (it indexes `self.spans[first]` directly). `copy_back_or_not` does check it first, so that holds. A `debug_assert!(self.covers(start, end))` at the top would document it. - `refresh` is equivalent to the old `sync_before` loop. One concern, about the pool: **Growing the capture buffer bypasses the pool.** `capture` takes a pooled buffer only when `capacity() == 0`, sized for the *first* declaration (`pooled(host.len())`). `pooled` returns 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 later `extend_from_slice` makes the `Vec` reallocate 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 what `POOL` exists to avoid ("a page fault per 4 KiB touched", per `give_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: ```rust let need = host.len(); // an upper bound on what this call appends if self.bytes.capacity() - self.bytes.len() < need { let mut grown = pooled(self.bytes.len() + need); grown.extend_from_slice(&self.bytes); let old = std::mem::replace(&mut self.bytes, grown); if old.capacity() != 0 { give_back(old); } } ``` This would replace the `capacity() == 0` check, 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::snapshot` now 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.
Author
Owner

@⁠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. ebd01cd grows 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.

**@⁠yumaste** on GitHub · 2026-11-26 13:12 UTC · [original](https://github.com/Marvisak/VIctory/pull/3211#issuecomment-7193031448) · 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. `ebd01cd` grows 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.
This pull request can be merged automatically.
You are not authorized to merge this pull request.
View command line instructions

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u origin '+refs/pull/3211/head:github-bot/gh-3211'
git switch 'github-bot/gh-3211'

Merge

Merge the changes and update on Forgejo.
git switch 'main'
git merge --no-ff 'github-bot/gh-3211'
git switch 'github-bot/gh-3211'
git rebase 'main'
git switch 'main'
git merge --ff-only 'github-bot/gh-3211'
git switch 'github-bot/gh-3211'
git rebase 'main'
git switch 'main'
git merge --no-ff 'github-bot/gh-3211'
git switch 'main'
git merge --squash 'github-bot/gh-3211'
git switch 'main'
git merge --ff-only 'github-bot/gh-3211'
git switch 'main'
git merge 'github-bot/gh-3211'
git push origin 'main'
This discussion has been locked. Commenting is limited to contributors.
No reviewers
No milestone
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set

Reference
github/Marvisak--VIctory!3211
No description provided.