⚡️ Speed up function find_leaf_nodes by 38,690%#302
Open
codeflash-ai[bot] wants to merge 1 commit intomainfrom
Open
⚡️ Speed up function find_leaf_nodes by 38,690%#302codeflash-ai[bot] wants to merge 1 commit intomainfrom
find_leaf_nodes by 38,690%#302codeflash-ai[bot] wants to merge 1 commit intomainfrom
Conversation
The original nested loop scanned all edges for every node (O(n·m)), spending 99.9% of runtime on dictionary lookups inside that double loop. The optimization builds a set of source IDs once (O(m)), then checks membership in O(1) per node (O(n) total), reducing the worst-case test from 17.6 ms to 57.5 µs. A try-except wrapper falls back to list membership if any source value is unhashable, preserving correctness while still delivering massive speedups (400× to 50,000× across large-graph tests). Micro-benchmarks on empty inputs regress by ~30% due to set-construction overhead, but all realistic workloads show dramatic 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.
📄 38,690% (386.90x) speedup for
find_leaf_nodesinsrc/algorithms/graph.py⏱️ Runtime :
469 milliseconds→1.21 milliseconds(best of180runs)📝 Explanation and details
The original nested loop scanned all edges for every node (O(n·m)), spending 99.9% of runtime on dictionary lookups inside that double loop. The optimization builds a set of source IDs once (O(m)), then checks membership in O(1) per node (O(n) total), reducing the worst-case test from 17.6 ms to 57.5 µs. A try-except wrapper falls back to list membership if any source value is unhashable, preserving correctness while still delivering massive speedups (400× to 50,000× across large-graph tests). Micro-benchmarks on empty inputs regress by ~30% due to set-construction overhead, but all realistic workloads show dramatic gains.
✅ 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-mmuc08dpand push.