From d0bc975f8dd84a832de74d99123746d2eb291478 Mon Sep 17 00:00:00 2001 From: fx408 Date: Wed, 10 Sep 2025 15:49:05 +0800 Subject: [PATCH 01/10] Allocate memory in one go The method AllNeighbors returns a maximum of 8 CellIDs, pre-allocates memory to avoid triggering expansion, and enhances performance. --- s2/cellid.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/s2/cellid.go b/s2/cellid.go index fbcf2f8..cf8bbd3 100644 --- a/s2/cellid.go +++ b/s2/cellid.go @@ -276,7 +276,7 @@ func (ci CellID) AllNeighbors(level int) []CellID { return nil } - var neighbors []CellID + var neighbors = make([]CellID, 0, 8) face, i, j, _ := ci.faceIJOrientation() From d732cab85ba2219c8c0e5e131c3e36994f45bcec Mon Sep 17 00:00:00 2001 From: fx408 Date: Thu, 11 Sep 2025 15:22:12 +0800 Subject: [PATCH 02/10] add a benchmark for CellID.AllNeighbors --- s2/cellid_test.go | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/s2/cellid_test.go b/s2/cellid_test.go index 4a2063b..a5da03f 100644 --- a/s2/cellid_test.go +++ b/s2/cellid_test.go @@ -1046,6 +1046,25 @@ func TestCellIDCenterFaceSiTi(t *testing.T) { } } +func BenchmarkAllNeighbors(b *testing.B) { + level := 12 + ll := LatLngFromDegrees(10.100001, 10.100002) + cellID := CellIDFromLatLng(ll).Parent(level) + + cases := []int{level, level + 2, level + 4} + + for _, n := range cases { + b.Run(fmt.Sprintf("level%d", n), func(b *testing.B) { + b.ResetTimer() + b.ReportAllocs() + + for i := 0; i < b.N; i++ { + cellID.AllNeighbors(n) + } + }) + } +} + // TODO(roberts): Remaining tests to convert. // Coverage // TraversalOrder From 84a4b67637658e227471573fccbfa8a0e5e9d494 Mon Sep 17 00:00:00 2001 From: fx408 Date: Thu, 11 Sep 2025 15:24:22 +0800 Subject: [PATCH 03/10] calculate the number of neighbors --- s2/cellid.go | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/s2/cellid.go b/s2/cellid.go index cf8bbd3..59544ad 100644 --- a/s2/cellid.go +++ b/s2/cellid.go @@ -276,8 +276,6 @@ func (ci CellID) AllNeighbors(level int) []CellID { return nil } - var neighbors = make([]CellID, 0, 8) - face, i, j, _ := ci.faceIJOrientation() // Find the coordinates of the lower left-hand leaf cell. We need to @@ -289,6 +287,9 @@ func (ci CellID) AllNeighbors(level int) []CellID { nbrSize := sizeIJ(level) + maxCellIDs := 4*(size/nbrSize+1) + var neighbors = make([]CellID, 0, maxCellIDs) + // We compute the top-bottom, left-right, and diagonal neighbors in one // pass. The loop test is at the end of the loop to avoid 32-bit overflow. for k := -nbrSize; ; k += nbrSize { From f55595dff9bf11a28ca2aee73763e5dfbd5f09f2 Mon Sep 17 00:00:00 2001 From: fx408 Date: Thu, 11 Sep 2025 15:34:31 +0800 Subject: [PATCH 04/10] code formatting --- s2/cellid.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/s2/cellid.go b/s2/cellid.go index 59544ad..101d675 100644 --- a/s2/cellid.go +++ b/s2/cellid.go @@ -287,7 +287,7 @@ func (ci CellID) AllNeighbors(level int) []CellID { nbrSize := sizeIJ(level) - maxCellIDs := 4*(size/nbrSize+1) + maxCellIDs := 4 * (size/nbrSize + 1) var neighbors = make([]CellID, 0, maxCellIDs) // We compute the top-bottom, left-right, and diagonal neighbors in one From 8b6def237531e0bf9146f78f8b7cfc71a66c55a6 Mon Sep 17 00:00:00 2001 From: fx408 Date: Thu, 11 Sep 2025 15:58:44 +0800 Subject: [PATCH 05/10] import fmt --- s2/cellid_test.go | 1 + 1 file changed, 1 insertion(+) diff --git a/s2/cellid_test.go b/s2/cellid_test.go index a5da03f..78a70a1 100644 --- a/s2/cellid_test.go +++ b/s2/cellid_test.go @@ -18,6 +18,7 @@ import ( "math" "reflect" "testing" + "fmt" "github.com/golang/geo/r2" "github.com/golang/geo/s1" From e89bae36b265e78aee7373b047c580a34823d8d8 Mon Sep 17 00:00:00 2001 From: fx408 Date: Thu, 11 Sep 2025 16:00:13 +0800 Subject: [PATCH 06/10] import fmt --- s2/cellid_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/s2/cellid_test.go b/s2/cellid_test.go index 78a70a1..be38eb8 100644 --- a/s2/cellid_test.go +++ b/s2/cellid_test.go @@ -15,10 +15,10 @@ package s2 import ( + "fmt" "math" "reflect" "testing" - "fmt" "github.com/golang/geo/r2" "github.com/golang/geo/s1" From 0be824ebae9296a5d6842a9796f0ca4da150c543 Mon Sep 17 00:00:00 2001 From: fx408 Date: Thu, 11 Sep 2025 20:33:48 +0800 Subject: [PATCH 07/10] calculate the number of cell IDs based on the level --- s2/cellid.go | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/s2/cellid.go b/s2/cellid.go index 101d675..f82364b 100644 --- a/s2/cellid.go +++ b/s2/cellid.go @@ -287,8 +287,7 @@ func (ci CellID) AllNeighbors(level int) []CellID { nbrSize := sizeIJ(level) - maxCellIDs := 4 * (size/nbrSize + 1) - var neighbors = make([]CellID, 0, maxCellIDs) + var neighbors = make([]CellID, 0, (2<<(level-ci.Level()))+4) // We compute the top-bottom, left-right, and diagonal neighbors in one // pass. The loop test is at the end of the loop to avoid 32-bit overflow. From acf54891baad72a589b8e7ddf6a9ec3f2cefb8ea Mon Sep 17 00:00:00 2001 From: fx408 Date: Thu, 11 Sep 2025 20:39:04 +0800 Subject: [PATCH 08/10] calculate the number of neighbors --- s2/cellid.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/s2/cellid.go b/s2/cellid.go index f82364b..f337299 100644 --- a/s2/cellid.go +++ b/s2/cellid.go @@ -287,7 +287,7 @@ func (ci CellID) AllNeighbors(level int) []CellID { nbrSize := sizeIJ(level) - var neighbors = make([]CellID, 0, (2<<(level-ci.Level()))+4) + var neighbors = make([]CellID, 0, (4<<(level-ci.Level()))+4) // We compute the top-bottom, left-right, and diagonal neighbors in one // pass. The loop test is at the end of the loop to avoid 32-bit overflow. From 5096d5cc6d5c11a04af8cbd699c1e52eb3f1fdd8 Mon Sep 17 00:00:00 2001 From: fx408 Date: Thu, 11 Sep 2025 20:41:14 +0800 Subject: [PATCH 09/10] calculate the number of neighbors --- s2/cellid.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/s2/cellid.go b/s2/cellid.go index f337299..8e2a565 100644 --- a/s2/cellid.go +++ b/s2/cellid.go @@ -276,6 +276,8 @@ func (ci CellID) AllNeighbors(level int) []CellID { return nil } + var neighbors = make([]CellID, 0, (2<<(level-ci.Level()))+4) + face, i, j, _ := ci.faceIJOrientation() // Find the coordinates of the lower left-hand leaf cell. We need to @@ -287,8 +289,6 @@ func (ci CellID) AllNeighbors(level int) []CellID { nbrSize := sizeIJ(level) - var neighbors = make([]CellID, 0, (4<<(level-ci.Level()))+4) - // We compute the top-bottom, left-right, and diagonal neighbors in one // pass. The loop test is at the end of the loop to avoid 32-bit overflow. for k := -nbrSize; ; k += nbrSize { From 17b6992a385b2dbff7369813a206c780de201853 Mon Sep 17 00:00:00 2001 From: fx408 Date: Thu, 11 Sep 2025 20:41:56 +0800 Subject: [PATCH 10/10] calculate the number of neighbors --- s2/cellid.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/s2/cellid.go b/s2/cellid.go index 8e2a565..bdf1978 100644 --- a/s2/cellid.go +++ b/s2/cellid.go @@ -276,7 +276,7 @@ func (ci CellID) AllNeighbors(level int) []CellID { return nil } - var neighbors = make([]CellID, 0, (2<<(level-ci.Level()))+4) + var neighbors = make([]CellID, 0, (4<<(level-ci.Level()))+4) face, i, j, _ := ci.faceIJOrientation()