Skip to content

Commit

Permalink
Replaced list with cyclic buffer
Browse files Browse the repository at this point in the history
  • Loading branch information
wrenge authored and ikpil committed Feb 17, 2024
1 parent d0bec37 commit 50ed8f2
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 6 deletions.
48 changes: 48 additions & 0 deletions src/DotRecast.Core/Buffers/RcCyclicBuffer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
using System;

namespace DotRecast.Core.Buffers
{
public class RcCyclicBuffer<T>
{
public int MinIndex { get; private set; }
public int MaxIndex { get; private set; }
public int Count => MaxIndex - MinIndex + 1;
public readonly int Size;

public T this[int index] => Get(index);

private readonly T[] _buffer;

public RcCyclicBuffer(in int size)
{
_buffer = new T[size];
Size = size;
MinIndex = 0;
MaxIndex = -1;
}

public void Add(in T item)
{
MaxIndex++;
var index = MaxIndex % Size;

if (MaxIndex >= Size)
MinIndex = MaxIndex - Size + 1;

_buffer[index] = item;
}

public T Get(in int index)
{
if (index < MinIndex || index > MaxIndex)
throw new ArgumentOutOfRangeException();

return _buffer[index % Size];
}

public Span<T> AsSpan()
{
return _buffer.AsSpan(0, Count);
}
}
}
19 changes: 13 additions & 6 deletions src/DotRecast.Detour.Crowd/DtCrowdTelemetry.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.Linq;
using System.Reflection.Emit;
using DotRecast.Core;
using DotRecast.Core.Buffers;
using DotRecast.Core.Numerics;

namespace DotRecast.Detour.Crowd
Expand All @@ -34,7 +35,7 @@ public class DtCrowdTelemetry
private float _maxTimeToFindPath;

private readonly Dictionary<DtCrowdTimerLabel, long> _executionTimings = new Dictionary<DtCrowdTimerLabel, long>();
private readonly Dictionary<DtCrowdTimerLabel, List<long>> _executionTimingSamples = new Dictionary<DtCrowdTimerLabel, List<long>>();
private readonly Dictionary<DtCrowdTimerLabel, RcCyclicBuffer<long>> _executionTimingSamples = new Dictionary<DtCrowdTimerLabel, RcCyclicBuffer<long>>();

public float MaxTimeToEnqueueRequest()
{
Expand Down Expand Up @@ -87,17 +88,23 @@ private void Stop(DtCrowdTimerLabel name)
long duration = RcFrequency.Ticks - _executionTimings[name];
if (!_executionTimingSamples.TryGetValue(name, out var s))
{
s = new List<long>();
s = new RcCyclicBuffer<long>(TIMING_SAMPLES);
_executionTimingSamples.Add(name, s);
}

if (s.Count == TIMING_SAMPLES)
s.Add(duration);
_executionTimings[name] = CalculateAverage(s.AsSpan());
}

private static long CalculateAverage(Span<long> buffer)
{
long sum = 0L;
foreach (var item in buffer)
{
s.RemoveAt(0);
sum += item;
}

s.Add(duration);
_executionTimings[name] = (long)s.Average();
return sum / buffer.Length;
}
}
}

0 comments on commit 50ed8f2

Please sign in to comment.