⚡️ Speed up function _get_units_from_attrs by 8,230%
#59
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.
📄 8,230% (82.30x) speedup for
_get_units_from_attrsinxarray/plot/utils.py⏱️ Runtime :
15.0 milliseconds→180 microseconds(best of36runs)📝 Explanation and details
The optimization achieves an 8229% speedup by eliminating the primary bottleneck: repeatedly calling
DuckArrayModule("pint").typeon every function invocation.Key optimizations applied:
Module-level caching of pint array type: The expensive
DuckArrayModule("pint").typecall that was consuming 99.2% of execution time (51ms out of 52ms total) is now executed once at import time and cached as_pint_array_type. This eliminates ~189μs per call overhead.Pre-allocated format string: The units format string
" [{}]"is moved to module scope as_units_fmt, avoiding repeated string allocation.Local variable for attrs:
da.attrsis stored in a local variable to reduce attribute lookup overhead during the conditional checks.Why this leads to massive speedup:
The original code's
DuckArrayModule("pint").typecall involves dynamic module loading and type resolution on every function call. Moving this to import time transforms an O(1) per-call operation into O(1) per-module-load, which is amortized across all function calls.Impact on existing workloads:
Based on the function references,
_get_units_from_attrsis called in plotting contexts:_title_for_slice()calls it for coordinate labeling in plot titleslabel_from_attrs()calls it for axis labeling in plotsSince plotting often involves processing multiple DataArrays (coordinates, variables), this optimization significantly benefits workflows that generate many plots or work with datasets containing numerous variables with units.
Test case performance:
The optimization excels across all test scenarios, with speedups ranging from 3,000% to 26,000%. It's particularly effective for:
The optimization maintains identical behavior while transforming this function from a performance bottleneck into a negligible operation.
✅ Correctness verification report:
🌀 Generated Regression Tests and Runtime
To edit these changes
git checkout codeflash/optimize-_get_units_from_attrs-mio99ck7and push.