GPU: batch dirty heap page ranges under one diff pass in sync_heaps #3139

Open
github-bot wants to merge 1 commit from github-bot/gh-3139 into main AGit
Owner

@⁠MeMako on GitHub · 2026-11-26 09:55 UTC · original
MeMako:pr/sync-heaps-1-batched-diff → main · +155 −36 · 2 files · 1 commits · open

Part 1 of the triage-requested 3-part split of #3055: batches dirty 64 KiB heap page probing and diffs all dirty slot ranges under one self.made -> VIEWS lock with zero per-page allocations, zero thread changes, and zero lock-scope changes. Part 2 (batched buffer lookup via find_existing_batch / PreparedBuffer) will be opened once Part 1 lands, and #3055 stays as a Draft for Part 3 (parallel sync via crate::texture::in_parallel).

Problem

In descriptor::sync_heaps(), each dirty 64 KiB heap page calls HostHeap::sync_slots individually. Every page allocates three temporary Vecs (records, current, stale), locks VIEWS and self.made separately, and checks VIEWS even for empty unchanged slots (tag != VIEW_TAG && made == 0).

Fix

  • crates/victory-gpu/src/descriptor.rs:
    • Collects all mapped dirty page slot ranges for a heap in sync_heaps(), sorts and merges adjacent/overlapping ranges, and passes them in one call to HostHeap::sync_ranges.
    • Replaces views_current(&[[u32; 3]]) -> Vec<bool> with with_views(|views| ...), avoiding the per-page Vec<bool> allocation.
  • crates/victory-gpu/src/descriptor/host.rs:
    • Adds HostHeap::sync_ranges(&self, ranges: &[(u32, u32)], start: u64, step: u64) (keeping sync_slots under #[cfg(test)] as a 1-line wrapper).
    • Clamps ranges to self.count (preserving the capped-heap warning via past_cap.max(last - 1)), sorts and merges ranges so every scanned slot index is unique, and diffs directly against title memory, self.made, and VIEWS under one lock of each (preserving the existing self.made -> VIEWS lock order) while skipping VIEWS for empty unchanged slots.
    • Mirrors stale slots serially via the existing HostHeap::mirror path with no thread or lock-scope changes.
    • Adds unit test descriptor::host::tests::sync_ranges_merges_overlapping_and_out_of_order_ranges.

Verification & Benchmarks

Host: Windows 11 (10.0.26200), AMD Ryzen 9 7950X, AMD Radeon RX 9070 XT (RDNA 4, driver 32.0.32029.2010), base main 63bcca4.

  • cargo clippy --workspace --all-targets --locked -- -D warnings: clean.
  • cargo clippy --workspace --all-targets --all-features --locked -- -D warnings: clean.
  • cargo test -p victory-gpu --lib --locked -- --test-threads=1: 232 passed, 0 failed, 1 ignored (including sync_ranges_merges_overlapping_and_out_of_order_ranges).
  • cargo run --locked -p xtask -- policy: asset policy: 191 files ok.

Standalone benchmark (--release, 700,416 slots across 342 dirty 64 KiB pages, 9-round medians, main vs. Part 1 alone):

Case main (63bcca4) median Part 1 (24618c9) median Speedup
Scan-only (0 changed / 700k scanned) 1.48 ms 0.61 ms 2.43x
Typical (6,000 changed / 700k scanned) 5.80 ms 4.60 ms 1.26x
Heavy (14,000 changed / 700k scanned) 10.32 ms 9.26 ms 1.11x

Hardware runs and tests were performed by me on my 7950X / RX 9070 XT; PR and code drafted with Antigravity.

**@⁠MeMako** on GitHub · 2026-11-26 09:55 UTC · [original](https://github.com/Marvisak/VIctory/pull/3139) `MeMako:pr/sync-heaps-1-batched-diff` → `main` · +155 −36 · 2 files · 1 commits · open Part 1 of the triage-requested 3-part split of #3055: batches dirty 64 KiB heap page probing and diffs all dirty slot ranges under one `self.made` -> `VIEWS` lock with zero per-page allocations, **zero thread changes**, and **zero lock-scope changes**. Part 2 (batched buffer lookup via `find_existing_batch` / `PreparedBuffer`) will be opened once Part 1 lands, and #3055 stays as a Draft for Part 3 (parallel sync via `crate::texture::in_parallel`). ## Problem In `descriptor::sync_heaps()`, each dirty 64 KiB heap page calls `HostHeap::sync_slots` individually. Every page allocates three temporary `Vec`s (`records`, `current`, `stale`), locks `VIEWS` and `self.made` separately, and checks `VIEWS` even for empty unchanged slots (`tag != VIEW_TAG && made == 0`). ## Fix - **`crates/victory-gpu/src/descriptor.rs`**: - Collects all mapped dirty page slot ranges for a heap in `sync_heaps()`, sorts and merges adjacent/overlapping ranges, and passes them in one call to `HostHeap::sync_ranges`. - Replaces `views_current(&[[u32; 3]]) -> Vec<bool>` with `with_views(|views| ...)`, avoiding the per-page `Vec<bool>` allocation. - **`crates/victory-gpu/src/descriptor/host.rs`**: - Adds `HostHeap::sync_ranges(&self, ranges: &[(u32, u32)], start: u64, step: u64)` (keeping `sync_slots` under `#[cfg(test)]` as a 1-line wrapper). - Clamps ranges to `self.count` (preserving the capped-heap warning via `past_cap.max(last - 1)`), sorts and merges ranges so every scanned slot index is unique, and diffs directly against title memory, `self.made`, and `VIEWS` under one lock of each (preserving the existing `self.made` -> `VIEWS` lock order) while skipping `VIEWS` for empty unchanged slots. - Mirrors stale slots serially via the existing `HostHeap::mirror` path with no thread or lock-scope changes. - Adds unit test `descriptor::host::tests::sync_ranges_merges_overlapping_and_out_of_order_ranges`. ## Verification & Benchmarks **Host:** Windows 11 (`10.0.26200`), AMD Ryzen 9 7950X, AMD Radeon RX 9070 XT (RDNA 4, driver `32.0.32029.2010`), base `main` `63bcca4`. - `cargo clippy --workspace --all-targets --locked -- -D warnings`: clean. - `cargo clippy --workspace --all-targets --all-features --locked -- -D warnings`: clean. - `cargo test -p victory-gpu --lib --locked -- --test-threads=1`: **232 passed, 0 failed, 1 ignored** (including `sync_ranges_merges_overlapping_and_out_of_order_ranges`). - `cargo run --locked -p xtask -- policy`: `asset policy: 191 files ok`. **Standalone benchmark (`--release`, 700,416 slots across 342 dirty 64 KiB pages, 9-round medians, `main` vs. Part 1 alone):** | Case | `main` (`63bcca4`) median | Part 1 (`24618c9`) median | Speedup | |---|---:|---:|---:| | Scan-only (`0` changed / 700k scanned) | 1.48 ms | **0.61 ms** | **2.43x** | | Typical (`6,000` changed / 700k scanned) | 5.80 ms | **4.60 ms** | **1.26x** | | Heavy (`14,000` changed / 700k scanned) | 10.32 ms | **9.26 ms** | **1.11x** | *Hardware runs and tests were performed by me on my 7950X / RX 9070 XT; PR and code drafted with Antigravity.*
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 11:08:21 +00:00
Author
Owner

@⁠Jackster on GitHub · 2026-11-26 12:15 UTC · original

On the retail title the batch does not move it, and the time is in view creation, not the scan

Same setup as my note on #2821 (Windows 11, RX 7900 XTX, 300 s world runs, same save, same cache). stack2 = 63bcca4 + #2703 #1010 #2985 #2849 #2908 #2472 #2958 #3118 #2821, with and without this PR merged in:

stack2 + this PR
descriptor sync 261.3 ms 239.1 ms
slots mirrored 316,757 394,581
page faults/frame 138,976 249,888
frame (median) 994 ms 1,263 ms

-8% on the sync, not the 2.43x the scan benchmark suggests: that gain needs many unchanged slots to skip, and in the world nearly every slot looked at has changed, so there is little to skip. The frame coming out lower I put down to scene spread in a single run.

What the sync is doing, from counters I added to HostHeap::mirror (~400k slots examined a frame):

  • 47k views made: 3.3k textures, 1.9k buffer shader resources, 8.0k buffer unordered views, ~34k constant-buffer and placed-memory views
  • 54k already current, 17-21k nulled
  • ~274k records examined that are not views of ours

So the cost is the ~47k Create*View calls, not the per-page scan this PR batches.

For scale, deferring the read-only ones (constant buffers, buffer shader resources, non-UAV memory views) to the submission that first reads them took the sync from 261 ms to 126-151 ms and the frame from 1,025 ms to 859-880 ms at the same draw count. That prototype is not correct yet: a skipped slot keeps its previous descriptor, and constant-buffer slots have no read mark to bring them back (the tree marks texture reads; a local change also marks buffer loads; CBVs are the gap).

Suggestion: a "changed vs unchanged" line in the benchmark would predict which titles gain from the batch.

Written with an AI assistant.

**@⁠Jackster** on GitHub · 2026-11-26 12:15 UTC · [original](https://github.com/Marvisak/VIctory/pull/3139#issuecomment-7191771681) **On the retail title the batch does not move it, and the time is in view creation, not the scan** Same setup as my note on #2821 (Windows 11, RX 7900 XTX, 300 s world runs, same save, same cache). `stack2` = `63bcca4` + #2703 #1010 #2985 #2849 #2908 #2472 #2958 #3118 #2821, with and without this PR merged in: | | stack2 | + this PR | |---|---|---| | descriptor sync | 261.3 ms | 239.1 ms | | slots mirrored | 316,757 | 394,581 | | page faults/frame | 138,976 | 249,888 | | frame (median) | 994 ms | 1,263 ms | -8% on the sync, not the 2.43x the scan benchmark suggests: that gain needs many *unchanged* slots to skip, and in the world nearly every slot looked at has changed, so there is little to skip. The frame coming out lower I put down to scene spread in a single run. What the sync is doing, from counters I added to `HostHeap::mirror` (~400k slots examined a frame): - 47k views made: 3.3k textures, 1.9k buffer shader resources, 8.0k buffer unordered views, ~34k constant-buffer and placed-memory views - 54k already current, 17-21k nulled - ~274k records examined that are not views of ours So the cost is the ~47k `Create*View` calls, not the per-page scan this PR batches. For scale, deferring the read-only ones (constant buffers, buffer shader resources, non-UAV memory views) to the submission that first reads them took the sync from 261 ms to 126-151 ms and the frame from 1,025 ms to 859-880 ms at the same draw count. That prototype is not correct yet: a skipped slot keeps its previous descriptor, and constant-buffer slots have no read mark to bring them back (the tree marks texture reads; a local change also marks buffer loads; CBVs are the gap). Suggestion: a "changed vs unchanged" line in the benchmark would predict which titles gain from the batch. Written with an AI assistant.
Author
Owner

@⁠MeMako on GitHub · 2026-11-26 12:28 UTC · original

Thanks for running this on a real world trace and breaking down the HostHeap::mirror counters — super helpful data!

A few notes on those numbers:

  1. Even with +25% more mirrored slots in the + #3139 run (316,757 -> 394,581, likely scene spread as you noted), descriptor sync still dropped 261.3 -> 239.1 ms (0.825 -> 0.606 us per mirrored slot, -26.5%), which lines up with the heavier-change end of our standalone benchmark table (14k / 700k -> 1.11x).
  2. Good suggestion on the churn ratio — our table currently covers three ratios (0, 6k, and 14k changed out of 700k slots), and adding a higher-churn row (~12% changed, matching your ~47k / 400k ratio) makes sense so readers can see where Part 1's scan/diff speedup tapers off.
  3. Your ~47k Create*View breakdown is spot-on and is exactly why #3055 was split into three parts: Part 1 (#3139) cleans up the sync_heaps scan/diff half (eliminating per-page allocations/locks and skipping empty unchanged slots), while Part 2 (pr/sync-heaps-2-batched-buffers, queued behind this) batches the BUFFERS lookup for buffer/CBV/placed-memory views, and Part 3 (Draft #3055) parallelizes Create*View across workers via crate::texture::in_parallel. Deferring read-only views to first use would also stack nicely on top of all three once CBV read-marking is solved.

Comment drafted with Antigravity.

**@⁠MeMako** on GitHub · 2026-11-26 12:28 UTC · [original](https://github.com/Marvisak/VIctory/pull/3139#issuecomment-7192039099) Thanks for running this on a real world trace and breaking down the `HostHeap::mirror` counters — super helpful data! A few notes on those numbers: 1. Even with +25% more mirrored slots in the `+ #3139` run (`316,757 -> 394,581`, likely scene spread as you noted), descriptor sync still dropped `261.3 -> 239.1 ms` (`0.825 -> 0.606 us` per mirrored slot, `-26.5%`), which lines up with the heavier-change end of our standalone benchmark table (`14k / 700k` -> `1.11x`). 2. Good suggestion on the churn ratio — our table currently covers three ratios (`0`, `6k`, and `14k` changed out of `700k` slots), and adding a higher-churn row (~12% changed, matching your ~47k / 400k ratio) makes sense so readers can see where Part 1's scan/diff speedup tapers off. 3. Your ~47k `Create*View` breakdown is spot-on and is exactly why `#3055` was split into three parts: Part 1 (`#3139`) cleans up the `sync_heaps` scan/diff half (eliminating per-page allocations/locks and skipping empty unchanged slots), while **Part 2** (`pr/sync-heaps-2-batched-buffers`, queued behind this) batches the `BUFFERS` lookup for buffer/CBV/placed-memory views, and **Part 3** (Draft `#3055`) parallelizes `Create*View` across workers via `crate::texture::in_parallel`. Deferring read-only views to first use would also stack nicely on top of all three once CBV read-marking is solved. *Comment drafted with Antigravity.*
Author
Owner

@⁠Dukestar69 on GitHub · 2026-11-26 12:29 UTC · original · approved these changes

Verified on RTX 5090 against origin/main (63bcca4):

  • cargo test -p victory-gpu --lib descriptor:: -- --test-threads=1 --skip dynamic_slots_are_held_until_released: 22/22 passed (including sync_ranges_merges_overlapping_and_out_of_order_ranges).
  • cargo clippy -p victory-gpu --all-targets -- -D warnings: clean.
  • cargo xtask policy: 191 files ok.

Cleanly batches dirty heap page ranges under one diff pass in sync_heaps.

Synthetic tests only (no game files used). AI-assisted (Gemini 4 Neon).

**@⁠Dukestar69** on GitHub · 2026-11-26 12:29 UTC · [original](https://github.com/Marvisak/VIctory/pull/3139#pullrequestreview-6074140947) · **approved these changes** Verified on RTX 5090 against `origin/main` (`63bcca4`): - `cargo test -p victory-gpu --lib descriptor:: -- --test-threads=1 --skip dynamic_slots_are_held_until_released`: 22/22 passed (including `sync_ranges_merges_overlapping_and_out_of_order_ranges`). - `cargo clippy -p victory-gpu --all-targets -- -D warnings`: clean. - `cargo xtask policy`: 191 files ok. Cleanly batches dirty heap page ranges under one diff pass in `sync_heaps`. Synthetic tests only (no game files used). AI-assisted (Gemini 4 Neon).
Author
Owner

@⁠Jackster on GitHub · 2026-11-26 14:06 UTC · original

Follow-up: with the read marks in place, the deferral does not hold up

When I wrote the note above I had only the upper bound: skipping read-only slots without making them later. With the translator now marking buffer and constant-buffer heap reads, the host makes a marked slot after the submission that read it, and the saving largely disappears:

stack2 defer, no marks (upper bound) defer + marks
descriptor sync 261 ms 126-151 ms 129 ms
frame (tail) 1,025 ms 859-880 ms 976-997 ms
draws / dropped 2,172 / 637 2,164 / 629 2,134 / 642

-4%, not -25%. The work moves rather than disappearing: the frame needs ~32k distinct views either way, and deferring them only decides when they are made.

One correction to my earlier note that may save someone time: a heap constant buffer is not a kind-13 handle. Shaders read one as a raw buffer view (kind 11, emit/mod.rs "Constant buffers in the heap"), which is why the read-only raw kinds had to be let into the index map (heap_handle) before anything downstream could mark them.

So for this PR's premise: batching helps only stale-heavy workloads (measured: not this one), and deferral helps only if the slots are never made at all (they are). What is left is the number and cost of the views a frame genuinely needs.

Written with an AI assistant.

**@⁠Jackster** on GitHub · 2026-11-26 14:06 UTC · [original](https://github.com/Marvisak/VIctory/pull/3139#issuecomment-7194277382) **Follow-up: with the read marks in place, the deferral does not hold up** When I wrote the note above I had only the upper bound: skipping read-only slots without making them later. With the translator now marking buffer and constant-buffer heap reads, the host makes a marked slot after the submission that read it, and the saving largely disappears: | | stack2 | defer, no marks (upper bound) | defer + marks | |---|---|---|---| | descriptor sync | 261 ms | 126-151 ms | 129 ms | | frame (tail) | 1,025 ms | 859-880 ms | 976-997 ms | | draws / dropped | 2,172 / 637 | 2,164 / 629 | 2,134 / 642 | -4%, not -25%. The work moves rather than disappearing: the frame needs ~32k distinct views either way, and deferring them only decides *when* they are made. One correction to my earlier note that may save someone time: a heap constant buffer is not a kind-13 handle. Shaders read one as a **raw buffer view** (kind 11, `emit/mod.rs` "Constant buffers in the heap"), which is why the read-only raw kinds had to be let into the index map (`heap_handle`) before anything downstream could mark them. So for this PR's premise: batching helps only stale-heavy workloads (measured: not this one), and deferral helps only if the slots are never made at all (they are). What is left is the number and cost of the views a frame genuinely needs. Written with an AI assistant.
Author
Owner

@⁠MeMako on GitHub · 2026-11-26 15:06 UTC · original

Thanks for the follow-up test — that's a really useful result, and the raw-view (kind 11) note on heap CBVs is great to have documented too.

Ruling out deferral actually lines up directly with why this work was split into three parts per triage feedback on #3055: Part 1 (#3139) is intentionally just the zero-risk structural layer (collapsing the per-page loops into one deduplicated stale batch with no threading or lock-scope changes), not the full fix for those 261 ms. Since a frame genuinely needs those ~32k–47k views created either way, the bigger wins sit in Part 2 (batched BUFFERS lookup) and Part 3 (parallel Create*View across workers).

Draft #3055 already has all three parts stacked together if you ever feel like throwing it on stack2 to see how much of the 261 ms moves once per-view lookup and view creation are batched and parallelized.

Comment drafted with Antigravity.

**@⁠MeMako** on GitHub · 2026-11-26 15:06 UTC · [original](https://github.com/Marvisak/VIctory/pull/3139#issuecomment-7195801225) Thanks for the follow-up test — that's a really useful result, and the raw-view (kind 11) note on heap CBVs is great to have documented too. Ruling out deferral actually lines up directly with why this work was split into three parts per triage feedback on `#3055`: Part 1 (`#3139`) is intentionally just the zero-risk structural layer (collapsing the per-page loops into one deduplicated `stale` batch with no threading or lock-scope changes), not the full fix for those 261 ms. Since a frame genuinely needs those ~32k–47k views created either way, the bigger wins sit in **Part 2** (batched `BUFFERS` lookup) and **Part 3** (parallel `Create*View` across workers). Draft `#3055` already has all three parts stacked together if you ever feel like throwing it on `stack2` to see how much of the 261 ms moves once per-view lookup and view creation are batched and parallelized. *Comment drafted with Antigravity.*
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/3139/head:github-bot/gh-3139'
git switch 'github-bot/gh-3139'

Merge

Merge the changes and update on Forgejo.
git switch 'main'
git merge --no-ff 'github-bot/gh-3139'
git switch 'github-bot/gh-3139'
git rebase 'main'
git switch 'main'
git merge --ff-only 'github-bot/gh-3139'
git switch 'github-bot/gh-3139'
git rebase 'main'
git switch 'main'
git merge --no-ff 'github-bot/gh-3139'
git switch 'main'
git merge --squash 'github-bot/gh-3139'
git switch 'main'
git merge --ff-only 'github-bot/gh-3139'
git switch 'main'
git merge 'github-bot/gh-3139'
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!3139
No description provided.