victory-gpu: dedupe copy-back ranges in a set, and take the 64 KiB piece buffer from the pool #1655
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/1656"
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 pes7 on 2026-11-24T20:33:15Z.
Two pieces of per-range work in the copy-back path scale with the number of things a submission declares: the ranges are deduplicated by scanning the list already collected, and each
copy_backallocates a fresh 64 KiB buffer for bytes it overwrites anyway. Both are in the term #400 measures at 54-110 ms a frame ("copying back"), and both are the same shape as the fix in #1556. ## 1. Deduplication by scanhost.rs, submission: everyStep::GpuWriteof every list is checked against what is already collected withrust !back.iter().any(|(s, a, l)| Arc::ptr_eq(s, shadow) && (*a, *l) == (*address, *len))The world's passes declare the same big ranges dozens of times, sobackgrows to hundreds of entries while the steps number in the thousands: a linear scan per step. AHashSet<(ptr, address, len)>makes it constant and keeps the order ofbackthe same. ## 2. A 64 KiB buffer per callshadow.rs::copy_backdoeslet mut title = vec![0u8; PIECE]withPIECE = 64 KiB, for every range it puts back. The bytes are then overwritten byread_intobefore they are read, so the zeroing is pure cost - and the file already has the pool it should come from (pooled/give_back, used bywritingfor the same reason: "fresh snapshots cost ~2.3 million page faults per world frame"). It now takes the piece from that pool and gives it back. ## Measured Without the title I can measure the shapes, not the frame: - The scan: 33 000 steps over 250 collected ranges costs 0.91 ms against 0.21 ms with a set (best of 7,refresh_merge_scan_vs_indexin #1556 measures the same comparison on the same machine); at 1 000 ranges, 3.18 ms against 0.24 ms. - The buffer:vec![0u8; 65536]is an allocation and a memset of 64 KiB per call;pooledis a mutex and aVecswap. The frame line'scopying backterm is 54-110 ms in #400, and it runs this once per range. Both are bounded by how many ranges a submission declares, which the diagnostics already print (N title copies,N refreshesper frame). ## Correctness - The set is keyed by exactly what the scan compared - theArcpointer, the address and the length - andbackkeeps its order. - The pooled buffer is only read afterread_intohas written it, and is returned to the pool after the loop;read_intofailing skips the piece as before.cargo test --workspace --locked, both clippy configurations with-D warnings,cargo xtask policy. No game data used. ## 3. The whole 64 KiB piece goes back for one changed bytecopy_backmerges a 64 KiB piece of title memory with the copy, and wrote the whole piece back whenever any byte of it had been taken from the GPU. A shader writing one dword made the layer write 64 KiB into title memory - and touch 64 KiB of its pages. It now tracks the first and last byte the title actually takes and writes that span (watch::writingmarks the same span, so our own write is not taken for the title's). ### Measured on a new synthetic harnessshadow::cost::shadow_cost(this PR) builds a realShadowover a real host copy and a synthetic title range, takes the snapshot, writes 640 bytes into every 64 KiB piece as a shader would, and times the copy-back: | range | written back before | after | copy-back before | after | |---:|---:|---:|---:|---:| | 4 MiB | 4.19 MB | 41 KB | 2.07 ms | 1.48 ms (0.71x) | | 16 MiB | 16.8 MB | 164 KB | 10.14 ms | 8.74 ms (0.86x) | | 64 MiB | 67.1 MB | 655 KB | 75.68 ms | 69.23 ms (0.92x) | Three orders of magnitude less written; 8-29% less time. The time does not fall further because the rest of the piece is the kernel read of title memory and the merge over it, not the write - which is what tells the next person thatGPU-SNAPSHOT-COSTis about the reads, not the writes. The harness needs no title:cargo test --release -p victory-gpu -- --ignored --nocapture shadow_cost. In-title numbers are not mine to give: I have no runnable copy yet, so whoever has one should run it there. What the harness does show is the per-byte cost of each part, which is what a frame has to be divided by. ## 4. The pool returned an empty buffer - caught by the harness The first commit took the 64 KiB piece frompooled(). That function returns the vector cleared (len == 0), andcopy_backslicespiece[..n]out of it, so the first copy-back of any changed piece would have panicked:range end index 24512 out of range for slice of length 0The workspace tests do not covercopy_back, so nothing caught it untilshadow_costdid - which is the argument for the harness as much as for the fix. It ispiece.resize(PIECE, 0)now: the pool saves the allocation, not the zeroing, and the section above says so. ## 5. Copy-back on several threads The ranges a submission wrote are independent - different buffers, or disjoint ranges of one - so they are copied back in parallel:std::thread::scopeover chunks of the collectedbacklist, the machine's cores up to eight.VICTORY_COPY_THREADS=1keeps it on one thread, so a real run can A/B the frame line.shadow_parallel, 16 ranges ╨УтАФ 4 MiB with 640 changed bytes per 64 KiB piece: | | time | | |---|---:|---| | one thread | 41.4 ms | | | eight threads | 12.4 ms | 3.35x | The bytes written back are identical (655 360either way). This is the term #400 measures at 54-110 ms a frame, so a 3.35x on it is 35-70 ms of a frame that is still dominated by the copies themselves. Run it withcargo test --release -p victory-gpu -- --ignored --nocapture shadow_parallel.6. The pre-submission copies on several threads
Batch::flushbrings every range a list reads up to date before the lists run, one buffer after another. The batch holds one entry per buffer, so the ranges are independent, and the copy is a kernel read of title memory.shadow_flush, 16 ranges Г— 4 MiB of synthetic memory with no write-watch, so every range is copied whole - the worst case:Less than the copy-back's 3.35x because this path is already at memcpy speed, so it runs into memory bandwidth instead of idling.
The caveat decides whether this shows in a frame: in the world most of
flushis the write-watch askings, andwatch::writesholds one global mutex acrossGetWriteWatch, so those stay serial however many threads there are.#776is the change that cuts the askings; this one spreads the copies behind them.VICTORY_COPY_THREADS=1turns both off, which is the A/B someone with a copy can run against the frame line.cargo test --release -p victory-gpu -- --ignored --nocapture shadow_flushImported from Marvisak/VIctory. Originally posted by HenriqueSBueno on 2026-11-24T21:04:10Z.
A data point for the size of this term on NVIDIA: on an RTX 3080 in the Series S profile (1280x720 internal), "copying back" was ~330 ms per world frame for ~12 MB actually written back (main + #189 #89 #211 #282 #493 #530 #587). So most of the cost is not the bytes returned. It is the work around them: one
[min, max]union range per shadow inBatch::add, a 64 KiB piece scan with byte compares, andReadProcessMemory/WriteProcessMemoryon our own process for each piece. This PR's dedup set and pooled piece buffer attack the per-range overhead. The larger wins we'd expect next are disjoint ranges instead of the union, and copying back only buffers the CPU actually reads (#1317, H-2.1, H-2.3, H-7.7; see also #1681 for a GPU-side comparison).AI-assisted (Claude Code)
Imported from Marvisak/VIctory. Originally posted by pes7 on 2026-11-24T21:58:06Z.
Closing this rather than rebasing it:
Shadowwas redesigned onmaintoday (c75fdbaone change journal for title memory,86a5ed6,6d021feworld frame cost,bacb06a), and the code this was written against no longer exists. ThePIECEloop,pending, the set of compared pieces and the 64 KiB pool are all gone;copy_back_pagenow works a page at a time againsttracked/before, andBatchalready holds the dedupe this PR added (current) plus the by-pointer index (#1556, also closed for the same reason).What was unique here is preserved in the measurements, and is worth a decision from you before anyone writes code again:
flush), one thread vs eightNeither win is obviously available on the new design, and I do not want to claim it without measuring:
copy_back_pagetakescrate::watch::writing(from, n)aroundwrite_mapped, and that is one global mutex, so the writes would serialise however many threads the reads are spread over - the same reason the parallelflushneeded #776 to pay off;// SAFETY: inside the mapping; only the submitting thread writes it.says the mapping was assumed single-writer, which is a decision for you, not something to change in a drive-by PR.If you want any of those on the new layout, say which and I will write it against
copy_back_page/copy_backand bring numbers from the same harness rather than from the old code. The harnesses (shadow_cost,shadow_parallel,flush_cost, all#[ignore]d and synthetic) are in the branch if they are useful as a starting point; no game data was used.Imported from Marvisak/VIctory. Originally posted by catminou4 on 2026-11-24T22:19:51Z.
Review: COMMENTED
Static review only: victory-gpu is Windows-only (D3D12/windows_sys) — nothing compiles or runs on macOS; source review of the diff.
Copy-back ranges deduped in a set, copy spread over up to 8 threads (VICTORY_COPY_THREADS): copy_threads() caps by machine, and the shadow side records what one submission pays for shader-written ranges. 3 tests.
Imported from Marvisak/VIctory. Originally posted by pes7 on 2026-11-24T22:03:53Z.
@HenriqueSBueno - 330 ms per world frame for 12 MB actually written back is the most useful number in this thread: it is ~27 ns per byte returned, which no amount of copying explains. It has to be the per-range work, exactly as you describe - the
[min, max]union inBatch::add, a piece scan, and aReadProcessMemory/WriteProcessMemorypair per piece.Two notes, since the PR is closed now (the path was redesigned on
maintoday, see above):main:Batch::addis keyed byArc::as_ptrand keeps one[from, to)per copy, so a copy's overlapping declarations are merged rather than unioned across copies. Part of the overhead you measured has moved since;ReadProcessMemoryis still there, and it is the one I would look at first.host::read_intoisReadProcessMemory(GetCurrentProcess(), ..), a kernel transition for memory in our own process, andcopy_back_pagenow makes that call once per 4 KiB page instead of once per piece - so the syscall count went up while the byte count stayed. Inside a registered shadow range the pages are mapped, so the compare could read them directly and keep the syscall only for the uncertain ranges.shadow_costprints per-byte time for snapshot and copy-back on synthetic memory (no title,#[ignore]d), so both of those are cheap experiments for whoever picks this up - much cheaper than the disjoint-range work in #1317, H-2.1/H-2.3/H-7.7, which is the bigger change and the one I would do next.Pull request closed