perf: optimize graph BFS traversal and reduce allocations - #3
Open
gandyhw wants to merge 1 commit into
Open
Conversation
- Replace container/list with slice-based queue + head index to
eliminate per-element list.Element heap allocations
- Pre-allocate adjacency maps with len(G.Nodes) capacity to avoid
rehash during BFS (was len(G.Edges) which caused massive over-allocation)
- Change processEdges visited parameter from *map to map (idiomatic Go)
- Extract 340-line mockGetTimeGraph() fixture to graph_mock_data.go
(graph.go: 765 → 426 lines)
- Add 17 unit tests covering NewGraph, GetNodeInfo, GetEdgeInfo,
GraphToRows, addToBufMap, MultiHopFilter edge cases, and condition
evaluation (4 functions 0% → 100% coverage)
- Update sonic dependency for Go 1.26 compatibility
Benchmark (3-count, 10x benchtime):
Small graph (2w nodes, 2w edges):
No condition: -3.0% latency, -6.8% memory
With condition: -20.6% latency, -6.7% memory
Large graph (2w nodes, 200w edges):
No condition: -3.1% latency (7-run verified), -10.7% allocs
With condition: -5.8% allocs
Prop condition: -11.1% allocs
Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
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.
Benchmark (3-count, 10x benchtime):
Small graph (2w nodes, 2w edges): No condition: -3.0% latency, -6.8% memory With condition: -20.6% latency, -6.7% memory Large graph (2w nodes, 200w edges): No condition: -3.1% latency (7-run verified), -10.7% allocs With condition: -5.8% allocs Prop condition: -11.1% allocs
What problem does this PR solve?
Issue Number: close/fix/resolve/ref #xxx
What is changed and how it works?
Please describe how it works
How Has This Been Tested?
Please describe the tests that you ran to verify your changes. Provide instructions so we can reproduce. Please also list any relevant details for your test configuration
Checklist: