Skip to content

perf(gpu): lock-free view generations, synced slot cache, and batched CopyDescriptors heap sync - #439

Open
gdemontalivet wants to merge 1 commit into
pragyaangaur:mainfrom
gdemontalivet:perf/macos-fast-heap-slots
Open

gdemontalivet wants to merge 1 commit into
pragyaangaur:mainfrom
gdemontalivet:perf/macos-fast-heap-slots

Conversation

@gdemontalivet

Copy link
Copy Markdown
Contributor

Summary

  • Adds a lock-free 2-level VIEW_GENS generation table (descriptor.rs), lock-free HostHeap::synced cache covering placed-texture-left-null & evicted slots, per-128-slot-block block_forgot checks, and a thread-local SYNC_PAGE_BUF.
  • Adds mirror_synced_batch() (VICTORY_FAST_HEAP_SYNC, default ON, 0 opts out for A/B testing) using a 256-slot CPU-only StagingHeap pre-populated with null descriptors plus per-batch template slots flushed via one CopyDescriptors call per page, and batches keep_batch, Viewed::set_batch, shadow::forget_reads, and host::retire_all.

Measured M5 Max (CrossOver / D3DMetal) Impact

  • Synthetic 1.5M-slot D3DMetal benchmark (bench_sync_slots_1_5m_fast_vs_legacy): 18.05 ms → 7.58 ms (2.38× faster uncontended).
  • Live M5 Max warm prologue (wave5-sync-all vs wave3-heap-bake): heap slots the title copied dropped from 63.8–67.6 ms/frame → 18.8 ms/frame (-71% / 3.5× faster)!

Verification

  • cargo xtask policy: pass
  • cargo test -p victory-gpu: 102 passed
  • Wine + D3DMetal (descriptor::): 41/41 passed (including fast_heap_sync_handles_placed_null_duplicates_forget_and_concurrent_rewrite and bench_sync_slots_1_5m_fast_vs_legacy)
  • cargo clippy --target x86_64-pc-windows-gnullvm --workspace --all-targets -- -D warnings (default & --features diagnostics): 0 warnings

…irroring

Cut submission-time descriptor heap sync overhead (`sync_heaps` /
`sync_slots_bytes`) across large dirty page ranges and repeated records:

- Add a lock-free 2-level generation mirror (`VIEW_GENS`) and batched
  `views_at_batch()` in `descriptor.rs`, plus a thread-local reusable page
  read buffer in `sync_heaps()`.
- Add a lock-free `synced` mirror of `made` on `HostHeap` (covering placed
  textures left null and evicted slots whose `ready` entry is 0), and
  scan `sync_slots_bytes` per 128-slot block and 64-slot `made_nonzero`
  word with zero lock acquisitions or heap allocations on clean slots.
- Add `mirror_synced_batch()` with a lazy CPU-only staging heap
  (`D3D12_DESCRIPTOR_HEAP_FLAG_NONE`) pre-populated with all 17 null
  SRV/UAV/sampler kinds plus per-batch template slots for repeated keys,
  flushing descriptor copies in a single `CopyDescriptors` call per page
  and batching `made`, `kept` (`keep_batch` with same-resource fast path),
  `viewed` (`Viewed::set_batch` with pointer-keyed `Seen` dedup),
  `shadow::forget_reads`, and `host::retire_all` under one lock hold each.
- Gate the optimized path behind `VICTORY_FAST_HEAP_SYNC` (default on;
  set `=0` for legacy A/B comparison).
- Add unit tests covering `view_gen_current`, `views_at_batch`, placed-null
  lock-free skips, duplicate template copies, immediate `forget()`
  invalidation, concurrent rewrites during `prepare()`, and a synthetic
  1.5M-slot fast-vs-legacy benchmark.
@patcliftongc

Copy link
Copy Markdown
Contributor

M2 Max measurements on f80b128, macOS 27.0.1, x86-64 Wine through Rosetta with D3DMetal 4.0 beta 3. I built the optimized diagnostic GPU library from source and ran the existing 1,507,328-slot fixture in five fresh processes. I then reversed just the two timed loop blocks and repeated five times, keeping the same data construction, warm-up and assertions.

Timing order Median legacy Median fast Paired speedup range
Legacy first 34.14 ms 10.79 ms 2.99–3.44×
Fast first 27.93 ms 13.33 ms 2.05–2.19×

Both orders agreed on the direction. The order affects the magnitude, so these figures apply to this synthetic workload. All 41 descriptor tests passed on the original revision, including the concurrent-rewrite and invalidation checks. The CPU/GPU shared-buffer coherence test also passed separately. This is no measurement of whole-game FPS or memory footprint.

@mikebuckets171

mikebuckets171 commented Nov 26, 2026 •

Copy link
Copy Markdown
Contributor

M5 Pro (48 GB), Rosetta + CrossOver/Wine 11.0, D3DMetal 4.0 beta 3: an encouraging same-executable VICTORY_FAST_HEAP_SYNC=0/1 world comparison of f80b128.

The tested integration is retained in Michael's fork, head 2443b68a54db0ec5607c1b13f7fde00a617eca3f: this PR added to the six-fix control at 8a53785 on bf99e728. It excludes #409. Both launches used the same retained profile, 720p output, diagnostics and fault tracking, with the same executable SHA-256 1499c15b4a373ef95a109369f0342c52305755caeab03063265d3cbcc5dd5082. The complete recorded launch configuration differs only in the fast-heap toggle; the mapped D3DMetal runtime was checked.

At the initial tutorial-room pose, each launch settled for 20 seconds and then remained stationary for 120 seconds with no manual input. These are medians of paired diagnostic interval averages, not individual-frame percentiles.

Metric Off On
Reports 21 19
Frame time 551.3 ms 396.5 ms
Preparation 287.4 ms 179.1 ms
Heap synchronization 169.7 ms 67.1 ms
GPU time 233.9 ms 194.2 ms
Copy-back 27.2 ms 20.3 ms
Guard wait 67.0 ms 73.2 ms
Draws/frame 5,029 4,905

In this pair, heap synchronization decreased 60.5% and total frame time 28.1%. This is one sequential off/on pair: idle animation and draw counts differ, and GPU time also moved. It supports the fast path on this workload, but does not establish a general speedup or attribute the entire frame difference to heap synchronization. The off build includes the PR's other infrastructure.

After the enabled measurement I verified movement away from the starting wall, stopping on release, independent camera, more than seven unpaused world minutes, pause/resume and camera response afterwards. Both runs ended intentionally and recovered; neither logged an unhandled title exception or Metal completion error. Same-setting relaunch of the enabled variant remains unqualified. Observed world performance remains roughly 2–3 FPS in this room, with darker/washed-out surfaces and existing menu clipping; this is not comfortable playability.

Integration gates: native 598 passed / 0 failed / 17 ignored; serial Wine 1,164 passed / 0 failed / 29 ignored, no SKIPPED lines. Native Clippy, strict Windows Clippy default/all-features and policy passed. Formatting still fails on the same 81 paths as the unmodified base; no fully green formatting gate is claimed.

Prepared and tested by GPT-6 Astra on Michael's Mac. Game artifacts and raw logs remain local.

@earpjonathan

Copy link
Copy Markdown
Contributor

In-game check on an M5 Pro (macOS 27.0.1, CrossOver 26.3 Wine with the repo's current patches, D3DMetal 4.0 beta 3, Series X), with #439 and #419 together on main at bf99e72 plus #401, #435 and #433. Same opening and scripted input in each run, into the first area you control.

  • Median time preparing submissions per frame: 292 ms without these two, 198 and 196 ms in two runs with them.
  • Median frame time went from 687 ms to 541 and 646 ms. Run-to-run spread is about 100 ms, so the CPU-side number is the one to trust.
  • The picture looked the same as without them, with no device loss and no frames over 10 s.

I didn't test them separately, so I can't say how the gain splits between them.

Prepared with Claude Code.

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.

4 participants