Skip to content

victory-gpu: dedupe copy-back ranges in a set, and take the 64 KiB piece buffer from the pool - #1656

Closed
pes7 wants to merge 5 commits into
Marvisak:mainfrom
pes7:perf/copyback-dedupe
Closed

pes7 wants to merge 5 commits into
Marvisak:mainfrom
pes7:perf/copyback-dedupe

Conversation

@pes7

@pes7 pes7 commented Nov 24, 2026 •

Copy link
Copy Markdown
Contributor

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_back allocates 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 scan host.rs, submission: every Step::GpuWrite of every list is checked against what is already collected with rust !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, so back grows to hundreds of entries while the steps number in the thousands: a linear scan per step. A HashSet<(ptr, address, len)> makes it constant and keeps the order of back the same. ## 2. A 64 KiB buffer per call shadow.rs::copy_back does let mut title = vec![0u8; PIECE] with PIECE = 64 KiB, for every range it puts back. The bytes are then overwritten by read_into before they are read, so the zeroing is pure cost - and the file already has the pool it should come from (pooled/give_back, used by writing for 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_index in #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; pooled is a mutex and a Vec swap. The frame line's copying back term 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 refreshes per frame). ## Correctness - The set is keyed by exactly what the scan compared - the Arc pointer, the address and the length - and back keeps its order. - The pooled buffer is only read after read_into has written it, and is returned to the pool after the loop; read_into failing 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 byte copy_back merges 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::writing marks the same span, so our own write is not taken for the title's). ### Measured on a new synthetic harness shadow::cost::shadow_cost (this PR) builds a real Shadow over 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 that GPU-SNAPSHOT-COST is 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 from pooled(). That function returns the vector cleared (len == 0), and copy_back slices piece[..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 0 The workspace tests do not cover copy_back, so nothing caught it until shadow_cost did - which is the argument for the harness as much as for the fix. It is piece.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::scope over chunks of the collected back list, the machine's cores up to eight. VICTORY_COPY_THREADS=1 keeps 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 360 either 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 with cargo test --release -p victory-gpu -- --ignored --nocapture shadow_parallel.

6. The pre-submission copies on several threads

Batch::flush brings 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:

time
one thread 10.5 ms
eight threads 5.7 ms 1.85x

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 flush is the write-watch askings, and watch::writes holds one global mutex across GetWriteWatch, so those stay serial however many threads there are. #776 is the change that cuts the askings; this one spreads the copies behind them. VICTORY_COPY_THREADS=1 turns 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_flush

…fer from the pool

Two pieces of per-range work in the copy-back path (Marvisak#400: 54-110 ms a
frame) scale with what a submission declares.

`execute` collects the ranges shaders wrote and skipped a range already
collected by scanning the vector with `Arc::ptr_eq`: the world's passes
declare the same big ranges dozens of times, so the vector grows to
hundreds while the steps number in the thousands. Key a set by (pointer,
address, length) - exactly what the scan compared - and keep the order.

`Shadow::copy_back` allocated `vec![0u8; 64 KiB]` per call for bytes that
`read_into` overwrites before they are read, so the allocation and the
memset were pure cost. Take the piece from the pool the file already uses
for the snapshot buffers, and give it back - the same reasoning as
"fresh snapshots cost ~2.3 million page faults per world frame".

Measured without the title: the scan costs 0.91 ms against 0.21 ms for 33k
steps over 250 ranges, and 3.18 ms against 0.24 ms at 1000 ranges (best of
7; the same comparison as `refresh_merge_scan_vs_index` in Marvisak#1556, same
machine).

cargo test --workspace --locked; both clippy configurations with
-D warnings; cargo xtask policy.
pes7 added 3 commits November 25, 2026 00:48
… 64 KiB piece

`copy_back` merges 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. Track the first and last byte the title
actually takes and write that span, and mark the same span in the watch.

New synthetic harness (`shadow::cost::shadow_cost`, no title needed): a
real `Shadow` over a real host copy and a synthetic title range, 640 bytes
written into every 64 KiB piece as a shader would:

  range    written 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 rest of a
piece is the kernel read of title memory and the merge over it, not the
write - which is what says GPU-SNAPSHOT-COST is about reads.

Run it with
`cargo test --release -p victory-gpu -- --ignored --nocapture shadow_cost`.

cargo test --workspace --locked; both clippy configurations with
-D warnings; cargo xtask policy.
`pooled()` returns the vector cleared, so the 64 KiB piece taken from it in
the previous commit had length 0 and `piece[..n]` panicked on the first
copy-back of a changed piece:

    range end index 24512 out of range for slice of length 0

Nothing in the workspace tests covers `copy_back`, so the new
`shadow::cost::shadow_cost` harness caught it. Size it once per call. The
pool saves the allocation; the zeroing stays, and the description of the
earlier commit says so.
The ranges a submission wrote are independent - different buffers, or
disjoint ranges of one - and `copy_back` is kernel reads and writes of
title memory, which one thread does at a fifth of what the machine can. It
is the term Marvisak#400 measures at 54-110 ms a frame.

Spread them over the machine's cores, at most eight, with
`std::thread::scope` over chunks of the collected list. `VICTORY_COPY_THREADS`
overrides the count, so a real run can A/B the frame line with it at 1.

New `shadow::parallel_cost::shadow_parallel`, 16 ranges of 4 MiB with 640
changed bytes per 64 KiB piece, synthetic memory and real host copies:

    one thread    41.4 ms
    eight threads 12.4 ms   3.35x

The bytes written back are identical either way (655 360).

cargo test --workspace --locked; both clippy configurations with
-D warnings; cargo xtask policy.
@HenriqueSBueno

Copy link
Copy Markdown
Contributor

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 in Batch::add, a 64 KiB piece scan with byte compares, and ReadProcessMemory/WriteProcessMemory on 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)

`Batch::flush` copies each range a list reads from title memory 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 that one thread does at a fraction of what the machine can.

Spread them the way the copy-back already is, over the same
`VICTORY_COPY_THREADS` count (1 for an A/B of the frame line).

New `shadow::flush_cost::shadow_flush`, 16 ranges of 4 MiB with no
write-watch, so every range is copied whole - the worst case:

    one thread     10.5 ms
    eight threads   5.7 ms   1.85x

Less than the copy-back's 3.35x: this path is already at memcpy speed, so
it runs into memory bandwidth rather than idling.

cargo test --workspace --locked; both clippy configurations with
-D warnings; cargo xtask policy.
@pes7

pes7 commented Nov 24, 2026

Copy link
Copy Markdown
Contributor Author

Closing this rather than rebasing it: Shadow was redesigned on main today (c75fdba one change journal for title memory, 86a5ed6, 6d021fe world frame cost, bacb06a), and the code this was written against no longer exists. The PIECE loop, pending, the set of compared pieces and the 64 KiB pool are all gone; copy_back_page now works a page at a time against tracked/before, and Batch already 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:

synthetic harness, this machine
copy-back, one thread vs eight 41.4 -> 12.4 ms, 3.35x
pre-submission refresh (flush), one thread vs eight 10.5 -> 5.7 ms, 1.93x
write-back of only the changed span instead of the whole piece written bytes ~1 000x fewer, time -8..29%

Neither win is obviously available on the new design, and I do not want to claim it without measuring:

If you want any of those on the new layout, say which and I will write it against copy_back_page/copy_back and 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.

@pes7 pes7 closed this Nov 24, 2026
@pes7

pes7 commented Nov 24, 2026

Copy link
Copy Markdown
Contributor Author

@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 in Batch::add, a piece scan, and a ReadProcessMemory/WriteProcessMemory pair per piece.

Two notes, since the PR is closed now (the path was redesigned on main today, see above):

  • the union is already gone from main: Batch::add is keyed by Arc::as_ptr and 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;
  • the self-ReadProcessMemory is still there, and it is the one I would look at first. host::read_into is ReadProcessMemory(GetCurrentProcess(), ..), a kernel transition for memory in our own process, and copy_back_page now 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_cost prints 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.

@catminou4 catminou4 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants