MemoryCallbackSystem - keep track of whether or not there are any callbacks of each type instead of checking .Count > 0, provides a small but noticeable speedup

This commit is contained in:
adelikat 2015-01-24 14:39:12 +00:00
parent 2d2a04f5f9
commit 3c5348eb86
1 changed files with 33 additions and 6 deletions

View File

@ -14,13 +14,26 @@ namespace BizHawk.Emulation.Common
bool empty = true; bool empty = true;
private bool _hasReads = false;
private bool _hasWrites = false;
private bool _hasExecutes = false;
public void Add(IMemoryCallback callback) public void Add(IMemoryCallback callback)
{ {
switch (callback.Type) switch (callback.Type)
{ {
case MemoryCallbackType.Execute: Execs.Add(callback); break; case MemoryCallbackType.Execute:
case MemoryCallbackType.Read: Reads.Add(callback); break; Execs.Add(callback);
case MemoryCallbackType.Write: Writes.Add(callback); break; _hasExecutes = true;
break;
case MemoryCallbackType.Read:
Reads.Add(callback);
_hasReads = true;
break;
case MemoryCallbackType.Write:
Writes.Add(callback);
_hasWrites = true;
break;
} }
if (empty) if (empty)
Changes(); Changes();
@ -53,17 +66,24 @@ namespace BizHawk.Emulation.Common
public bool HasReads public bool HasReads
{ {
get { return Reads.Count > 0; } get { return _hasReads; }
} }
public bool HasWrites public bool HasWrites
{ {
get { return Writes.Count > 0; } get { return _hasWrites; }
} }
public bool HasExecutes public bool HasExecutes
{ {
get { return Execs.Count > 0; } get { return _hasExecutes; }
}
private void UpdateHasVariables()
{
_hasReads = Reads.Count > 0;
_hasWrites = Reads.Count > 0;
_hasExecutes = Reads.Count > 0;
} }
private int RemoveInternal(Action action) private int RemoveInternal(Action action)
@ -72,6 +92,9 @@ namespace BizHawk.Emulation.Common
ret += Reads.RemoveAll(imc => imc.Callback == action); ret += Reads.RemoveAll(imc => imc.Callback == action);
ret += Writes.RemoveAll(imc => imc.Callback == action); ret += Writes.RemoveAll(imc => imc.Callback == action);
ret += Execs.RemoveAll(imc => imc.Callback == action); ret += Execs.RemoveAll(imc => imc.Callback == action);
UpdateHasVariables();
return ret; return ret;
} }
@ -100,6 +123,8 @@ namespace BizHawk.Emulation.Common
Changes(); Changes();
empty = newEmpty; empty = newEmpty;
} }
UpdateHasVariables();
} }
public void Clear() public void Clear()
@ -110,6 +135,8 @@ namespace BizHawk.Emulation.Common
if (!empty) if (!empty)
Changes(); Changes();
empty = true; empty = true;
UpdateHasVariables();
} }
public delegate void ActiveChangedEventHandler(); public delegate void ActiveChangedEventHandler();