⚡️ Speed up function _chunked_aware_interpnd by 27%
#43
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.
📄 27% (0.27x) speedup for
_chunked_aware_interpndinxarray/core/missing.py⏱️ Runtime :
4.51 milliseconds→3.55 milliseconds(best of9runs)📝 Explanation and details
The optimized code achieves a 26% speedup by focusing on the most expensive operations identified in the line profiler. The key optimizations target the
_localizefunction, which accounts for 85% of the total runtime in_chunked_aware_interpnd.Primary optimization in
_localize:index.get_indexer()twice separately forminvalandmaxval(41.2% + 27.2% = 68.4% of original runtime), the optimized version combines them into a single call usingnp.array([minval, maxval]). This reduces expensive pandas Index operations from two calls to one..valuesaccess: Thenew_x.valuesis cached once rather than accessed twice, eliminating redundant attribute lookups.for dim, pair in indexes_coords.items()withx, new_x = pairis more efficient than the list unpacking in the loop header.Secondary optimizations in other functions:
*tuple(range(...))instead oflist(range(...))for numpy transpose operations is more efficient as tuples are faster to construct and unpack.const_dims,var_dims) avoids repeated string formatting in loops.Impact on workloads:
The optimizations are particularly effective for test cases involving:
_localizeoptimizationsThe optimizations maintain identical functionality while reducing computational overhead in the critical interpolation preprocessing path, making them especially valuable for applications performing repeated spatial/temporal interpolations.
✅ Correctness verification report:
🌀 Generated Regression Tests and Runtime
To edit these changes
git checkout codeflash/optimize-_chunked_aware_interpnd-miijyp0rand push.