Removed allocations from MergeCorridorStartMoved

This commit is contained in:
wrenge 2024-11-13 12:09:48 +03:00
parent 2c6f6a50cc
commit 49da3fb454
1 changed files with 13 additions and 10 deletions

View File

@ -20,7 +20,10 @@ freely, subject to the following restrictions:
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using DotRecast.Core.Buffers;
using DotRecast.Core.Collections;
using DotRecast.Core.Numerics; using DotRecast.Core.Numerics;
using CollectionExtensions = DotRecast.Core.Collections.CollectionExtensions;
namespace DotRecast.Detour namespace DotRecast.Detour
{ {
@ -180,20 +183,20 @@ namespace DotRecast.Detour
} }
// Concatenate paths. // Concatenate paths.
var endIndex = nvisited - 1;
var length1 = endIndex - furthestVisited;
var length2 = npath - furthestPath;
using var result = RcRentedArray.Rent<long>(length1 + length2);
// Adjust beginning of the buffer to include the visited. // Adjust beginning of the buffer to include the visited.
List<long> result = new List<long>();
// Store visited // Store visited
for (int i = nvisited - 1; i > furthestVisited; --i) for (int i = 0; i < length1; ++i)
{ result[i] = visited[endIndex - i];
result.Add(visited[i]);
}
result.AddRange(path.GetRange(furthestPath, npath - furthestPath)); path.CopyTo(furthestPath, result.AsArray(), length1, length2);
path.Clear(); path.Clear();
path.AddRange(result); CollectionExtensions.AddRange(path, result.AsSpan());
return result.Count; return result.Length;
} }
public static int MergeCorridorEndMoved(List<long> path, int npath, int maxPath, Span<long> visited, int nvisited) public static int MergeCorridorEndMoved(List<long> path, int npath, int maxPath, Span<long> visited, int nvisited)