⚡️ Speed up method _Sum.__call__ by 23% - #12
Open
codeflash-ai[bot] wants to merge 1 commit into
Open
Conversation
The optimization replaces the blanket use of `numpy.ma.sum()` with conditional array type checking to avoid the overhead of masked array operations when they're unnecessary. **Key changes:** 1. **Caches array reference** (`arr = img.array`) to avoid repeated attribute access 2. **Type-specific sum operations**: - For `MaskedArray` with no actual masked values: uses `arr.data.sum()` directly on the underlying data - For regular `ndarray`: uses native `.sum()` method - Only falls back to `numpy.ma.sum()` when masking is actually present **Why this is faster:** - `numpy.ma.sum()` always performs mask checking and special handling even when no values are masked, adding significant overhead (79.1% of original runtime) - Direct array `.sum()` operations bypass this overhead entirely - The optimized version reduces sum operation time from ~2.09ms to ~0.68ms (67% reduction in sum operation time) **Performance by test case:** - **Best gains** (29-34% faster): Regular arrays and simple cases benefit most from bypassing masked array overhead - **Masked arrays with actual masks** show slight slowdown (4.4%) due to added type checking, but this preserves correctness - **Large arrays** still see 14-26% improvements, indicating the optimization scales well The optimization is particularly effective because most real-world image data uses `MaskedArray` containers for consistency but often contains no actual masked values, making the masked array overhead pure waste.
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.
📄 23% (0.23x) speedup for
_Sum.__call__insrc/titiler/core/titiler/core/algorithm/math.py⏱️ Runtime :
517 microseconds→422 microseconds(best of250runs)📝 Explanation and details
The optimization replaces the blanket use of
numpy.ma.sum()with conditional array type checking to avoid the overhead of masked array operations when they're unnecessary.Key changes:
arr = img.array) to avoid repeated attribute accessMaskedArraywith no actual masked values: usesarr.data.sum()directly on the underlying datandarray: uses native.sum()methodnumpy.ma.sum()when masking is actually presentWhy this is faster:
numpy.ma.sum()always performs mask checking and special handling even when no values are masked, adding significant overhead (79.1% of original runtime).sum()operations bypass this overhead entirelyPerformance by test case:
The optimization is particularly effective because most real-world image data uses
MaskedArraycontainers for consistency but often contains no actual masked values, making the masked array overhead pure waste.✅ Correctness verification report:
🌀 Generated Regression Tests and Runtime
To edit these changes
git checkout codeflash/optimize-_Sum.__call__-mifmxm73and push.