From 674997b345c4f96d17d60a4adb7569e143694be5 Mon Sep 17 00:00:00 2001 From: Morilli <35152647+Morilli@users.noreply.github.com> Date: Thu, 7 Mar 2024 22:52:53 +0100 Subject: [PATCH] Optimize speed and allocations of TasMovieMarker.Previous[OrCurrent] was quite a ridiculous amount of allocations due the amount of calls to this functions --- .../movie/tasproj/TasMovieMarker.cs | 13 +++++-------- 1 file changed, 5 insertions(+), 8 deletions(-) diff --git a/src/BizHawk.Client.Common/movie/tasproj/TasMovieMarker.cs b/src/BizHawk.Client.Common/movie/tasproj/TasMovieMarker.cs index edc8682b3d..5243ed6f53 100644 --- a/src/BizHawk.Client.Common/movie/tasproj/TasMovieMarker.cs +++ b/src/BizHawk.Client.Common/movie/tasproj/TasMovieMarker.cs @@ -3,6 +3,7 @@ using System.Collections.Generic; using System.Collections.Specialized; using System.Linq; using System.Text; +using BizHawk.Common.CollectionExtensions; namespace BizHawk.Client.Common { @@ -296,18 +297,14 @@ namespace BizHawk.Client.Common public TasMovieMarker Previous(int currentFrame) { - return this - .Where(m => m.Frame < currentFrame) - .OrderBy(m => m.Frame) - .LastOrDefault(); + return PreviousOrCurrent(currentFrame - 1); } public TasMovieMarker PreviousOrCurrent(int currentFrame) { - return this - .Where(m => m.Frame <= currentFrame) - .OrderBy(m => m.Frame) - .LastOrDefault(); + int lowerBoundIndex = this.LowerBoundBinarySearch(static m => m.Frame, currentFrame); + + return lowerBoundIndex < 0 ? null : this[lowerBoundIndex]; } public TasMovieMarker Next(int currentFrame)