victory-gpu: coalesce and parallelize descriptor::sync_heaps #1293

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

@⁠Vladimir-Human on GitHub · 2026-11-26 11:59 UTC · original
Vladimir-Human:perf/parallel-coalesced-sync-heaps → main · +1181 −258 · 2 files · 1 commits · open

Summary

Optimizes descriptor::sync_heaps (crates/victory-gpu/src/descriptor.rs & crates/victory-gpu/src/descriptor/host.rs):

  • Coalesced read_into + unmapped-page fallback (descriptor.rs): Merges touching/overlapping dirty [from, to) ranges without bridging clean gaps (coalesce_ranges) and splits runs into watch::PAGE-aligned 128 KiB chunks (chunk_ranges). Reads each chunk with a single read_into call, falling back to per-watch::PAGE read_into across the chunk on failure so valid neighbour blocks still mirror.
  • Parallel diff & slot creation (descriptor.rs + descriptor/host.rs):
    • Converts HostHeap::copied to lock-free OnceLock<Vec<AtomicU64>> fingerprints (copied_slots) and passes snapshot [u32; 3] words (record_words) into mirror_inner.
    • Shards HostHeap::made across KEPT_PIECES (64) mutexes matching HostHeap::kept, dropping index's shard before unmaking stale placed-texture slots via Spans::note_keyed + HostHeap::unmake_if_key.
    • Adds per-slot atomic presence flags (slot_flags: FLAG_TEXTURE, FLAG_PLACED, FLAG_BUFFER, FLAG_KEPT) maintained inside their respective subsystem locks so unrelated descriptor writes (e.g. constant buffer views) skip empty subsystem mutexes.
    • Guards pre-copied slice mirroring (mirror_copied_slice and VICTORY_HEAP_SYNC_PAGE=0 mirror_copied) with per-shard made_direct_epoch counters so a pre-copied snapshot never reverts a concurrent direct HostHeap::mirror.
    • Packs multi-chunk work LPT-first across crate::shadow::wide_run while preserving all VICTORY_HEAP_SYNC* toggles (=0, _CUT, _PAGE=0, _BESIDE, _TABLES) and SYNC_PARTS/SYNCED wall-time telemetry.

Verification

  • git diff --check origin/main, git merge-tree --write-tree origin/main HEAD, neutral-naming scan (0 hits)
  • cargo run -p xtask -- policy (220 files ok)
  • cargo clippy -p victory-gpu --all-targets -- -D warnings -A deprecated (default & --all-features: 0 warnings)
  • VICTORY_D3D12_DEBUG=1 cargo test -p victory-gpu -- --test-threads=1 (220 passed; 0 failed; 6 ignored — includes parity, mid-chunk unmapped-page fallback, chunk boundary/unchanged-slot skip, keep_views() mip-update stale unmake, requeued FLAG_TEXTURE eviction, and concurrent direct-mirror clobber guard tests)
  • VICTORY_D3D12_DEBUG=1 cargo test -p victory-gpu --features diagnostics -- --test-threads=1 (239 passed; 0 failed; 7 ignored)
  • cargo test --release -p victory-gpu --lib sync_heaps_coalesced_parallel_benchmark -- --nocapture (14,400 dirty slots x 10 iters):
    • Baseline (64 KiB serial): 2.504 ms/iter (8 reads/iter, parts=[take=0.014, read=0.038, walk=0.108, make=2.320])
    • Coalesced + parallel (128 KiB): 1.805 ms/iter (4 reads/iter, parts=[take=0.016, read=0.020, walk=0.089, make=1.677])
**@⁠Vladimir-Human** on GitHub · 2026-11-26 11:59 UTC · [original](https://github.com/fxceless/victory-fork/pull/1293) `Vladimir-Human:perf/parallel-coalesced-sync-heaps` → `main` · +1181 −258 · 2 files · 1 commits · open ## Summary Optimizes `descriptor::sync_heaps` (`crates/victory-gpu/src/descriptor.rs` & `crates/victory-gpu/src/descriptor/host.rs`): - **Coalesced `read_into` + unmapped-page fallback (`descriptor.rs`):** Merges touching/overlapping dirty `[from, to)` ranges without bridging clean gaps (`coalesce_ranges`) and splits runs into `watch::PAGE`-aligned `128 KiB` chunks (`chunk_ranges`). Reads each chunk with a single `read_into` call, falling back to per-`watch::PAGE` `read_into` across the chunk on failure so valid neighbour blocks still mirror. - **Parallel diff & slot creation (`descriptor.rs` + `descriptor/host.rs`):** - Converts `HostHeap::copied` to lock-free `OnceLock<Vec<AtomicU64>>` fingerprints (`copied_slots`) and passes snapshot `[u32; 3]` words (`record_words`) into `mirror_inner`. - Shards `HostHeap::made` across `KEPT_PIECES` (64) mutexes matching `HostHeap::kept`, dropping `index`'s shard before unmaking stale placed-texture slots via `Spans::note_keyed` + `HostHeap::unmake_if_key`. - Adds per-slot atomic presence flags (`slot_flags`: `FLAG_TEXTURE`, `FLAG_PLACED`, `FLAG_BUFFER`, `FLAG_KEPT`) maintained inside their respective subsystem locks so unrelated descriptor writes (e.g. constant buffer views) skip empty subsystem mutexes. - Guards pre-copied slice mirroring (`mirror_copied_slice` and `VICTORY_HEAP_SYNC_PAGE=0` `mirror_copied`) with per-shard `made_direct_epoch` counters so a pre-copied snapshot never reverts a concurrent direct `HostHeap::mirror`. - Packs multi-chunk work LPT-first across `crate::shadow::wide_run` while preserving all `VICTORY_HEAP_SYNC*` toggles (`=0`, `_CUT`, `_PAGE=0`, `_BESIDE`, `_TABLES`) and `SYNC_PARTS`/`SYNCED` wall-time telemetry. ## Verification - `git diff --check origin/main`, `git merge-tree --write-tree origin/main HEAD`, neutral-naming scan (`0` hits) - `cargo run -p xtask -- policy` (`220 files ok`) - `cargo clippy -p victory-gpu --all-targets -- -D warnings -A deprecated` (default & `--all-features`: `0` warnings) - `VICTORY_D3D12_DEBUG=1 cargo test -p victory-gpu -- --test-threads=1` (**220 passed; 0 failed; 6 ignored** — includes parity, mid-chunk unmapped-page fallback, chunk boundary/unchanged-slot skip, `keep_views()` mip-update stale unmake, requeued `FLAG_TEXTURE` eviction, and concurrent direct-mirror clobber guard tests) - `VICTORY_D3D12_DEBUG=1 cargo test -p victory-gpu --features diagnostics -- --test-threads=1` (**239 passed; 0 failed; 7 ignored**) - `cargo test --release -p victory-gpu --lib sync_heaps_coalesced_parallel_benchmark -- --nocapture` (`14,400` dirty slots x `10` iters): - Baseline (`64 KiB` serial): `2.504 ms/iter` (`8 reads/iter`, `parts=[take=0.014, read=0.038, walk=0.108, make=2.320]`) - Coalesced + parallel (`128 KiB`): `1.805 ms/iter` (`4 reads/iter`, `parts=[take=0.016, read=0.020, walk=0.089, make=1.677]`)
- Coalesce touching/overlapping dirty heap ranges up to 128 KiB (2x watch::PAGE) before read_into, falling back to per-block read_into when a chunk hits an unmapped gap so adjacent blocks still mirror.
- Replace HostHeap::copied's mutex with OnceLock<Vec<AtomicU64>> lock-free fingerprints and shard HostHeap::made across KEPT_PIECES (64) mutexes matching kept.
- Track Spans::keys and drop index's made shard before unmaking stale slots via unmake_if_key (preventing two-shard deadlocks and concurrent clobbers).
- Add per-slot presence hints (slot_flags) so unrelated descriptor rewrites (such as CBVs) skip untouched textures/placed/buffers/kept locks, keeping FLAG_TEXTURE and FLAG_BUFFER synchronized inside their respective mutexes.
- Guard pre-copied slice mirroring against reverting concurrent direct HostHeap::mirror writes via per-shard made_direct_epoch counters.
- Dispatch multi-chunk heap syncs across wide_run worker batches with balanced LPT packing, preserving all VICTORY_HEAP_SYNC* toggles and SYNC_PARTS/SYNCED accounting.
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 12:03:44 +00:00
Author
Owner

@⁠Garu667 on GitHub · 2026-11-26 12:04 UTC · original

Happy to A/B this on the RTX 3080 like #1279 (where your guard work gave 9.1 → 10.0 fps). It conflicts with current main (ceb9e1f) in descriptor/host.rs: main reworked mirror since (made_now / heap_locks() fast path, keep_views / same_placed_texture, MIRROR_* timing), and your mirror_inner rewrites the same body, so I'd rather not guess the merge. If you rebase it, I'll run it the same day against the same baseline.

**@⁠Garu667** on GitHub · 2026-11-26 12:04 UTC · [original](https://github.com/fxceless/victory-fork/pull/1293#issuecomment-7191527753) Happy to A/B this on the RTX 3080 like #1279 (where your guard work gave 9.1 → 10.0 fps). It conflicts with current `main` (`ceb9e1f`) in `descriptor/host.rs`: `main` reworked `mirror` since (`made_now` / `heap_locks()` fast path, `keep_views` / `same_placed_texture`, `MIRROR_*` timing), and your `mirror_inner` rewrites the same body, so I'd rather not guess the merge. If you rebase it, I'll run it the same day against the same baseline.
Author
Owner

@⁠KainanP on GitHub · 2026-11-26 13:08 UTC · original

Independent A/B of this PR and the other two open sync_heaps PRs (#1202, #1220), each against its own merge base, using one black-box benchmark. All three conflict with current main, so each PR head is compared with the commit it branched from.

Benchmark. descriptor::tests::sync_heaps_bench on KainanP/victory-fork@bench/sync-heaps, added unchanged to every tree. It only uses create_heap, create_cbv/create_srv/create_uav, sync_heaps, SYNCED and SYNC_PARTS.

  • Heap. A placed, write-watched view heap of 2^20 slots (the title's is "a little over 1,000,000"). It is filled with records and synced once.
  • Records. Three in four are constant buffer views of distinct addresses; the rest are null SRV and UAV views.
  • Each frame.
    • 280 random 64 KiB pages are chosen (196 distinct).
    • A 36 KiB window of each page is rewritten.
    • 50 records per page change.
    • One sync_heaps call is timed.
  • Shape. That gives 196 pages, 401,408 slots looked at and 9,298 mirrored per frame. The fork's runs 300–309 show about 150–245 pages, 165,000–250,000 slots looked at and 6,000–16,000 mirrored.
  • Measurement. Release build, median of 15 frames after 2. The seven builds run in rotation for each round (RTX: rounds 2–5, because round 1 overlapped other CPU jobs; Intel UHD: 3 rounds). The table shows the median of the rounds. CPU: a 24-thread laptop.
tree switch RTX 4060 Laptop, ms Intel UHD 8086:a78b, ms
main ceb9e1f 5.80 5.46
#1293 base 06702fe 5.96 5.36
#1293 4fe062c (default) 1.42 1.45
#1202 base 32b31f7 5.42 4.84
#1202 a8f9c9d VICTORY_HEAP_SYNC_PARALLEL=1 4.00 3.29
#1220 base 006c353 5.44 4.77
#1220 18e9683 VICTORY_HEAP_SYNC_ZERO_RUNS=1 5.32 4.66

Results.

  • All seven trees mirror the same number of records: 401,408 looked at, 9,298 mirrored per frame.
  • #1293 is about 4× faster than its base. Per frame, by part (RTX): reading 1.1 → 0.23 ms, walk 1.5 → 0.27 ms, making 2.9 → 0.8 ms.
  • #1202 saves 26–32 %.
  • #1220 changes nothing here, which is expected. Every record in this heap is non-zero, and #1220 only skips untouched all-zero runs. Its sparse-heap case is not modelled.

What this does not show.

  • Records are byte-for-byte the same? Not checked. The comparison covers counts only; I did not compare host descriptors, which are not CPU-readable in a shader-visible heap.
  • Making cost with real resources. Records name no real resources, so the "making" part has no real texture or buffer views, waiting textures or kept views. In the fork's runs that part is 5–11 ms a frame for about 10,000 records.
  • Threads in use. #1293 and #1202 use other threads. Here those threads are idle. In the game they share cores with the recording threads and texture checks, so the in-game gain may be smaller. #1083 reports that moving preparation work to other threads did not shorten the frame.
  • In-game frame time. Not measured: I have no game here.

@⁠MKmajster, @⁠CuteBooty (#1083): your profile had descriptor::sync_heaps at 21–26 ms per frame. If you can, please run this PR and compare its by-part line ("the pass over the title's view heap by part") and the frame time with its base.

AI use: Claude Code (Claude Opus 5.5) wrote the benchmark, built and ran the seven trees, and wrote this comment.

🤖 Generated with Claude Code

**@⁠KainanP** on GitHub · 2026-11-26 13:08 UTC · [original](https://github.com/fxceless/victory-fork/pull/1293#issuecomment-7192923419) Independent A/B of this PR and the other two open `sync_heaps` PRs (#1202, #1220), each against its own merge base, using one black-box benchmark. All three conflict with current `main`, so each PR head is compared with the commit it branched from. **Benchmark.** `descriptor::tests::sync_heaps_bench` on [KainanP/victory-fork@bench/sync-heaps](https://github.com/KainanP/victory-fork/commit/6cbb2aa), added unchanged to every tree. It only uses `create_heap`, `create_cbv`/`create_srv`/`create_uav`, `sync_heaps`, `SYNCED` and `SYNC_PARTS`. - **Heap.** A placed, write-watched view heap of 2^20 slots (the title's is "a little over 1,000,000"). It is filled with records and synced once. - **Records.** Three in four are constant buffer views of distinct addresses; the rest are null SRV and UAV views. - **Each frame.** - 280 random 64 KiB pages are chosen (196 distinct). - A 36 KiB window of each page is rewritten. - 50 records per page change. - One `sync_heaps` call is timed. - **Shape.** That gives 196 pages, 401,408 slots looked at and 9,298 mirrored per frame. The fork's runs 300–309 show about 150–245 pages, 165,000–250,000 slots looked at and 6,000–16,000 mirrored. - **Measurement.** Release build, median of 15 frames after 2. The seven builds run in rotation for each round (RTX: rounds 2–5, because round 1 overlapped other CPU jobs; Intel UHD: 3 rounds). The table shows the median of the rounds. CPU: a 24-thread laptop. | tree | switch | RTX 4060 Laptop, ms | Intel UHD 8086:a78b, ms | |---|---|---:|---:| | `main` ceb9e1f | | 5.80 | 5.46 | | #1293 base 06702fe | | 5.96 | 5.36 | | **#1293** 4fe062c | (default) | **1.42** | **1.45** | | #1202 base 32b31f7 | | 5.42 | 4.84 | | #1202 a8f9c9d | `VICTORY_HEAP_SYNC_PARALLEL=1` | 4.00 | 3.29 | | #1220 base 006c353 | | 5.44 | 4.77 | | #1220 18e9683 | `VICTORY_HEAP_SYNC_ZERO_RUNS=1` | 5.32 | 4.66 | **Results.** - **All seven trees mirror the same number of records**: 401,408 looked at, 9,298 mirrored per frame. - **#1293 is about 4× faster than its base.** Per frame, by part (RTX): reading 1.1 → 0.23 ms, walk 1.5 → 0.27 ms, making 2.9 → 0.8 ms. - **#1202 saves 26–32 %.** - **#1220 changes nothing here,** which is expected. Every record in this heap is non-zero, and #1220 only skips untouched all-zero runs. Its sparse-heap case is not modelled. **What this does not show.** - **Records are byte-for-byte the same?** Not checked. The comparison covers counts only; I did not compare host descriptors, which are not CPU-readable in a shader-visible heap. - **Making cost with real resources.** Records name no real resources, so the "making" part has no real texture or buffer views, waiting textures or kept views. In the fork's runs that part is 5–11 ms a frame for about 10,000 records. - **Threads in use.** #1293 and #1202 use other threads. Here those threads are idle. In the game they share cores with the recording threads and texture checks, so the in-game gain may be smaller. #1083 reports that moving preparation work to other threads did not shorten the frame. - **In-game frame time.** Not measured: I have no game here. @⁠MKmajster, @⁠CuteBooty (#1083): your profile had `descriptor::sync_heaps` at 21–26 ms per frame. If you can, please run this PR and compare its by-part line ("the pass over the title's view heap by part") and the frame time with its base. _AI use: Claude Code (Claude Opus 5.5) wrote the benchmark, built and ran the seven trees, and wrote this comment._ 🤖 Generated with [Claude Code](https://claude.com/claude-code)
Author
Owner

@⁠CuteBooty on GitHub · 2026-11-26 13:40 UTC · original

Thanks for the benchmark. One correction to the mention: the 21-26 ms for descriptor::sync_heaps was MKmajster's profile, not mine. My tree (CuteBooty/victory-8gb) does not have sync_heaps; it compares descriptor records in its own heap pass, about 240,000 records a frame at an estimated few milliseconds, so I cannot give a like-for-like number for this PR from it.

If a run of the fork's own code on an RTX 3070 8 GB with a Ryzen 5 3600 would still help, I can do one, but it would be the fork's build, not mine, and it would take me a little while to set up.

**@⁠CuteBooty** on GitHub · 2026-11-26 13:40 UTC · [original](https://github.com/fxceless/victory-fork/pull/1293#issuecomment-7193676006) Thanks for the benchmark. One correction to the mention: the 21-26 ms for `descriptor::sync_heaps` was MKmajster's profile, not mine. My tree (CuteBooty/victory-8gb) does not have `sync_heaps`; it compares descriptor records in its own heap pass, about 240,000 records a frame at an estimated few milliseconds, so I cannot give a like-for-like number for this PR from it. If a run of the fork's own code on an RTX 3070 8 GB with a Ryzen 5 3600 would still help, I can do one, but it would be the fork's build, not mine, and it would take me a little while to set up.
github-bot force-pushed github-bot/gh-1293 from 4fe062c062 to 8f3d81ef66 2026-11-26 14:00:03 +00:00 Compare
Author
Owner

@⁠Vladimir-Human on GitHub · 2026-11-26 13:56 UTC · original

@⁠KainanP Thank you for running that 7-tree benchmark — seeing 1.42 ms vs 5.96 ms (4.2× faster, with identical 401,408 looked-at / 9,298 mirrored counts) is great confirmation of the coalesced reads and parallel walk/make pipeline.

@⁠Garu667 Cleanly rebased onto current main (a20809d, including ceb9e1f) as 8f3d81e, preserving all of ceb9e1f's changes alongside this PR:

  • made_now / heap_locks() lock-free fast path (both pre-copied and direct paths in mirror_inner)
  • keep_views() / same_placed_texture mip-refinement retention (with Spans::keys stale-span unmake intact)
  • MIRROR_* diagnostics telemetry (MIRROR_HOW TLS per worker in mirror_copied_slice, all outcome counters in mirror_inner, MIRROR_US[0..3] across note_buffer_key + stale_to_unmake, view creation, and keep, and the sync_line breakdown)

Re-verified post-rebase:

  • cargo run --locked -p xtask -- policy: 220 files ok
  • cargo clippy -p victory-gpu --all-targets -- -D warnings -A deprecated (default & --all-features): clean
  • VICTORY_D3D12_DEBUG=1 cargo test -p victory-gpu -- --test-threads=1: 220 passed, 0 failed (default) and 239 passed, 0 failed (--features diagnostics), 0 D3D12 debug errors
  • cargo test --release -p victory-gpu --lib sync_heaps_coalesced_parallel_benchmark -- --nocapture: 2.035 ms/iter (4 reads/iter) vs 2.494 ms/iter (8 reads/iter) baseline

Ready for A/B testing against ceb9e1f / current main!

**@⁠Vladimir-Human** on GitHub · 2026-11-26 13:56 UTC · [original](https://github.com/fxceless/victory-fork/pull/1293#issuecomment-7194046583) @⁠KainanP Thank you for running that 7-tree benchmark — seeing `1.42 ms` vs `5.96 ms` (4.2× faster, with identical `401,408` looked-at / `9,298` mirrored counts) is great confirmation of the coalesced reads and parallel walk/make pipeline. @⁠Garu667 Cleanly rebased onto current `main` (`a20809d`, including `ceb9e1f`) as `8f3d81e`, preserving all of `ceb9e1f`'s changes alongside this PR: - `made_now` / `heap_locks()` lock-free fast path (both pre-copied and direct paths in `mirror_inner`) - `keep_views()` / `same_placed_texture` mip-refinement retention (with `Spans::keys` stale-span unmake intact) - `MIRROR_*` diagnostics telemetry (`MIRROR_HOW` TLS per worker in `mirror_copied_slice`, all outcome counters in `mirror_inner`, `MIRROR_US[0..3]` across `note_buffer_key` + `stale_to_unmake`, view creation, and `keep`, and the `sync_line` breakdown) Re-verified post-rebase: - `cargo run --locked -p xtask -- policy`: `220 files ok` - `cargo clippy -p victory-gpu --all-targets -- -D warnings -A deprecated` (default & `--all-features`): clean - `VICTORY_D3D12_DEBUG=1 cargo test -p victory-gpu -- --test-threads=1`: `220 passed, 0 failed` (default) and `239 passed, 0 failed` (`--features diagnostics`), 0 D3D12 debug errors - `cargo test --release -p victory-gpu --lib sync_heaps_coalesced_parallel_benchmark -- --nocapture`: `2.035 ms/iter` (4 reads/iter) vs `2.494 ms/iter` (8 reads/iter) baseline Ready for A/B testing against `ceb9e1f` / current `main`!
github-bot force-pushed github-bot/gh-1293 from 8f3d81ef66 to d12f002929 2026-11-26 16:46:38 +00:00 Compare
This pull request can be merged automatically.
This branch is out-of-date with the base branch
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/1293/head:github-bot/gh-1293'
git switch 'github-bot/gh-1293'

Merge

Merge the changes and update on Forgejo.
git switch 'main'
git merge --no-ff 'github-bot/gh-1293'
git switch 'github-bot/gh-1293'
git rebase 'main'
git switch 'main'
git merge --ff-only 'github-bot/gh-1293'
git switch 'github-bot/gh-1293'
git rebase 'main'
git switch 'main'
git merge --no-ff 'github-bot/gh-1293'
git switch 'main'
git merge --squash 'github-bot/gh-1293'
git switch 'main'
git merge --ff-only 'github-bot/gh-1293'
git switch 'main'
git merge 'github-bot/gh-1293'
git push origin 'main'
This discussion has been locked. Commenting is limited to contributors.
No description provided.