Skip to content

perf(victory-gpu): avoid O(slots) span rebuilds and hash-order scans in heap_pass - #400

Closed
Lewandox wants to merge 2 commits into
LooperSalty:mainfrom
Lewandox:perf/heap-pass-incremental-spans-and-scan
Closed

Lewandox wants to merge 2 commits into
LooperSalty:mainfrom
Lewandox:perf/heap-pass-incremental-spans-and-scan

Conversation

@Lewandox

Copy link
Copy Markdown
Collaborator

Summary

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):

  1. 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.
  2. In-place Picked::keep() update & remove redundant forget() calls:
    • 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.
  3. Cache-sequential slot scanning & lower hashing overhead:
    • 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.
  4. 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)
  • cargo xtask policy inside third_party/victory

@LooperSalty

Copy link
Copy Markdown
Owner

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:

  1. 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.
  2. Same pattern in orphan (early return when the record is unchanged). Same fix.
  3. 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.

🤖 Generated with Claude Code

@Lewandox

Copy link
Copy Markdown
Collaborator Author

Thanks for the detailed review — all three issues are now fixed:

  1. 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.
  2. orphan fast path likewise calls self.note_entry(slot, entry) before returning when self.orphans.get(&slot) == Some(&record).
  3. 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.

@Lewandox

Copy link
Copy Markdown
Collaborator Author

Closing as obsolete after #409 (\heap_pass.rs\ was removed in the combined base). Keeping the PR queue clean.

@Lewandox Lewandox closed this Nov 26, 2026
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.

2 participants