⚡️ Speed up method Cube.cell_measure_dims by 49%
#28
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.
📄 49% (0.49x) speedup for
Cube.cell_measure_dimsinlib/iris/cube.py⏱️ Runtime :
251 microseconds→169 microseconds(best of6runs)📝 Explanation and details
The optimization adds a fast-path lookup in
cell_measure_dimswhen the input is already aCellMeasureobject. Instead of always callingself.cell_measure()first (which performs a slower search through all cell measures), the optimized version directly searches_cell_measures_and_dimsusing identity comparison (is) when given aCellMeasureobject.Key changes:
isinstance(cell_measure, CellMeasure)check at the start ofcell_measure_dimsnext()with a generator expression for efficient single-match lookup:next((dims for cm_, dims in self._cell_measures_and_dims if cm_ is cell_measure), None)self.cell_measure()call when the fast path failsWhy this is faster:
The original code always called
self.cell_measures()which searches through all cell measures using equality comparison. The optimized version uses identity comparison (is) which is much faster in Python - it's just a pointer comparison rather than potentially expensive metadata equality checks. Thenext()function also short-circuits on the first match instead of building a full list.Performance characteristics:
Based on the profiler results, this optimization is particularly effective when
cell_measure_dimsis called withCellMeasureobjects (the common case), reducing execution time from ~1.1ms to ~0.4ms (63% improvement). The fast path handles 103 out of 106 calls in the test cases, demonstrating its effectiveness for the typical usage pattern where existingCellMeasureobjects are passed to determine their dimensions.✅ Correctness verification report:
⚙️ Existing Unit Tests and Runtime
unit/cube/test_Cube.py::TestCellMeasures.test_cell_measure_dimsunit/cube/test_Cube.py::TestCellMeasures.test_cell_measure_dims_by_nameunit/cube/test_Cube.py::TestCellMeasures.test_fail_cell_measure_dimsunit/cube/test_Cube.py::TestCellMeasures.test_fail_cell_measure_dims_by_name⏪ Replay Tests and Runtime
test_pytest_libiristestsintegrationtest_netcdf__loadsaveattrs_py_libiristestsunitlazy_datatest_non_lazy_p__replay_test_0.py::test_iris_cube_Cube_cell_measure_dimsTo edit these changes
git checkout codeflash/optimize-Cube.cell_measure_dims-mh53jxgtand push.