fix slow big O in TasStateManager, causing performance catastrophe when state scavenging begins. But I'm not 100% sure I did it right.

This commit is contained in:
zeromus 2015-08-05 19:42:01 -05:00
parent 65157f77e3
commit 4eb89c823d
1 changed files with 21 additions and 5 deletions

View File

@ -215,14 +215,30 @@ namespace BizHawk.Client.Common
{
shouldRemove++;
// No need to have two savestates with only lag frames between them.
for (int i = shouldRemove; i < States.Count - 1; i++)
// No need to have two savestates with only lag frames between them:
// zero 05-aug-2015 - changed algorithm to iterate through States (a SortedList) instead of repeatedly call ElementAt (which is slow)
// previously : for (int i = shouldRemove; i < States.Count - 1; i++) if (AllLag(States.ElementAt(i).Key, States.ElementAt(i + 1).Key)) { shouldRemove = i; break; } }
int ctr = 0;
KeyValuePair<int, byte[]>? prior = null;
foreach (var kvp in States)
{
if (AllLag(States.ElementAt(i).Key, States.ElementAt(i + 1).Key))
ctr++;
if (ctr < shouldRemove)
{
shouldRemove = i;
break;
prior = kvp;
continue;
}
if (prior.HasValue)
{
if (AllLag(prior.Value.Key, kvp.Key))
{
shouldRemove = ctr-1;
break;
}
}
prior = kvp;
}
// Keep marker states