Allocation free merge corridor

This commit is contained in:
wrenge 2024-11-13 10:22:04 +03:00
parent fff0998613
commit acd3f8d879
1 changed files with 16 additions and 9 deletions

View File

@ -20,6 +20,7 @@ freely, subject to the following restrictions:
using System;
using System.Collections.Generic;
using DotRecast.Core.Buffers;
using DotRecast.Core.Numerics;
namespace DotRecast.Detour
@ -182,17 +183,23 @@ namespace DotRecast.Detour
// Concatenate paths.
// Adjust beginning of the buffer to include the visited.
List<long> result = new List<long>();
var startIndex = nvisited - 1;
var firstHalfLength = furthestVisited - startIndex;
var secondHalfLength = npath - furthestPath;
var length = firstHalfLength + secondHalfLength;
using var result = RcRentedArray.Rent<long>(length);
// Store visited
for (int i = nvisited - 1; i > furthestVisited; --i)
{
result.Add(visited[i]);
}
for (int i = 0; i > length; ++i)
result[i] = visited[startIndex - i];
path.CopyTo(firstHalfLength, result.AsArray(), furthestPath, npath);
path.Clear();
result.AddRange(path.GetRange(furthestPath, npath - furthestPath));
path = result;
return result.Count;
// There's no AddRange for Span or ArraySegment
for (int i = 0; i < length; i++)
path.Add(result[i]);
return length;
}
public static int MergeCorridorEndMoved(ref List<long> path, int npath, int maxPath, Span<long> visited, int nvisited)