Optimize stack handling by replacing List with a fixed-size array and manual index management in RcLayers

This commit is contained in:
ikpil 2024-06-26 23:46:40 +09:00
parent d472d71795
commit ee48892223
1 changed files with 24 additions and 17 deletions

View File

@ -268,7 +268,9 @@ namespace DotRecast.Recast
// Create 2D layers from regions.
byte layerId = 0;
List<int> stack = new List<int>();
const int MAX_STACK = 64;
Span<byte> stack = stackalloc byte[MAX_STACK];
int nstack = 0;
for (int i = 0; i < nregs; ++i)
{
@ -281,14 +283,16 @@ namespace DotRecast.Recast
root.layerId = layerId;
root.@base = true;
stack.Add(i);
nstack = 0;
stack[nstack++] = ((byte)i);
while (stack.Count != 0)
while (0 != nstack)
{
// Pop front
int pop = stack[0]; // TODO : 여기에 stack 처럼 작동하게 했는데, 스택인지는 모르겠음
stack.RemoveAt(0);
RcLayerRegion reg = regs[pop];
RcLayerRegion reg = regs[stack[0]];
nstack--;
for (int j = 0; j < nstack; ++j)
stack[j] = stack[j+1];
foreach (int nei in reg.neis)
{
@ -307,8 +311,10 @@ namespace DotRecast.Recast
if ((ymax - ymin) >= 255)
continue;
if (nstack < MAX_STACK)
{
// Deepen
stack.Add(nei);
stack[nstack++] = (byte)nei;
// Mark layer id
regn.layerId = layerId;
@ -322,6 +328,7 @@ namespace DotRecast.Recast
root.ymax = Math.Max(root.ymax, regn.ymax);
}
}
}
layerId++;
}