Avoid DataArray construction when scanning for cell_measures#647
Merged
Conversation
cell_measures iterated obj.coords.values()/obj.data_vars.values() to read the cell_measures attribute, constructing (and resolving coordinates for) a DataArray per variable on every access. Read the attribute off the bare Variables instead, matching standard_names. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
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.
Summary
CFAccessor.cell_measuresscans every variable'scell_measuresattribute by iteratingobj.coords.values()andobj.data_vars.values():Iterating those mappings constructs a full
DataArray(resolving its coordinates) per variable just to read one attribute. Becausecell_measuresis reached from__getitem__→_get_all_cell_measures, anything that doesds.cf[[var]]pays an O(nvars) DataArray-construction cost, and code that loops over variables callingds.cf[[var]](e.g. grid/style probing) ends up O(nvars²).A downstream profile of a metadata endpoint over a many-variable dataset showed this
cell_measuresattr-scan as a top contributor toDataset._construct_dataarraytime:Change
Read the
cell_measuresattribute off the bareVariableobjects (obj.variablesfor aDataset;obj.coords.variables+obj.variablefor aDataArray), which carry.attrs/.encodingwithout constructing aDataArray. This mirrors howstandard_namesalready reads fromself._obj._variables.Behavior is unchanged (verified against
airdsfor both theDatasetandDataArraypaths). Fulltest_accessor.pypasses (208 passed, 1 skipped).🤖 Generated with Claude Code