Skip to content

perf(gpu): make a heap sync's changed slots under one hold of the slot lock - #409

Draft
hackgods wants to merge 1 commit into
pragyaangaur:mainfrom
hackgods:perf/heap-sync
Draft

hackgods wants to merge 1 commit into
pragyaangaur:mainfrom
hackgods:perf/heap-sync

Conversation

@hackgods

Copy link
Copy Markdown
Contributor

The heap sync was the largest single CPU cost of a world frame: 217 ms of a 557 ms frame on an M5 Pro, release build. This makes it about half as costly without changing what it does to any slot.

What it was spending the time on

Before every submission (about 21 a frame) the heap sync makes again the slots whose records the title copied into its shader-visible heaps as bytes. New diagnostics counters (below) split it per frame in the prologue world:

Slots per frame (about 375,000 on about 180 pages) Count Time
Already made from their record, passed by ~220,000 ~0
Holding no view ~125,000 ~0
Checked under the locks ~27,000 6–24 ms
Made again ~39,000 240–323 ms
Pages read; waiting for the heaps' lock; releases that forgot views 1 ms; 0; 0

Of the ~39,000 made again, about 22,000 are left null and the rest are views of only about 1,700 distinct records. Making the views themselves (prepare plus the host view) took under 20 ms. About half the time was spent waiting for the heap's slot lock (made). The heap sync took it once a slot, ~39,000 times a frame, while the title's threads recording draws take it to make their table slots.

What changed

  • HostHeap::mirror_synced does for each slot exactly what mirror does on the heap-sync path (for_draw == false), but takes the slot lock once for a page's slots instead of once a slot:
    • What each view needs is prepared first, without the lock, as mirror does.
    • A slot the title wrote again meanwhile is left to mirror.
    • Whether a view is still current is asked under the lock, as mirror asks with view_at.
    • Views are unpinned after the lock, whose holders their release may need.
  • mirror's null path and its making of a slot move into unmake_locked and make_locked, which both paths use, so the per-slot logic exists once.
  • Diagnostics builds log a heap sync per frame: line with the split above (SyncPages, SyncFast, SyncNoView, SyncChecked, SyncUnverified, SyncMade, their nanosecond times, ViewsForgot). watch::TimedNs times in nanoseconds, because these spans are well under a microsecond each and add up.

Measured in the game

M5 Pro, 64 GB, macOS 27.2.1, CrossOver 26.3 Wine, D3DMetal 4.0 beta 3, Series X. Every run used the same save (the prologue) and key script, with VICTORY_PATCH_MASKED=all VICTORY_FAULT_TRACKING=1 VICTORY_NO_HANG_WATCHDOG=1 VICTORY_STEP_OVER=pool and no GPU timing. Numbers are medians of the 5 s renderer reports with 500+ draws, from one minute after the world appears.

One diagnostics binary, with a temporary switch to turn the batching on and off, run interleaved (off, on, on, off):

Off (runs 1 and 6) On (runs 2 and 5)
Heap sync 297 / 231 ms 92 / 145 ms
Slots made again 39,864 / 40,872 in 286 / 221 ms 40,035 / 40,160 in 65 / 102 ms
Preparing 428 / 368 ms 212 / 335 ms
Frame 750 / 668 ms 462 / 639 ms

Release builds of main and of this branch, interleaved (main, branch, branch, main), 6 min each:

Release build, 6 min Frame Preparing GPU
main bf99e72, run 1 582 ms 312 ms 239 ms
This branch, run 2 585 ms 292 ms 242 ms
This branch, run 3 445 ms 185 ms 227 ms
main bf99e72, run 4 749 ms (p90 2,061) 452 ms 264 ms

Frame times vary a lot from run to run on this Mac: run 2 matches run 1, and run 4 had a slow phase. The release build has no heap-sync counter, so these frame times mix in every other cost. In an earlier release pair timed around sync_heaps (one run each), the heap sync went from 217 ms (204–224) to 126 ms (87–142) a frame.

The picture is unchanged. No crashes, ended submissions, queue moves or Metal errors occurred in any run.

How tested

  • Before and after on main bf99e72: cargo test --workspace 555 passed, 0 failed both times. tools/macos/test-wine.sh --workspace --no-fail-fast 1,124 → 1,125 passed, 0 failed (the new test).
  • New test: a_slot_rewritten_while_a_heap_sync_prepares_is_left_as_written_last. The title clears a slot while the sync prepares its view, through the existing WHILE_PREPARING hook; the slot must stay as the title left it, then be made once copied again. heap_syncs_make_the_slots_whose_records_changed, a_table_slot_rewritten_before_submission_refreshes_its_new_view and the bindless tests pass unchanged.
  • cargo clippy -p victory-gpu --all-targets --target x86_64-pc-windows-gnullvm -- -D warnings, with and without --features diagnostics, and cargo xtask policy pass. Only changed lines are formatted.

Tried and left out

  • A lock-free copy of made, so the heap sync passes by slots already made from their record. These are texture views left null and placed textures, whose ready is 0. It cut the check step from 21–30 ms to 7.5–10 ms. The total was not better in either interleaved pair, though, and it adds a rule every writer of made would have to keep, so it is not in this PR.
  • Leaving slots that no submitted draw reads to the draws. This is what avatarmanz/VIctory-7900XTX does. Here 12 of about 21 submissions a frame have heap-indexing shaders, so an exact version would save little. The fork's version gives up exactness: a slot picked by index shows its old view for up to four frames.

Notes

  • The lock is held for one page's slots, up to about 2,000. Chunking it would bound how long a title thread recording a draw can wait. That is possible tuning, but untested, so it is not done here.
  • This is item 1 of the CPU list from today's measurements. Item 2 is memory tracking: about 150 ms a frame in watches, the guard lock and the fault handler.

🤖 Generated with Claude Code

…t lock

Before every submission the heap sync makes again the slots whose records
the title copied into its shader-visible heaps as bytes: some 40,000 a
frame in the prologue's world (about 22,000 left null, the rest views of
only ~1,700 distinct records). Each went through HostHeap::mirror, which
takes the heap's slot lock (`made`) once a slot, while the title's threads
recording draws make their table slots under the same lock. On an M5 Pro
that cost 217 ms of a 557 ms frame in a release build, about half of it
waiting for the lock.

HostHeap::mirror_synced does for each slot exactly what mirror does on the
heap-sync path, but with the lock taken once for a page's slots: what each
view needs is prepared first without the lock, as mirror does; a slot the
title wrote again meanwhile is left to mirror; whether a view is still
current is asked under the lock, as mirror asks; views are unpinned after
the lock. mirror's null path and its making of a slot move into
unmake_locked and make_locked, which both use.

Measured in the game (M5 Pro, D3DMetal 4.0 beta 3, same save and key
script), interleaved runs of one diagnostics binary with the batching on
and off: heap sync 297/231 -> 92/145 ms a frame, preparing 428/368 ->
212/335 ms, frame 750/668 -> 462/639 ms.

Test: a_slot_rewritten_while_a_heap_sync_prepares_is_left_as_written_last.

Diagnostics builds now also log where a heap sync's time goes, per
frame: the pages a heap sync read and of their slots those passed by as
made from their record, those holding no view, those checked under the
locks (and of them those in blocks not verified since views were last
forgotten) and those made again, with the time spent checking, making,
reading pages and waiting for the heaps' lock, and the title's releases
that forgot views. These counters showed that the slots made again, not
the checks or the forgotten views, held the time. watch::TimedNs times
spans in nanoseconds, since these add up from many of well under a
microsecond.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@hackgods
hackgods marked this pull request as draft November 26, 2026 19:04
@hackgods

Copy link
Copy Markdown
Contributor Author

Moving this back to draft. Taking the lock once per page halves the heap sync, but about 120 ms a frame is still left: the same ~39,000 slots are still made again every frame (about 22,000 left null, the rest copies of only ~1,700 distinct views), plus their per-slot bookkeeping. I'm working on that in this PR and will mark it ready with new in-game measurements once the remaining cost is gone or shown to be irreducible.

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.

1 participant