⚡️ Speed up function find_last_node by 34,233%#215
Closed
codeflash-ai[bot] wants to merge 1 commit intomainfrom
Closed
⚡️ Speed up function find_last_node by 34,233%#215codeflash-ai[bot] wants to merge 1 commit intomainfrom
find_last_node by 34,233%#215codeflash-ai[bot] wants to merge 1 commit intomainfrom
Conversation
The optimized code achieves a **342x speedup** by replacing a **nested-loop O(N×M) algorithm** with a **linear O(N+M) approach** using a set for membership testing.
**Key Optimization:**
The original code uses `all(e["source"] != n["id"] for e in edges)` for each node, checking every edge repeatedly. For N nodes and M edges, this results in **N×M comparisons**—catastrophic for large graphs.
The optimized version:
1. **Pre-builds a set** of all source IDs once: `edge_source_ids = {e["source"] for e in edges}` (O(M))
2. **Uses O(1) set membership** instead of O(M) linear scan: `n["id"] not in edge_source_ids`
3. **Adds early-exit guard** for empty nodes list
**Why This Works:**
Python sets provide O(1) average-case lookup via hash tables, versus O(M) for iterating through all edges. For each node, the cost drops from M comparisons to a single hash lookup.
**Impact by Test Case:**
- **Small graphs** (2-3 nodes): 132-221% faster—modest gain since overhead is small
- **Linear chains** (1000 nodes): **57,566-65,412% faster**—the nested loop penalty becomes extreme as both N and M grow
- **Dense graphs** (100 nodes, 9,900 edges): **17,131% faster**—the original's O(N×M) ≈ 990,000 operations vs. optimized O(N+M) ≈ 10,000
- **Star graphs** (1 source, 999 targets): 210% faster—M is large but only one source, so set-building still helps
**Workload Suitability:**
Without `function_references`, we can't confirm hot-path usage, but the optimization is universally beneficial: it maintains correctness while drastically reducing algorithmic complexity. Any workload with moderate-to-large graphs (>100 nodes/edges) will see substantial gains.
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.
📄 34,233% (342.33x) speedup for
find_last_nodeinsrc/algorithms/graph.py⏱️ Runtime :
178 milliseconds→520 microseconds(best of250runs)📝 Explanation and details
The optimized code achieves a 342x speedup by replacing a nested-loop O(N×M) algorithm with a linear O(N+M) approach using a set for membership testing.
Key Optimization:
The original code uses
all(e["source"] != n["id"] for e in edges)for each node, checking every edge repeatedly. For N nodes and M edges, this results in N×M comparisons—catastrophic for large graphs.The optimized version:
edge_source_ids = {e["source"] for e in edges}(O(M))n["id"] not in edge_source_idsWhy This Works:
Python sets provide O(1) average-case lookup via hash tables, versus O(M) for iterating through all edges. For each node, the cost drops from M comparisons to a single hash lookup.
Impact by Test Case:
Workload Suitability:
Without
function_references, we can't confirm hot-path usage, but the optimization is universally beneficial: it maintains correctness while drastically reducing algorithmic complexity. Any workload with moderate-to-large graphs (>100 nodes/edges) will see substantial gains.✅ Correctness verification report:
🌀 Click to see Generated Regression Tests
To edit these changes
git checkout codeflash/optimize-find_last_node-mjiyqvy6and push.