⚡️ Speed up function find_last_node by 9,272%#268
Closed
codeflash-ai[bot] wants to merge 1 commit intooptimizefrom
Closed
⚡️ Speed up function find_last_node by 9,272%#268codeflash-ai[bot] wants to merge 1 commit intooptimizefrom
find_last_node by 9,272%#268codeflash-ai[bot] wants to merge 1 commit intooptimizefrom
Conversation
The optimized code achieves a **93x speedup** (9272% faster) by eliminating redundant iterations over the `edges` collection. **Key Optimization:** The original code uses a nested generator expression that iterates over `edges` for *every* node being checked: ```python next((n for n in nodes if all(e["source"] != n["id"] for e in edges)), None) ``` This creates O(N × M) comparisons where N = number of nodes and M = number of edges. For 500 nodes and 499 edges, this means ~250,000 comparisons. The optimized version: 1. **Detects re-iterable containers** (lists, tuples) vs single-use iterators 2. **For re-iterable edges** (the common case): Builds a set of all source IDs upfront in O(M) time, then checks membership in O(1) per node, reducing to O(N + M) total complexity 3. **For iterator edges** (rare case): Preserves original single-pass semantics to avoid breaking behavior when edges can only be consumed once **Performance Impact:** The test results show massive improvements on larger datasets: - `test_large_scale_last_node_at_end_of_long_list`: **17,559% faster** (4.39ms → 24.9μs) - `test_find_last_node_many_nodes_many_edges`: **11,880% faster** (2.07ms → 17.3μs) - `test_find_last_node_deep_chain_performance`: **17,678% faster** (4.41ms → 24.8μs) Even small graphs benefit significantly (50-120% faster) due to avoiding the nested iteration overhead. The optimization trades a tiny bit of upfront work (building the set) for dramatically reduced per-node checking cost, especially beneficial when nodes >> edges or when many nodes need checking before finding a match. Edge cases like empty inputs show minimal regression (<10% slower) because the set construction overhead dominates when there's no work to do, but this is an acceptable tradeoff given the dramatic wins on realistic workloads.
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.
📄 9,272% (92.72x) speedup for
find_last_nodeinsrc/algorithms/graph.py⏱️ Runtime :
11.5 milliseconds→122 microseconds(best of250runs)📝 Explanation and details
The optimized code achieves a 93x speedup (9272% faster) by eliminating redundant iterations over the
edgescollection.Key Optimization:
The original code uses a nested generator expression that iterates over
edgesfor every node being checked:This creates O(N × M) comparisons where N = number of nodes and M = number of edges. For 500 nodes and 499 edges, this means ~250,000 comparisons.
The optimized version:
Performance Impact:
The test results show massive improvements on larger datasets:
test_large_scale_last_node_at_end_of_long_list: 17,559% faster (4.39ms → 24.9μs)test_find_last_node_many_nodes_many_edges: 11,880% faster (2.07ms → 17.3μs)test_find_last_node_deep_chain_performance: 17,678% faster (4.41ms → 24.8μs)Even small graphs benefit significantly (50-120% faster) due to avoiding the nested iteration overhead. The optimization trades a tiny bit of upfront work (building the set) for dramatically reduced per-node checking cost, especially beneficial when nodes >> edges or when many nodes need checking before finding a match.
Edge cases like empty inputs show minimal regression (<10% slower) because the set construction overhead dominates when there's no work to do, but this is an acceptable tradeoff given the dramatic wins on realistic workloads.
✅ Correctness verification report:
🌀 Click to see Generated Regression Tests
To edit these changes
git checkout codeflash/optimize-find_last_node-mlddarcfand push.