Skip to content

PERF: fast paths for CRS equality and hashing - #1617

Open
greglucas wants to merge 2 commits into
pyproj4:mainfrom
greglucas:perf-crs-comparison
Open

PERF: fast paths for CRS equality and hashing#1617
greglucas wants to merge 2 commits into
pyproj4:mainfrom
greglucas:perf-crs-comparison

Conversation

@greglucas

Copy link
Copy Markdown
Contributor

Comparing two CRS objects goes through PROJ's proj_is_equivalent_to, which short-circuits on matching identifiers (EPSG codes) but otherwise falls through to a full recursive structural comparison of the datum, ellipsoid, prime meridian, coordinate system and conversion. For CRS built from PROJ strings, which carry no identifier, that costs 11-15us per comparison.

Noticed this in Cartopy where we do a check to see if self == src_crs to try and speed up transforms, but the CRS comparison itself can be slow.
https://github.com/SciTools/cartopy/blob/bdb6cc45e27ff4fe71741b445ac15449aee35ba3/lib/cartopy/crs.py#L410

The short circuits are the simple portion is self and srs1 == srs2, but the problem is that in missing cases we will still fall back to PROJ comparisons which is why I added the cache portion here which is really to speed up the != comparisons.

  • Tests added
  • Fully documented, including history.rst for all changes and api/*.rst for new API

Comment thread pyproj/crs/crs.py Outdated
Comparing two CRS objects goes through PROJ's proj_is_equivalent_to,
which short-circuits on matching identifiers (EPSG codes) but otherwise
falls through to a full recursive structural comparison of the datum,
ellipsoid, prime meridian, coordinate system and conversion. For CRS
built from PROJ strings, which carry no identifier, that costs 11-15us
per comparison.
@greglucas
greglucas force-pushed the perf-crs-comparison branch from b1d1dd7 to 31aa45d Compare August 9, 2026 20:09
Comment thread pyproj/crs/crs.py
Comment thread pyproj/crs/crs.py
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants