Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 22 additions & 5 deletions bench_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ var (
addr = cell.String()
geoBndry CellBoundary
cells []Cell
disks [][]Cell
)

func BenchmarkToString(b *testing.B) {
Expand All @@ -29,32 +30,48 @@ func BenchmarkFromString(b *testing.B) {
}
}

func BenchmarkToGeoRes15(b *testing.B) {
func BenchmarkCellToLatLng(b *testing.B) {
for n := 0; n < b.N; n++ {
geo, _ = CellToLatLng(cell)
}
}

func BenchmarkFromGeoRes15(b *testing.B) {
func BenchmarkLatLngToCell(b *testing.B) {
for n := 0; n < b.N; n++ {
cell, _ = LatLngToCell(geo, 15)
}
}

func BenchmarkToGeoBndryRes15(b *testing.B) {
func BenchmarkCellToBoundary(b *testing.B) {
for n := 0; n < b.N; n++ {
geoBndry, _ = CellToBoundary(cell)
}
}

func BenchmarkHexRange(b *testing.B) {
func BenchmarkGridDisk(b *testing.B) {
for n := 0; n < b.N; n++ {
cells, _ = cell.GridDisk(10)
}
}

func BenchmarkGridRing(b *testing.B) {
for range b.N {
cells, _ = cell.GridRing(10)
}
}

func BenchmarkPolyfill(b *testing.B) {
for n := 0; n < b.N; n++ {
cells, _ = PolygonToCells(validGeoPolygonHoles, 15)
cells, _ = PolygonToCells(validGeoPolygonHoles, 13)
}
}

func BenchmarkGridDisksUnsafe(b *testing.B) {
cells, _ = PolygonToCells(validGeoPolygonHoles, 12)

b.ResetTimer()

for n := 0; n < b.N; n++ {
disks, _ = GridDisksUnsafe(cells, 10)
}
}
24 changes: 6 additions & 18 deletions h3.go
Original file line number Diff line number Diff line change
Expand Up @@ -1176,18 +1176,14 @@ func intPow(n, m int) int {
}

func cellsFromC(chs []C.H3Index, prune, refit bool) []Cell {
// OPT: This could be more efficient if we unsafely cast the C array to a
// []H3Index.
out := make([]Cell, 0, len(chs))

for i := range chs {
if prune && chs[i] <= 0 {
in := unsafe.Slice((*Cell)(unsafe.Pointer(&chs[0])), len(chs))
out := in[:0]
for i := range in {
if prune && in[i] <= 0 {
continue
}

out = append(out, Cell(chs[i]))
out = append(out, in[i])
}

if refit {
// Some algorithms require a maximum sized array, but only use a subset
// of the memory. refit sizes the slice to the last non-empty element.
Expand All @@ -1197,7 +1193,6 @@ func cellsFromC(chs []C.H3Index, prune, refit bool) []Cell {
}
}
}

return out
}

Expand All @@ -1216,14 +1211,7 @@ func edgesFromC(chs []C.H3Index) []DirectedEdge {
}

func cellsToC(chs []Cell) []C.H3Index {
// OPT: This could be more efficient if we unsafely cast the array to a
// []C.H3Index.
out := make([]C.H3Index, len(chs))
for i := range chs {
out[i] = C.H3Index(chs[i])
}

return out
return unsafe.Slice((*C.H3Index)(unsafe.Pointer(&chs[0])), len(chs))
}

func intsFromC(chs []C.int) []int {
Expand Down
Loading