Skip to content

Commit 3dd36f5

Browse files
revert the explicit try/finally use for PeekByte/PokeByte monitor domain methods, testing seems to show the ref struct use makes EnterExit allocation (now forced to the stack) a non-issue performance wise
1 parent f01463e commit 3dd36f5

File tree

4 files changed

+18
-72
lines changed

4 files changed

+18
-72
lines changed

src/BizHawk.Common/IMonitor.cs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,12 @@ public static EnterExitWrapper EnterExit(this IMonitor m)
1515
=> new(m);
1616

1717
public readonly ref struct EnterExitWrapper
18-
{
19-
// yes, this can be null
20-
private readonly IMonitor? _m;
21-
22-
public EnterExitWrapper(IMonitor? m)
18+
{
19+
// yes, this can be null
20+
private readonly IMonitor? _m;
21+
22+
// disallow public construction outside of EnterExit extension
23+
internal EnterExitWrapper(IMonitor? m)
2324
{
2425
_m = m;
2526
_m?.Enter();

src/BizHawk.Emulation.Common/Base Implementations/MemoryDomainImpls.cs

Lines changed: 4 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -251,15 +251,10 @@ public override byte PeekByte(long addr)
251251
{
252252
if ((ulong)addr < (ulong)Size)
253253
{
254-
try
254+
using (_monitor.EnterExit())
255255
{
256-
_monitor.Enter();
257256
return ((byte*)Data)[addr];
258257
}
259-
finally
260-
{
261-
_monitor.Exit();
262-
}
263258
}
264259

265260
throw new ArgumentOutOfRangeException(nameof(addr));
@@ -271,15 +266,10 @@ public override void PokeByte(long addr, byte val)
271266
{
272267
if ((ulong)addr < (ulong)Size)
273268
{
274-
try
269+
using (_monitor.EnterExit())
275270
{
276-
_monitor.Enter();
277271
((byte*)Data)[addr] = val;
278272
}
279-
finally
280-
{
281-
_monitor.Exit();
282-
}
283273
}
284274
else
285275
{
@@ -361,15 +351,10 @@ public override byte PeekByte(long addr)
361351
{
362352
if ((ulong)addr < (ulong)Size)
363353
{
364-
try
354+
using (_monitor.EnterExit())
365355
{
366-
_monitor.Enter();
367356
return ((byte*)Data)[addr ^ 1];
368357
}
369-
finally
370-
{
371-
_monitor.Exit();
372-
}
373358
}
374359

375360
throw new ArgumentOutOfRangeException(nameof(addr));
@@ -381,15 +366,10 @@ public override void PokeByte(long addr, byte val)
381366
{
382367
if ((ulong)addr < (ulong)Size)
383368
{
384-
try
369+
using (_monitor.EnterExit())
385370
{
386-
_monitor.Enter();
387371
((byte*)Data)[addr ^ 1] = val;
388372
}
389-
finally
390-
{
391-
_monitor.Exit();
392-
}
393373
}
394374
else
395375
{

src/BizHawk.Emulation.Cores/Arcades/MAME/MAME.MemoryDomains.cs

Lines changed: 4 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -35,38 +35,18 @@ public MAMEMemoryDomain(string name, long size, Endian endian, int dataWidth, bo
3535

3636
public override byte PeekByte(long addr)
3737
{
38-
if (addr < 0 || addr >= _systemBusSize) throw new ArgumentOutOfRangeException(paramName: nameof(addr), addr, message: "address out of range");
39-
38+
if ((ulong)addr < (ulong)_systemBusSize) throw new ArgumentOutOfRangeException(paramName: nameof(addr), addr, message: "address out of range");
4039
addr += _firstOffset;
41-
42-
try
43-
{
44-
_monitor.Enter();
45-
return _core.mame_read_byte((uint)addr << _systemBusAddressShift);
46-
}
47-
finally
48-
{
49-
_monitor.Exit();
50-
}
40+
return _core.mame_read_byte((uint)addr << _systemBusAddressShift);
5141
}
5242

5343
public override void PokeByte(long addr, byte val)
5444
{
5545
if (Writable)
5646
{
57-
if (addr < 0 || addr >= _systemBusSize) throw new ArgumentOutOfRangeException(paramName: nameof(addr), addr, message: "address out of range");
58-
47+
if ((ulong)addr < (ulong)_systemBusSize) throw new ArgumentOutOfRangeException(paramName: nameof(addr), addr, message: "address out of range");
5948
addr += _firstOffset;
60-
61-
try
62-
{
63-
_monitor.Enter();
64-
_core.mame_lua_execute($"{MAMELuaCommand.GetSpace}:write_u8({addr << _systemBusAddressShift}, {val})");
65-
}
66-
finally
67-
{
68-
_monitor.Exit();
69-
}
49+
_core.mame_lua_execute($"{MAMELuaCommand.GetSpace}:write_u8({addr << _systemBusAddressShift}, {val})");
7050
}
7151
}
7252

src/BizHawk.Emulation.Cores/Waterbox/WaterboxMemoryDomain.cs

Lines changed: 4 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ public abstract class WaterboxMemoryDomain : MemoryDomain
2020
public static WaterboxMemoryDomain Create(MemoryArea m, WaterboxHost monitor)
2121
{
2222
return m.Flags.HasFlag(MemoryDomainFlags.FunctionHook)
23-
? (WaterboxMemoryDomain)new WaterboxMemoryDomainFunc(m, monitor)
23+
? new WaterboxMemoryDomainFunc(m, monitor)
2424
: new WaterboxMemoryDomainPointer(m, monitor);
2525
}
2626

@@ -73,15 +73,10 @@ public override byte PeekByte(long addr)
7373
{
7474
if ((ulong)addr < (ulong)Size)
7575
{
76-
try
76+
using (_monitor.EnterExit())
7777
{
78-
_monitor.Enter();
7978
return ((byte*)_data)[addr ^ _addressMangler];
8079
}
81-
finally
82-
{
83-
_monitor.Exit();
84-
}
8580
}
8681

8782
throw new ArgumentOutOfRangeException(nameof(addr));
@@ -93,15 +88,10 @@ public override void PokeByte(long addr, byte val)
9388
{
9489
if ((ulong)addr < (ulong)Size)
9590
{
96-
try
91+
using (_monitor.EnterExit())
9792
{
98-
_monitor.Enter();
9993
((byte*)_data)[addr ^ _addressMangler] = val;
10094
}
101-
finally
102-
{
103-
_monitor.Exit();
104-
}
10595
}
10696
else
10797
{
@@ -123,15 +113,10 @@ public override void BulkPeekByte(Range<long> addresses, byte[] values)
123113

124114
if (start < (ulong)Size && (start + count) <= (ulong)Size)
125115
{
126-
try
116+
using (_monitor.EnterExit())
127117
{
128-
_monitor.Enter();
129118
Marshal.Copy(Z.US((ulong)_data + start), values, 0, (int)count);
130119
}
131-
finally
132-
{
133-
_monitor.Exit();
134-
}
135120
}
136121
else
137122
{

0 commit comments

Comments
 (0)