From f01463e2b339e1324f9f28be0bffcb30471d570b Mon Sep 17 00:00:00 2001 From: CasualPokePlayer <50538166+CasualPokePlayer@users.noreply.github.com> Date: Sat, 3 Dec 2022 17:46:49 -0800 Subject: [PATCH] Convert EnterExitWrapper to a readonly ref struct, this should be a speedup in all cases. Will need to experiment to see if this mean the try/finally from #3296 can be avoided --- .../RetroAchievements.Memory.cs | 14 ++++---- src/BizHawk.Common/IMonitor.cs | 32 +++++++------------ 2 files changed, 18 insertions(+), 28 deletions(-) diff --git a/src/BizHawk.Client.EmuHawk/RetroAchievements/RetroAchievements.Memory.cs b/src/BizHawk.Client.EmuHawk/RetroAchievements/RetroAchievements.Memory.cs index b57860c073..5646345e5a 100644 --- a/src/BizHawk.Client.EmuHawk/RetroAchievements/RetroAchievements.Memory.cs +++ b/src/BizHawk.Client.EmuHawk/RetroAchievements/RetroAchievements.Memory.cs @@ -79,7 +79,7 @@ namespace BizHawk.Client.EmuHawk protected virtual byte ReadMem(int addr) { - using (MemGuard?.EnterExit()) + using (MemGuard.EnterExit()) { return _domain.PeekByte(FixAddr(addr) ^ _addressMangler); } @@ -87,7 +87,7 @@ namespace BizHawk.Client.EmuHawk protected virtual void WriteMem(int addr, byte val) { - using (MemGuard?.EnterExit()) + using (MemGuard.EnterExit()) { _domain.PokeByte(FixAddr(addr) ^ _addressMangler, val); } @@ -102,7 +102,7 @@ namespace BizHawk.Client.EmuHawk return 0; } - using (MemGuard?.EnterExit()) + using (MemGuard.EnterExit()) { var end = Math.Min(addr + bytes, _domainAddrStart + BankSize); var length = end - addr; @@ -191,7 +191,7 @@ namespace BizHawk.Client.EmuHawk return 0; } - using (MemGuard?.EnterExit()) + using (MemGuard.EnterExit()) { var end = Math.Min(addr + bytes, BankSize); var length = end - addr; @@ -236,7 +236,7 @@ namespace BizHawk.Client.EmuHawk protected override byte ReadMem(int addr) { - using (MemGuard?.EnterExit()) + using (MemGuard.EnterExit()) { if (addr < 0x40) { @@ -251,7 +251,7 @@ namespace BizHawk.Client.EmuHawk protected override void WriteMem(int addr, byte val) { - using (MemGuard?.EnterExit()) + using (MemGuard.EnterExit()) { if (addr < 0x40) { @@ -275,7 +275,7 @@ namespace BizHawk.Client.EmuHawk return 0; } - using (MemGuard?.EnterExit()) + using (MemGuard.EnterExit()) { var regs = _debuggable.GetCpuFlagsAndRegisters(); var end = Math.Min(addr + bytes, BankSize); diff --git a/src/BizHawk.Common/IMonitor.cs b/src/BizHawk.Common/IMonitor.cs index 988b6ad3fe..76301ec555 100644 --- a/src/BizHawk.Common/IMonitor.cs +++ b/src/BizHawk.Common/IMonitor.cs @@ -11,32 +11,22 @@ namespace BizHawk.Common public static class MonitorExtensions { - public static IDisposable EnterExit(this IMonitor m) + public static EnterExitWrapper EnterExit(this IMonitor m) + => new(m); + + public readonly ref struct EnterExitWrapper { - var ret = new EnterExitWrapper(m); - m.Enter(); - return ret; - } + // yes, this can be null + private readonly IMonitor? _m; - private class EnterExitWrapper : IDisposable - { - private readonly IMonitor _m; - - private bool _disposed; - - public EnterExitWrapper(IMonitor m) - { - _m = m; + public EnterExitWrapper(IMonitor? m) + { + _m = m; + _m?.Enter(); } public void Dispose() - { - if (!_disposed) - { - _m.Exit(); - _disposed = true; - } - } + => _m?.Exit(); } } }