From 815a83e3cb87841a20dace847ae4927ef6456735 Mon Sep 17 00:00:00 2001 From: wrenge Date: Wed, 13 Nov 2024 13:42:43 +0300 Subject: [PATCH] Use precached queue instead of linked list --- src/DotRecast.Detour/DtNavMeshQuery.cs | 25 +++++++++++++------------ 1 file changed, 13 insertions(+), 12 deletions(-) diff --git a/src/DotRecast.Detour/DtNavMeshQuery.cs b/src/DotRecast.Detour/DtNavMeshQuery.cs index ee0e699..7ff5d4b 100644 --- a/src/DotRecast.Detour/DtNavMeshQuery.cs +++ b/src/DotRecast.Detour/DtNavMeshQuery.cs @@ -1837,6 +1837,7 @@ namespace DotRecast.Detour return DtStatus.DT_SUCCESS | (straightPathCount >= maxStraightPath ? DtStatus.DT_BUFFER_TOO_SMALL : DtStatus.DT_STATUS_NOTHING); } + private readonly Queue MoveAlongSurface_queue = new Queue(); /// @par /// /// This method is optimized for small delta movement and a small number of @@ -1892,8 +1893,8 @@ namespace DotRecast.Detour startNode.total = 0; startNode.id = startRef; startNode.flags = DtNodeFlags.DT_NODE_CLOSED; - LinkedList stack = new LinkedList(); - stack.AddLast(startNode); + MoveAlongSurface_queue.Clear(); + MoveAlongSurface_queue.Enqueue(startNode); RcVec3f bestPos = new RcVec3f(); float bestDist = float.MaxValue; @@ -1909,11 +1910,10 @@ namespace DotRecast.Detour const int MAX_NEIS = 8; Span neis = stackalloc long[MAX_NEIS]; - while (0 < stack.Count) + while (0 < MoveAlongSurface_queue.Count) { // Pop front. - DtNode curNode = stack.First?.Value; - stack.RemoveFirst(); + DtNode curNode = MoveAlongSurface_queue.Dequeue(); // Get poly and tile. // The API input has been checked already, skip checking internal data. @@ -2012,7 +2012,7 @@ namespace DotRecast.Detour // Mark as the node as visited and push to queue. neighbourNode.pidx = m_tinyNodePool.GetNodeIdx(curNode); neighbourNode.flags |= DtNodeFlags.DT_NODE_CLOSED; - stack.AddLast(neighbourNode); + MoveAlongSurface_queue.Enqueue(neighbourNode); } } } @@ -2903,6 +2903,7 @@ namespace DotRecast.Detour return DtStatus.DT_SUCCESS; } + private readonly Queue FindLocalNeighbourhood_queue = new Queue(); /// @par /// /// This method is optimized for a small search radius and small number of result @@ -2954,8 +2955,8 @@ namespace DotRecast.Detour startNode.pidx = 0; startNode.id = startRef; startNode.flags = DtNodeFlags.DT_NODE_CLOSED; - LinkedList stack = new LinkedList(); - stack.AddLast(startNode); + FindLocalNeighbourhood_queue.Clear(); + FindLocalNeighbourhood_queue.Enqueue(startNode); resultRef.Add(startNode.id); resultParent.Add(0L); @@ -2965,11 +2966,11 @@ namespace DotRecast.Detour Span pa = stackalloc float[m_nav.GetMaxVertsPerPoly() * 3]; Span pb = stackalloc float[m_nav.GetMaxVertsPerPoly() * 3]; - while (0 < stack.Count) + while (0 < FindLocalNeighbourhood_queue.Count) { // Pop front. - DtNode curNode = stack.First?.Value; - stack.RemoveFirst(); + + DtNode curNode = FindLocalNeighbourhood_queue.Dequeue(); // Get poly and tile. // The API input has been checked already, skip checking internal data. @@ -3082,7 +3083,7 @@ namespace DotRecast.Detour resultRef.Add(neighbourRef); resultParent.Add(curRef); - stack.AddLast(neighbourNode); + FindLocalNeighbourhood_queue.Enqueue(neighbourNode); } }