From 592ecebe1e99f6770113429bbc4ddfa6f571db40 Mon Sep 17 00:00:00 2001 From: wrenge Date: Wed, 13 Nov 2024 13:25:13 +0300 Subject: [PATCH] Proximity grid list reuse --- src/DotRecast.Detour.Crowd/DtProximityGrid.cs | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) 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); }