diff --git a/src/GameUtils/Entity/AStar.cs b/src/GameUtils/Entity/AStar.cs index 1305f11..85ad8e5 100644 --- a/src/GameUtils/Entity/AStar.cs +++ b/src/GameUtils/Entity/AStar.cs @@ -1,3 +1,5 @@ +using System.Runtime.InteropServices; + namespace GameUtils.Entity; /// @@ -126,9 +128,10 @@ public bool Solve(T start, T end, Func heuristic, out List path) var tentativeG = currentG + weight; - if (tentativeG < gScore.GetValueOrDefault(neighbor, float.MaxValue)) + ref var neighborG = ref CollectionsMarshal.GetValueRefOrAddDefault(gScore, neighbor, out var exists); + if (!exists || tentativeG < neighborG) { - gScore[neighbor] = tentativeG; + neighborG = tentativeG; previousNodes[neighbor] = current; open.Enqueue(neighbor, tentativeG + heuristic(neighbor, end)); }