Added struct DtCrowdScopedTimer to avoid allocations in scoped timer calls @wrenge

- https://github.com/ikpil/DotRecast/pull/54
This commit is contained in:
ikpil 2024-02-20 01:10:54 +09:00
parent 3158dfc29c
commit 804cb275a7
4 changed files with 29 additions and 25 deletions

View File

@ -8,6 +8,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).
### Added ### Added
- Added RcCircularBuffer<T> [@ikpil](https://github.com/ikpil) - Added RcCircularBuffer<T> [@ikpil](https://github.com/ikpil)
- Added struct DtCrowdScopedTimer to avoid allocations in scoped timer calls. [@wrenge](https://github.com/wrenge)
### Fixed ### Fixed

View File

@ -20,6 +20,7 @@ freely, subject to the following restrictions:
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Threading.Tasks;
using DotRecast.Core; using DotRecast.Core;
using DotRecast.Core.Collections; using DotRecast.Core.Collections;
using DotRecast.Core.Numerics; using DotRecast.Core.Numerics;

View File

@ -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);
}
}
}

View File

@ -27,24 +27,6 @@ namespace DotRecast.Detour.Crowd
{ {
public class DtCrowdTelemetry 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; public const int TIMING_SAMPLES = 10;
private float _maxTimeToEnqueueRequest; private float _maxTimeToEnqueueRequest;
private float _maxTimeToFindPath; private float _maxTimeToFindPath;
@ -87,18 +69,17 @@ namespace DotRecast.Detour.Crowd
_maxTimeToFindPath = Math.Max(_maxTimeToFindPath, time); _maxTimeToFindPath = Math.Max(_maxTimeToFindPath, time);
} }
public DisposableHandle ScopedTimer(DtCrowdTimerLabel label) internal DtCrowdScopedTimer ScopedTimer(DtCrowdTimerLabel label)
{ {
Start(label); return new DtCrowdScopedTimer(this, label);
return new DisposableHandle(this, label);
} }
private void Start(DtCrowdTimerLabel name) internal void Start(DtCrowdTimerLabel name)
{ {
_executionTimings.Add(name, RcFrequency.Ticks); _executionTimings.Add(name, RcFrequency.Ticks);
} }
private void Stop(DtCrowdTimerLabel name) internal void Stop(DtCrowdTimerLabel name)
{ {
long duration = RcFrequency.Ticks - _executionTimings[name]; long duration = RcFrequency.Ticks - _executionTimings[name];
if (!_executionTimingSamples.TryGetValue(name, out var cb)) if (!_executionTimingSamples.TryGetValue(name, out var cb))
@ -110,7 +91,5 @@ namespace DotRecast.Detour.Crowd
cb.PushBack(duration); cb.PushBack(duration);
_executionTimings[name] = (long)cb.Average(); _executionTimings[name] = (long)cb.Average();
} }
} }
} }