Optimize RamSearchEngine.Preview

This commit is contained in:
Morilli 2024-08-29 02:22:35 +02:00
parent 53436e81f5
commit 3c218c6642
2 changed files with 4 additions and 8 deletions

View File

@ -23,7 +23,6 @@ namespace BizHawk.Client.Common.RamSearchEngine
private IMiniWatch[] _watchList = Array.Empty<IMiniWatch>(); private IMiniWatch[] _watchList = Array.Empty<IMiniWatch>();
private readonly SearchEngineSettings _settings; private readonly SearchEngineSettings _settings;
private readonly UndoHistory<IEnumerable<IMiniWatch>> _history = new UndoHistory<IEnumerable<IMiniWatch>>(true, new List<IMiniWatch>()); //TODO use IList instead of IEnumerable and stop calling `.ToArray()` (i.e. cloning) on reads and writes? private readonly UndoHistory<IEnumerable<IMiniWatch>> _history = new UndoHistory<IEnumerable<IMiniWatch>>(true, new List<IMiniWatch>()); //TODO use IList instead of IEnumerable and stop calling `.ToArray()` (i.e. cloning) on reads and writes?
private bool _isSorted = true; // Tracks whether or not the array is sorted by address, if it is, binary search can be used for finding watches
public RamSearchEngine(SearchEngineSettings settings, IMemoryDomains memoryDomains) public RamSearchEngine(SearchEngineSettings settings, IMemoryDomains memoryDomains)
{ {
@ -153,11 +152,10 @@ namespace BizHawk.Client.Common.RamSearchEngine
return before - _watchList.Length; return before - _watchList.Length;
} }
public bool Preview(long address) public bool Preview(int index)
{ {
var listOfOne = Enumerable.Repeat(_isSorted var addressWatch = _watchList[index];
? _watchList.BinarySearch(w => w.Address, address) IMiniWatch[] listOfOne = [ addressWatch ];
: _watchList.FirstOrDefault(w => w.Address == address), 1);
return _compareTo switch return _compareTo switch
{ {
@ -284,12 +282,10 @@ namespace BizHawk.Client.Common.RamSearchEngine
}; };
_watchList = (append ? _watchList.Concat(list) : list).ToArray(); _watchList = (append ? _watchList.Concat(list) : list).ToArray();
_isSorted = false; //TODO can this be smarter, such as by inserting instead of appending?
} }
public void Sort(string column, bool reverse) public void Sort(string column, bool reverse)
{ {
_isSorted = column == WatchList.Address && !reverse;
switch (column) switch (column)
{ {
case WatchList.Address: case WatchList.Address:

View File

@ -188,7 +188,7 @@ namespace BizHawk.Client.EmuHawk
var nextColor = Color.White; var nextColor = Color.White;
var search = _searches[index]; var search = _searches[index];
var isCheat = MainForm.CheatList.IsActive(_settings.Domain, search.Address); var isCheat = MainForm.CheatList.IsActive(_settings.Domain, search.Address);
var isWeeded = Settings.PreviewMode && !_forcePreviewClear && _searches.Preview(search.Address); var isWeeded = Settings.PreviewMode && !_forcePreviewClear && _searches.Preview(index);
if (!search.IsValid) if (!search.IsValid)
{ {