GPU: buffer-copy refreshes choose how to copy their changed runs from measured costs (adaptive by default) #3916
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/3916"
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 Jineggegg on 2026-11-30T22:55:38Z.
What's wrong
Shadow::update_trackedbrings a host copy up to date by copying each run of changed 64 KiB pages thatTracked::take_splitreturns 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_POLICYchooses how a refresh of several runs organises its copies. It isadaptiveby default;VICTORY_COPY_POLICY=fixedkeeps today's copies, by the same code; an unknown value warns and copies asfixed.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
Fixedonly for a measured gain:Fixedis measured first (twice) and again every 32 refreshes of the class;Fixedthere, or, not measured there yet, if a small fitted cost model (kernel call + per-page read; pool join + speedup) predicts ≥ 30% less thanFixedand anything measured;A wrong model therefore costs one slower refresh per organisation and class, then ever rarer ones. Semantics are unchanged:
take_splitstill 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 inupdate_tracked,Shadow::copiedfactored out ofShadow::copy, andparallel_items, a bounded fan-out on the process pool that joins every callback and re-raises a callback's panic); one diagnostics line incopystat.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-itemhot_pathsregression 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/Wholewould 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 --lockedpasses exceptdescriptor::host::tests::dynamic_slots_are_held_until_releasedandmemory::tests::lists_share_a_table_and_copies_keep_their_views, which fail the same way onmain(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
mainas 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
Imported from Marvisak/VIctory. Originally posted by passerbyyhx on 2026-12-01T12:27:52Z.
On
6efc5b4d, I timed the productionShadow::updatepath 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
Fixed184 times andParallel26 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.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.