⚡️ Speed up function find_last_node
by 13,550%
#66
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.
📄 13,550% (135.50x) speedup for
find_last_node
insrc/dsa/nodes.py
⏱️ Runtime :
53.2 milliseconds
→390 microseconds
(best of528
runs)📝 Explanation and details
The optimization transforms an O(n*m) algorithm into an O(n+m) algorithm by eliminating redundant work through preprocessing.
Key Optimization: Set-based Preprocessing
The original code uses a nested loop structure where for each node, it checks all edges to see if any edge has that node as a source. This creates an O(n*m) time complexity where n is the number of nodes and m is the number of edges.
The optimized version preprocesses all edge sources into a set (
sources = {e["source"] for e in edges}
), then performs a simple O(1) set membership check (n["id"] not in sources
) for each node. This reduces the overall complexity to O(n+m).Specific Changes:
all(e["source"] != n["id"] for e in edges)
check with a fast set membership testWhy This Creates Massive Speedup:
Test Case Performance Patterns:
The optimization is particularly effective for graph analysis scenarios where edge density is high relative to the number of sink nodes (nodes with no outgoing edges).
✅ Correctness verification report:
🌀 Generated Regression Tests and Runtime
To edit these changes
git checkout codeflash/optimize-find_last_node-mdpcvjcb
and push.