Skip to content

Commit

Permalink
Optimize stack handling by replacing List with a fixed-size array and…
Browse files Browse the repository at this point in the history
… manual index management in RcLayers
  • Loading branch information
ikpil committed Jun 26, 2024
1 parent d472d71 commit ee48892
Showing 1 changed file with 24 additions and 17 deletions.
41 changes: 24 additions & 17 deletions src/DotRecast.Recast/RcLayers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,9 @@ public static bool BuildHeightfieldLayers(RcContext ctx, RcCompactHeightfield ch
// 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)
{
Expand All @@ -281,14 +283,16 @@ public static bool BuildHeightfieldLayers(RcContext ctx, RcCompactHeightfield ch
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)
{
Expand All @@ -307,19 +311,22 @@ public static bool BuildHeightfieldLayers(RcContext ctx, RcCompactHeightfield ch
if ((ymax - ymin) >= 255)
continue;

// Deepen
stack.Add(nei);

// Mark layer id
regn.layerId = layerId;
// Merge current layers to root.
foreach (int layer in regn.layers)
if (nstack < MAX_STACK)
{
AddUnique(root.layers, layer);
}
// Deepen
stack[nstack++] = (byte)nei;

// Mark layer id
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);
root.ymin = Math.Min(root.ymin, regn.ymin);
root.ymax = Math.Max(root.ymax, regn.ymax);
}
}
}

Expand Down

0 comments on commit ee48892

Please sign in to comment.