Memoize CRS construction in grid_mappings#646
Merged
Conversation
pyproj.CRS.from_cf re-parses the datum/ellipsoid on every call, which is expensive for grid mappings carrying explicit ellipsoid parameters (e.g. geostationary). A dataset references the same grid mapping from many variables and .cf.grid_mappings may be accessed repeatedly, so cache the constructed CRS on the grid-mapping variable's attrs. 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
GridMapping.crsis built viapyproj.CRS.from_cf(var.attrs)inside_create_grid_mapping, which is called once per grid mapping every time.cf.grid_mappingsis accessed.pyproj.CRS.from_cfre-parses the datum/ellipsoid from scratch on each call — cheap for EPSG-coded CRSs, but expensive for grid mappings carrying explicit ellipsoid parameters (semi_major_axis/semi_minor_axis/inverse_flattening), e.g. geostationary.In a downstream tile server, the cold path of a metadata endpoint that probes every data variable's grid (
guess_grid_metadata→ds.cf.grid_mappings) spent ~70s entirely inDatum.__new__viafrom_cf— N data variables × repeated detection, all rebuilding the same CRS:Change
Memoize the CRS construction on a hashable form of the grid-mapping variable's attrs (
_crs_from_cf_attrs+_hashable_attrs). A dataset references the same grid mapping from many variables and the property may be accessed repeatedly, so each distinct grid mapping is now built once. This matches the existing caching pattern in this module (_parse_grid_mapping_attribute).Only the generic
from_cfpath is cached (the demonstrated hotspot); thefrom_json_dictHEALPix / reduced-gaussian branches are left as-is.Test
test_grid_mappings_crs_construction_is_cachedspies onpyproj.CRS.from_cfand asserts it's called exactly once per distinct grid mapping (3 forhrrrds) across repeatedDatasetandDataArrayproperty accesses. Fulltest_accessor.pysuite passes (209 passed, 1 skipped).🤖 Generated with Claude Code