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