1
0
mirror of https://github.com/ppy/osu.git synced 2025-01-27 02:32:59 +08:00

Move lookup/storage/compute logic to base class (and consume in ScorePerformanceCache)

This commit is contained in:
Dean Herbert 2020-11-06 13:50:51 +09:00
parent 517a656899
commit a2606d31c7
2 changed files with 45 additions and 23 deletions

View File

@ -2,6 +2,9 @@
// See the LICENCE file in the repository root for full licence text.
using System.Collections.Concurrent;
using System.Threading;
using System.Threading.Tasks;
using JetBrains.Annotations;
using osu.Framework.Graphics;
namespace osu.Game.Database
@ -12,6 +15,34 @@ namespace osu.Game.Database
/// </summary>
public abstract class MemoryCachingComponent<TLookup, TValue> : Component
{
protected readonly ConcurrentDictionary<TLookup, TValue> Cache = new ConcurrentDictionary<TLookup, TValue>();
private readonly ConcurrentDictionary<TLookup, TValue> cache = new ConcurrentDictionary<TLookup, TValue>();
protected virtual bool CacheNullValues => true;
/// <summary>
/// Retrieve the cached value for the given <see cref="TLookup"/>.
/// </summary>
/// <param name="lookup">The lookup to retrieve.</param>
/// <param name="token">An optional <see cref="CancellationToken"/> to cancel the operation.</param>
protected async Task<TValue> GetAsync([NotNull] TLookup lookup, CancellationToken token = default)
{
if (cache.TryGetValue(lookup, out TValue performance))
return performance;
var computed = await ComputeValueAsync(lookup, token);
if (computed != null || CacheNullValues)
cache[lookup] = computed;
return computed;
}
/// <summary>
/// Called on cache miss to compute the value for the specified lookup.
/// </summary>
/// <param name="lookup">The lookup to retrieve.</param>
/// <param name="token">An optional <see cref="CancellationToken"/> to cancel the operation.</param>
/// <returns>The computed value.</returns>
protected abstract Task<TValue> ComputeValueAsync(TLookup lookup, CancellationToken token = default);
}
}

View File

@ -15,28 +15,25 @@ namespace osu.Game.Scoring
/// A component which performs and acts as a central cache for performance calculations of locally databased scores.
/// Currently not persisted between game sessions.
/// </summary>
public class ScorePerformanceCache : MemoryCachingComponent<ScorePerformanceCache.PerformanceCacheLookup, double>
public class ScorePerformanceCache : MemoryCachingComponent<ScorePerformanceCache.PerformanceCacheLookup, double?>
{
[Resolved]
private BeatmapDifficultyCache difficultyCache { get; set; }
protected override bool CacheNullValues => false;
/// <summary>
/// Calculates performance for the given <see cref="ScoreInfo"/>.
/// </summary>
/// <param name="score">The score to do the calculation on. </param>
/// <param name="token">An optional <see cref="CancellationToken"/> to cancel the operation.</param>
public Task<double?> CalculatePerformanceAsync([NotNull] ScoreInfo score, CancellationToken token = default)
public Task<double?> CalculatePerformanceAsync([NotNull] ScoreInfo score, CancellationToken token = default) =>
GetAsync(new PerformanceCacheLookup(score), token);
protected override async Task<double?> ComputeValueAsync(PerformanceCacheLookup lookup, CancellationToken token = default)
{
var lookupKey = new PerformanceCacheLookup(score);
var score = lookup.ScoreInfo;
if (Cache.TryGetValue(lookupKey, out double performance))
return Task.FromResult((double?)performance);
return computePerformanceAsync(score, lookupKey, token);
}
private async Task<double?> computePerformanceAsync(ScoreInfo score, PerformanceCacheLookup lookupKey, CancellationToken token = default)
{
var attributes = await difficultyCache.GetDifficultyAsync(score.Beatmap, score.Ruleset, score.Mods, token);
// Performance calculation requires the beatmap and ruleset to be locally available. If not, return a default value.
@ -46,31 +43,25 @@ namespace osu.Game.Scoring
token.ThrowIfCancellationRequested();
var calculator = score.Ruleset.CreateInstance().CreatePerformanceCalculator(attributes.Attributes, score);
var total = calculator?.Calculate();
if (total.HasValue)
Cache[lookupKey] = total.Value;
return total;
return calculator?.Calculate();
}
public readonly struct PerformanceCacheLookup
{
public readonly string ScoreHash;
public readonly int LocalScoreID;
public readonly ScoreInfo ScoreInfo;
public PerformanceCacheLookup(ScoreInfo info)
{
ScoreHash = info.Hash;
LocalScoreID = info.ID;
ScoreInfo = info;
}
public override int GetHashCode()
{
var hash = new HashCode();
hash.Add(ScoreHash);
hash.Add(LocalScoreID);
hash.Add(ScoreInfo.Hash);
hash.Add(ScoreInfo.ID);
return hash.ToHashCode();
}