⚡️ Speed up function find_last_node by 16,863%
#256
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,863% (168.63x) speedup for
find_last_nodeinsrc/algorithms/graph.py⏱️ Runtime :
52.0 milliseconds→307 microseconds(best of152runs)📝 Explanation and details
Brief: The optimized version replaces a repeated scan of the edges for every node with a single pass that collects all edge sources into a set, turning an O(N*M) check into O(N+M) work. It also adds a small fast-path for the empty-edges case to avoid unnecessary work and preserve the original function's behavior. These changes explain the ~168x measured speedup (52 ms → 307 μs).
What changed and why it’s faster
next((n for n in nodes if all(e["source"] != n["id"] for e in edges)), None)
which, for each node, iterates over edges (the all(...) generator). That is O(N * M) comparisons in the worst case (N = number of nodes, M = number of edges).
The optimized code computes sources = {e["source"] for e in edges} once (O(M)) and then checks n["id"] not in sources for each node (O(1) average per membership test). Total complexity becomes O(M + N).
Evidence in the profiler
Behavioral impact and correctness
When this matters (based on tests)
Complexity summary
In short: change from repeated edge scans to a single set-construction + O(1) membership checks explains the significant runtime improvement while preserving the original function’s behavior.
✅ Correctness verification report:
🌀 Click to see Generated Regression Tests
To edit these changes
git checkout codeflash/optimize-find_last_node-mkewfjzmand push.