mirror of
https://github.com/ppy/osu.git
synced 2025-01-27 19:23:21 +08:00
Move lookup/storage/compute logic to base class (and consume in ScorePerformanceCache)
This commit is contained in:
parent
517a656899
commit
a2606d31c7
@ -2,6 +2,9 @@
|
|||||||
// See the LICENCE file in the repository root for full licence text.
|
// See the LICENCE file in the repository root for full licence text.
|
||||||
|
|
||||||
using System.Collections.Concurrent;
|
using System.Collections.Concurrent;
|
||||||
|
using System.Threading;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
using JetBrains.Annotations;
|
||||||
using osu.Framework.Graphics;
|
using osu.Framework.Graphics;
|
||||||
|
|
||||||
namespace osu.Game.Database
|
namespace osu.Game.Database
|
||||||
@ -12,6 +15,34 @@ namespace osu.Game.Database
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
public abstract class MemoryCachingComponent<TLookup, TValue> : Component
|
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);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -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.
|
/// A component which performs and acts as a central cache for performance calculations of locally databased scores.
|
||||||
/// Currently not persisted between game sessions.
|
/// Currently not persisted between game sessions.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public class ScorePerformanceCache : MemoryCachingComponent<ScorePerformanceCache.PerformanceCacheLookup, double>
|
public class ScorePerformanceCache : MemoryCachingComponent<ScorePerformanceCache.PerformanceCacheLookup, double?>
|
||||||
{
|
{
|
||||||
[Resolved]
|
[Resolved]
|
||||||
private BeatmapDifficultyCache difficultyCache { get; set; }
|
private BeatmapDifficultyCache difficultyCache { get; set; }
|
||||||
|
|
||||||
|
protected override bool CacheNullValues => false;
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Calculates performance for the given <see cref="ScoreInfo"/>.
|
/// Calculates performance for the given <see cref="ScoreInfo"/>.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="score">The score to do the calculation on. </param>
|
/// <param name="score">The score to do the calculation on. </param>
|
||||||
/// <param name="token">An optional <see cref="CancellationToken"/> to cancel the operation.</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);
|
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.
|
// 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();
|
token.ThrowIfCancellationRequested();
|
||||||
|
|
||||||
var calculator = score.Ruleset.CreateInstance().CreatePerformanceCalculator(attributes.Attributes, score);
|
var calculator = score.Ruleset.CreateInstance().CreatePerformanceCalculator(attributes.Attributes, score);
|
||||||
var total = calculator?.Calculate();
|
|
||||||
|
|
||||||
if (total.HasValue)
|
return calculator?.Calculate();
|
||||||
Cache[lookupKey] = total.Value;
|
|
||||||
|
|
||||||
return total;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public readonly struct PerformanceCacheLookup
|
public readonly struct PerformanceCacheLookup
|
||||||
{
|
{
|
||||||
public readonly string ScoreHash;
|
public readonly ScoreInfo ScoreInfo;
|
||||||
public readonly int LocalScoreID;
|
|
||||||
|
|
||||||
public PerformanceCacheLookup(ScoreInfo info)
|
public PerformanceCacheLookup(ScoreInfo info)
|
||||||
{
|
{
|
||||||
ScoreHash = info.Hash;
|
ScoreInfo = info;
|
||||||
LocalScoreID = info.ID;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public override int GetHashCode()
|
public override int GetHashCode()
|
||||||
{
|
{
|
||||||
var hash = new HashCode();
|
var hash = new HashCode();
|
||||||
|
|
||||||
hash.Add(ScoreHash);
|
hash.Add(ScoreInfo.Hash);
|
||||||
hash.Add(LocalScoreID);
|
hash.Add(ScoreInfo.ID);
|
||||||
|
|
||||||
return hash.ToHashCode();
|
return hash.ToHashCode();
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user