diff --git a/CHANGELOG.md b/CHANGELOG.md index 42ba3d2..5d86e5b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,6 +9,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/). ### Added - Added RcCircularBuffer [@ikpil](https://github.com/ikpil) - 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 diff --git a/src/DotRecast.Core/RcAnonymousDisposable.cs b/src/DotRecast.Core/RcAnonymousDisposable.cs deleted file mode 100644 index 6cf1050..0000000 --- a/src/DotRecast.Core/RcAnonymousDisposable.cs +++ /dev/null @@ -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; - } - } -} \ No newline at end of file diff --git a/src/DotRecast.Core/RcContext.cs b/src/DotRecast.Core/RcContext.cs index bb6fd2d..8d707d4 100644 --- a/src/DotRecast.Core/RcContext.cs +++ b/src/DotRecast.Core/RcContext.cs @@ -49,10 +49,9 @@ namespace DotRecast.Core _timerAccum = new ConcurrentDictionary(); } - public IDisposable ScopedTimer(RcTimerLabel label) + public RcScopedTimer ScopedTimer(RcTimerLabel label) { - StartTimer(label); - return new RcAnonymousDisposable(() => StopTimer(label)); + return new RcScopedTimer(this, label); } public void StartTimer(RcTimerLabel label) diff --git a/src/DotRecast.Core/RcScopedTimer.cs b/src/DotRecast.Core/RcScopedTimer.cs new file mode 100644 index 0000000..6b84e28 --- /dev/null +++ b/src/DotRecast.Core/RcScopedTimer.cs @@ -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); + } + } +} \ No newline at end of file