From 3c5348eb861183aadb361063afa1c559daaeca8e Mon Sep 17 00:00:00 2001 From: adelikat Date: Sat, 24 Jan 2015 14:39:12 +0000 Subject: [PATCH] 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 --- .../MemoryCallbackSystem.cs | 39 ++++++++++++++++--- 1 file changed, 33 insertions(+), 6 deletions(-) diff --git a/BizHawk.Emulation.Common/Base Implementations/MemoryCallbackSystem.cs b/BizHawk.Emulation.Common/Base Implementations/MemoryCallbackSystem.cs index 052a191b58..ea2fa89649 100644 --- a/BizHawk.Emulation.Common/Base Implementations/MemoryCallbackSystem.cs +++ b/BizHawk.Emulation.Common/Base Implementations/MemoryCallbackSystem.cs @@ -14,13 +14,26 @@ namespace BizHawk.Emulation.Common bool empty = true; + private bool _hasReads = false; + private bool _hasWrites = false; + private bool _hasExecutes = false; + public void Add(IMemoryCallback callback) { switch (callback.Type) { - case MemoryCallbackType.Execute: Execs.Add(callback); break; - case MemoryCallbackType.Read: Reads.Add(callback); break; - case MemoryCallbackType.Write: Writes.Add(callback); break; + case MemoryCallbackType.Execute: + Execs.Add(callback); + _hasExecutes = true; + break; + case MemoryCallbackType.Read: + Reads.Add(callback); + _hasReads = true; + break; + case MemoryCallbackType.Write: + Writes.Add(callback); + _hasWrites = true; + break; } if (empty) Changes(); @@ -53,17 +66,24 @@ namespace BizHawk.Emulation.Common public bool HasReads { - get { return Reads.Count > 0; } + get { return _hasReads; } } public bool HasWrites { - get { return Writes.Count > 0; } + get { return _hasWrites; } } 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) @@ -72,6 +92,9 @@ namespace BizHawk.Emulation.Common ret += Reads.RemoveAll(imc => imc.Callback == action); ret += Writes.RemoveAll(imc => imc.Callback == action); ret += Execs.RemoveAll(imc => imc.Callback == action); + + UpdateHasVariables(); + return ret; } @@ -100,6 +123,8 @@ namespace BizHawk.Emulation.Common Changes(); empty = newEmpty; } + + UpdateHasVariables(); } public void Clear() @@ -110,6 +135,8 @@ namespace BizHawk.Emulation.Common if (!empty) Changes(); empty = true; + + UpdateHasVariables(); } public delegate void ActiveChangedEventHandler();