From 05abb3adfa2b208388f18df66c5ecbc01aebc1d0 Mon Sep 17 00:00:00 2001 From: Morilli <35152647+Morilli@users.noreply.github.com> Date: Sun, 10 Mar 2024 19:21:05 +0100 Subject: [PATCH] Optimize IndexOfFrame function in TAStudio --- .../movie/tasproj/TasBranch.cs | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) diff --git a/src/BizHawk.Client.Common/movie/tasproj/TasBranch.cs b/src/BizHawk.Client.Common/movie/tasproj/TasBranch.cs index b4ace9f0c5..f6ca142a6d 100644 --- a/src/BizHawk.Client.Common/movie/tasproj/TasBranch.cs +++ b/src/BizHawk.Client.Common/movie/tasproj/TasBranch.cs @@ -290,14 +290,19 @@ namespace BizHawk.Client.Common { public static int IndexOfFrame(this IList list, int frame) { - var branch = list - .Where(b => b.Frame == frame) - .OrderByDescending(b => b.TimeStamp) - .FirstOrDefault(); + // intentionally not using linq here because this is called many times per frame + int index = -1; + var timeStamp = DateTime.MaxValue; + for (int i = 0; i < list.Count; i++) + { + if (list[i].Frame == frame && list[i].TimeStamp < timeStamp) + { + index = i; + timeStamp = list[i].TimeStamp; + } + } - return branch == null - ? -1 - : list.IndexOf(branch); + return index; } // TODO: stop relying on the index value of a branch