Skip to content

Conversation

ciscorn
Copy link
Contributor

@ciscorn ciscorn commented Feb 14, 2025

I found that we can simplify and optimize the conversion between XYZ tile coordinates and the Hilbert tile IDs.

This optimization is primarily based on the fact that the tile IDs for (zi, 0, 0) are given by the sum of a geometric series with a common ratio of 4.

Related PR: protomaps/PMTiles#527

@ciscorn ciscorn changed the title Optimize Hilbert tile ID <-> XYZ conversion Simplify and optimize Hilbert tile ID <-> XYZ conversion Feb 14, 2025
@ciscorn
Copy link
Contributor Author

ciscorn commented Feb 16, 2025

I benchmarked this on Go 1.24 for both amd64 and arm64 architectures:

  • ZxyToId is about 20% faster
  • ParentId is about 2000% faster
Go 1.24

Linux AMD64

pkg: github.com/protomaps/go-pmtiles/pmtiles
cpu: AMD EPYC 7642 48-Core Processor
BenchmarkZxyToIdOriginal-2    	       7	 156841629 ns/op
BenchmarkZxyToId-2            	       8	 131925635 ns/op (1.19x)
BenchmarkIdToZxyOriginal-2    	       1	2449533628 ns/op
BenchmarkIdToZxy-2            	       1	2011963536 ns/op (1.22x)
BenchmarkParentIdOriginal-2   	       3	 388430708 ns/op
BenchmarkParentId-2           	      69	  17235591 ns/op (22.5x)

MacOS ARM64

pkg: github.com/protomaps/go-pmtiles/pmtiles
cpu: Apple M1 Pro
BenchmarkZxyToIdOriginal-8             9         117123093 ns/op
BenchmarkZxyToId-8                    12          96429438 ns/op (1.21x)
BenchmarkIdToZxyOriginal-8             1        1490087625 ns/op
BenchmarkIdToZxy-8                     1        1448143084 ns/op (1.03x)
BenchmarkParentIdOriginal-8            6         172983236 ns/op
BenchmarkParentId-8                  134           8708995 ns/op (19.9x)

@bdon bdon merged commit 8ea4ca3 into protomaps:main Feb 17, 2025
3 of 4 checks passed
@bdon
Copy link
Member

bdon commented Feb 17, 2025

Nice optimization, thanks for checking the speed!

@bdon
Copy link
Member

bdon commented Feb 17, 2025

v1.25.1 released https://github.com/protomaps/go-pmtiles/releases/tag/v1.25.1

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