Skip to content

Improve performance of H3 geo functions (combined sincos in coordinate transforms)#109399

Merged
alexey-milovidov merged 1 commit into
masterfrom
h3-sincos-optimization
Jul 5, 2026
Merged

Improve performance of H3 geo functions (combined sincos in coordinate transforms)#109399
alexey-milovidov merged 1 commit into
masterfrom
h3-sincos-optimization

Conversation

@alexey-milovidov

@alexey-milovidov alexey-milovidov commented Jul 4, 2026

Copy link
Copy Markdown
Member

Updates the contrib/h3 submodule to ClickHouse/h3#11, which speeds up the H3 geo coordinate transforms by computing paired sin/cos together (one sincos call) and removing redundant trig evaluations in the hot lat/lng <-> cell routines (_geoToVec3d, _geoAzimuthRads, _geoAzDistanceRads, _geoToHex2d).

Why

PR #100272 explored replacing the H3 C library with the Rust h3o crate for performance. Investigation showed that in a fair -O3 build the C library is already competitive with h3o (the large gaps in that PR came from an un-inlined C build), and the genuinely portable win is the combined-sincos / redundant-trig cleanup — which needs no Rust, no FFI wrappers, no panics across the boundary, and no backward-incompatible metric changes.

Correctness

Bit-for-bit identical output — the change only fuses paired calls (which return the same values) and removes duplicate evaluations. All 311 upstream h3 unit tests pass unchanged, so no ClickHouse .reference files change.

Performance (10M rows, clang -O3)

function speedup
h3ToGeoBoundary ~14%
h3ToGeo ~9–11%
h3CellAreaM2 / h3CellAreaRads2 ~8%
geoToH3 ~3–4%

h3ToParent / h3IsValid are already optimal (loopless bit-twiddling) and unchanged.

Note: the submodule currently points at the sincos-optimization branch commit; it will be re-pointed to the merge commit once ClickHouse/h3#11 is merged into ClickHouse/v4.

Changelog category (leave one):

  • Performance Improvement

Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):

Improve the performance of H3 geo functions (h3ToGeoBoundary, h3ToGeo, h3CellAreaM2/h3CellAreaRads2, geoToH3) by computing paired sine/cosine together and eliminating redundant trigonometric calls in the coordinate transforms. Results are unchanged (bit-for-bit identical).

Version info

  • Merged into: 26.7.1.535 (included in 26.7 and later)

Bump contrib/h3 to pick up combined sincos + redundant-trig removal in
the lat/lng <-> H3 cell transforms. Bit-for-bit identical output; speeds
up h3ToGeoBoundary ~14%, h3ToGeo ~9-11%, h3CellArea* ~8%, geoToH3 ~3-4%.

Recovers the coordinate-transform speedups from the Rust h3o experiment
(#100272) while keeping the C library backend.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
@clickhouse-gh

clickhouse-gh Bot commented Jul 4, 2026

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [f5ff4b2]

Summary:


AI Review

Summary

This PR updates contrib/h3 to one upstream commit that replaces repeated sin/cos evaluations in the hot coordinate-transform helpers with a shared _sincos helper. I found no correctness issue in the current diff or ClickHouse integration, and a local old-vs-new comparison of latLngToCell, cellToLatLng, cellToBoundary, cellAreaM2, and cellAreaRads2 over 21,936 inputs and 18,300 unique cells matched bit-for-bit.

Final Verdict

Status: ✅ Approve

@clickhouse-gh clickhouse-gh Bot added pr-performance Pull request with some performance improvements submodule changed At least one submodule changed in this PR. labels Jul 4, 2026
@alexey-milovidov alexey-milovidov self-assigned this Jul 5, 2026
@alexey-milovidov

Copy link
Copy Markdown
Member Author

Sounds good.

@alexey-milovidov
alexey-milovidov merged commit 478216d into master Jul 5, 2026
174 of 175 checks passed
@alexey-milovidov
alexey-milovidov deleted the h3-sincos-optimization branch July 5, 2026 18:33
@robot-clickhouse-ci-1 robot-clickhouse-ci-1 added the pr-synced-to-cloud The PR is synced to the cloud repo label Jul 5, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

pr-performance Pull request with some performance improvements pr-synced-to-cloud The PR is synced to the cloud repo submodule changed At least one submodule changed in this PR.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants