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