⚡️ Speed up function find_leaf_nodes by 37,712%#301
Open
codeflash-ai[bot] wants to merge 1 commit intonext-genfrom
Open
⚡️ Speed up function find_leaf_nodes by 37,712%#301codeflash-ai[bot] wants to merge 1 commit intonext-genfrom
find_leaf_nodes by 37,712%#301codeflash-ai[bot] wants to merge 1 commit intonext-genfrom
Conversation
The optimized function cuts runtime from 59.4 ms to 0.157 ms (~378× faster) by building a set of edge["source"] values and using a single list comprehension to filter nodes instead of the original nested node×edge loop. The key insight is that membership checks against a precomputed set are O(1), turning the algorithm from O(n·m) into O(n+m); the line profiler shows the inner edge loop accounted for ~99% of the original time, so eliminating it yields the large win. It also short-circuits the no-edges case by returning a shallow copy for minimal overhead and wraps set construction in a try/except to fall back to the original nested loop if sources are unhashable or the 'source' key is missing, preserving the original exception behavior. Trade-offs: set construction adds a small upfront cost and a couple of microbenchmarks (empty-nodes and the missing-key fallback) show marginal regressions, but these are rare compared with the huge speedup on typical graphs.
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.
📄 37,712% (377.12x) speedup for
find_leaf_nodesinsrc/algorithms/graph.py⏱️ Runtime :
59.4 milliseconds→157 microseconds(best of250runs)📝 Explanation and details
The optimized function cuts runtime from 59.4 ms to 0.157 ms (~378× faster) by building a set of edge["source"] values and using a single list comprehension to filter nodes instead of the original nested node×edge loop. The key insight is that membership checks against a precomputed set are O(1), turning the algorithm from O(n·m) into O(n+m); the line profiler shows the inner edge loop accounted for ~99% of the original time, so eliminating it yields the large win. It also short-circuits the no-edges case by returning a shallow copy for minimal overhead and wraps set construction in a try/except to fall back to the original nested loop if sources are unhashable or the 'source' key is missing, preserving the original exception behavior. Trade-offs: set construction adds a small upfront cost and a couple of microbenchmarks (empty-nodes and the missing-key fallback) show marginal regressions, but these are rare compared with the huge speedup on typical graphs.
✅ Correctness verification report:
🌀 Click to see Generated Regression Tests
🔎 Click to see Concolic Coverage Tests
To edit these changes
git checkout codeflash/optimize-find_leaf_nodes-mmub3pdyand push.