From 13097f9661baf50513dd20d5a19b5442d1124019 Mon Sep 17 00:00:00 2001 From: wreng Date: Wed, 21 Feb 2024 14:40:22 +0300 Subject: [PATCH] Added stackalloc where it's acceptable --- src/DotRecast.Detour/DtNavMesh.cs | 4 ++-- src/DotRecast.Detour/DtNavMeshQuery.cs | 9 +++++---- src/DotRecast.Detour/DtUtils.cs | 2 +- 3 files changed, 8 insertions(+), 7 deletions(-) diff --git a/src/DotRecast.Detour/DtNavMesh.cs b/src/DotRecast.Detour/DtNavMesh.cs index a10cf07..4b5f49d 100644 --- a/src/DotRecast.Detour/DtNavMesh.cs +++ b/src/DotRecast.Detour/DtNavMesh.cs @@ -358,8 +358,8 @@ namespace DotRecast.Detour var tbmax = tile.data.header.bmax; float qfac = tile.data.header.bvQuantFactor; // Calculate quantized box - int[] bmin = new int[3]; - int[] bmax = new int[3]; + Span bmin = stackalloc int[3]; + Span bmax = stackalloc int[3]; // dtClamp query box to world box. float minx = Math.Clamp(qmin.X, tbmin.X, tbmax.X) - tbmin.X; float miny = Math.Clamp(qmin.Y, tbmin.Y, tbmax.Y) - tbmin.Y; diff --git a/src/DotRecast.Detour/DtNavMeshQuery.cs b/src/DotRecast.Detour/DtNavMeshQuery.cs index fae330c..c4dd060 100644 --- a/src/DotRecast.Detour/DtNavMeshQuery.cs +++ b/src/DotRecast.Detour/DtNavMeshQuery.cs @@ -583,8 +583,8 @@ namespace DotRecast.Detour var tbmax = tile.data.header.bmax; float qfac = tile.data.header.bvQuantFactor; // Calculate quantized box - int[] bmin = new int[3]; - int[] bmax = new int[3]; + Span bmin = stackalloc int[3]; + Span bmax = stackalloc int[3]; // dtClamp query box to world box. float minx = Math.Clamp(qmin.X, tbmin.X, tbmax.X) - tbmin.X; float miny = Math.Clamp(qmin.Y, tbmin.Y, tbmax.Y) - tbmin.Y; @@ -1824,6 +1824,9 @@ namespace DotRecast.Detour float[] verts = new float[m_nav.GetMaxVertsPerPoly() * 3]; + const int MAX_NEIS = 8; + Span neis = stackalloc long[MAX_NEIS]; + while (0 < stack.Count) { // Pop front. @@ -1854,9 +1857,7 @@ namespace DotRecast.Detour for (int i = 0, j = curPoly.vertCount - 1; i < curPoly.vertCount; j = i++) { // Find links to neighbours. - int MAX_NEIS = 8; int nneis = 0; - long[] neis = new long[MAX_NEIS]; if ((curPoly.neis[j] & DtNavMesh.DT_EXT_LINK) != 0) { diff --git a/src/DotRecast.Detour/DtUtils.cs b/src/DotRecast.Detour/DtUtils.cs index 894710b..91644e5 100644 --- a/src/DotRecast.Detour/DtUtils.cs +++ b/src/DotRecast.Detour/DtUtils.cs @@ -64,7 +64,7 @@ namespace DotRecast.Detour /// @param[in] bmax Maximum bounds of box B. [(x, y, z)] /// @return True if the two AABB's overlap. /// @see dtOverlapBounds - public static bool OverlapQuantBounds(int[] amin, int[] amax, int[] bmin, int[] bmax) + public static bool OverlapQuantBounds(Span amin, Span amax, Span bmin, Span bmax) { bool overlap = true; overlap = (amin[0] > bmax[0] || amax[0] < bmin[0]) ? false : overlap;