Skip to content

perf(gpu): re-read a heap sync's stale slots in one kernel call per page - #633

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

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

Conversation

@hackgods

Copy link
Copy Markdown
Contributor

What. When a heap sync remakes a page's stale slots (#439's fast path, HostHeap::mirror_synced_batch), it reads each slot's record again from title memory, under the slot lock, so that it can leave to mirror any slot a title thread rewrote after the page snapshot. Each slot was read with its own ReadProcessMemory call. The slots of a page are now read again together (live_records): one call for each run of stale slots within 64 KiB (in practice one per page), made after the slot lock is taken and before any slot is made. That is the same hold of the lock as before, so a slot rewritten meanwhile is still found and deferred to mirror.

If a run's read fails (a page of it unmapped meanwhile), its slots are read one at a time, as before, and an unreadable slot is skipped, as before. VICTORY_SYNC_REREAD=slot brings back one call per slot, for timing.

With --features diagnostics the copy statistics count the re-read calls (SyncRereads).

Why. Under Wine a kernel copy costs about 0.46 µs whatever its size up to a page. Measured on an M5 Pro in release: 12 bytes 462 ns, 4 KiB 538 ns, 64 KiB 1.8 µs. In the world the sync remakes some 30,000 slots a frame, mostly per-draw descriptor tables that the title copies into its heaps as bytes. The re-reads therefore cost about 14 ms of every frame's preparing, on the submitting thread.

For reference, in the same probe a buffer view made with CreateShaderResourceView costs 0.8–1.1 µs, a CopyDescriptors entry 20–22 ns and a null view 5 ns. What remains of the sync is mostly the views it makes; the new diagnostics line says how much of that a cache of made views could save.

Tests

  • descriptor::host::tests::stale_records_are_read_again_together_as_each_alone: stale slots spread over three 64 KiB pages, the middle page reserved but not committed, one slot rewritten after its snapshot. The batched read and the per-slot read give the same result: the live record, the rewritten record where it changed, and None on the page with nothing mapped. The run that spans the unmapped page falls back to per-slot reads.
  • fast_heap_sync_handles_placed_null_duplicates_forget_and_concurrent_rewrite, unchanged, still covers a rewrite during prepare being left to mirror.
  • bench_sync_slots_1_5m_fast_vs_legacy under Wine (release, 5 interleaved runs each way, ~39,000 stale slots in all): fast path 28.7–270 ms (median 81.5) with a call per slot, 10.6–34.7 ms (median 22.3) with the reads together. The machine was loaded and the spread is wide; the minimums differ by ~18 ms, about 39,000 × 0.46 µs.
  • Branch perf/heap-sync-reread-main (fork, d1609d9 + this one commit, d8573e45): tools/macos/test-wine.sh -p victory-gpu for descriptor::, shadow:: and memory::: 132 passed, 0 failed, 5 ignored. Native cargo test --workspace: all pass except pipeline::schedule::tests::compute_waits_stop_after_pipeline_and_frame_caps, which fails the same way on plain origin/main (not this change). gnullvm clippy --all-targets -D warnings, with and without --features diagnostics: clean. cargo xtask policy: ok.

In game (M5 Pro, macOS 27, release build, last 120 s of each run). Binary: main 7772921 with this PR and the null-page PR. The off arm sets VICTORY_SYNC_REREAD=slot VICTORY_NULL_PAGE=0; the on arm leaves both unset.

arm frame (ms) preparing (ms) copy-back (ms)
both off (1 valid run; the other started with a cold cache) 526 175 28
both on (2 runs) 405 / 392 88 / 78 20

The two changes were measured together. This one's share is expected to be ~13–14 ms of preparing (stale slots × 0.46 µs). A run with only VICTORY_SYNC_REREAD=slot set, with the new diagnostics line, will give its own number.

🤖 Generated with Claude Code

A heap sync re-reads each stale slot's record under the slot lock, to
leave a slot a title thread rewrote meanwhile to `mirror`. Each re-read
was a ReadProcessMemory of its own, about 0.46 us under Wine whatever
its size up to a page (12 bytes 462 ns, 4 KiB 538 ns, 64 KiB 1.8 us on
an M5 Pro), and a world frame re-reads some 30,000 slots.

The stale slots of a page are now read in one call, after the lock is
taken and before any slot is made, as each was read before: under the
same hold. A run with a page that cannot be read falls back to a call
per slot, which gives what it did before (the slot is skipped).
VICTORY_SYNC_REREAD=slot keeps a call per slot, for timing.

Diagnostics builds log a heap sync line: slots looked at, stale, made
again, the time making them and the re-read calls.

bench_sync_slots_1_5m_fast_vs_legacy under Wine (release, 5 interleaved
runs each, ~39,000 stale slots): fast path 28.7-270 ms (median 81.5)
per slot, 10.6-34.7 ms (median 22.3) together.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@hackgods

Copy link
Copy Markdown
Contributor Author

Superseded by #640, which landed with the same change (the stale records of a batch read together under self.made, one slot at a time when that read fails). Closing this one. Thanks for merging #634.

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