From 58b1cf7f49ec4fb4f5fd5939b1d2e276308c592d Mon Sep 17 00:00:00 2001 From: Nikola Milenkovic Date: Fri, 27 Nov 2026 23:30:15 +0100 Subject: [PATCH] perf(gpu): read a heap sync batch's stale slots once a page, not once a slot mirror_synced_batch re-read every stale slot's record under self.made with its own ReadProcessMemory call, to catch a title thread that rewrote the slot. Under Wine each call is a kernel call of ~0.5 us: 39,100 of them took 18-21 ms alone on an M5 Pro, ~70 % of the batch, and made the default fast heap sync slower than the legacy path in bench_sync_slots_1_5m_fast_vs_legacy (0.83-0.90x). The span of a batch's stale slots (a page's at most) is now read once under the same lock and each slot compared in that copy; a span that cannot be read whole falls back to a read per slot, as before. The race check is unchanged: the read is still under self.made, after prepare, and a later write is caught by the next sync as before. Benchmark (M5 Pro, CrossOver Wine + D3DMetal, release, 3 runs): fast path 27-29 ms -> 10-13 ms for 1.5M slots scanned and ~39,100 made again, 2.5-2.7x the legacy path. Not measured in the game. Co-Authored-By: Claude Opus 5.5 --- crates/victory-gpu/src/descriptor/host.rs | 22 +++++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/crates/victory-gpu/src/descriptor/host.rs b/crates/victory-gpu/src/descriptor/host.rs index 7097f20cd..02020dda2 100644 --- a/crates/victory-gpu/src/descriptor/host.rs +++ b/crates/victory-gpu/src/descriptor/host.rs @@ -21,6 +21,9 @@ use crate::backend::{HostGpu, HostMemory}; use crate::host::host; const SYNC_BLOCK_SLOTS: u32 = 128; +/// The widest span of stale slots a heap sync's batch reads in one call +/// (`mirror_synced_batch`); its callers pass a page's slots (64 KiB). +const SPAN_READ_MAX: u64 = 1 << 20; thread_local! { /// True while this thread is inside [`HostHeap::publish`], preventing @@ -1789,6 +1792,20 @@ impl HostHeap { .map(|&(_, data, _)| data); let addr_slots = self.address_slots(); + // The live records of every stale slot, read once under + // `self.made` (one `ReadProcessMemory` for the span, a page's at + // most): read a slot at a time it was a call per slot, ~0.5 us + // each under Wine, most of the batch's time for ~39,000 slots. + // A span that cannot be read whole is read a slot at a time. + let span = stale + .iter() + .map(|&(i, _)| at(i)) + .fold((u64::MAX, 0u64), |(lo, hi), a| (lo.min(a), hi.max(a + 12))); + let mut live_span: Vec = Vec::new(); + let span_read = !stale.is_empty() && span.1 - span.0 <= SPAN_READ_MAX && { + live_span.resize((span.1 - span.0) as usize, 0); + crate::host::read_into(span.0 as *const u8, &mut live_span) + }; for (entry_idx, &(index, snapshot)) in stale.iter().enumerate() { let addr = at(index); // Re-read live title memory under `self.made`: if a title @@ -1796,7 +1813,10 @@ impl HostHeap { // during `prepare`), defer to `mirror` after dropping locks. // SAFETY: the caller holds the heap alive until the sync completes. let mut raw = [0u8; 12]; - if !crate::host::read_into(addr as *const u8, &mut raw) { + if span_read { + let off = (addr - span.0) as usize; + raw.copy_from_slice(&live_span[off..off + 12]); + } else if !crate::host::read_into(addr as *const u8, &mut raw) { continue; } let live = [