Skip to content

Commit 804cb27

Browse files
committed
Added struct DtCrowdScopedTimer to avoid allocations in scoped timer calls @wrenge
- #54
1 parent 3158dfc commit 804cb27

File tree

4 files changed

+29
-25
lines changed

4 files changed

+29
-25
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).
88

99
### Added
1010
- Added RcCircularBuffer<T> [@ikpil](https://github.com/ikpil)
11+
- Added struct DtCrowdScopedTimer to avoid allocations in scoped timer calls. [@wrenge](https://github.com/wrenge)
1112

1213
### Fixed
1314

src/DotRecast.Detour.Crowd/DtCrowd.cs

+1
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ 3. This notice may not be removed or altered from any source distribution.
2020

2121
using System;
2222
using System.Collections.Generic;
23+
using System.Threading.Tasks;
2324
using DotRecast.Core;
2425
using DotRecast.Core.Collections;
2526
using DotRecast.Core.Numerics;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
using System;
2+
3+
namespace DotRecast.Detour.Crowd
4+
{
5+
internal readonly struct DtCrowdScopedTimer : IDisposable
6+
{
7+
private readonly DtCrowdTimerLabel _label;
8+
private readonly DtCrowdTelemetry _telemetry;
9+
10+
internal DtCrowdScopedTimer(DtCrowdTelemetry telemetry, DtCrowdTimerLabel label)
11+
{
12+
_telemetry = telemetry;
13+
_label = label;
14+
15+
_telemetry.Start(_label);
16+
}
17+
18+
public void Dispose()
19+
{
20+
_telemetry.Stop(_label);
21+
}
22+
}
23+
}

src/DotRecast.Detour.Crowd/DtCrowdTelemetry.cs

+4-25
Original file line numberDiff line numberDiff line change
@@ -27,24 +27,6 @@ namespace DotRecast.Detour.Crowd
2727
{
2828
public class DtCrowdTelemetry
2929
{
30-
public readonly struct DisposableHandle : IDisposable
31-
{
32-
private readonly DtCrowdTimerLabel _label;
33-
private readonly DtCrowdTelemetry _telemetry;
34-
35-
public DisposableHandle(DtCrowdTelemetry telemetry, DtCrowdTimerLabel label)
36-
{
37-
_telemetry = telemetry;
38-
_label = label;
39-
}
40-
41-
public void Dispose()
42-
{
43-
_telemetry.Stop(_label);
44-
}
45-
}
46-
47-
4830
public const int TIMING_SAMPLES = 10;
4931
private float _maxTimeToEnqueueRequest;
5032
private float _maxTimeToFindPath;
@@ -87,18 +69,17 @@ public void RecordMaxTimeToFindPath(float time)
8769
_maxTimeToFindPath = Math.Max(_maxTimeToFindPath, time);
8870
}
8971

90-
public DisposableHandle ScopedTimer(DtCrowdTimerLabel label)
72+
internal DtCrowdScopedTimer ScopedTimer(DtCrowdTimerLabel label)
9173
{
92-
Start(label);
93-
return new DisposableHandle(this, label);
74+
return new DtCrowdScopedTimer(this, label);
9475
}
9576

96-
private void Start(DtCrowdTimerLabel name)
77+
internal void Start(DtCrowdTimerLabel name)
9778
{
9879
_executionTimings.Add(name, RcFrequency.Ticks);
9980
}
10081

101-
private void Stop(DtCrowdTimerLabel name)
82+
internal void Stop(DtCrowdTimerLabel name)
10283
{
10384
long duration = RcFrequency.Ticks - _executionTimings[name];
10485
if (!_executionTimingSamples.TryGetValue(name, out var cb))
@@ -110,7 +91,5 @@ private void Stop(DtCrowdTimerLabel name)
11091
cb.PushBack(duration);
11192
_executionTimings[name] = (long)cb.Average();
11293
}
113-
114-
11594
}
11695
}

0 commit comments

Comments
 (0)