⚡️ Speed up function _gather_error by 30% - #3
Open
codeflash-ai[bot] wants to merge 1 commit into
Open
Conversation
The optimization adds a conditional check `if len(errs) > 1:` before sorting the error list. This simple change provides a **30% speedup** by avoiding unnecessary sorting operations when there are 0 or 1 errors. **Key optimization:** - **Conditional sorting**: Only sorts when `len(errs) > 1`, since lists with 0-1 elements are already "sorted" - **Line profiler evidence**: Shows sorting went from 3,086 calls (every function call) to only 80 calls (when multiple errors exist) **Why this works:** - Python's `list.sort()` has overhead even for small lists - it still needs to allocate temporary space and perform comparisons - In many practical scenarios, most nodes have 0-1 errors, making sorting unnecessary - The `len()` check is extremely fast (O(1)) compared to sorting overhead **Performance by test case:** - **Best gains** (30-35%): Large-scale tests with many nodes having few/no errors (`test_performance_with_large_flat_upstream`, `test_large_mixed_tree_with_some_errors`) - **Moderate gains** (15-30%): Single nodes and small hierarchies where sorting is frequently skipped - **Minimal impact** (2-5% slower): Cases with multiple errors where sorting still occurs, due to added length check overhead This optimization is particularly effective for error collection in pipeline scenarios where most nodes succeed and only occasional failures need sorting.
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.
📄 30% (0.30x) speedup for
_gather_errorinsrc/spdl/pipeline/_node.py⏱️ Runtime :
1.19 milliseconds→914 microseconds(best of431runs)📝 Explanation and details
The optimization adds a conditional check
if len(errs) > 1:before sorting the error list. This simple change provides a 30% speedup by avoiding unnecessary sorting operations when there are 0 or 1 errors.Key optimization:
len(errs) > 1, since lists with 0-1 elements are already "sorted"Why this works:
list.sort()has overhead even for small lists - it still needs to allocate temporary space and perform comparisonslen()check is extremely fast (O(1)) compared to sorting overheadPerformance by test case:
test_performance_with_large_flat_upstream,test_large_mixed_tree_with_some_errors)This optimization is particularly effective for error collection in pipeline scenarios where most nodes succeed and only occasional failures need sorting.
✅ Correctness verification report:
🌀 Generated Regression Tests and Runtime
To edit these changes
git checkout codeflash/optimize-_gather_error-mgqntodtand push.