Skip to content

Commit

Permalink
Replaced arrays with spans in query. Replaced array allocation with b…
Browse files Browse the repository at this point in the history
…uffer rent in query.

comment
  • Loading branch information
wrenge authored and ikpil committed Dec 15, 2024
1 parent 7812cbd commit 6896842
Show file tree
Hide file tree
Showing 7 changed files with 23 additions and 17 deletions.
6 changes: 6 additions & 0 deletions src/DotRecast.Core/Buffers/RcRentedArray.cs
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,12 @@ public ref T this[int index]
}
}

[Obsolete("This method is risky, and we are considering alternative approaches.")]
public T[] AsArray()
{
return _items;
}

public Span<T> AsSpan()
{
if (_disposed)
Expand Down
2 changes: 1 addition & 1 deletion src/DotRecast.Detour/DtCallbackPolyQuery.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public DtCallbackPolyQuery(Action<DtMeshTile, DtPoly, long> callback)
_callback = callback;
}

public void Process(DtMeshTile tile, DtPoly[] poly, Span<long> refs, int count)
public void Process(DtMeshTile tile, Span<DtPoly> poly, Span<long> refs, int count)
{
for (int i = 0; i < count; ++i)
{
Expand Down
2 changes: 1 addition & 1 deletion src/DotRecast.Detour/DtCollectPolysQuery.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public bool Overflowed()
return m_overflow;
}

public void Process(DtMeshTile tile, DtPoly[] poly, Span<long> refs, int count)
public void Process(DtMeshTile tile, Span<DtPoly> poly, Span<long> refs, int count)
{
int numLeft = m_maxPolys - m_numCollected;
int toCopy = count;
Expand Down
2 changes: 1 addition & 1 deletion src/DotRecast.Detour/DtFindNearestPolyQuery.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public DtFindNearestPolyQuery(DtNavMeshQuery query, RcVec3f center)
_overPoly = default;
}

public void Process(DtMeshTile tile, DtPoly[] poly, Span<long> refs, int count)
public void Process(DtMeshTile tile, Span<DtPoly> poly, Span<long> refs, int count)
{
for (int i = 0; i < count; ++i)
{
Expand Down
16 changes: 8 additions & 8 deletions src/DotRecast.Detour/DtNavMeshQuery.cs
Original file line number Diff line number Diff line change
Expand Up @@ -267,9 +267,9 @@ public DtStatus FindRandomPointAroundCircle(long startRef, RcVec3f centerPos, fl
float radiusSqr = maxRadius * maxRadius;
float areaSum = 0.0f;

using RcRentedArray<float> polyVertsBuffer = RcRentedArray.Rent<float>(MAX_VERT_BUFFER_SIZE);
using RcRentedArray<float> randomPolyVertsBuffer = RcRentedArray.Rent<float>(MAX_VERT_BUFFER_SIZE);
using RcRentedArray<float> constrainedVertsBuffer = RcRentedArray.Rent<float>(MAX_VERT_BUFFER_SIZE);
using RcRentedArray<float> polyVertsBuffer = RcRentedArray.Shared.Rent<float>(MAX_VERT_BUFFER_SIZE);
using RcRentedArray<float> randomPolyVertsBuffer = RcRentedArray.Shared.Rent<float>(MAX_VERT_BUFFER_SIZE);
using RcRentedArray<float> constrainedVertsBuffer = RcRentedArray.Shared.Rent<float>(MAX_VERT_BUFFER_SIZE);

DtPoly randomPoly = null;
long randomPolyRef = 0;
Expand Down Expand Up @@ -608,7 +608,7 @@ protected void QueryPolygonsInTile<TQuery>(DtMeshTile tile, RcVec3f qmin, RcVec3
{
const int batchSize = 32;
Span<long> polyRefs = stackalloc long[batchSize];
using RcRentedArray<DtPoly> polysRent = RcRentedArray.Rent<DtPoly>(batchSize);
using RcRentedArray<DtPoly> polysRent = RcRentedArray.Shared.Rent<DtPoly>(batchSize);
Span<DtPoly> polys = polysRent.AsSpan();
int n = 0;

Expand Down Expand Up @@ -799,7 +799,7 @@ public DtStatus QueryPolygons<TQuery>(RcVec3f center, RcVec3f halfExtents, IDtQu
m_nav.CalcTileLoc(bmax, out var maxx, out var maxy);

const int MAX_NEIS = 32;
using RcRentedArray<DtMeshTile> neisRent = RcRentedArray.Rent<DtMeshTile>(MAX_NEIS);
using RcRentedArray<DtMeshTile> neisRent = RcRentedArray.Shared.Rent<DtMeshTile>(MAX_NEIS);
Span<DtMeshTile> neis = neisRent.AsSpan();

for (int y = miny; y <= maxy; ++y)
Expand Down Expand Up @@ -891,7 +891,7 @@ public DtStatus FindPath(long startRef, long endRef, RcVec3f startPos, RcVec3f e
float lastBestNodeCost = startNode.total;

DtRaycastHit rayHit = new DtRaycastHit();
using var pathRent = RcRentedArray.Rent<long>(MAX_PATH_LENGTH);
using var pathRent = RcRentedArray.Shared.Rent<long>(MAX_PATH_LENGTH);
rayHit.path = pathRent.AsArray();

Check warning on line 895 in src/DotRecast.Detour/DtNavMeshQuery.cs

View workflow job for this annotation

GitHub Actions / test-macos-latest-8

'RcRentedArray<long>.AsArray()' is obsolete: 'This method is risky, and we are considering alternative approaches.'

Check warning on line 895 in src/DotRecast.Detour/DtNavMeshQuery.cs

View workflow job for this annotation

GitHub Actions / test-macos-latest-9

'RcRentedArray<long>.AsArray()' is obsolete: 'This method is risky, and we are considering alternative approaches.'

Check warning on line 895 in src/DotRecast.Detour/DtNavMeshQuery.cs

View workflow job for this annotation

GitHub Actions / test-ubuntu-latest-8

'RcRentedArray<long>.AsArray()' is obsolete: 'This method is risky, and we are considering alternative approaches.'

Check warning on line 895 in src/DotRecast.Detour/DtNavMeshQuery.cs

View workflow job for this annotation

GitHub Actions / test-ubuntu-latest-9

'RcRentedArray<long>.AsArray()' is obsolete: 'This method is risky, and we are considering alternative approaches.'

Check warning on line 895 in src/DotRecast.Detour/DtNavMeshQuery.cs

View workflow job for this annotation

GitHub Actions / test-windows-latest-9

'RcRentedArray<long>.AsArray()' is obsolete: 'This method is risky, and we are considering alternative approaches.'

Check warning on line 895 in src/DotRecast.Detour/DtNavMeshQuery.cs

View workflow job for this annotation

GitHub Actions / test-windows-latest-8

'RcRentedArray<long>.AsArray()' is obsolete: 'This method is risky, and we are considering alternative approaches.'
while (!m_openList.IsEmpty())
{
Expand Down Expand Up @@ -1181,7 +1181,7 @@ public virtual DtStatus UpdateSlicedFindPath(int maxIter, out int doneIters)
}

var rayHit = new DtRaycastHit();
using var pathRent = RcRentedArray.Rent<long>(MAX_PATH_LENGTH);
using var pathRent = RcRentedArray.Shared.Rent<long>(MAX_PATH_LENGTH);
rayHit.path = pathRent.AsArray();

Check warning on line 1185 in src/DotRecast.Detour/DtNavMeshQuery.cs

View workflow job for this annotation

GitHub Actions / test-macos-latest-8

'RcRentedArray<long>.AsArray()' is obsolete: 'This method is risky, and we are considering alternative approaches.'

Check warning on line 1185 in src/DotRecast.Detour/DtNavMeshQuery.cs

View workflow job for this annotation

GitHub Actions / test-macos-latest-9

'RcRentedArray<long>.AsArray()' is obsolete: 'This method is risky, and we are considering alternative approaches.'

Check warning on line 1185 in src/DotRecast.Detour/DtNavMeshQuery.cs

View workflow job for this annotation

GitHub Actions / test-ubuntu-latest-8

'RcRentedArray<long>.AsArray()' is obsolete: 'This method is risky, and we are considering alternative approaches.'

Check warning on line 1185 in src/DotRecast.Detour/DtNavMeshQuery.cs

View workflow job for this annotation

GitHub Actions / test-ubuntu-latest-9

'RcRentedArray<long>.AsArray()' is obsolete: 'This method is risky, and we are considering alternative approaches.'

Check warning on line 1185 in src/DotRecast.Detour/DtNavMeshQuery.cs

View workflow job for this annotation

GitHub Actions / test-windows-latest-9

'RcRentedArray<long>.AsArray()' is obsolete: 'This method is risky, and we are considering alternative approaches.'

Check warning on line 1185 in src/DotRecast.Detour/DtNavMeshQuery.cs

View workflow job for this annotation

GitHub Actions / test-windows-latest-8

'RcRentedArray<long>.AsArray()' is obsolete: 'This method is risky, and we are considering alternative approaches.'

int iter = 0;
Expand Down Expand Up @@ -2274,7 +2274,7 @@ public DtStatus Raycast(long startRef, RcVec3f startPos, RcVec3f endPos,
out float t, out RcVec3f hitNormal, ref List<long> path)
{
DtRaycastHit hit = new DtRaycastHit();
using var pathRent = RcRentedArray.Rent<long>(MAX_PATH_LENGTH);
using var pathRent = RcRentedArray.Shared.Rent<long>(MAX_PATH_LENGTH);
hit.path = pathRent.AsArray();

Check warning on line 2278 in src/DotRecast.Detour/DtNavMeshQuery.cs

View workflow job for this annotation

GitHub Actions / test-macos-latest-8

'RcRentedArray<long>.AsArray()' is obsolete: 'This method is risky, and we are considering alternative approaches.'

Check warning on line 2278 in src/DotRecast.Detour/DtNavMeshQuery.cs

View workflow job for this annotation

GitHub Actions / test-macos-latest-9

'RcRentedArray<long>.AsArray()' is obsolete: 'This method is risky, and we are considering alternative approaches.'

Check warning on line 2278 in src/DotRecast.Detour/DtNavMeshQuery.cs

View workflow job for this annotation

GitHub Actions / test-ubuntu-latest-8

'RcRentedArray<long>.AsArray()' is obsolete: 'This method is risky, and we are considering alternative approaches.'

Check warning on line 2278 in src/DotRecast.Detour/DtNavMeshQuery.cs

View workflow job for this annotation

GitHub Actions / test-ubuntu-latest-9

'RcRentedArray<long>.AsArray()' is obsolete: 'This method is risky, and we are considering alternative approaches.'

Check warning on line 2278 in src/DotRecast.Detour/DtNavMeshQuery.cs

View workflow job for this annotation

GitHub Actions / test-windows-latest-9

'RcRentedArray<long>.AsArray()' is obsolete: 'This method is risky, and we are considering alternative approaches.'

Check warning on line 2278 in src/DotRecast.Detour/DtNavMeshQuery.cs

View workflow job for this annotation

GitHub Actions / test-windows-latest-8

'RcRentedArray<long>.AsArray()' is obsolete: 'This method is risky, and we are considering alternative approaches.'

DtStatus status = Raycast(startRef, startPos, endPos, filter, 0, ref hit, 0);
Expand Down
10 changes: 5 additions & 5 deletions src/DotRecast.Detour/DtPathUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -186,12 +186,12 @@ public static int MergeCorridorStartMoved(List<long> path, int npath, int maxPat
var endIndex = nvisited - 1;
var length1 = endIndex - furthestVisited;
var length2 = npath - furthestPath;
using var result = RcRentedArray.Rent<long>(length1 + length2);
using var result = RcRentedArray.Shared.Rent<long>(length1 + length2);
// Adjust beginning of the buffer to include the visited.
// Store visited
for (int i = 0; i < length1; ++i)
for (int i = 0; i < length1; ++i)
result[i] = visited[endIndex - i];

path.CopyTo(furthestPath, result.AsArray(), length1, length2);

Check warning on line 195 in src/DotRecast.Detour/DtPathUtils.cs

View workflow job for this annotation

GitHub Actions / test-macos-latest-8

'RcRentedArray<long>.AsArray()' is obsolete: 'This method is risky, and we are considering alternative approaches.'

Check warning on line 195 in src/DotRecast.Detour/DtPathUtils.cs

View workflow job for this annotation

GitHub Actions / test-macos-latest-9

'RcRentedArray<long>.AsArray()' is obsolete: 'This method is risky, and we are considering alternative approaches.'

Check warning on line 195 in src/DotRecast.Detour/DtPathUtils.cs

View workflow job for this annotation

GitHub Actions / test-ubuntu-latest-8

'RcRentedArray<long>.AsArray()' is obsolete: 'This method is risky, and we are considering alternative approaches.'

Check warning on line 195 in src/DotRecast.Detour/DtPathUtils.cs

View workflow job for this annotation

GitHub Actions / test-ubuntu-latest-9

'RcRentedArray<long>.AsArray()' is obsolete: 'This method is risky, and we are considering alternative approaches.'

Check warning on line 195 in src/DotRecast.Detour/DtPathUtils.cs

View workflow job for this annotation

GitHub Actions / test-windows-latest-9

'RcRentedArray<long>.AsArray()' is obsolete: 'This method is risky, and we are considering alternative approaches.'

Check warning on line 195 in src/DotRecast.Detour/DtPathUtils.cs

View workflow job for this annotation

GitHub Actions / test-windows-latest-8

'RcRentedArray<long>.AsArray()' is obsolete: 'This method is risky, and we are considering alternative approaches.'

path.Clear();
Expand Down Expand Up @@ -233,7 +233,7 @@ public static int MergeCorridorEndMoved(List<long> path, int npath, int maxPath,
// Concatenate paths.
var length1 = furthestPath;
var length2 = nvisited - furthestVisited;
using var result = RcRentedArray.Rent<long>(length1 + length2);
using var result = RcRentedArray.Shared.Rent<long>(length1 + length2);
path.CopyTo(0, result.AsArray(), 0, length1);

Check warning on line 237 in src/DotRecast.Detour/DtPathUtils.cs

View workflow job for this annotation

GitHub Actions / test-macos-latest-8

'RcRentedArray<long>.AsArray()' is obsolete: 'This method is risky, and we are considering alternative approaches.'

Check warning on line 237 in src/DotRecast.Detour/DtPathUtils.cs

View workflow job for this annotation

GitHub Actions / test-macos-latest-9

'RcRentedArray<long>.AsArray()' is obsolete: 'This method is risky, and we are considering alternative approaches.'

Check warning on line 237 in src/DotRecast.Detour/DtPathUtils.cs

View workflow job for this annotation

GitHub Actions / test-ubuntu-latest-8

'RcRentedArray<long>.AsArray()' is obsolete: 'This method is risky, and we are considering alternative approaches.'

Check warning on line 237 in src/DotRecast.Detour/DtPathUtils.cs

View workflow job for this annotation

GitHub Actions / test-ubuntu-latest-9

'RcRentedArray<long>.AsArray()' is obsolete: 'This method is risky, and we are considering alternative approaches.'

Check warning on line 237 in src/DotRecast.Detour/DtPathUtils.cs

View workflow job for this annotation

GitHub Actions / test-windows-latest-9

'RcRentedArray<long>.AsArray()' is obsolete: 'This method is risky, and we are considering alternative approaches.'

Check warning on line 237 in src/DotRecast.Detour/DtPathUtils.cs

View workflow job for this annotation

GitHub Actions / test-windows-latest-8

'RcRentedArray<long>.AsArray()' is obsolete: 'This method is risky, and we are considering alternative approaches.'
visited.Slice(furthestVisited, nvisited - furthestVisited).CopyTo(result.AsSpan().Slice(length1, length2));

Expand Down Expand Up @@ -278,7 +278,7 @@ public static int MergeCorridorStartShortcut(List<long> path, int npath, int max
// Adjust beginning of the buffer to include the visited.
var length1 = furthestVisited;
var length2 = npath - furthestPath;
using var result = RcRentedArray.Rent<long>(length1 + length2);
using var result = RcRentedArray.Shared.Rent<long>(length1 + length2);
visited.CopyTo(0, result.AsArray(), 0, length1);

Check warning on line 282 in src/DotRecast.Detour/DtPathUtils.cs

View workflow job for this annotation

GitHub Actions / test-macos-latest-8

'RcRentedArray<long>.AsArray()' is obsolete: 'This method is risky, and we are considering alternative approaches.'

Check warning on line 282 in src/DotRecast.Detour/DtPathUtils.cs

View workflow job for this annotation

GitHub Actions / test-macos-latest-9

'RcRentedArray<long>.AsArray()' is obsolete: 'This method is risky, and we are considering alternative approaches.'

Check warning on line 282 in src/DotRecast.Detour/DtPathUtils.cs

View workflow job for this annotation

GitHub Actions / test-ubuntu-latest-8

'RcRentedArray<long>.AsArray()' is obsolete: 'This method is risky, and we are considering alternative approaches.'

Check warning on line 282 in src/DotRecast.Detour/DtPathUtils.cs

View workflow job for this annotation

GitHub Actions / test-ubuntu-latest-9

'RcRentedArray<long>.AsArray()' is obsolete: 'This method is risky, and we are considering alternative approaches.'

Check warning on line 282 in src/DotRecast.Detour/DtPathUtils.cs

View workflow job for this annotation

GitHub Actions / test-windows-latest-9

'RcRentedArray<long>.AsArray()' is obsolete: 'This method is risky, and we are considering alternative approaches.'

Check warning on line 282 in src/DotRecast.Detour/DtPathUtils.cs

View workflow job for this annotation

GitHub Actions / test-windows-latest-8

'RcRentedArray<long>.AsArray()' is obsolete: 'This method is risky, and we are considering alternative approaches.'
path.CopyTo(furthestPath, result.AsArray(), length1, length2);

Check warning on line 283 in src/DotRecast.Detour/DtPathUtils.cs

View workflow job for this annotation

GitHub Actions / test-macos-latest-8

'RcRentedArray<long>.AsArray()' is obsolete: 'This method is risky, and we are considering alternative approaches.'

Check warning on line 283 in src/DotRecast.Detour/DtPathUtils.cs

View workflow job for this annotation

GitHub Actions / test-macos-latest-9

'RcRentedArray<long>.AsArray()' is obsolete: 'This method is risky, and we are considering alternative approaches.'

Check warning on line 283 in src/DotRecast.Detour/DtPathUtils.cs

View workflow job for this annotation

GitHub Actions / test-ubuntu-latest-8

'RcRentedArray<long>.AsArray()' is obsolete: 'This method is risky, and we are considering alternative approaches.'

Check warning on line 283 in src/DotRecast.Detour/DtPathUtils.cs

View workflow job for this annotation

GitHub Actions / test-ubuntu-latest-9

'RcRentedArray<long>.AsArray()' is obsolete: 'This method is risky, and we are considering alternative approaches.'

Check warning on line 283 in src/DotRecast.Detour/DtPathUtils.cs

View workflow job for this annotation

GitHub Actions / test-windows-latest-9

'RcRentedArray<long>.AsArray()' is obsolete: 'This method is risky, and we are considering alternative approaches.'

Check warning on line 283 in src/DotRecast.Detour/DtPathUtils.cs

View workflow job for this annotation

GitHub Actions / test-windows-latest-8

'RcRentedArray<long>.AsArray()' is obsolete: 'This method is risky, and we are considering alternative approaches.'

Expand Down
2 changes: 1 addition & 1 deletion src/DotRecast.Detour/IDtPolyQuery.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,6 @@ public interface IDtPolyQuery
{
/// Called for each batch of unique polygons touched by the search area in dtNavMeshQuery::queryPolygons.
/// This can be called multiple times for a single query.
void Process(DtMeshTile tile, DtPoly[] poly, Span<long> refs, int count);
void Process(DtMeshTile tile, Span<DtPoly> poly, Span<long> refs, int count);
}
}

0 comments on commit 6896842

Please sign in to comment.