From 3158dfc29ce021ff3ab411eb49c2b442ca575fa1 Mon Sep 17 00:00:00 2001 From: wreng Date: Sun, 18 Feb 2024 19:29:54 +0300 Subject: [PATCH] Avoid allocation in ScopedTimer call --- .../DtCrowdTelemetry.cs | 22 +++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) diff --git a/src/DotRecast.Detour.Crowd/DtCrowdTelemetry.cs b/src/DotRecast.Detour.Crowd/DtCrowdTelemetry.cs index a474f85..9935286 100644 --- a/src/DotRecast.Detour.Crowd/DtCrowdTelemetry.cs +++ b/src/DotRecast.Detour.Crowd/DtCrowdTelemetry.cs @@ -27,6 +27,24 @@ 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; @@ -69,10 +87,10 @@ namespace DotRecast.Detour.Crowd _maxTimeToFindPath = Math.Max(_maxTimeToFindPath, time); } - public IDisposable ScopedTimer(DtCrowdTimerLabel label) + public DisposableHandle ScopedTimer(DtCrowdTimerLabel label) { Start(label); - return new RcAnonymousDisposable(() => Stop(label)); + return new DisposableHandle(this, label); } private void Start(DtCrowdTimerLabel name)