Skip to content

Commit

Permalink
Avoid allocation in ScopedTimer call
Browse files Browse the repository at this point in the history
  • Loading branch information
wrenge authored and ikpil committed Feb 19, 2024
1 parent 01b3bcf commit 3158dfc
Showing 1 changed file with 20 additions and 2 deletions.
22 changes: 20 additions & 2 deletions src/DotRecast.Detour.Crowd/DtCrowdTelemetry.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -69,10 +87,10 @@ public void RecordMaxTimeToFindPath(float time)
_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)
Expand Down

0 comments on commit 3158dfc

Please sign in to comment.