Speed up geo coordinate transforms: combined sincos + fewer trig calls#11
Merged
Merged
Conversation
The lat/lng <-> H3 cell transforms are dominated by transcendental
function calls. Several hot routines computed sin(x) and cos(x) of the
same angle with two separate library calls, and a few recomputed the
same value more than once:
* _geoToVec3d - sin/cos of the latitude and of the longitude
* _geoAzimuthRads - sin/cos pairs; cos(p2->lat) was evaluated twice
* _geoAzDistanceRads- sin/cos of p1->lat, the distance and the azimuth
were each evaluated twice
* _geoToHex2d - sin/cos of theta
Compute each sin/cos pair together via a small _sincos() helper (it
lowers to a single libm `sincos` call where available and falls back to
separate sin()/cos() otherwise) and reuse already-computed values. The
outputs are bit-for-bit identical; this only removes redundant work.
Measured on 10M rows (clang -O3, no LTO):
cellToLatLng +9..11%
cellToBoundary +14%
cellArea +8%
latLngToCell +3..4%
All 311 existing tests pass unchanged.
Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Member
Author
|
Looks ok. |
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.
Speed up the geo coordinate transforms
The
lat/lng <-> H3 celltransforms are dominated by transcendentalfunction calls. Profiling the hot paths showed two easy, safe wins:
sin(x)andcos(x)of the same angle but computed them with two independent libmcalls. A small
_sincos()helper computes them together — it lowersto a single
sincoscall where libm provides one, and falls back toseparate
sin()/cos()otherwise, so it stays portable.sine/cosine more than once.
Functions touched:
_geoToVec3dsincosfor the latitude, one for the longitude_geoAzimuthRadssincospairs;cos(p2->lat)was evaluated twice_geoAzDistanceRadssincosforp1->lat, distance and azimuth, each previously evaluated twice_geoToHex2dsincosforthetaCorrectness
The outputs are bit-for-bit identical to before — this only removes
redundant work and fuses paired calls (which return the same values).
All 311 existing unit tests pass unchanged.
Performance
Measured on 10M random cells,
clang -O3, no LTO:cellToBoundarycellToLatLngcellArea*latLngToCellcellToParent/isValidCellwere already limited by their (alreadyoptimal) bit-twiddling and are unchanged.
This backs a ClickHouse-side change: it lets ClickHouse keep the C H3
library while recovering the coordinate-transform speedups that motivated
the experiment in ClickHouse/ClickHouse#100272, without switching the
backend to Rust.