Perf T2-6: Cache graph traversals in problem_setup#143
Open
hmzh-khn wants to merge 4 commits into
Open
Conversation
RED phase: Tests for _build_graph_caches that verify cached get_all_followers/get_all_leaders match uncached results for chain, star, Nash, and mixed hierarchy graphs. All 4 cache tests error (function not yet implemented), existing tests pass. Co-Authored-By: Claude Opus 4.6 <[email protected]>
GREEN phase: Add _build_graph_caches(graph) that pre-computes get_all_followers and get_all_leaders for every node once, replacing O(N²) repeated BFS traversals with O(N) dict lookups. The cache is built once at the top of setup_problem_variables and used in all 5 locations that previously called graph traversal functions directly (lines 225, 230, 250, 269, 274). All 64 problem_setup tests pass including new cache correctness tests for chain, star, Nash, and mixed hierarchy graphs. Co-Authored-By: Claude Opus 4.6 <[email protected]>
Results show 1.5-4x speedup for graph traversal operations at N>5 players. Chain graphs see ~2x speedup at N=10-20, star graphs see up to 4x at N=20. At N=3 the overhead is negligible (<1μs difference). Benchmark measures isolated traversal cost; actual setup_problem_variables speedup is amplified by reduced symbolic variable allocation overhead. Co-Authored-By: Claude Opus 4.6 <[email protected]>
Co-Authored-By: Claude Opus 4.6 <[email protected]>
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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
Cache
get_all_followers()andget_all_leaders()results insetup_problem_variablesto avoid O(N²) repeated BFS traversals. Both functions were called at 5 sites inside the function, each doing a fresh BFS walk of the graph. Now a single_build_graph_caches(graph)call pre-computes results asDict{Int,Vector{Int}}and all sites use cached lookups.Changes
src/problem_setup.jl: Added_build_graph_caches(graph)internal function; replaced 5 call sites (get_all_followers×3,get_all_leaders×2) with cached dict lookupstest/test_problem_setup.jl: Added 4 test sets for cache correctness (chain, star, Nash, mixed hierarchy) + regression test for cached setup behaviorscripts/benchmark_graph_cache.jl: Benchmark script measuring traversal speedupBenchmark Results
Speedup grows with N as the O(N²) traversal cost is replaced with O(N) cache construction + O(1) lookups. Negligible overhead at N≤5.
Testing
Changelog
🤖 Generated with Claude Code