⚡️ Speed up function find_leaf_nodes by 16,375%#283
Open
codeflash-ai[bot] wants to merge 1 commit intopython-onlyfrom
Open
⚡️ Speed up function find_leaf_nodes by 16,375%#283codeflash-ai[bot] wants to merge 1 commit intopython-onlyfrom
find_leaf_nodes by 16,375%#283codeflash-ai[bot] wants to merge 1 commit intopython-onlyfrom
Conversation
The optimized code achieves a **164x speedup** (from 39.1ms to 237μs) by eliminating an expensive nested loop pattern that scaled poorly with graph size.
## Key Optimizations
**1. Algorithmic Improvement: O(N×M) → O(N+M)**
The original code used a nested loop where for each node, it iterated through all edges to check for outgoing connections. This resulted in O(N×M) complexity where N is the number of nodes and M is the number of edges.
The optimized version builds a set of all source node IDs once (`sources = {edge["source"] for edge in edges}`), then performs O(1) membership checks for each node. This reduces complexity to O(N+M), providing dramatic speedups especially as graph size increases.
**2. Fast Path for Empty Edges**
When there are no edges, all nodes are leaves. The optimization immediately returns a shallow copy of the nodes list, avoiding unnecessary iteration.
## Why This Works
From the line profiler data, the original code spent:
- **45.4%** of time iterating through edges (367.6ms)
- **53.7%** of time checking `edge["source"] == node["id"]` (434.7ms)
These nested dictionary lookups happened 1.28 million times across all function calls. The optimized version performs dictionary lookups only during set construction, then uses fast hash-based membership checks.
## Performance Characteristics
The test results show the optimization scales exceptionally well:
- **Small graphs** (3-10 nodes): 16-62% faster
- **Medium graphs** (100 nodes): 2,600-5,900% faster
- **Large graphs** (1000 nodes): 24,700-32,000% faster
The speedup increases with graph density. For example:
- Linear chain (1000 nodes, 999 edges): **320x faster** (15.1ms → 46.9μs)
- Dense graph (200 nodes, 600 edges): **96x faster** (1.89ms → 19.5μs)
- 500 nodes with single source: **234x faster** (7.66ms → 32.7μs)
This optimization is particularly valuable for any workload processing moderately-sized graphs, transforming what was a millisecond-scale operation into microsecond-scale, enabling much higher throughput for graph analysis pipelines.
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.
📄 16,375% (163.75x) speedup for
find_leaf_nodesinsrc/algorithms/graph.py⏱️ Runtime :
39.1 milliseconds→237 microseconds(best of182runs)📝 Explanation and details
The optimized code achieves a 164x speedup (from 39.1ms to 237μs) by eliminating an expensive nested loop pattern that scaled poorly with graph size.
Key Optimizations
1. Algorithmic Improvement: O(N×M) → O(N+M)
The original code used a nested loop where for each node, it iterated through all edges to check for outgoing connections. This resulted in O(N×M) complexity where N is the number of nodes and M is the number of edges.
The optimized version builds a set of all source node IDs once (
sources = {edge["source"] for edge in edges}), then performs O(1) membership checks for each node. This reduces complexity to O(N+M), providing dramatic speedups especially as graph size increases.2. Fast Path for Empty Edges
When there are no edges, all nodes are leaves. The optimization immediately returns a shallow copy of the nodes list, avoiding unnecessary iteration.
Why This Works
From the line profiler data, the original code spent:
edge["source"] == node["id"](434.7ms)These nested dictionary lookups happened 1.28 million times across all function calls. The optimized version performs dictionary lookups only during set construction, then uses fast hash-based membership checks.
Performance Characteristics
The test results show the optimization scales exceptionally well:
The speedup increases with graph density. For example:
This optimization is particularly valuable for any workload processing moderately-sized graphs, transforming what was a millisecond-scale operation into microsecond-scale, enabling much higher throughput for graph analysis pipelines.
✅ Correctness verification report:
🌀 Click to see Generated Regression Tests
To edit these changes
git checkout codeflash/optimize-find_leaf_nodes-mlulvjd2and push.