⚡️ Speed up function _infer_xy_labels_3d by 23%
#58
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
_infer_xy_labels_3dinxarray/plot/utils.py⏱️ Runtime :
352 microseconds→285 microseconds(best of5runs)📝 Explanation and details
The optimization achieves a 23% speedup through several targeted micro-optimizations in two critical xarray functions used for plotting and dataset indexing:
Key Optimizations Applied
1.
_infer_xy_labels_3dFunction (xarray/plot/utils.py)darray.dims: Stored frequently accesseddarray.dimsin a local variable to eliminate repeated attribute lookupsinchecks to a direct loop that avoids redundant membership testsnot_nonelist creation and merged validation steps2.
Dataset.iselMethod (xarray/core/dataset.py)indexers.values(): Stored the values in a local variable to avoid repeated method calls in theis_fancy_indexercheckvar_indexersPerformance Impact Analysis
Based on the annotated test results, these optimizations are particularly effective for:
Hot Path Benefits
The
_infer_xy_labels_3dfunction is called from_infer_xy_labelsduring 3D plotting operations (imshow), making these optimizations valuable for:The
Dataset.iseloptimization benefits any indexing operation on xarray Datasets, which is fundamental to data selection and manipulation workflows throughout the xarray ecosystem.✅ Correctness verification report:
🌀 Generated Regression Tests and Runtime
To edit these changes
git checkout codeflash/optimize-_infer_xy_labels_3d-mio81wkeand push.