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)