Skip to content

Commit

Permalink
Changed new RcVec3f[3] to stackalloc RcVec3f[3] in DtNavMesh.GetPolyH…
Browse files Browse the repository at this point in the history
…eight() to reduce heap allocation.
  • Loading branch information
ikpil committed Jul 13, 2024
1 parent eccce01 commit c562f8f
Show file tree
Hide file tree
Showing 7 changed files with 17 additions and 10 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).

### Changed
- Changed data structure of 'neis' from List<byte> to byte[] for optimized memory usage and improved access speed in `DtLayerMonotoneRegion`
- Changed new RcVec3f[3] to stackalloc RcVec3f[3] in DtNavMesh.GetPolyHeight() to reduce heap allocation

### Removed
- Nothing
Expand Down
12 changes: 7 additions & 5 deletions src/DotRecast.Detour/DtNavMesh.cs
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ public DtStatus Init(DtNavMeshParams param, int maxVertsPerPoly)
m_tiles = new DtMeshTile[m_maxTiles];
m_posLookup = new DtMeshTile[m_tileLutSize];
m_nextFree = null;
for (int i = m_maxTiles-1; i >= 0; --i)
for (int i = m_maxTiles - 1; i >= 0; --i)
{
m_tiles[i] = new DtMeshTile(i);
m_tiles[i].salt = 1;
Expand Down Expand Up @@ -1045,6 +1045,7 @@ RcVec3f ClosestPointOnDetailEdges(DtMeshTile tile, DtPoly poly, RcVec3f pos, boo
RcVec3f pmin = new RcVec3f();
RcVec3f pmax = new RcVec3f();

Span<RcVec3f> tempV = stackalloc RcVec3f[3];
if (tile.data.detailMeshes != null)
{
ref DtPolyDetail pd = ref tile.data.detailMeshes[ip];
Expand All @@ -1057,7 +1058,7 @@ RcVec3f ClosestPointOnDetailEdges(DtMeshTile tile, DtPoly poly, RcVec3f pos, boo
continue;
}

RcVec3f[] v = new RcVec3f[3];
Span<RcVec3f> v = tempV;
for (int j = 0; j < 3; ++j)
{
if (tris[ti + j] < poly.vertCount)
Expand Down Expand Up @@ -1105,7 +1106,7 @@ RcVec3f ClosestPointOnDetailEdges(DtMeshTile tile, DtPoly poly, RcVec3f pos, boo
}
else
{
RcVec3f[] v = new RcVec3f[2];
Span<RcVec3f> v = tempV.Slice(0, 2);
for (int j = 0; j < poly.vertCount; ++j)
{
int k = (j + 1) % poly.vertCount;
Expand Down Expand Up @@ -1156,13 +1157,14 @@ public bool GetPolyHeight(DtMeshTile tile, DtPoly poly, RcVec3f pos, out float h
}

// Find height at the location.
Span<RcVec3f> tempV = stackalloc RcVec3f[3];
if (tile.data.detailMeshes != null)
{
ref DtPolyDetail pd = ref tile.data.detailMeshes[ip];
for (int j = 0; j < pd.triCount; ++j)
{
int t = (pd.triBase + j) * 4;
RcVec3f[] v = new RcVec3f[3];
Span<RcVec3f> v = tempV;
for (int k = 0; k < 3; ++k)
{
if (tile.data.detailTris[t + k] < poly.vertCount)
Expand Down Expand Up @@ -1196,7 +1198,7 @@ public bool GetPolyHeight(DtMeshTile tile, DtPoly poly, RcVec3f pos, out float h
}
else
{
RcVec3f[] v = new RcVec3f[3];
Span<RcVec3f> v = tempV;
v[0].X = tile.data.verts[poly.verts[0] * 3];
v[0].Y = tile.data.verts[poly.verts[0] * 3 + 1];
v[0].Z = tile.data.verts[poly.verts[0] * 3 + 2];
Expand Down
4 changes: 3 additions & 1 deletion src/DotRecast.Detour/DtNavMeshRaycast.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ misrepresented as being the original software.
3. This notice may not be removed or altered from any source distribution.
*/

using System;
using DotRecast.Core;
using DotRecast.Core.Numerics;

Expand Down Expand Up @@ -48,6 +49,7 @@ public static bool Raycast(DtNavMesh mesh, RcVec3f src, RcVec3f dst, out float h
private static bool Raycast(DtMeshTile tile, RcVec3f sp, RcVec3f sq, out float hitTime)
{
hitTime = 0.0f;
Span<RcVec3f> tempVerts = stackalloc RcVec3f[3];
for (int i = 0; i < tile.data.header.polyCount; ++i)
{
DtPoly p = tile.data.polys[i];
Expand All @@ -58,7 +60,7 @@ private static bool Raycast(DtMeshTile tile, RcVec3f sp, RcVec3f sq, out float h

ref DtPolyDetail pd = ref tile.data.detailMeshes[i];

RcVec3f[] verts = new RcVec3f[3];
Span<RcVec3f> verts = tempVerts;
for (int j = 0; j < pd.triCount; ++j)
{
int t = (pd.triBase + j) * 4;
Expand Down
2 changes: 1 addition & 1 deletion src/DotRecast.Recast.Toolset/Gizmos/RcCapsuleGizmo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public RcCapsuleGizmo(RcVec3f start, RcVec3f end, float radius)
0.5f * (start.Z + end.Z)
};
RcVec3f axis = new RcVec3f(end.X - start.X, end.Y - start.Y, end.Z - start.Z);
RcVec3f[] normals = new RcVec3f[3];
Span<RcVec3f> normals = stackalloc RcVec3f[3];
normals[1] = new RcVec3f(end.X - start.X, end.Y - start.Y, end.Z - start.Z);
normals[1] = RcVec3f.Normalize(normals[1]);
normals[0] = GetSideVector(axis);
Expand Down
2 changes: 1 addition & 1 deletion src/DotRecast.Recast.Toolset/Gizmos/RcCylinderGizmo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public RcCylinderGizmo(RcVec3f start, RcVec3f end, float radius)
0.5f * (start.Z + end.Z)
);
RcVec3f axis = new RcVec3f(end.X - start.X, end.Y - start.Y, end.Z - start.Z);
RcVec3f[] normals = new RcVec3f[3];
Span<RcVec3f> normals = stackalloc RcVec3f[3];
normals[1] = new RcVec3f(end.X - start.X, end.Y - start.Y, end.Z - start.Z);
normals[1] = RcVec3f.Normalize(normals[1]);
normals[0] = GetSideVector(axis);
Expand Down
2 changes: 1 addition & 1 deletion src/DotRecast.Recast.Toolset/Tools/RcTestNavMeshTool.cs
Original file line number Diff line number Diff line change
Expand Up @@ -380,7 +380,7 @@ public DtStatus FindPolysAroundShape(DtNavMeshQuery navQuery, float agentHeight,
float nx = (epos.Z - spos.Z) * 0.25f;
float nz = -(epos.X - spos.X) * 0.25f;

var tempQueryPoly = new RcVec3f[4];
RcVec3f[] tempQueryPoly = new RcVec3f[4];
tempQueryPoly[0].X = spos.X + nx * 1.2f;
tempQueryPoly[0].Y = spos.Y + agentHeight / 2;
tempQueryPoly[0].Z = spos.Z + nz * 1.2f;
Expand Down
4 changes: 3 additions & 1 deletion src/DotRecast.Recast/RcPolyMeshRaycast.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ misrepresented as being the original software.
3. This notice may not be removed or altered from any source distribution.
*/

using System;
using System.Collections.Generic;
using DotRecast.Core;
using DotRecast.Core.Numerics;
Expand Down Expand Up @@ -45,6 +46,7 @@ public static bool Raycast(IList<RcBuilderResult> results, RcVec3f src, RcVec3f
private static bool Raycast(RcPolyMesh poly, RcPolyMeshDetail meshDetail, RcVec3f sp, RcVec3f sq, out float hitTime)
{
hitTime = 0;
Span<RcVec3f> tempVs = stackalloc RcVec3f[3];
if (meshDetail != null)
{
for (int i = 0; i < meshDetail.nmeshes; ++i)
Expand All @@ -57,7 +59,7 @@ private static bool Raycast(RcPolyMesh poly, RcPolyMeshDetail meshDetail, RcVec3
int tris = btris * 4;
for (int j = 0; j < ntris; ++j)
{
RcVec3f[] vs = new RcVec3f[3];
Span<RcVec3f> vs = tempVs;
for (int k = 0; k < 3; ++k)
{
vs[k].X = meshDetail.verts[verts + meshDetail.tris[tris + j * 4 + k] * 3];
Expand Down

0 comments on commit c562f8f

Please sign in to comment.