Optimize ZwinderStateManger.GetStateClosestToFrame

AllStates() sorted the returned states, but this order was not used anywhere else, so I've removed it. GetStateClosestToFrame is solvable in O(n), so sorting is not required there either.

This is an O(n log(n)) -> O(n) improvement that is mostly relevant for the state history integrity checker
This commit is contained in:
Morilli 2024-10-20 16:55:02 +02:00
parent 7ab2ca6b89
commit b7b8788354
1 changed files with 11 additions and 5 deletions

View File

@ -251,14 +251,13 @@ namespace BizHawk.Client.Common
}
/// <summary>
/// Enumerate all states in reverse order
/// Enumerate all states in the following order: current -> recent -> gap -> reserved states
/// </summary>
internal IEnumerable<StateInfo> AllStates()
{
return CurrentAndRecentStates()
.Concat(GapStates())
.Concat(ReservedStates())
.OrderByDescending(s => s.Frame);
.Concat(ReservedStates());
}
public int Last => StateCache.Max();
@ -474,8 +473,15 @@ namespace BizHawk.Client.Common
if (frame < 0)
throw new ArgumentOutOfRangeException(nameof(frame));
var si = AllStates().First(s => s.Frame <= frame);
return new KeyValuePair<int, Stream>(si.Frame, si.Read());
StateInfo closestState = null;
foreach (var state in AllStates())
{
if (state.Frame <= frame && (closestState is null || state.Frame > closestState.Frame))
{
closestState = state;
}
}
return new KeyValuePair<int, Stream>(closestState!.Frame, closestState.Read());
}
public bool HasState(int frame)