⚡️ Speed up function find_last_node by 10,019%#272
Closed
codeflash-ai[bot] wants to merge 1 commit intooptimizefrom
Closed
⚡️ Speed up function find_last_node by 10,019%#272codeflash-ai[bot] wants to merge 1 commit intooptimizefrom
find_last_node by 10,019%#272codeflash-ai[bot] wants to merge 1 commit intooptimizefrom
Conversation
The optimized code achieves a **100x speedup** (10018% faster, from 24.8ms to 245μs) by replacing the nested `all()` comprehension with a set-based membership test.
**Key Optimization:**
The original code uses:
```python
next((n for n in nodes if all(e["source"] != n["id"] for e in edges)), None)
```
This creates **O(N × M)** complexity where N is the number of nodes and M is the number of edges. For each node, it must iterate through ALL edges to verify none match.
The optimized code:
1. **Pre-computes sources** into a set: `sources = {e["source"] for e in edges}`
2. **Uses O(1) membership testing**: `if n["id"] not in sources`
This reduces complexity to **O(N + M)** - one pass to build the set, one pass to check nodes.
**Performance Impact by Test Case:**
- **Large-scale tests show dramatic gains**: The 500-node chain test improves from 4.85ms to 55.8μs (8581% faster), and the 999-node test from 17.9ms to 48.8μs (36528% faster)
- **Small graphs also benefit**: Even simple cases like 3-node chains see 79.9-91.7% improvements
- **Edge case handling**: Empty edges are handled efficiently with early return
- **Only regression**: Very long node IDs (10k characters) see 67% slowdown due to hashing overhead, but this is an extreme edge case
**Why It's Faster:**
Set membership (`in`) uses hash lookups (O(1) average), while the original's `all()` must scan every edge for every node. With Python's dictionary/set implementation, even small graphs benefit from eliminating redundant iterations. The optimization includes a try/except to preserve generator compatibility while maximizing performance for common list/tuple inputs.
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.
📄 10,019% (100.19x) speedup for
find_last_nodeinsrc/algorithms/graph.py⏱️ Runtime :
24.8 milliseconds→245 microseconds(best of250runs)📝 Explanation and details
The optimized code achieves a 100x speedup (10018% faster, from 24.8ms to 245μs) by replacing the nested
all()comprehension with a set-based membership test.Key Optimization:
The original code uses:
This creates O(N × M) complexity where N is the number of nodes and M is the number of edges. For each node, it must iterate through ALL edges to verify none match.
The optimized code:
sources = {e["source"] for e in edges}if n["id"] not in sourcesThis reduces complexity to O(N + M) - one pass to build the set, one pass to check nodes.
Performance Impact by Test Case:
Why It's Faster:
Set membership (
in) uses hash lookups (O(1) average), while the original'sall()must scan every edge for every node. With Python's dictionary/set implementation, even small graphs benefit from eliminating redundant iterations. The optimization includes a try/except to preserve generator compatibility while maximizing performance for common list/tuple inputs.✅ Correctness verification report:
🌀 Click to see Generated Regression Tests
To edit these changes
git checkout codeflash/optimize-find_last_node-mldvkd8iand push.