From 24182eb88ec481855d45236b6f73ee65968e8dc0 Mon Sep 17 00:00:00 2001 From: Dean Herbert Date: Fri, 18 Oct 2024 13:53:48 +0900 Subject: [PATCH] Cache finalised attributes to avoid conversion overhead every usage --- .../Stores/BeatmapStore.cs | 41 +++++++++++-------- 1 file changed, 23 insertions(+), 18 deletions(-) diff --git a/osu.Server.Queues.ScoreStatisticsProcessor/Stores/BeatmapStore.cs b/osu.Server.Queues.ScoreStatisticsProcessor/Stores/BeatmapStore.cs index 59d503f8..4001c16a 100644 --- a/osu.Server.Queues.ScoreStatisticsProcessor/Stores/BeatmapStore.cs +++ b/osu.Server.Queues.ScoreStatisticsProcessor/Stores/BeatmapStore.cs @@ -30,7 +30,7 @@ public class BeatmapStore private static readonly string beatmap_download_path = Environment.GetEnvironmentVariable("BEATMAP_DOWNLOAD_PATH") ?? "https://osu.ppy.sh/osu/{0}"; private readonly ConcurrentDictionary beatmapCache = new ConcurrentDictionary(); - private readonly ConcurrentDictionary attributeCache = new ConcurrentDictionary(); + private readonly ConcurrentDictionary attributeCache = new ConcurrentDictionary(); private readonly IReadOnlyDictionary blacklist; private BeatmapStore(IEnumerable> blacklist) @@ -82,29 +82,34 @@ public static async Task CreateAsync(MySqlConnection connection, M return calculator.Calculate(mods); } - BeatmapDifficultyAttribute[]? rawDifficultyAttributes; - LegacyMods legacyModValue = getLegacyModsForAttributeLookup(beatmap, ruleset, mods); + DifficultyAttributeKey key = new DifficultyAttributeKey(beatmap.beatmap_id, (uint)ruleset.RulesetInfo.OnlineID, (uint)legacyModValue); - if (!attributeCache.TryGetValue(key, out rawDifficultyAttributes)) - { - rawDifficultyAttributes = attributeCache[key] = (await connection.QueryAsync( - "SELECT * FROM osu_beatmap_difficulty_attribs WHERE `beatmap_id` = @BeatmapId AND `mode` = @RulesetId AND `mods` = @ModValue", new - { - key.BeatmapId, - key.RulesetId, - key.ModValue - }, transaction: transaction)).ToArray(); - } + if (attributeCache.TryGetValue(key, out DifficultyAttributes? difficultyAttributes)) + return difficultyAttributes; - if (rawDifficultyAttributes == null || rawDifficultyAttributes.Length == 0) - return null; + BeatmapDifficultyAttribute[] dbAttribs = (await connection.QueryAsync( + "SELECT * FROM osu_beatmap_difficulty_attribs WHERE `beatmap_id` = @BeatmapId AND `mode` = @RulesetId AND `mods` = @ModValue", new + { + key.BeatmapId, + key.RulesetId, + key.ModValue + }, transaction: transaction)).ToArray(); - DifficultyAttributes difficultyAttributes = LegacyRulesetHelper.CreateDifficultyAttributes(ruleset.RulesetInfo.OnlineID); - difficultyAttributes.FromDatabaseAttributes(rawDifficultyAttributes.ToDictionary(a => (int)a.attrib_id, a => (double)a.value), beatmap); + try + { + if (dbAttribs.Length == 0) + return difficultyAttributes; - return difficultyAttributes; + difficultyAttributes = LegacyRulesetHelper.CreateDifficultyAttributes(ruleset.RulesetInfo.OnlineID); + difficultyAttributes.FromDatabaseAttributes(dbAttribs.ToDictionary(a => (int)a.attrib_id, a => (double)a.value), beatmap); + return difficultyAttributes; + } + finally + { + attributeCache[key] = difficultyAttributes; + } } ///