diff --git a/CHANGELOG.md b/CHANGELOG.md index d1d194a..18e0d06 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,6 +8,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/). ### Added - Added RcCircularBuffer [@ikpil](https://github.com/ikpil) +- Added struct DtCrowdScopedTimer to avoid allocations in scoped timer calls. [@wrenge](https://github.com/wrenge) ### Fixed diff --git a/src/DotRecast.Detour.Crowd/DtCrowd.cs b/src/DotRecast.Detour.Crowd/DtCrowd.cs index 3888e89..2db2f99 100644 --- a/src/DotRecast.Detour.Crowd/DtCrowd.cs +++ b/src/DotRecast.Detour.Crowd/DtCrowd.cs @@ -20,6 +20,7 @@ freely, subject to the following restrictions: using System; using System.Collections.Generic; +using System.Threading.Tasks; using DotRecast.Core; using DotRecast.Core.Collections; using DotRecast.Core.Numerics; diff --git a/src/DotRecast.Detour.Crowd/DtCrowdScopedTimer.cs b/src/DotRecast.Detour.Crowd/DtCrowdScopedTimer.cs new file mode 100644 index 0000000..a78e69b --- /dev/null +++ b/src/DotRecast.Detour.Crowd/DtCrowdScopedTimer.cs @@ -0,0 +1,23 @@ +using System; + +namespace DotRecast.Detour.Crowd +{ + internal readonly struct DtCrowdScopedTimer : IDisposable + { + private readonly DtCrowdTimerLabel _label; + private readonly DtCrowdTelemetry _telemetry; + + internal DtCrowdScopedTimer(DtCrowdTelemetry telemetry, DtCrowdTimerLabel label) + { + _telemetry = telemetry; + _label = label; + + _telemetry.Start(_label); + } + + public void Dispose() + { + _telemetry.Stop(_label); + } + } +} \ No newline at end of file diff --git a/src/DotRecast.Detour.Crowd/DtCrowdTelemetry.cs b/src/DotRecast.Detour.Crowd/DtCrowdTelemetry.cs index 9935286..b7b88d4 100644 --- a/src/DotRecast.Detour.Crowd/DtCrowdTelemetry.cs +++ b/src/DotRecast.Detour.Crowd/DtCrowdTelemetry.cs @@ -27,24 +27,6 @@ namespace DotRecast.Detour.Crowd { public class DtCrowdTelemetry { - public readonly struct DisposableHandle : IDisposable - { - private readonly DtCrowdTimerLabel _label; - private readonly DtCrowdTelemetry _telemetry; - - public DisposableHandle(DtCrowdTelemetry telemetry, DtCrowdTimerLabel label) - { - _telemetry = telemetry; - _label = label; - } - - public void Dispose() - { - _telemetry.Stop(_label); - } - } - - public const int TIMING_SAMPLES = 10; private float _maxTimeToEnqueueRequest; private float _maxTimeToFindPath; @@ -87,18 +69,17 @@ namespace DotRecast.Detour.Crowd _maxTimeToFindPath = Math.Max(_maxTimeToFindPath, time); } - public DisposableHandle ScopedTimer(DtCrowdTimerLabel label) + internal DtCrowdScopedTimer ScopedTimer(DtCrowdTimerLabel label) { - Start(label); - return new DisposableHandle(this, label); + return new DtCrowdScopedTimer(this, label); } - private void Start(DtCrowdTimerLabel name) + internal void Start(DtCrowdTimerLabel name) { _executionTimings.Add(name, RcFrequency.Ticks); } - private void Stop(DtCrowdTimerLabel name) + internal void Stop(DtCrowdTimerLabel name) { long duration = RcFrequency.Ticks - _executionTimings[name]; if (!_executionTimingSamples.TryGetValue(name, out var cb)) @@ -110,7 +91,5 @@ namespace DotRecast.Detour.Crowd cb.PushBack(duration); _executionTimings[name] = (long)cb.Average(); } - - } } \ No newline at end of file