Skip to content

Commit

Permalink
reduce virtual call
Browse files Browse the repository at this point in the history
  • Loading branch information
Sarofc authored and ikpil committed Jul 18, 2024
1 parent d988a0f commit 1fefff7
Show file tree
Hide file tree
Showing 4 changed files with 114 additions and 67 deletions.
6 changes: 5 additions & 1 deletion src/DotRecast.Core/DotRecast.Core.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,11 @@
</PropertyGroup>

<ItemGroup>
<None Include="../../README.md" Pack="true" PackagePath="\"/>
<None Include="../../README.md" Pack="true" PackagePath="\" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="System.Runtime.CompilerServices.Unsafe" Version="6.0.0" />
</ItemGroup>


Expand Down
74 changes: 74 additions & 0 deletions src/DotRecast.Core/FCollectionsMarshal.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
/*
Copyright (c) 2009-2010 Mikko Mononen [email protected]
recast4j copyright (c) 2015-2019 Piotr Piastucki [email protected]
DotRecast Copyright (c) 2023-2024 Choi Ikpil [email protected]
This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any damages
arising from the use of this software.
Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it
freely, subject to the following restrictions:
1. The origin of this software must not be misrepresented; you must not
claim that you wrote the original software. If you use this software
in a product, an acknowledgment in the product documentation would be
appreciated but is not required.
2. Altered source versions must be plainly marked as such, and must not be
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 System.Runtime.CompilerServices;
using System.Runtime.InteropServices;

namespace DotRecast.Core
{
public static class FCollectionsMarshal
{
/// <summary>
/// similar as AsSpan but modify size to create fixed-size span.
/// </summary>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Span<T> CreateSpan<T>(List<T> list, int count)
{
#if NET8_0_OR_GREATER
CollectionsMarshal.SetCount(list, count);
return CollectionsMarshal.AsSpan(list);
#else
// TODO 有一些差异,CollectionsMarshal.SetCount 会清掉引用类型的对象
if (list.Capacity < count)
list.Capacity = count;

ref var view = ref Unsafe.As<List<T>, ListView<T>>(ref list); // 0 gc
view._size = count;
return view._items.AsSpan(0, count);
#endif
}

/// <summary>
/// similar as AsSpan but modify size to create fixed-size span.
/// </summary>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Span<T> AsSpan<T>(List<T> list)
{
#if NET6_0_OR_GREATER
return CollectionsMarshal.AsSpan(list);
#else
ref var view = ref Unsafe.As<List<T>, ListView<T>>(ref list);
return view._items.AsSpan(0, view._size);
#endif
}

#if !NET8_0_OR_GREATER
// NOTE: These structure depndent on .NET 7, if changed, require to keep same structure.
internal class ListView<T>
{
public T[] _items;

Check warning on line 68 in src/DotRecast.Core/FCollectionsMarshal.cs

View workflow job for this annotation

GitHub Actions / test-ubuntu-latest-7

Field 'FCollectionsMarshal.ListView<T>._items' is never assigned to, and will always have its default value null

Check warning on line 68 in src/DotRecast.Core/FCollectionsMarshal.cs

View workflow job for this annotation

GitHub Actions / test-ubuntu-latest-7

Field 'FCollectionsMarshal.ListView<T>._items' is never assigned to, and will always have its default value null

Check warning on line 68 in src/DotRecast.Core/FCollectionsMarshal.cs

View workflow job for this annotation

GitHub Actions / test-macos-latest-7

Field 'FCollectionsMarshal.ListView<T>._items' is never assigned to, and will always have its default value null

Check warning on line 68 in src/DotRecast.Core/FCollectionsMarshal.cs

View workflow job for this annotation

GitHub Actions / test-macos-latest-7

Field 'FCollectionsMarshal.ListView<T>._items' is never assigned to, and will always have its default value null

Check warning on line 68 in src/DotRecast.Core/FCollectionsMarshal.cs

View workflow job for this annotation

GitHub Actions / test-ubuntu-latest-6

Field 'FCollectionsMarshal.ListView<T>._items' is never assigned to, and will always have its default value null

Check warning on line 68 in src/DotRecast.Core/FCollectionsMarshal.cs

View workflow job for this annotation

GitHub Actions / test-ubuntu-latest-6

Field 'FCollectionsMarshal.ListView<T>._items' is never assigned to, and will always have its default value null

Check warning on line 68 in src/DotRecast.Core/FCollectionsMarshal.cs

View workflow job for this annotation

GitHub Actions / test-macos-latest-6

Field 'FCollectionsMarshal.ListView<T>._items' is never assigned to, and will always have its default value null

Check warning on line 68 in src/DotRecast.Core/FCollectionsMarshal.cs

View workflow job for this annotation

GitHub Actions / test-macos-latest-6

Field 'FCollectionsMarshal.ListView<T>._items' is never assigned to, and will always have its default value null
public int _size;
public int _version;

Check warning on line 70 in src/DotRecast.Core/FCollectionsMarshal.cs

View workflow job for this annotation

GitHub Actions / test-ubuntu-latest-7

Field 'FCollectionsMarshal.ListView<T>._version' is never assigned to, and will always have its default value 0

Check warning on line 70 in src/DotRecast.Core/FCollectionsMarshal.cs

View workflow job for this annotation

GitHub Actions / test-ubuntu-latest-7

Field 'FCollectionsMarshal.ListView<T>._version' is never assigned to, and will always have its default value 0

Check warning on line 70 in src/DotRecast.Core/FCollectionsMarshal.cs

View workflow job for this annotation

GitHub Actions / test-macos-latest-7

Field 'FCollectionsMarshal.ListView<T>._version' is never assigned to, and will always have its default value 0

Check warning on line 70 in src/DotRecast.Core/FCollectionsMarshal.cs

View workflow job for this annotation

GitHub Actions / test-macos-latest-7

Field 'FCollectionsMarshal.ListView<T>._version' is never assigned to, and will always have its default value 0

Check warning on line 70 in src/DotRecast.Core/FCollectionsMarshal.cs

View workflow job for this annotation

GitHub Actions / test-ubuntu-latest-6

Field 'FCollectionsMarshal.ListView<T>._version' is never assigned to, and will always have its default value 0

Check warning on line 70 in src/DotRecast.Core/FCollectionsMarshal.cs

View workflow job for this annotation

GitHub Actions / test-ubuntu-latest-6

Field 'FCollectionsMarshal.ListView<T>._version' is never assigned to, and will always have its default value 0

Check warning on line 70 in src/DotRecast.Core/FCollectionsMarshal.cs

View workflow job for this annotation

GitHub Actions / test-macos-latest-6

Field 'FCollectionsMarshal.ListView<T>._version' is never assigned to, and will always have its default value 0

Check warning on line 70 in src/DotRecast.Core/FCollectionsMarshal.cs

View workflow job for this annotation

GitHub Actions / test-macos-latest-6

Field 'FCollectionsMarshal.ListView<T>._version' is never assigned to, and will always have its default value 0
}
#endif
}
}
62 changes: 31 additions & 31 deletions src/DotRecast.Detour.Crowd/DtCrowd.cs
Original file line number Diff line number Diff line change
Expand Up @@ -371,7 +371,8 @@ public bool ResetMoveTarget(DtCrowdAgent agent)
*
* @return List of active agents
*/
public IList<DtCrowdAgent> GetActiveAgents()
[MethodImpl(MethodImplOptions.AggressiveInlining)]

Check failure on line 374 in src/DotRecast.Detour.Crowd/DtCrowd.cs

View workflow job for this annotation

GitHub Actions / test-ubuntu-latest-7

The type or namespace name 'MethodImplAttribute' could not be found (are you missing a using directive or an assembly reference?)

Check failure on line 374 in src/DotRecast.Detour.Crowd/DtCrowd.cs

View workflow job for this annotation

GitHub Actions / test-ubuntu-latest-7

The type or namespace name 'MethodImpl' could not be found (are you missing a using directive or an assembly reference?)

Check failure on line 374 in src/DotRecast.Detour.Crowd/DtCrowd.cs

View workflow job for this annotation

GitHub Actions / test-ubuntu-latest-7

The name 'MethodImplOptions' does not exist in the current context

Check failure on line 374 in src/DotRecast.Detour.Crowd/DtCrowd.cs

View workflow job for this annotation

GitHub Actions / test-ubuntu-latest-7

The type or namespace name 'MethodImplAttribute' could not be found (are you missing a using directive or an assembly reference?)

Check failure on line 374 in src/DotRecast.Detour.Crowd/DtCrowd.cs

View workflow job for this annotation

GitHub Actions / test-ubuntu-latest-7

The type or namespace name 'MethodImpl' could not be found (are you missing a using directive or an assembly reference?)

Check failure on line 374 in src/DotRecast.Detour.Crowd/DtCrowd.cs

View workflow job for this annotation

GitHub Actions / test-ubuntu-latest-7

The name 'MethodImplOptions' does not exist in the current context

Check failure on line 374 in src/DotRecast.Detour.Crowd/DtCrowd.cs

View workflow job for this annotation

GitHub Actions / test-macos-latest-8

The type or namespace name 'MethodImplAttribute' could not be found (are you missing a using directive or an assembly reference?)

Check failure on line 374 in src/DotRecast.Detour.Crowd/DtCrowd.cs

View workflow job for this annotation

GitHub Actions / test-macos-latest-8

The type or namespace name 'MethodImpl' could not be found (are you missing a using directive or an assembly reference?)

Check failure on line 374 in src/DotRecast.Detour.Crowd/DtCrowd.cs

View workflow job for this annotation

GitHub Actions / test-macos-latest-8

The name 'MethodImplOptions' does not exist in the current context

Check failure on line 374 in src/DotRecast.Detour.Crowd/DtCrowd.cs

View workflow job for this annotation

GitHub Actions / test-macos-latest-8

The type or namespace name 'MethodImplAttribute' could not be found (are you missing a using directive or an assembly reference?)

Check failure on line 374 in src/DotRecast.Detour.Crowd/DtCrowd.cs

View workflow job for this annotation

GitHub Actions / test-macos-latest-8

The type or namespace name 'MethodImpl' could not be found (are you missing a using directive or an assembly reference?)

Check failure on line 374 in src/DotRecast.Detour.Crowd/DtCrowd.cs

View workflow job for this annotation

GitHub Actions / test-macos-latest-8

The name 'MethodImplOptions' does not exist in the current context

Check failure on line 374 in src/DotRecast.Detour.Crowd/DtCrowd.cs

View workflow job for this annotation

GitHub Actions / test-macos-latest-7

The type or namespace name 'MethodImplAttribute' could not be found (are you missing a using directive or an assembly reference?)

Check failure on line 374 in src/DotRecast.Detour.Crowd/DtCrowd.cs

View workflow job for this annotation

GitHub Actions / test-macos-latest-7

The type or namespace name 'MethodImpl' could not be found (are you missing a using directive or an assembly reference?)

Check failure on line 374 in src/DotRecast.Detour.Crowd/DtCrowd.cs

View workflow job for this annotation

GitHub Actions / test-macos-latest-7

The name 'MethodImplOptions' does not exist in the current context

Check failure on line 374 in src/DotRecast.Detour.Crowd/DtCrowd.cs

View workflow job for this annotation

GitHub Actions / test-macos-latest-7

The type or namespace name 'MethodImplAttribute' could not be found (are you missing a using directive or an assembly reference?)

Check failure on line 374 in src/DotRecast.Detour.Crowd/DtCrowd.cs

View workflow job for this annotation

GitHub Actions / test-macos-latest-7

The type or namespace name 'MethodImpl' could not be found (are you missing a using directive or an assembly reference?)

Check failure on line 374 in src/DotRecast.Detour.Crowd/DtCrowd.cs

View workflow job for this annotation

GitHub Actions / test-macos-latest-7

The name 'MethodImplOptions' does not exist in the current context

Check failure on line 374 in src/DotRecast.Detour.Crowd/DtCrowd.cs

View workflow job for this annotation

GitHub Actions / test-ubuntu-latest-6

The type or namespace name 'MethodImplAttribute' could not be found (are you missing a using directive or an assembly reference?)

Check failure on line 374 in src/DotRecast.Detour.Crowd/DtCrowd.cs

View workflow job for this annotation

GitHub Actions / test-ubuntu-latest-6

The type or namespace name 'MethodImpl' could not be found (are you missing a using directive or an assembly reference?)

Check failure on line 374 in src/DotRecast.Detour.Crowd/DtCrowd.cs

View workflow job for this annotation

GitHub Actions / test-ubuntu-latest-6

The name 'MethodImplOptions' does not exist in the current context

Check failure on line 374 in src/DotRecast.Detour.Crowd/DtCrowd.cs

View workflow job for this annotation

GitHub Actions / test-ubuntu-latest-6

The type or namespace name 'MethodImplAttribute' could not be found (are you missing a using directive or an assembly reference?)

Check failure on line 374 in src/DotRecast.Detour.Crowd/DtCrowd.cs

View workflow job for this annotation

GitHub Actions / test-ubuntu-latest-6

The type or namespace name 'MethodImpl' could not be found (are you missing a using directive or an assembly reference?)

Check failure on line 374 in src/DotRecast.Detour.Crowd/DtCrowd.cs

View workflow job for this annotation

GitHub Actions / test-ubuntu-latest-6

The name 'MethodImplOptions' does not exist in the current context

Check failure on line 374 in src/DotRecast.Detour.Crowd/DtCrowd.cs

View workflow job for this annotation

GitHub Actions / test-ubuntu-latest-8

The type or namespace name 'MethodImplAttribute' could not be found (are you missing a using directive or an assembly reference?)

Check failure on line 374 in src/DotRecast.Detour.Crowd/DtCrowd.cs

View workflow job for this annotation

GitHub Actions / test-ubuntu-latest-8

The type or namespace name 'MethodImpl' could not be found (are you missing a using directive or an assembly reference?)

Check failure on line 374 in src/DotRecast.Detour.Crowd/DtCrowd.cs

View workflow job for this annotation

GitHub Actions / test-ubuntu-latest-8

The name 'MethodImplOptions' does not exist in the current context

Check failure on line 374 in src/DotRecast.Detour.Crowd/DtCrowd.cs

View workflow job for this annotation

GitHub Actions / test-ubuntu-latest-8

The type or namespace name 'MethodImplAttribute' could not be found (are you missing a using directive or an assembly reference?)

Check failure on line 374 in src/DotRecast.Detour.Crowd/DtCrowd.cs

View workflow job for this annotation

GitHub Actions / test-ubuntu-latest-8

The type or namespace name 'MethodImpl' could not be found (are you missing a using directive or an assembly reference?)

Check failure on line 374 in src/DotRecast.Detour.Crowd/DtCrowd.cs

View workflow job for this annotation

GitHub Actions / test-ubuntu-latest-8

The name 'MethodImplOptions' does not exist in the current context

Check failure on line 374 in src/DotRecast.Detour.Crowd/DtCrowd.cs

View workflow job for this annotation

GitHub Actions / test-macos-latest-6

The type or namespace name 'MethodImplAttribute' could not be found (are you missing a using directive or an assembly reference?)

Check failure on line 374 in src/DotRecast.Detour.Crowd/DtCrowd.cs

View workflow job for this annotation

GitHub Actions / test-macos-latest-6

The type or namespace name 'MethodImpl' could not be found (are you missing a using directive or an assembly reference?)

Check failure on line 374 in src/DotRecast.Detour.Crowd/DtCrowd.cs

View workflow job for this annotation

GitHub Actions / test-macos-latest-6

The name 'MethodImplOptions' does not exist in the current context

Check failure on line 374 in src/DotRecast.Detour.Crowd/DtCrowd.cs

View workflow job for this annotation

GitHub Actions / test-macos-latest-6

The type or namespace name 'MethodImplAttribute' could not be found (are you missing a using directive or an assembly reference?)

Check failure on line 374 in src/DotRecast.Detour.Crowd/DtCrowd.cs

View workflow job for this annotation

GitHub Actions / test-macos-latest-6

The type or namespace name 'MethodImpl' could not be found (are you missing a using directive or an assembly reference?)

Check failure on line 374 in src/DotRecast.Detour.Crowd/DtCrowd.cs

View workflow job for this annotation

GitHub Actions / test-macos-latest-6

The name 'MethodImplOptions' does not exist in the current context
public List<DtCrowdAgent> GetActiveAgents()
{
return _activeAgents;
}
Expand Down Expand Up @@ -412,7 +413,7 @@ public DtCrowdTelemetry Update(float dt, DtCrowdAgentDebugInfo debug)

_telemetry.Start();

IList<DtCrowdAgent> agents = GetActiveAgents();
var agents = FCollectionsMarshal.AsSpan(GetActiveAgents());

// Check that all agents still have valid paths.
CheckPathValidity(agents, dt);
Expand Down Expand Up @@ -454,12 +455,11 @@ public DtCrowdTelemetry Update(float dt, DtCrowdAgentDebugInfo debug)
return _telemetry;
}


private void CheckPathValidity(IList<DtCrowdAgent> agents, float dt)
private void CheckPathValidity(ReadOnlySpan<DtCrowdAgent> agents, float dt)
{
using var timer = _telemetry.ScopedTimer(DtCrowdTimerLabel.CheckPathValidity);

for (var i = 0; i < agents.Count; i++)
for (var i = 0; i < agents.Length; i++)
{
var ag = agents[i];
if (ag.state != DtCrowdAgentState.DT_CROWDAGENT_STATE_WALKING)
Expand Down Expand Up @@ -566,7 +566,7 @@ private void CheckPathValidity(IList<DtCrowdAgent> agents, float dt)

RcSortedQueue<DtCrowdAgent> queue1 = new RcSortedQueue<DtCrowdAgent>(512, (a1, a2) => a2.targetReplanTime.CompareTo(a1.targetReplanTime));

private void UpdateMoveRequest(IList<DtCrowdAgent> agents, float dt)
private void UpdateMoveRequest(ReadOnlySpan<DtCrowdAgent> agents, float dt)
{
using var timer = _telemetry.ScopedTimer(DtCrowdTimerLabel.UpdateMoveRequest);

Expand All @@ -575,7 +575,7 @@ private void UpdateMoveRequest(IList<DtCrowdAgent> agents, float dt)

// Fire off new requests.
List<long> reqPath = new List<long>(); // TODO alloc temp
for (var i = 0; i < agents.Count; i++)
for (var i = 0; i < agents.Length; i++)
{
var ag = agents[i];
if (ag.state == DtCrowdAgentState.DT_CROWDAGENT_STATE_INVALID)
Expand Down Expand Up @@ -689,7 +689,7 @@ private void UpdateMoveRequest(IList<DtCrowdAgent> agents, float dt)
}

// Process path results.
for (var i = 0; i < agents.Count; i++)
for (var i = 0; i < agents.Length; i++)
{
var ag = agents[i];
if (ag.targetState == DtMoveRequestState.DT_CROWDAGENT_TARGET_NONE
Expand Down Expand Up @@ -825,14 +825,14 @@ private void UpdateMoveRequest(IList<DtCrowdAgent> agents, float dt)

RcSortedQueue<DtCrowdAgent> queue2 = new RcSortedQueue<DtCrowdAgent>(512, (a1, a2) => a2.topologyOptTime.CompareTo(a1.topologyOptTime));// TODO alloc temp

private void UpdateTopologyOptimization(IList<DtCrowdAgent> agents, float dt)
private void UpdateTopologyOptimization(ReadOnlySpan<DtCrowdAgent> agents, float dt)
{
using var timer = _telemetry.ScopedTimer(DtCrowdTimerLabel.UpdateTopologyOptimization);

var queue = queue2;
queue.Clear();

for (var i = 0; i < agents.Count; i++)
for (var i = 0; i < agents.Length; i++)
{
var ag = agents[i];
if (ag.state != DtCrowdAgentState.DT_CROWDAGENT_STATE_WALKING)
Expand Down Expand Up @@ -866,13 +866,13 @@ private void UpdateTopologyOptimization(IList<DtCrowdAgent> agents, float dt)
}
}

private void BuildProximityGrid(IList<DtCrowdAgent> agents)
private void BuildProximityGrid(ReadOnlySpan<DtCrowdAgent> agents)
{
using var timer = _telemetry.ScopedTimer(DtCrowdTimerLabel.BuildProximityGrid);

_grid = new DtProximityGrid(_config.maxAgentRadius * 3);

for (var i = 0; i < agents.Count; i++)
for (var i = 0; i < agents.Length; i++)
{
var ag = agents[i];
RcVec3f p = ag.npos;
Expand All @@ -881,11 +881,11 @@ private void BuildProximityGrid(IList<DtCrowdAgent> agents)
}
}

private void BuildNeighbours(IList<DtCrowdAgent> agents)
private void BuildNeighbours(ReadOnlySpan<DtCrowdAgent> agents)
{
using var timer = _telemetry.ScopedTimer(DtCrowdTimerLabel.BuildNeighbours);

for (var i = 0; i < agents.Count; i++)
for (var i = 0; i < agents.Length; i++)
{
var ag = agents[i];
if (ag.state != DtCrowdAgentState.DT_CROWDAGENT_STATE_WALKING)
Expand Down Expand Up @@ -989,12 +989,12 @@ private int GetNeighbours(RcVec3f pos, float height, float range, DtCrowdAgent s
return n;
}

private void FindCorners(IList<DtCrowdAgent> agents, DtCrowdAgentDebugInfo debug)
private void FindCorners(ReadOnlySpan<DtCrowdAgent> agents, DtCrowdAgentDebugInfo debug)
{
using var timer = _telemetry.ScopedTimer(DtCrowdTimerLabel.FindCorners);

DtCrowdAgent debugAgent = debug != null ? debug.agent : null;
for (var i = 0; i < agents.Count; i++)
for (var i = 0; i < agents.Length; i++)
{
var ag = agents[i];
if (ag.state != DtCrowdAgentState.DT_CROWDAGENT_STATE_WALKING)
Expand Down Expand Up @@ -1038,12 +1038,12 @@ private void FindCorners(IList<DtCrowdAgent> agents, DtCrowdAgentDebugInfo debug
}
}

private void TriggerOffMeshConnections(IList<DtCrowdAgent> agents)
private void TriggerOffMeshConnections(ReadOnlySpan<DtCrowdAgent> agents)
{
using var timer = _telemetry.ScopedTimer(DtCrowdTimerLabel.TriggerOffMeshConnections);

Span<long> refs = stackalloc long[2];
for (var i = 0; i < agents.Count; i++)
for (var i = 0; i < agents.Length; i++)
{
var ag = agents[i];
if (ag.state != DtCrowdAgentState.DT_CROWDAGENT_STATE_WALKING)
Expand Down Expand Up @@ -1087,11 +1087,11 @@ private void TriggerOffMeshConnections(IList<DtCrowdAgent> agents)
}
}

private void CalculateSteering(IList<DtCrowdAgent> agents)
private void CalculateSteering(ReadOnlySpan<DtCrowdAgent> agents)
{
using var timer = _telemetry.ScopedTimer(DtCrowdTimerLabel.CalculateSteering);

for (var i = 0; i < agents.Count; i++)
for (var i = 0; i < agents.Length; i++)
{
var ag = agents[i];
if (ag.state != DtCrowdAgentState.DT_CROWDAGENT_STATE_WALKING)
Expand Down Expand Up @@ -1185,12 +1185,12 @@ private void CalculateSteering(IList<DtCrowdAgent> agents)
}
}

private unsafe void PlanVelocity(DtCrowdAgentDebugInfo debug, IList<DtCrowdAgent> agents)
private unsafe void PlanVelocity(DtCrowdAgentDebugInfo debug, ReadOnlySpan<DtCrowdAgent> agents)
{
using var timer = _telemetry.ScopedTimer(DtCrowdTimerLabel.PlanVelocity);

DtCrowdAgent debugAgent = debug != null ? debug.agent : null;
for (var i = 0; i < agents.Count; i++)
for (var i = 0; i < agents.Length; i++)
{
var ag = agents[i];
if (ag.state != DtCrowdAgentState.DT_CROWDAGENT_STATE_WALKING)
Expand Down Expand Up @@ -1257,11 +1257,11 @@ private unsafe void PlanVelocity(DtCrowdAgentDebugInfo debug, IList<DtCrowdAgent
}
}

private void Integrate(float dt, IList<DtCrowdAgent> agents)
private void Integrate(float dt, ReadOnlySpan<DtCrowdAgent> agents)
{
using var timer = _telemetry.ScopedTimer(DtCrowdTimerLabel.Integrate);

for (var i = 0; i < agents.Count; i++)
for (var i = 0; i < agents.Length; i++)
{
var ag = agents[i];
if (ag.state != DtCrowdAgentState.DT_CROWDAGENT_STATE_WALKING)
Expand All @@ -1273,13 +1273,13 @@ private void Integrate(float dt, IList<DtCrowdAgent> agents)
}
}

private void HandleCollisions(IList<DtCrowdAgent> agents)
private void HandleCollisions(ReadOnlySpan<DtCrowdAgent> agents)
{
using var timer = _telemetry.ScopedTimer(DtCrowdTimerLabel.HandleCollisions);

for (int iter = 0; iter < 4; ++iter)
{
for (var i = 0; i < agents.Count; i++)
for (var i = 0; i < agents.Length; i++)
{
var ag = agents[i];
long idx0 = ag.idx;
Expand Down Expand Up @@ -1338,7 +1338,7 @@ private void HandleCollisions(IList<DtCrowdAgent> agents)
}
}

for (var i = 0; i < agents.Count; i++)
for (var i = 0; i < agents.Length; i++)
{
var ag = agents[i];
if (ag.state != DtCrowdAgentState.DT_CROWDAGENT_STATE_WALKING)
Expand All @@ -1351,11 +1351,11 @@ private void HandleCollisions(IList<DtCrowdAgent> agents)
}
}

private void MoveAgents(IList<DtCrowdAgent> agents)
private void MoveAgents(ReadOnlySpan<DtCrowdAgent> agents)
{
using var timer = _telemetry.ScopedTimer(DtCrowdTimerLabel.MoveAgents);

for (var i = 0; i < agents.Count; i++)
for (var i = 0; i < agents.Length; i++)
{
var ag = agents[i];
if (ag.state != DtCrowdAgentState.DT_CROWDAGENT_STATE_WALKING)
Expand All @@ -1378,11 +1378,11 @@ private void MoveAgents(IList<DtCrowdAgent> agents)
}
}

private void UpdateOffMeshConnections(IList<DtCrowdAgent> agents, float dt)
private void UpdateOffMeshConnections(ReadOnlySpan<DtCrowdAgent> agents, float dt)
{
using var timer = _telemetry.ScopedTimer(DtCrowdTimerLabel.UpdateOffMeshConnections);

for (var i = 0; i < agents.Count; i++)
for (var i = 0; i < agents.Length; i++)
{
var ag = agents[i];
DtCrowdAgentAnimation anim = ag.animation;
Expand Down
39 changes: 4 additions & 35 deletions src/DotRecast.Detour/DtPathUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ 3. This notice may not be removed or altered from any source distribution.
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using DotRecast.Core.Numerics;
using DotRecast.Core;

namespace DotRecast.Detour
{
Expand Down Expand Up @@ -278,11 +279,11 @@ public static int MergeCorridorStartShortcut(ref List<long> path, int npath, int

// TODO reuse tests
#if NET6_0_OR_GREATER
var visitedSlice = CollectionsMarshal.AsSpan(visited).Slice(0, furthestVisited);
var pathSlice = CollectionsMarshal.AsSpan(path).Slice(furthestPath, npath - furthestPath);
var visitedSlice = FCollectionsMarshal.AsSpan(visited).Slice(0, furthestVisited);
var pathSlice = FCollectionsMarshal.AsSpan(path).Slice(furthestPath, npath - furthestPath);
var count = visitedSlice.Length + pathSlice.Length;
var result = new List<long>();
var span = FMemoryMarshal.CreateSpan(result, count);
var span = FCollectionsMarshal.CreateSpan(result, count);
visitedSlice.CopyTo(span);
pathSlice.CopyTo(span.Slice(visitedSlice.Length));
path = result;
Expand All @@ -292,36 +293,4 @@ public static int MergeCorridorStartShortcut(ref List<long> path, int npath, int
#endif
}
}

public static class FMemoryMarshal
{
/// <summary>
/// similar as AsSpan but modify size to create fixed-size span.
/// </summary>
public static Span<T> CreateSpan<T>(List<T> list, int count)
{
#if NET8_0_OR_GREATER
CollectionsMarshal.SetCount(list, count);
return CollectionsMarshal.AsSpan(list);
#else
// TODO 有一些差异,CollectionsMarshal.SetCount 会清掉引用类型的对象
if (list.Capacity < count)
list.Capacity = count;

ref var view = ref Unsafe.As<List<T>, ListView<T>>(ref list); // 没有gc
view._size = count;
return view._items.AsSpan(0, count);
#endif
}

#if !NET8_0_OR_GREATER
// NOTE: These structure depndent on .NET 7, if changed, require to keep same structure.
internal class ListView<T>
{
public T[] _items;
public int _size;
public int _version;
}
#endif
}
}

0 comments on commit 1fefff7

Please sign in to comment.