From 6e1b4152c07a250426e7e89e632db21043dd31cd Mon Sep 17 00:00:00 2001 From: Andrei Zavatski Date: Thu, 15 Feb 2024 00:00:39 +0300 Subject: [PATCH 1/3] Redice allocations during aggregate beatmap sort --- .../Select/Carousel/CarouselBeatmapSet.cs | 34 +++++++++++++++++-- 1 file changed, 31 insertions(+), 3 deletions(-) diff --git a/osu.Game/Screens/Select/Carousel/CarouselBeatmapSet.cs b/osu.Game/Screens/Select/Carousel/CarouselBeatmapSet.cs index 6d2e938fb7..d2b71b1d5e 100644 --- a/osu.Game/Screens/Select/Carousel/CarouselBeatmapSet.cs +++ b/osu.Game/Screens/Select/Carousel/CarouselBeatmapSet.cs @@ -127,12 +127,40 @@ namespace osu.Game.Screens.Select.Carousel /// /// All beatmaps which are not filtered and valid for display. /// - protected IEnumerable ValidBeatmaps => Beatmaps.Where(b => !b.Filtered.Value || b.State.Value == CarouselItemState.Selected).Select(b => b.BeatmapInfo); + protected IEnumerable ValidBeatmaps + { + get + { + foreach (var item in Items) // iterating over Items directly to not allocate 2 enumerators + { + if (item is CarouselBeatmap b && (!b.Filtered.Value || b.State.Value == CarouselItemState.Selected)) + yield return b.BeatmapInfo; + } + } + } + + /// + /// Whether there are available beatmaps which are not filtered and valid for display. + /// Cheaper alternative to .Any() + /// + public bool HasValidBeatmaps + { + get + { + foreach (var item in Items) // iterating over Items directly to not allocate 2 enumerators + { + if (item is CarouselBeatmap b && (!b.Filtered.Value || b.State.Value == CarouselItemState.Selected)) + return true; + } + + return false; + } + } private int compareUsingAggregateMax(CarouselBeatmapSet other, Func func) { - bool ourBeatmaps = ValidBeatmaps.Any(); - bool otherBeatmaps = other.ValidBeatmaps.Any(); + bool ourBeatmaps = HasValidBeatmaps; + bool otherBeatmaps = other.HasValidBeatmaps; if (!ourBeatmaps && !otherBeatmaps) return 0; if (!ourBeatmaps) return -1; From 80abf6aab346b2f5eed5745c81a521547bf81c26 Mon Sep 17 00:00:00 2001 From: Dean Herbert Date: Thu, 15 Feb 2024 09:45:24 +0800 Subject: [PATCH 2/3] Avoid some further enumerator allocations --- osu.Game/Screens/Select/Carousel/CarouselGroup.cs | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/osu.Game/Screens/Select/Carousel/CarouselGroup.cs b/osu.Game/Screens/Select/Carousel/CarouselGroup.cs index b2ca117cec..62d694976f 100644 --- a/osu.Game/Screens/Select/Carousel/CarouselGroup.cs +++ b/osu.Game/Screens/Select/Carousel/CarouselGroup.cs @@ -2,6 +2,8 @@ // See the LICENCE file in the repository root for full licence text. using System.Collections.Generic; +using osu.Framework.Extensions.ListExtensions; +using osu.Framework.Lists; namespace osu.Game.Screens.Select.Carousel { @@ -12,7 +14,7 @@ namespace osu.Game.Screens.Select.Carousel { public override DrawableCarouselItem? CreateDrawableRepresentation() => null; - public IReadOnlyList Items => items; + public SlimReadOnlyListWrapper Items => items.AsSlimReadOnly(); public int TotalItemsNotFiltered { get; private set; } From 674ee91bb5dd28d9f4f79a3f0c6d220274a8c3dd Mon Sep 17 00:00:00 2001 From: Dean Herbert Date: Thu, 15 Feb 2024 11:40:50 +0800 Subject: [PATCH 3/3] Define aggregate max delegates as static to further reduce allocations --- osu.Game/Screens/Select/Carousel/CarouselBeatmapSet.cs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/osu.Game/Screens/Select/Carousel/CarouselBeatmapSet.cs b/osu.Game/Screens/Select/Carousel/CarouselBeatmapSet.cs index d2b71b1d5e..cee68cf9a5 100644 --- a/osu.Game/Screens/Select/Carousel/CarouselBeatmapSet.cs +++ b/osu.Game/Screens/Select/Carousel/CarouselBeatmapSet.cs @@ -91,19 +91,19 @@ namespace osu.Game.Screens.Select.Carousel break; case SortMode.LastPlayed: - comparison = -compareUsingAggregateMax(otherSet, b => (b.LastPlayed ?? DateTimeOffset.MinValue).ToUnixTimeSeconds()); + comparison = -compareUsingAggregateMax(otherSet, static b => (b.LastPlayed ?? DateTimeOffset.MinValue).ToUnixTimeSeconds()); break; case SortMode.BPM: - comparison = compareUsingAggregateMax(otherSet, b => b.BPM); + comparison = compareUsingAggregateMax(otherSet, static b => b.BPM); break; case SortMode.Length: - comparison = compareUsingAggregateMax(otherSet, b => b.Length); + comparison = compareUsingAggregateMax(otherSet, static b => b.Length); break; case SortMode.Difficulty: - comparison = compareUsingAggregateMax(otherSet, b => b.StarRating); + comparison = compareUsingAggregateMax(otherSet, static b => b.StarRating); break; case SortMode.DateSubmitted: