⚡️ Speed up function find_last_node by 17,641%#278
Open
codeflash-ai[bot] wants to merge 1 commit intooptimizefrom
Open
⚡️ Speed up function find_last_node by 17,641%#278codeflash-ai[bot] wants to merge 1 commit intooptimizefrom
find_last_node by 17,641%#278codeflash-ai[bot] wants to merge 1 commit intooptimizefrom
Conversation
The optimized code achieves a **176x speedup** (17,641% faster) by eliminating redundant work in the core algorithm. The original implementation had O(N×M) complexity where it checked every edge against every node, resulting in excessive dictionary lookups and comparisons. The optimization reduces this to O(N+M) by preprocessing edges into a set of sources.
**Key optimization:**
The original code uses a nested generator expression that checks `all(e["source"] != n["id"] for e in edges)` for each node, causing each node to scan through the entire edges list. With 1000 nodes and 999 edges, this results in ~1 million edge checks.
The optimized code instead:
1. Builds a set of all source IDs from edges once: `sources = {e["source"] for e in edges}`
2. For each node, performs a single O(1) set membership check: `if n["id"] not in sources`
This transforms the algorithm from checking each (node, edge) pair to a simple linear scan with constant-time lookups.
**Why it's faster:**
- **Set membership is O(1)** vs. O(M) linear edge scanning per node
- **Single edge traversal** instead of N traversals (once per node)
- **Reduced dictionary access overhead**: Each edge's "source" key is accessed once, not N times
**Test results demonstrate:**
- Small graphs (2-4 nodes): 50-95% faster due to reduced overhead
- Large graphs (1000 nodes): 120x-360x faster, proving the algorithmic improvement scales
- The `test_large_scale_single_chain_1000_nodes` shows the most dramatic speedup (36,045% faster) because the original code would scan nearly all 999 edges for each of the 1000 nodes
**Edge case handling:**
The optimization includes special handling for single-pass iterators to preserve original semantics, and checks for empty edge lists to avoid accessing `n["id"]` unnecessarily, maintaining backward compatibility with nodes that may lack an 'id' key when no edges exist.
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,641% (176.41x) speedup for
find_last_nodeinsrc/algorithms/graph.py⏱️ Runtime :
78.8 milliseconds→444 microseconds(best of187runs)📝 Explanation and details
The optimized code achieves a 176x speedup (17,641% faster) by eliminating redundant work in the core algorithm. The original implementation had O(N×M) complexity where it checked every edge against every node, resulting in excessive dictionary lookups and comparisons. The optimization reduces this to O(N+M) by preprocessing edges into a set of sources.
Key optimization:
The original code uses a nested generator expression that checks
all(e["source"] != n["id"] for e in edges)for each node, causing each node to scan through the entire edges list. With 1000 nodes and 999 edges, this results in ~1 million edge checks.The optimized code instead:
sources = {e["source"] for e in edges}if n["id"] not in sourcesThis transforms the algorithm from checking each (node, edge) pair to a simple linear scan with constant-time lookups.
Why it's faster:
Test results demonstrate:
test_large_scale_single_chain_1000_nodesshows the most dramatic speedup (36,045% faster) because the original code would scan nearly all 999 edges for each of the 1000 nodesEdge case handling:
The optimization includes special handling for single-pass iterators to preserve original semantics, and checks for empty edge lists to avoid accessing
n["id"]unnecessarily, maintaining backward compatibility with nodes that may lack an 'id' key when no edges exist.✅ Correctness verification report:
🌀 Click to see Generated Regression Tests
To edit these changes
git checkout codeflash/optimize-find_last_node-mlfrkaieand push.