From 01b3bcf77195980c5bd3783ab19fe017c5a64a46 Mon Sep 17 00:00:00 2001 From: ikpil Date: Mon, 19 Feb 2024 00:36:44 +0900 Subject: [PATCH] added RcCylicBuffer extensions --- src/DotRecast.Core/Buffers/RcCyclicBuffers.cs | 54 +++++++++++++++++++ .../DtCrowdTelemetry.cs | 11 +--- .../Tools/RcCrowdAgentProfilingTool.cs | 19 +++---- 3 files changed, 62 insertions(+), 22 deletions(-) create mode 100644 src/DotRecast.Core/Buffers/RcCyclicBuffers.cs diff --git a/src/DotRecast.Core/Buffers/RcCyclicBuffers.cs b/src/DotRecast.Core/Buffers/RcCyclicBuffers.cs new file mode 100644 index 0000000..cadcf48 --- /dev/null +++ b/src/DotRecast.Core/Buffers/RcCyclicBuffers.cs @@ -0,0 +1,54 @@ +namespace DotRecast.Core.Buffers +{ + public static class RcCyclicBuffers + { + public static long Sum(this RcCyclicBuffer source) + { + long sum = 0; + checked + { + source.ForEach(x => sum += x); + } + + return sum; + } + + public static double Average(this RcCyclicBuffer source) + { + if (0 >= source.Size) + return 0; + + return source.Sum() / (double)source.Size; + } + + public static long Min(this RcCyclicBuffer source) + { + if (0 >= source.Size) + return 0; + + long minValue = long.MaxValue; + source.ForEach(x => + { + if (x < minValue) + minValue = x; + }); + + return minValue; + } + + public static long Max(this RcCyclicBuffer source) + { + if (0 >= source.Size) + return 0; + + long maxValue = long.MinValue; + source.ForEach(x => + { + if (x > maxValue) + maxValue = x; + }); + + return maxValue; + } + } +} \ No newline at end of file diff --git a/src/DotRecast.Detour.Crowd/DtCrowdTelemetry.cs b/src/DotRecast.Detour.Crowd/DtCrowdTelemetry.cs index 0347d00..a474f85 100644 --- a/src/DotRecast.Detour.Crowd/DtCrowdTelemetry.cs +++ b/src/DotRecast.Detour.Crowd/DtCrowdTelemetry.cs @@ -90,18 +90,9 @@ namespace DotRecast.Detour.Crowd } cb.PushBack(duration); - _executionTimings[name] = CalculateAverage(cb); + _executionTimings[name] = (long)cb.Average(); } - private static long CalculateAverage(RcCyclicBuffer buffer) - { - long sum = 0L; - buffer.ForEach(item => - { - sum += item; - }); - return sum / buffer.Size; - } } } \ No newline at end of file diff --git a/src/DotRecast.Recast.Toolset/Tools/RcCrowdAgentProfilingTool.cs b/src/DotRecast.Recast.Toolset/Tools/RcCrowdAgentProfilingTool.cs index c2c16fd..6a5227b 100644 --- a/src/DotRecast.Recast.Toolset/Tools/RcCrowdAgentProfilingTool.cs +++ b/src/DotRecast.Recast.Toolset/Tools/RcCrowdAgentProfilingTool.cs @@ -1,7 +1,9 @@ using System; using System.Collections.Generic; using System.Linq; +using System.Numerics; using DotRecast.Core; +using DotRecast.Core.Buffers; using DotRecast.Core.Collections; using DotRecast.Core.Numerics; using DotRecast.Detour; @@ -25,7 +27,7 @@ namespace DotRecast.Recast.Toolset.Tools private const int SamplingCount = 500; private long _samplingUpdateTime; - private readonly List _updateTimes; + private readonly RcCyclicBuffer _updateTimes; private long _curUpdateTime; private long _avgUpdateTime; private long _minUpdateTime; @@ -36,7 +38,7 @@ namespace DotRecast.Recast.Toolset.Tools _cfg = new RcCrowdAgentProfilingToolConfig(); _agCfg = new DtCrowdAgentConfig(); _polyPoints = new List(); - _updateTimes = new List(); + _updateTimes = new RcCyclicBuffer(SamplingCount); } public string GetName() @@ -264,20 +266,14 @@ namespace DotRecast.Recast.Toolset.Tools } var currentTime = endTime - startTime; - _updateTimes.Add(currentTime); + _updateTimes.PushBack(currentTime); - if ((int)(SamplingCount * 1.25f) < _updateTimes.Count) - { - _updateTimes.RemoveRange(0, _updateTimes.Count - SamplingCount); - } - // for benchmark _samplingUpdateTime = _updateTimes.Sum() / TimeSpan.TicksPerMillisecond; _curUpdateTime = currentTime / TimeSpan.TicksPerMillisecond; _avgUpdateTime = (long)(_updateTimes.Average() / TimeSpan.TicksPerMillisecond); _minUpdateTime = _updateTimes.Min() / TimeSpan.TicksPerMillisecond; _maxUpdateTime = _updateTimes.Max() / TimeSpan.TicksPerMillisecond; - } private void MoveMob(DtNavMeshQuery navquery, IDtQueryFilter filter, DtCrowdAgent ag, RcCrowdAgentData crowAgentData) @@ -392,16 +388,15 @@ namespace DotRecast.Recast.Toolset.Tools { return _avgUpdateTime; } - + public long GetCrowdUpdateMinTime() { return _minUpdateTime; } - + public long GetCrowdUpdateMaxTime() { return _maxUpdateTime; } - } } \ No newline at end of file