mirror of
https://github.com/ppy/osu.git
synced 2025-02-19 10:33:21 +08:00
Optimised GetSearchableTerms
Reduced memory allocations to 1
This commit is contained in:
parent
29be26b150
commit
91d206e8d2
@ -1,7 +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.Linq;
|
||||
using System;
|
||||
using osu.Framework.Localisation;
|
||||
|
||||
namespace osu.Game.Beatmaps
|
||||
@ -29,10 +29,29 @@ namespace osu.Game.Beatmaps
|
||||
return new RomanisableString($"{metadata.GetPreferred(true)}".Trim(), $"{metadata.GetPreferred(false)}".Trim());
|
||||
}
|
||||
|
||||
public static string[] GetSearchableTerms(this IBeatmapInfo beatmapInfo) => new[]
|
||||
public static ReadOnlySpan<string> GetSearchableTerms(this IBeatmapInfo beatmapInfo)
|
||||
{
|
||||
beatmapInfo.DifficultyName
|
||||
}.Concat(beatmapInfo.Metadata.GetSearchableTerms()).Where(s => !string.IsNullOrEmpty(s)).ToArray();
|
||||
Span<string> terms = new string[8];
|
||||
int i = 0;
|
||||
if (!string.IsNullOrEmpty(beatmapInfo.DifficultyName))
|
||||
terms[i++] = beatmapInfo.DifficultyName;
|
||||
var metadata = beatmapInfo.Metadata;
|
||||
if (!string.IsNullOrEmpty(metadata.Author.Username))
|
||||
terms[i++] = metadata.Author.Username;
|
||||
if (!string.IsNullOrEmpty(metadata.Artist))
|
||||
terms[i++] = metadata.Artist;
|
||||
if (!string.IsNullOrEmpty(metadata.ArtistUnicode))
|
||||
terms[i++] = metadata.ArtistUnicode;
|
||||
if (!string.IsNullOrEmpty(metadata.Title))
|
||||
terms[i++] = metadata.Title;
|
||||
if (!string.IsNullOrEmpty(metadata.TitleUnicode))
|
||||
terms[i++] = metadata.TitleUnicode;
|
||||
if (!string.IsNullOrEmpty(metadata.Source))
|
||||
terms[i++] = metadata.Source;
|
||||
if (!string.IsNullOrEmpty(metadata.Tags))
|
||||
terms[i++] = metadata.Tags;
|
||||
return terms[..i];
|
||||
}
|
||||
|
||||
private static string getVersionString(IBeatmapInfo beatmapInfo) => string.IsNullOrEmpty(beatmapInfo.DifficultyName) ? string.Empty : $"[{beatmapInfo.DifficultyName}]";
|
||||
}
|
||||
|
@ -55,12 +55,31 @@ namespace osu.Game.Screens.Select.Carousel
|
||||
|
||||
match &= !criteria.UserStarDifficulty.HasFilter || criteria.UserStarDifficulty.IsInRange(BeatmapInfo.StarRating);
|
||||
|
||||
if (match && criteria.SearchTerms.Length > 0)
|
||||
if (!match)
|
||||
{
|
||||
string[] terms = BeatmapInfo.GetSearchableTerms();
|
||||
Filtered.Value = !match;
|
||||
return;
|
||||
}
|
||||
|
||||
if (criteria.SearchTerms.Length > 0)
|
||||
{
|
||||
var terms = BeatmapInfo.GetSearchableTerms();
|
||||
|
||||
foreach (string criteriaTerm in criteria.SearchTerms)
|
||||
match &= terms.Any(term => term.Contains(criteriaTerm, StringComparison.InvariantCultureIgnoreCase));
|
||||
{
|
||||
bool any = false;
|
||||
|
||||
// ReSharper disable once LoopCanBeConvertedToQuery
|
||||
foreach (string term in terms)
|
||||
{
|
||||
if (!term.Contains(criteriaTerm, StringComparison.InvariantCultureIgnoreCase)) continue;
|
||||
|
||||
any = true;
|
||||
break;
|
||||
}
|
||||
|
||||
match &= any;
|
||||
}
|
||||
|
||||
// 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.
|
||||
@ -71,8 +90,13 @@ namespace osu.Game.Screens.Select.Carousel
|
||||
}
|
||||
}
|
||||
|
||||
if (match)
|
||||
match &= criteria.CollectionBeatmapMD5Hashes?.Contains(BeatmapInfo.MD5Hash) ?? true;
|
||||
if (!match)
|
||||
{
|
||||
Filtered.Value = !match;
|
||||
return;
|
||||
}
|
||||
|
||||
match &= criteria.CollectionBeatmapMD5Hashes?.Contains(BeatmapInfo.MD5Hash) ?? true;
|
||||
|
||||
if (match && criteria.RulesetCriteria != null)
|
||||
match &= criteria.RulesetCriteria.Matches(BeatmapInfo);
|
||||
|
Loading…
Reference in New Issue
Block a user