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

This commit is contained in:
CasualPokePlayer 2022-12-03 17:46:49 -08:00
parent d508b734d6
commit f01463e2b3
2 changed files with 18 additions and 28 deletions

View File

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

View File

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