Added struct RcScopedTimer to avoid allocations

This commit is contained in:
ikpil 2024-02-20 02:02:05 +09:00
parent c908daa8c3
commit 6034bfa28a
4 changed files with 26 additions and 23 deletions

View File

@ -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

View File

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

View File

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

View File

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