From d8ae9f229742c0f78a223bcae9a8a56adc3dc6c6 Mon Sep 17 00:00:00 2001 From: ikpil Date: Tue, 23 Jan 2024 22:49:36 +0900 Subject: [PATCH 1/6] fix: SOH issue (#41) --- .../DtObstacleAvoidanceQuery.cs | 3 +- src/DotRecast.Detour/DtNavMeshQuery.cs | 41 +++++++++++-------- 2 files changed, 25 insertions(+), 19 deletions(-) diff --git a/src/DotRecast.Detour.Crowd/DtObstacleAvoidanceQuery.cs b/src/DotRecast.Detour.Crowd/DtObstacleAvoidanceQuery.cs index 1b6e240..fc7057c 100644 --- a/src/DotRecast.Detour.Crowd/DtObstacleAvoidanceQuery.cs +++ b/src/DotRecast.Detour.Crowd/DtObstacleAvoidanceQuery.cs @@ -20,6 +20,7 @@ freely, subject to the following restrictions: using System; using DotRecast.Core; +using DotRecast.Core.Buffers; using DotRecast.Core.Numerics; @@ -402,7 +403,7 @@ namespace DotRecast.Detour.Crowd debug.Reset(); // Build sampling pattern aligned to desired velocity. - float[] pat = new float[(DT_MAX_PATTERN_DIVS * DT_MAX_PATTERN_RINGS + 1) * 2]; + using var pat = RcRentedArray.RentDisposableArray((DT_MAX_PATTERN_DIVS * DT_MAX_PATTERN_RINGS + 1) * 2); int npat = 0; int ndivs = m_params.adaptiveDivs; diff --git a/src/DotRecast.Detour/DtNavMeshQuery.cs b/src/DotRecast.Detour/DtNavMeshQuery.cs index fae330c..82fe4fe 100644 --- a/src/DotRecast.Detour/DtNavMeshQuery.cs +++ b/src/DotRecast.Detour/DtNavMeshQuery.cs @@ -21,6 +21,7 @@ freely, subject to the following restrictions: using System; using System.Collections.Generic; using DotRecast.Core; +using DotRecast.Core.Buffers; using DotRecast.Core.Collections; using DotRecast.Core.Numerics; @@ -36,6 +37,7 @@ namespace DotRecast.Detour protected readonly DtNodeQueue m_openList; protected DtQueryData m_query; + protected readonly DtNodePool m_tinyNodePool; /// < Sliced query state. public DtNavMeshQuery(DtNavMesh nav) @@ -44,6 +46,7 @@ namespace DotRecast.Detour m_tinyNodePool = new DtNodePool(); m_nodePool = new DtNodePool(); m_openList = new DtNodeQueue(); + m_tinyNodePool = new DtNodePool(); } /// Returns random location on navmesh. @@ -457,16 +460,16 @@ namespace DotRecast.Detour } // Collect vertices. - float[] verts = new float[m_nav.GetMaxVertsPerPoly() * 3]; - float[] edged = new float[m_nav.GetMaxVertsPerPoly()]; - float[] edget = new float[m_nav.GetMaxVertsPerPoly()]; + using var verts = RcRentedArray.RentDisposableArray(m_nav.GetMaxVertsPerPoly() * 3); + using var edged = RcRentedArray.RentDisposableArray(m_nav.GetMaxVertsPerPoly()); + using var edget = RcRentedArray.RentDisposableArray(m_nav.GetMaxVertsPerPoly()); int nv = poly.vertCount; for (int i = 0; i < nv; ++i) { - RcArrays.Copy(tile.data.verts, poly.verts[i] * 3, verts, i * 3, 3); + RcArrays.Copy(tile.data.verts, poly.verts[i] * 3, verts.AsRentedArray(), i * 3, 3); } - if (DtUtils.DistancePtPolyEdgesSqr(pos, verts, nv, edged, edget)) + if (DtUtils.DistancePtPolyEdgesSqr(pos, verts.AsRentedArray(), nv, edged.AsRentedArray(), edget.AsRentedArray())) { closest = pos; } @@ -486,7 +489,7 @@ namespace DotRecast.Detour int va = imin * 3; int vb = ((imin + 1) % nv) * 3; - closest = RcVecUtils.Lerp(verts, va, vb, edget[imin]); + closest = RcVecUtils.Lerp(verts.AsRentedArray(), va, vb, edget[imin]); } return DtStatus.DT_SUCCESS; @@ -1793,7 +1796,9 @@ namespace DotRecast.Detour resultPos = RcVec3f.Zero; if (null != visited) + { visited.Clear(); + } // Validate input if (!m_nav.IsValidPolyRef(startRef) || !startPos.IsFinite() @@ -1822,7 +1827,7 @@ namespace DotRecast.Detour var searchPos = RcVec3f.Lerp(startPos, endPos, 0.5f); float searchRadSqr = RcMath.Sqr(RcVec3f.Distance(startPos, endPos) / 2.0f + 0.001f); - float[] verts = new float[m_nav.GetMaxVertsPerPoly() * 3]; + using var verts = RcRentedArray.RentDisposableArray(m_nav.GetMaxVertsPerPoly() * 3); while (0 < stack.Count) { @@ -1839,11 +1844,11 @@ namespace DotRecast.Detour int nverts = curPoly.vertCount; for (int i = 0; i < nverts; ++i) { - RcArrays.Copy(curTile.data.verts, curPoly.verts[i] * 3, verts, i * 3, 3); + RcArrays.Copy(curTile.data.verts, curPoly.verts[i] * 3, verts.AsRentedArray(), i * 3, 3); } // If target is inside the poly, stop search. - if (DtUtils.PointInPolygon(endPos, verts, nverts)) + if (DtUtils.PointInPolygon(endPos, verts.AsRentedArray(), nverts)) { bestNode = curNode; bestPos = endPos; @@ -1856,7 +1861,7 @@ namespace DotRecast.Detour // Find links to neighbours. int MAX_NEIS = 8; int nneis = 0; - long[] neis = new long[MAX_NEIS]; + using var neis = RcRentedArray.RentDisposableArray(MAX_NEIS); if ((curPoly.neis[j] & DtNavMesh.DT_EXT_LINK) != 0) { @@ -1896,11 +1901,11 @@ namespace DotRecast.Detour // Wall edge, calc distance. int vj = j * 3; int vi = i * 3; - var distSqr = DtUtils.DistancePtSegSqr2D(endPos, verts, vj, vi, out var tseg); + var distSqr = DtUtils.DistancePtSegSqr2D(endPos, verts.AsRentedArray(), vj, vi, out var tseg); if (distSqr < bestDist) { // Update nearest distance. - bestPos = RcVecUtils.Lerp(verts, vj, vi, tseg); + bestPos = RcVecUtils.Lerp(verts.AsRentedArray(), vj, vi, tseg); bestDist = distSqr; bestNode = curNode; } @@ -1920,7 +1925,7 @@ namespace DotRecast.Detour // TODO: Maybe should use GetPortalPoints(), but this one is way faster. int vj = j * 3; int vi = i * 3; - var distSqr = DtUtils.DistancePtSegSqr2D(searchPos, verts, vj, vi, out var _); + var distSqr = DtUtils.DistancePtSegSqr2D(searchPos, verts.AsRentedArray(), vj, vi, out var _); if (distSqr > searchRadSqr) { continue; @@ -2873,8 +2878,8 @@ namespace DotRecast.Detour float radiusSqr = RcMath.Sqr(radius); - float[] pa = new float[m_nav.GetMaxVertsPerPoly() * 3]; - float[] pb = new float[m_nav.GetMaxVertsPerPoly() * 3]; + using var pa = RcRentedArray.RentDisposableArray(m_nav.GetMaxVertsPerPoly() * 3); + using var pb = RcRentedArray.RentDisposableArray(m_nav.GetMaxVertsPerPoly() * 3); while (0 < stack.Count) { @@ -2945,7 +2950,7 @@ namespace DotRecast.Detour int npa = neighbourPoly.vertCount; for (int k = 0; k < npa; ++k) { - RcArrays.Copy(neighbourTile.data.verts, neighbourPoly.verts[k] * 3, pa, k * 3, 3); + RcArrays.Copy(neighbourTile.data.verts, neighbourPoly.verts[k] * 3, pa.AsRentedArray(), k * 3, 3); } bool overlap = false; @@ -2976,10 +2981,10 @@ namespace DotRecast.Detour int npb = pastPoly.vertCount; for (int k = 0; k < npb; ++k) { - RcArrays.Copy(pastTile.data.verts, pastPoly.verts[k] * 3, pb, k * 3, 3); + RcArrays.Copy(pastTile.data.verts, pastPoly.verts[k] * 3, pb.AsRentedArray(), k * 3, 3); } - if (DtUtils.OverlapPolyPoly2D(pa, npa, pb, npb)) + if (DtUtils.OverlapPolyPoly2D(pa.AsRentedArray(), npa, pb.AsRentedArray(), npb)) { overlap = true; break; From 30bce34eaf7cc30cb1b7d768f7865b6e6e46419d Mon Sep 17 00:00:00 2001 From: ikpil Date: Wed, 24 Jan 2024 00:08:42 +0900 Subject: [PATCH 2/6] fix: SOH issue step2 (#41) https://github.com/ikpil/DotRecast/issues/41 --- src/DotRecast.Detour.Crowd/DtCrowd.cs | 1 + 1 file changed, 1 insertion(+) diff --git a/src/DotRecast.Detour.Crowd/DtCrowd.cs b/src/DotRecast.Detour.Crowd/DtCrowd.cs index 892c4b3..4afc826 100644 --- a/src/DotRecast.Detour.Crowd/DtCrowd.cs +++ b/src/DotRecast.Detour.Crowd/DtCrowd.cs @@ -22,6 +22,7 @@ using System; using System.Collections.Generic; using System.Threading.Tasks; using DotRecast.Core; +using DotRecast.Core.Buffers; using DotRecast.Core.Collections; using DotRecast.Core.Numerics; From 2f2d68c29f5d241ba063f0e70a1c71d9a07acd0a Mon Sep 17 00:00:00 2001 From: ikpil Date: Wed, 24 Jan 2024 13:48:51 +0900 Subject: [PATCH 3/6] fix: SOH issue step3 (#41) - https://github.com/ikpil/DotRecast/issues/41 --- src/DotRecast.Detour/DtNavMeshQuery.cs | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/src/DotRecast.Detour/DtNavMeshQuery.cs b/src/DotRecast.Detour/DtNavMeshQuery.cs index 82fe4fe..ad7debc 100644 --- a/src/DotRecast.Detour/DtNavMeshQuery.cs +++ b/src/DotRecast.Detour/DtNavMeshQuery.cs @@ -140,18 +140,18 @@ namespace DotRecast.Detour } // Randomly pick point on polygon. - float[] verts = new float[3 * m_nav.GetMaxVertsPerPoly()]; - float[] areas = new float[m_nav.GetMaxVertsPerPoly()]; - RcArrays.Copy(tile.data.verts, poly.verts[0] * 3, verts, 0, 3); + using var verts = RcRentedArray.RentDisposableArray(3 * m_nav.GetMaxVertsPerPoly()); + using var areas = RcRentedArray.RentDisposableArray(m_nav.GetMaxVertsPerPoly()); + RcArrays.Copy(tile.data.verts, poly.verts[0] * 3, verts.AsRentedArray(), 0, 3); for (int j = 1; j < poly.vertCount; ++j) { - RcArrays.Copy(tile.data.verts, poly.verts[j] * 3, verts, j * 3, 3); + RcArrays.Copy(tile.data.verts, poly.verts[j] * 3, verts.AsRentedArray(), j * 3, 3); } float s = frand.Next(); float t = frand.Next(); - var pt = DtUtils.RandomPointInConvexPoly(verts, poly.vertCount, areas, s, t); + var pt = DtUtils.RandomPointInConvexPoly(verts.AsRentedArray(), poly.vertCount, areas.AsRentedArray(), s, t); ClosestPointOnPoly(polyRef, pt, out var closest, out var _); randomRef = polyRef; @@ -389,8 +389,8 @@ namespace DotRecast.Detour float s = frand.Next(); float t = frand.Next(); - float[] areas = new float[randomPolyVerts.Length / 3]; - RcVec3f pt = DtUtils.RandomPointInConvexPoly(randomPolyVerts, randomPolyVerts.Length / 3, areas, s, t); + using var areas = RcRentedArray.RentDisposableArray(randomPolyVerts.Length / 3); + RcVec3f pt = DtUtils.RandomPointInConvexPoly(randomPolyVerts, randomPolyVerts.Length / 3, areas.AsRentedArray(), s, t); ClosestPointOnPoly(randomPolyRef, pt, out var closest, out var _); randomRef = randomPolyRef; From b0644a70b71eb58e53d00a04a0708491b2c8c6ba Mon Sep 17 00:00:00 2001 From: ikpil Date: Thu, 25 Jan 2024 00:43:40 +0900 Subject: [PATCH 4/6] fix : SOH issue (#41) - https://github.com/ikpil/DotRecast/issues/41#issuecomment-1908359895 --- src/DotRecast.Detour/DtNavMesh.cs | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/DotRecast.Detour/DtNavMesh.cs b/src/DotRecast.Detour/DtNavMesh.cs index a10cf07..094bd5d 100644 --- a/src/DotRecast.Detour/DtNavMesh.cs +++ b/src/DotRecast.Detour/DtNavMesh.cs @@ -21,6 +21,7 @@ freely, subject to the following restrictions: using System; using System.Collections.Generic; using DotRecast.Core; +using DotRecast.Core.Buffers; using DotRecast.Core.Numerics; namespace DotRecast.Detour @@ -1246,14 +1247,14 @@ namespace DotRecast.Detour int ip = poly.index; - float[] verts = new float[m_maxVertPerPoly * 3]; + using var verts = RcRentedArray.RentDisposableArray(m_maxVertPerPoly * 3); int nv = poly.vertCount; for (int i = 0; i < nv; ++i) { - RcArrays.Copy(tile.data.verts, poly.verts[i] * 3, verts, i * 3, 3); + RcArrays.Copy(tile.data.verts, poly.verts[i] * 3, verts.AsRentedArray(), i * 3, 3); } - if (!DtUtils.PointInPolygon(pos, verts, nv)) + if (!DtUtils.PointInPolygon(pos, verts.AsRentedArray(), nv)) { return false; } From 0c6921ad6b3bcf7b4a34e349e5a33f1f72b75ea7 Mon Sep 17 00:00:00 2001 From: ikpil Date: Thu, 25 Jan 2024 00:57:12 +0900 Subject: [PATCH 5/6] fix: SOH issue (#41) - https://github.com/ikpil/DotRecast/issues/41#issuecomment-1908365394 --- test/DotRecast.Core.Test/RcRentedArrayTest.cs | 49 +++++++++++++++++++ 1 file changed, 49 insertions(+) diff --git a/test/DotRecast.Core.Test/RcRentedArrayTest.cs b/test/DotRecast.Core.Test/RcRentedArrayTest.cs index 07d1f1b..c314fc3 100644 --- a/test/DotRecast.Core.Test/RcRentedArrayTest.cs +++ b/test/DotRecast.Core.Test/RcRentedArrayTest.cs @@ -1,6 +1,9 @@ using System; using System.Collections.Generic; +using System.Linq; +using System.Xml; using DotRecast.Core.Buffers; +using Microsoft.VisualStudio.TestPlatform.CommunicationUtilities.Serialization; using NUnit.Framework; namespace DotRecast.Core.Test; @@ -56,4 +59,50 @@ public class RcRentedArrayTest Assert.Throws(() => rentedArray[^1] = 0); } } + + [Test] + public void TestSame() + { + // not same + { + using var r1 = RcRentedArray.RentDisposableArray(1024); + using var r2 = RcRentedArray.RentDisposableArray(1024); + + Assert.That(r2.AsRentedArray() != r1.AsRentedArray(), Is.EqualTo(true)); + } + + // same + { + // error case + float[] r1Array; + using (var r1 = RcRentedArray.RentDisposableArray(1024)) + { + r1Array = r1.AsRentedArray(); + for (int i = 0; i < r1.Length; ++i) + { + r1[i] = 123; + } + } + + using var r2 = RcRentedArray.RentDisposableArray(1024); + + Assert.That(r2.AsRentedArray() == r1Array, Is.EqualTo(true)); + Assert.That(r2.AsRentedArray().Sum(), Is.EqualTo(0)); + } + } + + [Test] + public void TestDispose() + { + var r1 = RcRentedArray.RentDisposableArray(1024); + for (int i = 0; i < r1.Length; ++i) + { + r1[i] = 123; + } + + Assert.That(r1.IsDisposed, Is.EqualTo(false)); + r1.Dispose(); + Assert.That(r1.IsDisposed, Is.EqualTo(true)); + Assert.That(r1.AsRentedArray(), Is.Null); + } } \ No newline at end of file From 6a57c067acba1ae2ce6f09183e3cdb97a3c6b3f9 Mon Sep 17 00:00:00 2001 From: ikpil Date: Thu, 25 Jan 2024 01:00:03 +0900 Subject: [PATCH 6/6] fix: SOH issue(#41) https://github.com/ikpil/DotRecast/issues/41#issuecomment-1908367226 array benchmark benchmark array test step2 test ss --- .../DtObstacleAvoidanceQuery.cs | 2 +- src/DotRecast.Detour/DtNavMesh.cs | 6 +- src/DotRecast.Detour/DtNavMeshQuery.cs | 56 +++++++++---------- test/DotRecast.Core.Test/RcRentedArrayTest.cs | 20 +++---- 4 files changed, 41 insertions(+), 43 deletions(-) diff --git a/src/DotRecast.Detour.Crowd/DtObstacleAvoidanceQuery.cs b/src/DotRecast.Detour.Crowd/DtObstacleAvoidanceQuery.cs index fc7057c..c06b210 100644 --- a/src/DotRecast.Detour.Crowd/DtObstacleAvoidanceQuery.cs +++ b/src/DotRecast.Detour.Crowd/DtObstacleAvoidanceQuery.cs @@ -403,7 +403,7 @@ namespace DotRecast.Detour.Crowd debug.Reset(); // Build sampling pattern aligned to desired velocity. - using var pat = RcRentedArray.RentDisposableArray((DT_MAX_PATTERN_DIVS * DT_MAX_PATTERN_RINGS + 1) * 2); + using var pat = RcRentedArray.Rent((DT_MAX_PATTERN_DIVS * DT_MAX_PATTERN_RINGS + 1) * 2); int npat = 0; int ndivs = m_params.adaptiveDivs; diff --git a/src/DotRecast.Detour/DtNavMesh.cs b/src/DotRecast.Detour/DtNavMesh.cs index 094bd5d..2f0c8d4 100644 --- a/src/DotRecast.Detour/DtNavMesh.cs +++ b/src/DotRecast.Detour/DtNavMesh.cs @@ -1247,14 +1247,14 @@ namespace DotRecast.Detour int ip = poly.index; - using var verts = RcRentedArray.RentDisposableArray(m_maxVertPerPoly * 3); + using var verts = RcRentedArray.Rent(m_maxVertPerPoly * 3); int nv = poly.vertCount; for (int i = 0; i < nv; ++i) { - RcArrays.Copy(tile.data.verts, poly.verts[i] * 3, verts.AsRentedArray(), i * 3, 3); + RcArrays.Copy(tile.data.verts, poly.verts[i] * 3, verts.AsArray(), i * 3, 3); } - if (!DtUtils.PointInPolygon(pos, verts.AsRentedArray(), nv)) + if (!DtUtils.PointInPolygon(pos, verts.AsArray(), nv)) { return false; } diff --git a/src/DotRecast.Detour/DtNavMeshQuery.cs b/src/DotRecast.Detour/DtNavMeshQuery.cs index ad7debc..bc7eab5 100644 --- a/src/DotRecast.Detour/DtNavMeshQuery.cs +++ b/src/DotRecast.Detour/DtNavMeshQuery.cs @@ -37,13 +37,11 @@ namespace DotRecast.Detour protected readonly DtNodeQueue m_openList; protected DtQueryData m_query; - protected readonly DtNodePool m_tinyNodePool; /// < Sliced query state. public DtNavMeshQuery(DtNavMesh nav) { m_nav = nav; - m_tinyNodePool = new DtNodePool(); m_nodePool = new DtNodePool(); m_openList = new DtNodeQueue(); m_tinyNodePool = new DtNodePool(); @@ -140,18 +138,18 @@ namespace DotRecast.Detour } // Randomly pick point on polygon. - using var verts = RcRentedArray.RentDisposableArray(3 * m_nav.GetMaxVertsPerPoly()); - using var areas = RcRentedArray.RentDisposableArray(m_nav.GetMaxVertsPerPoly()); - RcArrays.Copy(tile.data.verts, poly.verts[0] * 3, verts.AsRentedArray(), 0, 3); + using var verts = RcRentedArray.Rent(3 * m_nav.GetMaxVertsPerPoly()); + using var areas = RcRentedArray.Rent(m_nav.GetMaxVertsPerPoly()); + RcArrays.Copy(tile.data.verts, poly.verts[0] * 3, verts.AsArray(), 0, 3); for (int j = 1; j < poly.vertCount; ++j) { - RcArrays.Copy(tile.data.verts, poly.verts[j] * 3, verts.AsRentedArray(), j * 3, 3); + RcArrays.Copy(tile.data.verts, poly.verts[j] * 3, verts.AsArray(), j * 3, 3); } float s = frand.Next(); float t = frand.Next(); - var pt = DtUtils.RandomPointInConvexPoly(verts.AsRentedArray(), poly.vertCount, areas.AsRentedArray(), s, t); + var pt = DtUtils.RandomPointInConvexPoly(verts.AsArray(), poly.vertCount, areas.AsArray(), s, t); ClosestPointOnPoly(polyRef, pt, out var closest, out var _); randomRef = polyRef; @@ -389,8 +387,8 @@ namespace DotRecast.Detour float s = frand.Next(); float t = frand.Next(); - using var areas = RcRentedArray.RentDisposableArray(randomPolyVerts.Length / 3); - RcVec3f pt = DtUtils.RandomPointInConvexPoly(randomPolyVerts, randomPolyVerts.Length / 3, areas.AsRentedArray(), s, t); + using var areas = RcRentedArray.Rent(randomPolyVerts.Length / 3); + RcVec3f pt = DtUtils.RandomPointInConvexPoly(randomPolyVerts, randomPolyVerts.Length / 3, areas.AsArray(), s, t); ClosestPointOnPoly(randomPolyRef, pt, out var closest, out var _); randomRef = randomPolyRef; @@ -460,16 +458,16 @@ namespace DotRecast.Detour } // Collect vertices. - using var verts = RcRentedArray.RentDisposableArray(m_nav.GetMaxVertsPerPoly() * 3); - using var edged = RcRentedArray.RentDisposableArray(m_nav.GetMaxVertsPerPoly()); - using var edget = RcRentedArray.RentDisposableArray(m_nav.GetMaxVertsPerPoly()); + using var verts = RcRentedArray.Rent(m_nav.GetMaxVertsPerPoly() * 3); + using var edged = RcRentedArray.Rent(m_nav.GetMaxVertsPerPoly()); + using var edget = RcRentedArray.Rent(m_nav.GetMaxVertsPerPoly()); int nv = poly.vertCount; for (int i = 0; i < nv; ++i) { - RcArrays.Copy(tile.data.verts, poly.verts[i] * 3, verts.AsRentedArray(), i * 3, 3); + RcArrays.Copy(tile.data.verts, poly.verts[i] * 3, verts.AsArray(), i * 3, 3); } - if (DtUtils.DistancePtPolyEdgesSqr(pos, verts.AsRentedArray(), nv, edged.AsRentedArray(), edget.AsRentedArray())) + if (DtUtils.DistancePtPolyEdgesSqr(pos, verts.AsArray(), nv, edged.AsArray(), edget.AsArray())) { closest = pos; } @@ -489,7 +487,7 @@ namespace DotRecast.Detour int va = imin * 3; int vb = ((imin + 1) % nv) * 3; - closest = RcVecUtils.Lerp(verts.AsRentedArray(), va, vb, edget[imin]); + closest = RcVecUtils.Lerp(verts.AsArray(), va, vb, edget[imin]); } return DtStatus.DT_SUCCESS; @@ -1827,7 +1825,7 @@ namespace DotRecast.Detour var searchPos = RcVec3f.Lerp(startPos, endPos, 0.5f); float searchRadSqr = RcMath.Sqr(RcVec3f.Distance(startPos, endPos) / 2.0f + 0.001f); - using var verts = RcRentedArray.RentDisposableArray(m_nav.GetMaxVertsPerPoly() * 3); + using var verts = RcRentedArray.Rent(m_nav.GetMaxVertsPerPoly() * 3); while (0 < stack.Count) { @@ -1844,11 +1842,11 @@ namespace DotRecast.Detour int nverts = curPoly.vertCount; for (int i = 0; i < nverts; ++i) { - RcArrays.Copy(curTile.data.verts, curPoly.verts[i] * 3, verts.AsRentedArray(), i * 3, 3); + RcArrays.Copy(curTile.data.verts, curPoly.verts[i] * 3, verts.AsArray(), i * 3, 3); } // If target is inside the poly, stop search. - if (DtUtils.PointInPolygon(endPos, verts.AsRentedArray(), nverts)) + if (DtUtils.PointInPolygon(endPos, verts.AsArray(), nverts)) { bestNode = curNode; bestPos = endPos; @@ -1861,7 +1859,7 @@ namespace DotRecast.Detour // Find links to neighbours. int MAX_NEIS = 8; int nneis = 0; - using var neis = RcRentedArray.RentDisposableArray(MAX_NEIS); + using var neis = RcRentedArray.Rent(MAX_NEIS); if ((curPoly.neis[j] & DtNavMesh.DT_EXT_LINK) != 0) { @@ -1901,11 +1899,11 @@ namespace DotRecast.Detour // Wall edge, calc distance. int vj = j * 3; int vi = i * 3; - var distSqr = DtUtils.DistancePtSegSqr2D(endPos, verts.AsRentedArray(), vj, vi, out var tseg); + var distSqr = DtUtils.DistancePtSegSqr2D(endPos, verts.AsArray(), vj, vi, out var tseg); if (distSqr < bestDist) { // Update nearest distance. - bestPos = RcVecUtils.Lerp(verts.AsRentedArray(), vj, vi, tseg); + bestPos = RcVecUtils.Lerp(verts.AsArray(), vj, vi, tseg); bestDist = distSqr; bestNode = curNode; } @@ -1925,7 +1923,7 @@ namespace DotRecast.Detour // TODO: Maybe should use GetPortalPoints(), but this one is way faster. int vj = j * 3; int vi = i * 3; - var distSqr = DtUtils.DistancePtSegSqr2D(searchPos, verts.AsRentedArray(), vj, vi, out var _); + var distSqr = DtUtils.DistancePtSegSqr2D(searchPos, verts.AsArray(), vj, vi, out var _); if (distSqr > searchRadSqr) { continue; @@ -2249,7 +2247,7 @@ namespace DotRecast.Detour hit.path.Clear(); hit.pathCost = 0; - RcVec3f[] verts = new RcVec3f[m_nav.GetMaxVertsPerPoly() + 1]; + using var verts = RcRentedArray.Rent(m_nav.GetMaxVertsPerPoly() + 1); RcVec3f curPos = RcVec3f.Zero; RcVec3f lastPos = RcVec3f.Zero; @@ -2283,7 +2281,7 @@ namespace DotRecast.Detour nv++; } - bool intersects = DtUtils.IntersectSegmentPoly2D(startPos, endPos, verts, nv, out var tmin, out var tmax, out var segMin, out var segMax); + bool intersects = DtUtils.IntersectSegmentPoly2D(startPos, endPos, verts.AsArray(), nv, out var tmin, out var tmax, out var segMin, out var segMax); if (!intersects) { // Could not hit the polygon, keep the old t and report hit. @@ -2878,8 +2876,8 @@ namespace DotRecast.Detour float radiusSqr = RcMath.Sqr(radius); - using var pa = RcRentedArray.RentDisposableArray(m_nav.GetMaxVertsPerPoly() * 3); - using var pb = RcRentedArray.RentDisposableArray(m_nav.GetMaxVertsPerPoly() * 3); + using var pa = RcRentedArray.Rent(m_nav.GetMaxVertsPerPoly() * 3); + using var pb = RcRentedArray.Rent(m_nav.GetMaxVertsPerPoly() * 3); while (0 < stack.Count) { @@ -2950,7 +2948,7 @@ namespace DotRecast.Detour int npa = neighbourPoly.vertCount; for (int k = 0; k < npa; ++k) { - RcArrays.Copy(neighbourTile.data.verts, neighbourPoly.verts[k] * 3, pa.AsRentedArray(), k * 3, 3); + RcArrays.Copy(neighbourTile.data.verts, neighbourPoly.verts[k] * 3, pa.AsArray(), k * 3, 3); } bool overlap = false; @@ -2981,10 +2979,10 @@ namespace DotRecast.Detour int npb = pastPoly.vertCount; for (int k = 0; k < npb; ++k) { - RcArrays.Copy(pastTile.data.verts, pastPoly.verts[k] * 3, pb.AsRentedArray(), k * 3, 3); + RcArrays.Copy(pastTile.data.verts, pastPoly.verts[k] * 3, pb.AsArray(), k * 3, 3); } - if (DtUtils.OverlapPolyPoly2D(pa.AsRentedArray(), npa, pb.AsRentedArray(), npb)) + if (DtUtils.OverlapPolyPoly2D(pa.AsArray(), npa, pb.AsArray(), npb)) { overlap = true; break; diff --git a/test/DotRecast.Core.Test/RcRentedArrayTest.cs b/test/DotRecast.Core.Test/RcRentedArrayTest.cs index c314fc3..50007f2 100644 --- a/test/DotRecast.Core.Test/RcRentedArrayTest.cs +++ b/test/DotRecast.Core.Test/RcRentedArrayTest.cs @@ -65,36 +65,36 @@ public class RcRentedArrayTest { // not same { - using var r1 = RcRentedArray.RentDisposableArray(1024); - using var r2 = RcRentedArray.RentDisposableArray(1024); + using var r1 = RcRentedArray.Rent(1024); + using var r2 = RcRentedArray.Rent(1024); - Assert.That(r2.AsRentedArray() != r1.AsRentedArray(), Is.EqualTo(true)); + Assert.That(r2.AsArray() != r1.AsArray(), Is.EqualTo(true)); } // same { // error case float[] r1Array; - using (var r1 = RcRentedArray.RentDisposableArray(1024)) + using (var r1 = RcRentedArray.Rent(1024)) { - r1Array = r1.AsRentedArray(); + r1Array = r1.AsArray(); for (int i = 0; i < r1.Length; ++i) { r1[i] = 123; } } - using var r2 = RcRentedArray.RentDisposableArray(1024); + using var r2 = RcRentedArray.Rent(1024); - Assert.That(r2.AsRentedArray() == r1Array, Is.EqualTo(true)); - Assert.That(r2.AsRentedArray().Sum(), Is.EqualTo(0)); + Assert.That(r2.AsArray() == r1Array, Is.EqualTo(true)); + Assert.That(r2.AsArray().Sum(), Is.EqualTo(0)); } } [Test] public void TestDispose() { - var r1 = RcRentedArray.RentDisposableArray(1024); + var r1 = RcRentedArray.Rent(1024); for (int i = 0; i < r1.Length; ++i) { r1[i] = 123; @@ -103,6 +103,6 @@ public class RcRentedArrayTest Assert.That(r1.IsDisposed, Is.EqualTo(false)); r1.Dispose(); Assert.That(r1.IsDisposed, Is.EqualTo(true)); - Assert.That(r1.AsRentedArray(), Is.Null); + Assert.That(r1.AsArray(), Is.Null); } } \ No newline at end of file