You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
perf(victory-gpu): avoid O(slots) span rebuilds and hash-order scans in heap_pass - #400
Optimizes the view-heap buffer pass (third_party/victory/crates/victory-gpu/src/heap_pass.rs and draw.rs::heap_buffers), which docs/research/stutter-2026-11-26.md identifies as the largest single frame-spike category (~43% of lost time across 164k–227k descriptor slots):
Avoid O(slots) per-frame HashMap rebuild in Picked::spans() on the default path:
Picked::keep and Picked::forget already maintain per-copy spans and stale flags identically for both full = true (default) and full = false (VICTORY_HEAP_PASS_DIRTY=1).
Previously, Picked::spans() discarded that incremental state when self.full == true and allocated a fresh SipHash HashMap scanning all ~227k slots every pass. Using the already-maintained self.copies reduces spans() from O(slots) to O(copies) with identical results and zero temporary HashMap allocation.
Previously, draw.rs::heap_buffers called ranges.forget(index) before every slot and Picked::keep() called self.forget(slot) a second time — doing redundant map removals on full sweeps and marking copy.stale = true (forcing full re-scans of every slot belonging to that host copy) even when a re-checked slot still referenced the same copy with an unchanged or wider span.
Picked::keep() now updates slots in place when the host copy key is unchanged, marking copy.stale = true only when a slot actually shrinks off a copy boundary, and draw.rs calls ranges.forget(index) only on early-exit paths where the slot no longer maps to a buffer view.
Stores slots and orphans in BTreeMap so Picked::plan() scans title descriptor heap memory in ascending slot order (sequential prefetching instead of random hash-bucket order) and accepts sorted full-sweep slot lists (HostHeap::buffer_slots) without hash-table rehashing.
Replaces temporary HashSet<u32> allocation in Picked::plan() with in-place Vec<u32> + sort_unstable + dedup.
Uses a lightweight deterministic integer hasher (IntBuildHasher) for copies, Copy::slots, and by_entry instead of SipHash-1-3 on u32/usize keys.
Unit tests:
Verifies full = true and full = false produce identical spans across adds, in-place updates, expansions, boundary shrinks, copy switches, and removals.
Verifies in-place same-copy updates do not needlessly mark copy.stale, while boundary shrinks do and recompute exact spans.
Verifies plan() deduplicates slots appearing via changed, forgotten, and record-change scans in ascending order.
Verification
cargo test -p victory-gpu --lib heap_pass::tests (default and --features diagnostics)
Thanks for this one. Holding it for now: it changes the default path (spans() now reads copies instead of rebuilding from slots), and the review found three issues:
heap_pass.rs, new fast path in keep: note_entry only runs when old_record != record. plan() removes each forgotten entry from by_entry; when the slot is seen again with the same record it is not registered again, so the next release of that entry is missed. This regresses VICTORY_HEAP_PASS_DIRTY=1, where the old code always re-registered. Fix: always call note_entry.
Same pattern in orphan (early return when the record is unchanged). Same fix.
IntHasher::write_usize is (h ^ i) * C. The keys of copies are Arc addresses, aligned, so their low bits are zero and so are the hash's low bits; hashbrown indexes with those bits, so ~4,000 copies collide heavily. Mix the high bits in (rotate_left, or a fold of h >> 32).
slots as a BTreeMap also makes keep/forget O(log n) over ~227,000 slots: a frame-time measurement before and after would help. We are measuring it in game on our side too.
Thanks for the detailed review — all three issues are now fixed:
keep fast path now unconditionally calls self.note_entry(slot, entry) even when old_record == record, so entries drained by plan() are always re-registered under VICTORY_HEAP_PASS_DIRTY=1.
orphan fast path likewise calls self.note_entry(slot, entry) before returning when self.orphans.get(&slot) == Some(&record).
IntHasher now applies a full 64-bit avalanche mix (mix64 in finish(), plus rotate_left(5) on writes) so aligned Arc pointers distribute uniformly across both low bucket bits and top H2 tag bits in hashbrown.
Added regression tests for both (forgotten_entry_re_registers_on_repeated_keep_and_orphan_with_same_record and int_hasher_avalanche_mixes_aligned_pointer_low_bits). Verified with cargo test -p victory-gpu --lib heap_pass::tests (default and --features diagnostics) + cargo xtask policy.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Optimizes the view-heap buffer pass (
third_party/victory/crates/victory-gpu/src/heap_pass.rsanddraw.rs::heap_buffers), whichdocs/research/stutter-2026-11-26.mdidentifies as the largest single frame-spike category (~43% of lost time across 164k–227k descriptor slots):HashMaprebuild inPicked::spans()on the default path:Picked::keepandPicked::forgetalready maintain per-copy spans andstaleflags identically for bothfull = true(default) andfull = false(VICTORY_HEAP_PASS_DIRTY=1).Picked::spans()discarded that incremental state whenself.full == trueand allocated a fresh SipHashHashMapscanning all ~227k slots every pass. Using the already-maintainedself.copiesreducesspans()fromO(slots)toO(copies)with identical results and zero temporaryHashMapallocation.Picked::keep()update & remove redundantforget()calls:draw.rs::heap_bufferscalledranges.forget(index)before every slot andPicked::keep()calledself.forget(slot)a second time — doing redundant map removals on full sweeps and markingcopy.stale = true(forcing full re-scans of every slot belonging to that host copy) even when a re-checked slot still referenced the same copy with an unchanged or wider span.Picked::keep()now updates slots in place when the host copy key is unchanged, markingcopy.stale = trueonly when a slot actually shrinks off a copy boundary, anddraw.rscallsranges.forget(index)only on early-exit paths where the slot no longer maps to a buffer view.slotsandorphansinBTreeMapsoPicked::plan()scans title descriptor heap memory in ascending slot order (sequential prefetching instead of random hash-bucket order) and accepts sorted full-sweep slot lists (HostHeap::buffer_slots) without hash-table rehashing.HashSet<u32>allocation inPicked::plan()with in-placeVec<u32>+sort_unstable+dedup.IntBuildHasher) forcopies,Copy::slots, andby_entryinstead of SipHash-1-3 onu32/usizekeys.full = trueandfull = falseproduce identical spans across adds, in-place updates, expansions, boundary shrinks, copy switches, and removals.copy.stale, while boundary shrinks do and recompute exact spans.plan()deduplicates slots appearing viachanged,forgotten, and record-change scans in ascending order.Verification
cargo test -p victory-gpu --lib heap_pass::tests(default and--features diagnostics)cargo xtask policyinsidethird_party/victory