diff --git a/src/DotRecast.Detour.Crowd/DtProximityGrid.cs b/src/DotRecast.Detour.Crowd/DtProximityGrid.cs index 3cd182c..e394982 100644 --- a/src/DotRecast.Detour.Crowd/DtProximityGrid.cs +++ b/src/DotRecast.Detour.Crowd/DtProximityGrid.cs @@ -22,6 +22,7 @@ using System; using System.Collections.Generic; using System.Linq; using System.Runtime.CompilerServices; +using DotRecast.Core.Buffers; namespace DotRecast.Detour.Crowd { @@ -30,12 +31,14 @@ namespace DotRecast.Detour.Crowd private readonly float _cellSize; private readonly float _invCellSize; private readonly Dictionary> _items; + private readonly RcObjectPool> _listPool; public DtProximityGrid(float cellSize) { _cellSize = cellSize; _invCellSize = 1.0f / cellSize; _items = new Dictionary>(); + _listPool = new RcObjectPool>(() => new List()); } [MethodImpl(MethodImplOptions.AggressiveInlining)] @@ -57,6 +60,8 @@ namespace DotRecast.Detour.Crowd public void Clear() { + foreach (var pair in _items) + _listPool.Return(pair.Value); _items.Clear(); } @@ -74,7 +79,7 @@ namespace DotRecast.Detour.Crowd long key = CombineKey(x, y); if (!_items.TryGetValue(key, out var ids)) { - ids = new List(); + ids = _listPool.Get(); _items.Add(key, ids); }