Skip to content

Commit f01463e

Browse files
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
1 parent d508b73 commit f01463e

File tree

2 files changed

+17
-27
lines changed

2 files changed

+17
-27
lines changed

src/BizHawk.Client.EmuHawk/RetroAchievements/RetroAchievements.Memory.cs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -79,15 +79,15 @@ protected virtual int FixAddr(int addr)
7979

8080
protected virtual byte ReadMem(int addr)
8181
{
82-
using (MemGuard?.EnterExit())
82+
using (MemGuard.EnterExit())
8383
{
8484
return _domain.PeekByte(FixAddr(addr) ^ _addressMangler);
8585
}
8686
}
8787

8888
protected virtual void WriteMem(int addr, byte val)
8989
{
90-
using (MemGuard?.EnterExit())
90+
using (MemGuard.EnterExit())
9191
{
9292
_domain.PokeByte(FixAddr(addr) ^ _addressMangler, val);
9393
}
@@ -102,7 +102,7 @@ protected virtual int ReadMemBlock(int addr, IntPtr buffer, int bytes)
102102
return 0;
103103
}
104104

105-
using (MemGuard?.EnterExit())
105+
using (MemGuard.EnterExit())
106106
{
107107
var end = Math.Min(addr + bytes, _domainAddrStart + BankSize);
108108
var length = end - addr;
@@ -191,7 +191,7 @@ protected override int ReadMemBlock(int addr, IntPtr buffer, int bytes)
191191
return 0;
192192
}
193193

194-
using (MemGuard?.EnterExit())
194+
using (MemGuard.EnterExit())
195195
{
196196
var end = Math.Min(addr + bytes, BankSize);
197197
var length = end - addr;
@@ -236,7 +236,7 @@ private byte ReadVRAMPacked(int addr)
236236

237237
protected override byte ReadMem(int addr)
238238
{
239-
using (MemGuard?.EnterExit())
239+
using (MemGuard.EnterExit())
240240
{
241241
if (addr < 0x40)
242242
{
@@ -251,7 +251,7 @@ protected override byte ReadMem(int addr)
251251

252252
protected override void WriteMem(int addr, byte val)
253253
{
254-
using (MemGuard?.EnterExit())
254+
using (MemGuard.EnterExit())
255255
{
256256
if (addr < 0x40)
257257
{
@@ -275,7 +275,7 @@ protected override int ReadMemBlock(int addr, IntPtr buffer, int bytes)
275275
return 0;
276276
}
277277

278-
using (MemGuard?.EnterExit())
278+
using (MemGuard.EnterExit())
279279
{
280280
var regs = _debuggable.GetCpuFlagsAndRegisters();
281281
var end = Math.Min(addr + bytes, BankSize);

src/BizHawk.Common/IMonitor.cs

Lines changed: 10 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -11,32 +11,22 @@ public interface IMonitor
1111

1212
public static class MonitorExtensions
1313
{
14-
public static IDisposable EnterExit(this IMonitor m)
15-
{
16-
var ret = new EnterExitWrapper(m);
17-
m.Enter();
18-
return ret;
19-
}
14+
public static EnterExitWrapper EnterExit(this IMonitor m)
15+
=> new(m);
2016

21-
private class EnterExitWrapper : IDisposable
17+
public readonly ref struct EnterExitWrapper
2218
{
23-
private readonly IMonitor _m;
19+
// yes, this can be null
20+
private readonly IMonitor? _m;
2421

25-
private bool _disposed;
26-
27-
public EnterExitWrapper(IMonitor m)
28-
{
29-
_m = m;
22+
public EnterExitWrapper(IMonitor? m)
23+
{
24+
_m = m;
25+
_m?.Enter();
3026
}
3127

3228
public void Dispose()
33-
{
34-
if (!_disposed)
35-
{
36-
_m.Exit();
37-
_disposed = true;
38-
}
39-
}
29+
=> _m?.Exit();
4030
}
4131
}
4232
}

0 commit comments

Comments
 (0)