⚡️ Speed up function find_last_node by 17,067%#273
Closed
codeflash-ai[bot] wants to merge 1 commit intooptimizefrom
Closed
⚡️ Speed up function find_last_node by 17,067%#273codeflash-ai[bot] wants to merge 1 commit intooptimizefrom
find_last_node by 17,067%#273codeflash-ai[bot] wants to merge 1 commit intooptimizefrom
Conversation
The optimized code achieves a **17,067% speedup** (47.5ms → 277μs) by eliminating a nested iteration anti-pattern that caused O(N×M) time complexity.
**Key optimization:**
The original code uses a nested generator expression that checks `all(e["source"] != n["id"] for e in edges)` for each node. This means for every node in the graph, it iterates through ALL edges to verify none have that node as a source. With N nodes and M edges, this becomes O(N×M) comparisons.
The optimized version precomputes a set of all source IDs once: `sources = {e["source"] for e in edges}`. Then it performs a single O(1) set membership check per node: `if n["id"] not in sources`. This reduces complexity to O(N + M).
**Why this matters:**
- **Set lookups are O(1)** vs linear search through edges which is O(M)
- **Single pass through edges** vs M passes (one per node)
- **No generator overhead** - direct iteration is faster than nested generators with `next()` and `all()`
**Performance characteristics from tests:**
- Small graphs (≤10 nodes): 60-160% faster - modest gains due to overhead of set creation
- Medium graphs (100-500 nodes): 2,900-6,700% faster - optimization dominates
- Large graphs (≥500 nodes): 16,000-38,000% faster - dramatic impact as the O(N×M) penalty compounds
The test results show the optimization is universally beneficial but particularly critical for graphs with hundreds of nodes and edges, which appear to be common in this codebase based on the comprehensive test coverage for large-scale scenarios.
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.
📄 17,067% (170.67x) speedup for
find_last_nodeinsrc/algorithms/graph.py⏱️ Runtime :
47.5 milliseconds→277 microseconds(best of250runs)📝 Explanation and details
The optimized code achieves a 17,067% speedup (47.5ms → 277μs) by eliminating a nested iteration anti-pattern that caused O(N×M) time complexity.
Key optimization:
The original code uses a nested generator expression that checks
all(e["source"] != n["id"] for e in edges)for each node. This means for every node in the graph, it iterates through ALL edges to verify none have that node as a source. With N nodes and M edges, this becomes O(N×M) comparisons.The optimized version precomputes a set of all source IDs once:
sources = {e["source"] for e in edges}. Then it performs a single O(1) set membership check per node:if n["id"] not in sources. This reduces complexity to O(N + M).Why this matters:
next()andall()Performance characteristics from tests:
The test results show the optimization is universally beneficial but particularly critical for graphs with hundreds of nodes and edges, which appear to be common in this codebase based on the comprehensive test coverage for large-scale scenarios.
✅ Correctness verification report:
🌀 Click to see Generated Regression Tests
To edit these changes
git checkout codeflash/optimize-find_last_node-mldx8gg7and push.