From 50db827e57eb2f710773f75662ee1fa77e67d411 Mon Sep 17 00:00:00 2001 From: wisplite Date: Thu, 14 May 2026 10:42:28 -0500 Subject: [PATCH] pre-allocate queue to save on reallocations later --- astar.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/astar.go b/astar.go index 0745588..3e6b4bd 100644 --- a/astar.go +++ b/astar.go @@ -60,7 +60,7 @@ func (a *AStar) Init(width int, height int) { a.gridTypes = make([]byte, width*height) a.gScores = make([]float32, width*height) a.parents = make([]byte, width*height) - a.openSet = make(PriorityQueue, 0) + a.openSet = make(PriorityQueue, 0, 2000000) // pre-allocate space for 2 million cells to avoid reallocations a.closedSet = make([]bool, width*height) a.heuristic = func(x int, y int, endX int, endY int) float32 { return float32(math.Abs(float64(x-endX)) + math.Abs(float64(y-endY))) // Manhattan distance default @@ -89,7 +89,7 @@ func (a *AStar) RebuildGrid(width int, height int) { a.gridTypes = make([]byte, width*height) a.gScores = make([]float32, width*height) a.parents = make([]byte, width*height) - a.openSet = make(PriorityQueue, 0) + a.openSet = make(PriorityQueue, 0, 2000000) a.closedSet = make([]bool, width*height) a.width = width a.height = height