GPU: buffer-copy refreshes choose how to copy their changed runs from measured costs (adaptive by default) #3916

Open
Jineggegg wants to merge 1 commit from upstream-pr/3916 into main
Jineggegg commented 2026-11-30 22:55:38 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by Jineggegg on 2026-11-30T22:55:38Z.


What's wrong

Shadow::update_tracked brings a host copy up to date by copying each run of changed 64 KiB pages that Tracked::take_split returns with one kernel copy (copy_mapped, i.e. ReadProcessMemory) on the submission thread. Scattered changed pages — the usual steady-state shape of a large title buffer — are therefore copied one page at a time on one thread. On an RTX 5090 desktop (24 cores), copying 10% of a 1 GiB buffer's pages scattered took ~18 ms per refresh, 50% ~83 ms.

What this does

VICTORY_COPY_POLICY chooses how a refresh of several runs organises its copies. It is adaptive by default; VICTORY_COPY_POLICY=fixed keeps today's copies, by the same code; an unknown value warns and copies as fixed.

Organisations: Fixed (today's: one kernel copy per run), Parallel (the runs cut at 2 MiB and copied on the Windows process pool, the caller working too), Merged (runs bridged across cheap gaps, read through a scratch buffer), Whole (2 MiB pieces read on the pool through scratch). Only the runs' bytes ever reach the copy — gaps are read into scratch and never written, because a copy can hold shader results the frame withheld from title memory.

The policy leaves Fixed only for a measured gain:

  • per size class (log2 of the bytes × log2 of the runs) it keeps what each organisation took per page; Fixed is measured first (twice) and again every 32 refreshes of the class;
  • another organisation is chosen only if it measured ≥ 15% less than Fixed there, or, not measured there yet, if a small fitted cost model (kernel call + per-page read; pool join + speedup) predicts ≥ 30% less than Fixed and anything measured;
  • one that measures not clearly cheaper, or slower than another, is not tried again for 64 refreshes of the class, doubled each time in a row (up to 4096);
  • refreshes of one run or fewer than 8 pages never reach the policy and are copied exactly as today.

A wrong model therefore costs one slower refresh per organisation and class, then ever rarer ones. Semantics are unchanged: take_split still guards before any byte is read; saved and snapshot bytes are brought up to date per run after all of a refresh's copies; copy-back paths are not routed through the policy; the model's mutex is never held across a copy.

Files: new crates/victory-gpu/src/shadow/policy.rs (policy, model, copies, tests); shadow.rs (the hook in update_tracked, Shadow::copied factored out of Shadow::copy, and parallel_items, a bounded fan-out on the process pool that joins every callback and re-raises a callback's panic); one diagnostics line in copystat.rs; one README row.

Measurements

Measured on a fork of this repository (Jineggegg/VIctory, merged there as #7), with a refresh benchmark (pipeline::hot::mem::matrix, not part of this PR) on an RTX 5090: same binary in both arms, only the switch changed, 30 alternating ABBA pairs (4 GiB: 10), pairs overlapping other GPU/CPU use dropped. Median refresh, fixed → adaptive, for scattered / page-crossing / clumped writes at 10% and 50% density: −44% to −56% at 256 MiB, 1 GiB and 4 GiB (e.g. 1 GiB 10% scattered 18.1 → 9.5 ms, 1 GiB 50% page-crossing 72.7 → 34.4 ms); 1% scattered at 1 GiB −40% to −49%; 16 MiB 50% scattered −47% to −58%. No cell of 64 (16 MiB–4 GiB × 4 densities × 5 distributions) was slower; the single-run cells and the fork's 19-item hot_paths regression showed no difference, and 30-minute soaks (fixed, adaptive, adaptive, fixed) had no verification failures, device removals or drift.

Caveat: the fork's baseline already spread a single run of ≥ 8 MiB over the pool, this repository's does not. That changes nothing here — single runs are copied by the baseline code in both — and the gains above come from refreshes of many runs, which both baselines copied one run at a time on one thread. They are synthetic benchmarks of the refresh path, not game frame rates; one machine only (laptops and Wine/Proton, where a kernel call costs more and Merged/Whole would matter, are not measured).

Tests

New, no GPU needed (shadow::policy::tests): every organisation writes the same bytes as the fixed copy over densities, distributions, sub-page edges and unmapped pages, and leaves bytes between runs untouched; four threads writing title memory during 400 take+copy rounds lose nothing; calibration stays finite and bounded under 200,000 garbage samples and converges on a known machine; a model kept wrong costs at most a few slower refreshes in 640; measured gains are kept; a measured kind gives way to a faster one; small gains keep the fixed copy; a pool fit from one size stays sane; small refreshes skip the model.

On this branch (Windows, RTX 5090): both Clippy configurations of CI (--workspace --all-targets, with and without --all-features, -D warnings) are clean; cargo test --workspace --locked passes except descriptor::host::tests::dynamic_slots_are_held_until_released and memory::tests::lists_share_a_table_and_copies_keep_their_views, which fail the same way on main (63bcca4) on this hardware adapter.

Notes

Merges here have been quiet for a few days; that does not change anything about this PR — it applies to main as it is and needs nothing else from the fork.

AI-assisted (Claude Code): the policy, its tests, the benchmark runs and this write-up. The numbers come from running both arms on real hardware.

🤖 Generated with Claude Code

<!-- greyfinch-sync:Marvisak/VIctory:issue:5575276352 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/3916). Originally posted by [Jineggegg](https://github.com/Jineggegg) on 2026-11-30T22:55:38Z. --- ## What's wrong `Shadow::update_tracked` brings a host copy up to date by copying each run of changed 64 KiB pages that `Tracked::take_split` returns with one kernel copy (`copy_mapped`, i.e. `ReadProcessMemory`) on the submission thread. Scattered changed pages — the usual steady-state shape of a large title buffer — are therefore copied one page at a time on one thread. On an RTX 5090 desktop (24 cores), copying 10% of a 1 GiB buffer's pages scattered took ~18 ms per refresh, 50% ~83 ms. ## What this does `VICTORY_COPY_POLICY` chooses how a refresh of **several** runs organises its copies. It is **`adaptive` by default**; `VICTORY_COPY_POLICY=fixed` keeps today's copies, by the same code; an unknown value warns and copies as `fixed`. Organisations: `Fixed` (today's: one kernel copy per run), `Parallel` (the runs cut at 2 MiB and copied on the Windows process pool, the caller working too), `Merged` (runs bridged across cheap gaps, read through a scratch buffer), `Whole` (2 MiB pieces read on the pool through scratch). Only the runs' bytes ever reach the copy — gaps are read into scratch and never written, because a copy can hold shader results the frame withheld from title memory. The policy leaves `Fixed` only for a **measured** gain: - per size class (log2 of the bytes × log2 of the runs) it keeps what each organisation took per page; `Fixed` is measured first (twice) and again every 32 refreshes of the class; - another organisation is chosen only if it measured **≥ 15% less** than `Fixed` there, or, not measured there yet, if a small fitted cost model (kernel call + per-page read; pool join + speedup) predicts **≥ 30% less** than `Fixed` and anything measured; - one that measures not clearly cheaper, or slower than another, is not tried again for 64 refreshes of the class, doubled each time in a row (up to 4096); - refreshes of **one run** or **fewer than 8 pages** never reach the policy and are copied exactly as today. A wrong model therefore costs one slower refresh per organisation and class, then ever rarer ones. Semantics are unchanged: `take_split` still guards before any byte is read; saved and snapshot bytes are brought up to date per run after all of a refresh's copies; copy-back paths are not routed through the policy; the model's mutex is never held across a copy. Files: new `crates/victory-gpu/src/shadow/policy.rs` (policy, model, copies, tests); `shadow.rs` (the hook in `update_tracked`, `Shadow::copied` factored out of `Shadow::copy`, and `parallel_items`, a bounded fan-out on the process pool that joins every callback and re-raises a callback's panic); one diagnostics line in `copystat.rs`; one README row. ## Measurements Measured on a fork of this repository (Jineggegg/VIctory, merged there as #7), with a refresh benchmark (`pipeline::hot::mem::matrix`, not part of this PR) on an RTX 5090: same binary in both arms, only the switch changed, 30 alternating ABBA pairs (4 GiB: 10), pairs overlapping other GPU/CPU use dropped. Median refresh, fixed → adaptive, for scattered / page-crossing / clumped writes at 10% and 50% density: **−44% to −56%** at 256 MiB, 1 GiB and 4 GiB (e.g. 1 GiB 10% scattered 18.1 → 9.5 ms, 1 GiB 50% page-crossing 72.7 → 34.4 ms); 1% scattered at 1 GiB −40% to −49%; 16 MiB 50% scattered −47% to −58%. No cell of 64 (16 MiB–4 GiB × 4 densities × 5 distributions) was slower; the single-run cells and the fork's 19-item `hot_paths` regression showed no difference, and 30-minute soaks (fixed, adaptive, adaptive, fixed) had no verification failures, device removals or drift. Caveat: the fork's baseline already spread a single run of ≥ 8 MiB over the pool, this repository's does not. That changes nothing here — single runs are copied by the baseline code in both — and the gains above come from refreshes of many runs, which both baselines copied one run at a time on one thread. They are synthetic benchmarks of the refresh path, not game frame rates; one machine only (laptops and Wine/Proton, where a kernel call costs more and `Merged`/`Whole` would matter, are not measured). ## Tests New, no GPU needed (`shadow::policy::tests`): every organisation writes the same bytes as the fixed copy over densities, distributions, sub-page edges and unmapped pages, and leaves bytes between runs untouched; four threads writing title memory during 400 take+copy rounds lose nothing; calibration stays finite and bounded under 200,000 garbage samples and converges on a known machine; a model kept wrong costs at most a few slower refreshes in 640; measured gains are kept; a measured kind gives way to a faster one; small gains keep the fixed copy; a pool fit from one size stays sane; small refreshes skip the model. On this branch (Windows, RTX 5090): both Clippy configurations of CI (`--workspace --all-targets`, with and without `--all-features`, `-D warnings`) are clean; `cargo test --workspace --locked` passes except `descriptor::host::tests::dynamic_slots_are_held_until_released` and `memory::tests::lists_share_a_table_and_copies_keep_their_views`, which fail the same way on `main` (63bcca4) on this hardware adapter. ## Notes Merges here have been quiet for a few days; that does not change anything about this PR — it applies to `main` as it is and needs nothing else from the fork. AI-assisted (Claude Code): the policy, its tests, the benchmark runs and this write-up. The numbers come from running both arms on real hardware. 🤖 Generated with [Claude Code](https://claude.com/claude-code)
passerbyyhx commented 2026-12-01 12:27:52 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by passerbyyhx on 2026-12-01T12:27:52Z.


On 6efc5b4d, I timed the production Shadow::update path on a Windows laptop with the optimized diagnostics build. Each refresh copied 32 of 128 64-KiB pages, either as one run or as 32 isolated pages. Six fixed/adaptive process pairs alternated order. Each process took 35 timings per layout after warm-up, for 840 samples in total.

For isolated pages, the median paired difference (adaptive minus fixed) was −4.7 µs, ranging from −117.0 to +220.3 µs. Three pairs were faster and three slower. Adaptive chose Fixed 184 times and Parallel 26 times. The single-run control also had three faster and three slower pairs, with a median difference of −12.5 µs. All 12 processes copied the changed bytes correctly and preserved all 6 MiB of distinct gap sentinels. This 8 MiB workload showed no consistent laptop speedup.

<!-- greyfinch-sync:Marvisak/VIctory:comment:7311168820 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/3916#issuecomment-7311168820). Originally posted by [passerbyyhx](https://github.com/passerbyyhx) on 2026-12-01T12:27:52Z. --- On `6efc5b4d`, I timed the production `Shadow::update` path on a Windows laptop with the optimized diagnostics build. Each refresh copied 32 of 128 64-KiB pages, either as one run or as 32 isolated pages. Six fixed/adaptive process pairs alternated order. Each process took 35 timings per layout after warm-up, for 840 samples in total. For isolated pages, the median paired difference (adaptive minus fixed) was −4.7 µs, ranging from −117.0 to +220.3 µs. Three pairs were faster and three slower. Adaptive chose `Fixed` 184 times and `Parallel` 26 times. The single-run control also had three faster and three slower pairs, with a median difference of −12.5 µs. All 12 processes copied the changed bytes correctly and preserved all 6 MiB of distinct gap sentinels. This 8 MiB workload showed no consistent laptop speedup.
This pull request cannot be merged automatically due to conflicts.
Merge manually to resolve the conflicts.
View command line instructions

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u origin 'upstream-pr/3916:upstream-pr/3916'
git switch 'upstream-pr/3916'

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.

git switch 'main'
git merge --no-ff 'upstream-pr/3916'
git switch 'upstream-pr/3916'
git rebase 'main'
git switch 'main'
git merge --ff-only 'upstream-pr/3916'
git switch 'upstream-pr/3916'
git rebase 'main'
git switch 'main'
git merge --no-ff 'upstream-pr/3916'
git switch 'main'
git merge --squash 'upstream-pr/3916'
git switch 'main'
git merge --ff-only 'upstream-pr/3916'
git switch 'main'
git merge 'upstream-pr/3916'
git push origin 'main'
Sign in to join this conversation.
No reviewers
No labels
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.

Reference
Marvisak/VIctory!3916
No description provided.