Skip to content

perf: compute latLngToVec3 with math.Sincos in x/h3go - #156

Merged
justinhwang merged 1 commit into
masterfrom
perf/latlng-sincos
Oct 2, 2026
Merged

justinhwang merged 1 commit into
masterfrom
perf/latlng-sincos

Conversation

@justinhwang

@justinhwang justinhwang commented Oct 2, 2026 •

Copy link
Copy Markdown
Collaborator

What

latLngToVec3 took the sine and cosine of both the latitude and the longitude through four separate math.Sin / math.Cos calls, range-reducing each angle twice. math.Sincos returns sine and cosine from a single reduction, so the forward projection now makes two calls instead of four.

Discovered by #137

Why it's safe

  • The projected output is unchanged: the pinned known-value tests, the cgo parity suite (x/h3go/paritytest), and the conformance suite all still pass.
  • The forward path stays allocation-free.

Results

latLngToVec3 is unexported, so unlike the other benchmarks it can't be reached from paritytest; BenchmarkLatLngToVec3 is added in-package to guard it.

benchstat, fifteen interleaved rounds of each side on an Apple M3 Max:

                │     old      │                new                 │
                │    sec/op    │   sec/op     vs base               │
LatLngToVec3-16   13.750n ± 1%   8.614n ± 4%  -37.35% (p=0.000 n=15)

End to end the win is small — around 1% off LatLngToCell — since the projection is well under a tenth of that call, which is dominated by the face-IJK descent.

🤖 Generated with Claude Code

latLngToVec3 took the sine and cosine of both the latitude and the longitude
through four separate math.Sin / math.Cos calls, range-reducing each angle
twice. math.Sincos returns sine and cosine from a single reduction, so the
forward projection now makes two calls instead of four.

The projected output is unchanged: the pinned known-value tests, the cgo parity
suite and the conformance suite all still pass, and the path stays
allocation-free. latLngToVec3 is unexported, so unlike the other benchmarks it
cannot be reached from paritytest; BenchmarkLatLngToVec3 is added in-package to
guard it.

benchstat, fifteen interleaved rounds of each side on an Apple M3 Max:

                │     old      │                new                 │
                │    sec/op    │   sec/op     vs base               │
LatLngToVec3-16   13.750n ± 1%   8.614n ± 4%  -37.35% (p=0.000 n=15)

End to end the win is small, around 1% off LatLngToCell, since the projection is
well under a tenth of that call, which is dominated by the face-IJK descent.

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

Copy link
Copy Markdown

Coverage Report for CI Build 37078028382

Coverage remained the same at 100.0%

Details

  • Coverage remained the same as the base build.
  • Patch coverage: 5 of 5 lines across 1 file are fully covered (100%).
  • No coverage regressions found.

Uncovered Changes

No uncovered changes found.

Coverage Regressions

No coverage regressions found.


Coverage Stats

Coverage Status
Relevant Lines: 4517
Covered Lines: 4517
Line Coverage: 100.0%
Coverage Strength: 2669198.5 hits per line

💛 - Coveralls

@justinhwang
justinhwang merged commit 548075a into master Oct 2, 2026
14 checks passed
@justinhwang
justinhwang deleted the perf/latlng-sincos branch October 2, 2026 23:42
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