From 49da3fb4544fc32b14a192bde122efb5c76d35f5 Mon Sep 17 00:00:00 2001 From: wrenge Date: Wed, 13 Nov 2024 12:09:48 +0300 Subject: [PATCH] Removed allocations from MergeCorridorStartMoved --- src/DotRecast.Detour/DtPathUtils.cs | 23 +++++++++++++---------- 1 file changed, 13 insertions(+), 10 deletions(-) diff --git a/src/DotRecast.Detour/DtPathUtils.cs b/src/DotRecast.Detour/DtPathUtils.cs index 688f2a0..2d77f6e 100644 --- a/src/DotRecast.Detour/DtPathUtils.cs +++ b/src/DotRecast.Detour/DtPathUtils.cs @@ -20,7 +20,10 @@ freely, subject to the following restrictions: using System; using System.Collections.Generic; +using DotRecast.Core.Buffers; +using DotRecast.Core.Collections; using DotRecast.Core.Numerics; +using CollectionExtensions = DotRecast.Core.Collections.CollectionExtensions; namespace DotRecast.Detour { @@ -180,20 +183,20 @@ namespace DotRecast.Detour } // Concatenate paths. - + var endIndex = nvisited - 1; + var length1 = endIndex - furthestVisited; + var length2 = npath - furthestPath; + using var result = RcRentedArray.Rent(length1 + length2); // Adjust beginning of the buffer to include the visited. - List result = new List(); // Store visited - for (int i = nvisited - 1; i > furthestVisited; --i) - { - result.Add(visited[i]); - } - - result.AddRange(path.GetRange(furthestPath, npath - furthestPath)); + for (int i = 0; i < length1; ++i) + result[i] = visited[endIndex - i]; + + path.CopyTo(furthestPath, result.AsArray(), length1, length2); path.Clear(); - path.AddRange(result); - return result.Count; + CollectionExtensions.AddRange(path, result.AsSpan()); + return result.Length; } public static int MergeCorridorEndMoved(List path, int npath, int maxPath, Span visited, int nvisited)