Skip to content

Commit

Permalink
Avoid list construction when doing filtering
Browse files Browse the repository at this point in the history
  • Loading branch information
peppy committed Dec 5, 2023
1 parent 27e778a commit 36eb338
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 33 deletions.
22 changes: 10 additions & 12 deletions osu.Game/Beatmaps/BeatmapInfoExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
// Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the MIT Licence.
// See the LICENCE file in the repository root for full licence text.

using System.Collections.Generic;
using osu.Framework.Localisation;
using osu.Game.Screens.Select;

namespace osu.Game.Beatmaps
{
Expand All @@ -29,20 +29,18 @@ public static RomanisableString GetDisplayTitleRomanisable(this IBeatmapInfo bea
return new RomanisableString($"{metadata.GetPreferred(true)}".Trim(), $"{metadata.GetPreferred(false)}".Trim());
}

public static List<string> GetSearchableTerms(this IBeatmapInfo beatmapInfo)
public static bool Match(this IBeatmapInfo beatmapInfo, params FilterCriteria.OptionalTextFilter[] filters)
{
var termsList = new List<string>(BeatmapMetadataInfoExtensions.MAX_SEARCHABLE_TERM_COUNT + 1);

addIfNotNull(beatmapInfo.DifficultyName);

BeatmapMetadataInfoExtensions.CollectSearchableTerms(beatmapInfo.Metadata, termsList);
return termsList;

void addIfNotNull(string? s)
foreach (var filter in filters)
{
if (!string.IsNullOrEmpty(s))
termsList.Add(s);
if (filter.Matches(beatmapInfo.DifficultyName))
return true;

if (BeatmapMetadataInfoExtensions.Match(beatmapInfo.Metadata, filters))
return true;
}

return false;
}

private static string getVersionString(IBeatmapInfo beatmapInfo) => string.IsNullOrEmpty(beatmapInfo.DifficultyName) ? string.Empty : $"[{beatmapInfo.DifficultyName}]";
Expand Down
19 changes: 18 additions & 1 deletion osu.Game/Beatmaps/BeatmapMetadataInfoExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,14 @@

using System.Collections.Generic;
using osu.Framework.Localisation;
using osu.Game.Screens.Select;

namespace osu.Game.Beatmaps
{
public static class BeatmapMetadataInfoExtensions
{
internal const int MAX_SEARCHABLE_TERM_COUNT = 7;

/// <summary>
/// An array of all searchable terms provided in contained metadata.
/// </summary>
Expand All @@ -18,7 +21,21 @@ public static string[] GetSearchableTerms(this IBeatmapMetadataInfo metadataInfo
return termsList.ToArray();
}

internal const int MAX_SEARCHABLE_TERM_COUNT = 7;
public static bool Match(IBeatmapMetadataInfo metadataInfo, FilterCriteria.OptionalTextFilter[] filters)
{
foreach (var filter in filters)
{
if (filter.Matches(metadataInfo.Author.Username)) return true;
if (filter.Matches(metadataInfo.Artist)) return true;
if (filter.Matches(metadataInfo.ArtistUnicode)) return true;
if (filter.Matches(metadataInfo.Title)) return true;
if (filter.Matches(metadataInfo.TitleUnicode)) return true;
if (filter.Matches(metadataInfo.Source)) return true;
if (filter.Matches(metadataInfo.Tags)) return true;
}

return false;
}

internal static void CollectSearchableTerms(IBeatmapMetadataInfo metadataInfo, IList<string> termsList)
{
Expand Down
22 changes: 2 additions & 20 deletions osu.Game/Screens/Select/Carousel/CarouselBeatmap.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the MIT Licence.
// See the LICENCE file in the repository root for full licence text.

using System.Collections.Generic;
using System.Linq;
using osu.Game.Beatmaps;
using osu.Game.Screens.Select.Filter;
Expand Down Expand Up @@ -66,26 +67,7 @@ private bool checkMatch(FilterCriteria criteria)

if (criteria.SearchTerms.Length > 0)
{
var searchableTerms = BeatmapInfo.GetSearchableTerms();

foreach (FilterCriteria.OptionalTextFilter criteriaTerm in criteria.SearchTerms)
{
bool any = false;

// ReSharper disable once ForeachCanBeConvertedToQueryUsingAnotherGetEnumerator
foreach (string searchTerm in searchableTerms)
{
if (!criteriaTerm.Matches(searchTerm)) continue;

any = true;
break;
}

if (any) continue;

match = false;
break;
}
match = BeatmapInfo.Match(criteria.SearchTerms);

// if a match wasn't found via text matching of terms, do a second catch-all check matching against online IDs.
// this should be done after text matching so we can prioritise matching numbers in metadata.
Expand Down

0 comments on commit 36eb338

Please sign in to comment.