forked from bit/DotRecastNetSim
Added struct RcScopedTimer to avoid allocations
This commit is contained in:
parent
c908daa8c3
commit
6034bfa28a
|
@ -9,6 +9,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)
|
- Added struct DtCrowdScopedTimer to avoid allocations in scoped timer calls. [@wrenge](https://github.com/wrenge)
|
||||||
|
- Added struct RcScopedTimer to avoid allocations in RcContext scoped timer [@ikpil](https://github.com/ikpil)
|
||||||
|
|
||||||
### Fixed
|
### Fixed
|
||||||
|
|
||||||
|
|
|
@ -1,20 +0,0 @@
|
||||||
using System;
|
|
||||||
|
|
||||||
namespace DotRecast.Core
|
|
||||||
{
|
|
||||||
public struct RcAnonymousDisposable : IDisposable
|
|
||||||
{
|
|
||||||
private Action _dispose;
|
|
||||||
|
|
||||||
public RcAnonymousDisposable(Action dispose)
|
|
||||||
{
|
|
||||||
_dispose = dispose;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void Dispose()
|
|
||||||
{
|
|
||||||
_dispose?.Invoke();
|
|
||||||
_dispose = null;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -49,10 +49,9 @@ namespace DotRecast.Core
|
||||||
_timerAccum = new ConcurrentDictionary<string, RcAtomicLong>();
|
_timerAccum = new ConcurrentDictionary<string, RcAtomicLong>();
|
||||||
}
|
}
|
||||||
|
|
||||||
public IDisposable ScopedTimer(RcTimerLabel label)
|
public RcScopedTimer ScopedTimer(RcTimerLabel label)
|
||||||
{
|
{
|
||||||
StartTimer(label);
|
return new RcScopedTimer(this, label);
|
||||||
return new RcAnonymousDisposable(() => StopTimer(label));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void StartTimer(RcTimerLabel label)
|
public void StartTimer(RcTimerLabel label)
|
||||||
|
|
|
@ -0,0 +1,23 @@
|
||||||
|
using System;
|
||||||
|
|
||||||
|
namespace DotRecast.Core
|
||||||
|
{
|
||||||
|
public readonly struct RcScopedTimer : IDisposable
|
||||||
|
{
|
||||||
|
private readonly RcContext _context;
|
||||||
|
private readonly RcTimerLabel _label;
|
||||||
|
|
||||||
|
internal RcScopedTimer(RcContext context, RcTimerLabel label)
|
||||||
|
{
|
||||||
|
_context = context;
|
||||||
|
_label = label;
|
||||||
|
|
||||||
|
_context.StartTimer(_label);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void Dispose()
|
||||||
|
{
|
||||||
|
_context.StopTimer(_label);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue