Skip to content

Commit

Permalink
Changed data structure of 'neis' from List<byte> to byte[] for optimi…
Browse files Browse the repository at this point in the history
…zed memory usage and improved access speed in `DtLayerMonotoneRegion`
  • Loading branch information
ikpil committed Jul 12, 2024
1 parent 00950b1 commit 990dbcf
Show file tree
Hide file tree
Showing 7 changed files with 28 additions and 16 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).
- Nothing

### Changed
- Nothing
- Changed data structure of 'neis' from List<byte> to byte[] for optimized memory usage and improved access speed in `DtLayerMonotoneRegion`

### Removed
- Nothing
Expand Down
8 changes: 8 additions & 0 deletions src/DotRecast.Core/Numerics/RcVec.cs
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,14 @@ public static void Copy(float[] @out, int n, float[] @in, int m)
@out[n + 1] = @in[m + 1];
@out[n + 2] = @in[m + 2];
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void Copy(Span<float> @out, int n, Span<float> @in, int m)
{
@out[n + 0] = @in[m + 0];
@out[n + 1] = @in[m + 1];
@out[n + 2] = @in[m + 2];
}

/// Returns the distance between two points.
/// @param[in] v1 A point. [(x, y, z)]
Expand Down
3 changes: 2 additions & 1 deletion src/DotRecast.Detour.TileCache/DtLayerMonotoneRegion.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ public class DtLayerMonotoneRegion
public const int DT_LAYER_MAX_NEIS = 16;

public int area;
public List<byte> neis = new List<byte>(DT_LAYER_MAX_NEIS);
public byte[] neis = new byte[DT_LAYER_MAX_NEIS];
public byte nneis;
public byte regId;
public byte areaId;
};
Expand Down
20 changes: 12 additions & 8 deletions src/DotRecast.Detour.TileCache/DtTileCacheBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -181,8 +181,8 @@ public static void BuildTileCacheRegions(DtTileCacheLayer layer, int walkableCli
byte rai = layer.regs[ymi];
if (rai != 0xff && rai != ri)
{
AddUniqueLast(regs[ri].neis, rai);
AddUniqueLast(regs[rai].neis, ri);
AddUniqueLast(regs[ri].neis, ref regs[ri].nneis, rai);
AddUniqueLast(regs[rai].neis, ref regs[rai].nneis, ri);
}
}
}
Expand All @@ -197,8 +197,9 @@ public static void BuildTileCacheRegions(DtTileCacheLayer layer, int walkableCli

int merge = -1;
int mergea = 0;
foreach (int nei in reg.neis)
for (int j = 0; j < reg.nneis; ++j)
{
byte nei = reg.neis[j];
DtLayerMonotoneRegion regn = regs[nei];
if (reg.regId == regn.regId)
continue;
Expand Down Expand Up @@ -246,12 +247,13 @@ public static void BuildTileCacheRegions(DtTileCacheLayer layer, int walkableCli
}
}

public static void AddUniqueLast(List<byte> a, byte v)
public static void AddUniqueLast(byte[] a, ref byte an, byte v)
{
int n = a.Count;
int n = an;
if (n > 0 && a[n - 1] == v)
return;
a.Add(v);
a[an] = v;
an++;
}

public static bool IsConnected(DtTileCacheLayer layer, int ia, int ib, int walkableClimb)
Expand All @@ -271,9 +273,11 @@ public static bool CanMerge(int oldRegId, int newRegId, DtLayerMonotoneRegion[]
DtLayerMonotoneRegion reg = regs[i];
if (reg.regId != oldRegId)
continue;
foreach (int nei in reg.neis)

int nnei = reg.nneis;
for (int j = 0; j < nnei ; ++j)
{
if (regs[nei].regId == newRegId)
if (regs[reg.neis[j]].regId == newRegId)
count++;
}
}
Expand Down
1 change: 0 additions & 1 deletion src/DotRecast.Recast/RcLayers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ public static class RcLayers
const int RC_MAX_LAYERS = RcRecast.RC_NOT_CONNECTED;
const int RC_MAX_NEIS = 16;


private static void AddUnique(List<int> a, int v)
{
if (!a.Contains(v))
Expand Down
4 changes: 2 additions & 2 deletions src/DotRecast.Recast/RcRasterizations.cs
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@ public static void AddSpan(RcHeightfield heightfield, int x, int z, int min, int
/// @param[out] outVerts2Count The number of resulting polygon 2 vertices
/// @param[in] axisOffset THe offset along the specified axis
/// @param[in] axis The separating axis
private static void DividePoly(float[] inVerts, int inVertsOffset, int inVertsCount,
private static void DividePoly(Span<float> inVerts, int inVertsOffset, int inVertsCount,
int outVerts1, out int outVerts1Count,
int outVerts2, out int outVerts2Count,
float axisOffset, int axis)
Expand Down Expand Up @@ -322,7 +322,7 @@ private static bool RasterizeTri(float[] verts, int v0, int v1, int v2,
z1 = Math.Clamp(z1, 0, h - 1);

// Clip the triangle into all grid cells it touches.
float[] buf = new float[7 * 3 * 4];
Span<float> buf = stackalloc float[7 * 3 * 4];
int @in = 0;
int inRow = 7 * 3;
int p1 = inRow + 7 * 3;
Expand Down
6 changes: 3 additions & 3 deletions src/DotRecast.Recast/RcRegions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1665,8 +1665,8 @@ public static void BuildRegions(RcContext ctx, RcCompactHeightfield chf, int min

ctx.StartTimer(RcTimerLabel.RC_TIMER_BUILD_REGIONS_WATERSHED);

int LOG_NB_STACKS = 3;
int NB_STACKS = 1 << LOG_NB_STACKS;
const int LOG_NB_STACKS = 3;
const int NB_STACKS = 1 << LOG_NB_STACKS;
List<List<RcLevelStackEntry>> lvlStacks = new List<List<RcLevelStackEntry>>();
for (int i = 0; i < NB_STACKS; ++i)
{
Expand Down Expand Up @@ -1756,7 +1756,7 @@ public static void BuildRegions(RcContext ctx, RcCompactHeightfield chf, int min
ExpandRegions(expandIters * 8, 0, chf, srcReg, srcDist, stack, true);

ctx.StopTimer(RcTimerLabel.RC_TIMER_BUILD_REGIONS_WATERSHED);

ctx.StartTimer(RcTimerLabel.RC_TIMER_BUILD_REGIONS_FILTER);

// Merge regions and filter out small regions.
Expand Down

0 comments on commit 990dbcf

Please sign in to comment.