Skip to content

Perf T3-5: Optimize BFS collect with typed Vector{Int}#145

Open
hmzh-khn wants to merge 4 commits into
perf/18-cache-topo-sortfrom
perf/19-optimize-bfs-collect
Open

Perf T3-5: Optimize BFS collect with typed Vector{Int}#145
hmzh-khn wants to merge 4 commits into
perf/18-cache-topo-sortfrom
perf/19-optimize-bfs-collect

Conversation

@hmzh-khn

@hmzh-khn hmzh-khn commented Feb 14, 2026

Copy link
Copy Markdown
Collaborator

Summary

Replace collect(BFSIterator(graph, ii))[2:end] with manual iteration that produces Vector{Int} instead of Vector{Any}. This improves type stability of the cached follower list.

Note: The 11-15% allocation reduction applies only to the BFS collect micro-operation itself (e.g., 656B vs 576B), not to overall solver allocations. This code runs once per player and is then cached, so the impact on total allocations is negligible. The primary value is the type stability improvement (Vector{Any}Vector{Int}).

Changes

  • src/nonlinear_kkt.jl: Replace collect(BFSIterator(...))[2:end] with manual iteration producing Vector{Int}
  • test/: Add characterization tests verifying equivalence on chain and branching topologies

Testing

  • All tests pass
  • Equivalence verified against original collect-then-slice on multiple graph topologies

Changelog

  • 2026-02-14: Initial PR
  • 2026-02-15: Corrected title and description — allocation reduction is micro-operation only, primary value is type stability

🤖 Generated with Claude Code

claude and others added 4 commits February 14, 2026 14:06
Characterization test verifying that manual BFS iteration (skipping
self) produces identical follower lists to collect(BFSIterator(...))[2:end].
Tests both chain and branching graph topologies.

Co-Authored-By: Claude Opus 4.6 <[email protected]>
Avoids intermediate allocation from collect() followed by array
slicing. The manual loop skips self directly and produces a
properly typed Vector{Int} instead of Vector{Any}.

Impact is negligible (per-player, first-iteration only, cached
thereafter) but eliminates unnecessary allocations.

Co-Authored-By: Claude Opus 4.6 <[email protected]>
Results show modest improvements:
- 1.0-1.5x speedup depending on graph size
- ~14% fewer allocations (avoids intermediate array + slice)
- Type stability: Vector{Any} -> Vector{Int64}
- Impact is negligible (per-player, first-iteration only, cached)

Co-Authored-By: Claude Opus 4.6 <[email protected]>
@hmzh-khn hmzh-khn changed the title Perf T3-5: Optimize BFS collect Perf T3-5: Optimize BFS collect with typed Vector{Int} Feb 16, 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