1
0
mirror of https://github.com/ppy/osu.git synced 2025-01-06 04:13:11 +08:00

Add incremental processing

This commit is contained in:
Dean Herbert 2024-11-25 19:43:42 +09:00
parent 33d725e889
commit 5668258182
No known key found for this signature in database
5 changed files with 74 additions and 12 deletions

View File

@ -13,27 +13,45 @@ namespace osu.Game.Benchmarks
{
public class BenchmarkUnstableRate : BenchmarkTest
{
private List<HitEvent> events = null!;
private readonly List<List<HitEvent>> incrementalEventLists = new List<List<HitEvent>>();
public override void SetUp()
{
base.SetUp();
events = new List<HitEvent>();
var events = new List<HitEvent>();
for (int i = 0; i < 2048; i++)
{
// Ensure the object has hit windows populated.
var hitObject = new HitCircle();
hitObject.ApplyDefaults(new ControlPointInfo(), new BeatmapDifficulty());
events.Add(new HitEvent(RNG.NextDouble(-200.0, 200.0), RNG.NextDouble(1.0, 2.0), HitResult.Great, hitObject, null, null));
incrementalEventLists.Add(new List<HitEvent>(events));
}
}
[Benchmark]
public void CalculateUnstableRate()
{
_ = events.CalculateUnstableRate();
for (int i = 0; i < 2048; i++)
{
var events = incrementalEventLists[i];
_ = events.CalculateUnstableRate();
}
}
[Benchmark]
public void CalculateUnstableRateUsingIncrementalCalculation()
{
HitEventExtensions.UnstableRateCalculationResult? last = null;
for (int i = 0; i < 2048; i++)
{
var events = incrementalEventLists[i];
last = events.CalculateUnstableRate(last);
}
}
}
}

View File

@ -26,7 +26,47 @@ namespace osu.Game.Tests.NonVisual.Ranking
var unstableRate = new UnstableRate(events);
Assert.IsNotNull(unstableRate.Value);
Assert.IsTrue(Precision.AlmostEquals(unstableRate.Value.Value, 10 * Math.Sqrt(10)));
Assert.AreEqual(unstableRate.Value.Value, 10 * Math.Sqrt(10), Precision.DOUBLE_EPSILON);
}
[Test]
public void TestDistributedHitsIncrementalRewind()
{
var events = Enumerable.Range(-5, 11)
.Select(t => new HitEvent(t - 5, 1.0, HitResult.Great, new HitObject(), null, null))
.ToList();
HitEventExtensions.UnstableRateCalculationResult result = null;
for (int i = 0; i < events.Count; i++)
{
result = events.GetRange(0, i + 1)
.CalculateUnstableRate(result);
}
result = events.GetRange(0, 2).CalculateUnstableRate(result);
Assert.IsNotNull(result!.Result);
Assert.AreEqual(5, result.Result, Precision.DOUBLE_EPSILON);
}
[Test]
public void TestDistributedHitsIncremental()
{
var events = Enumerable.Range(-5, 11)
.Select(t => new HitEvent(t - 5, 1.0, HitResult.Great, new HitObject(), null, null))
.ToList();
HitEventExtensions.UnstableRateCalculationResult result = null;
for (int i = 0; i < events.Count; i++)
{
result = events.GetRange(0, i + 1)
.CalculateUnstableRate(result);
}
Assert.IsNotNull(result!.Result);
Assert.AreEqual(10 * Math.Sqrt(10), result.Result, Precision.DOUBLE_EPSILON);
}
[Test]

View File

@ -20,16 +20,18 @@ namespace osu.Game.Rulesets.Scoring
/// A non-null <see langword="double"/> value if unstable rate could be calculated,
/// and <see langword="null"/> if unstable rate cannot be calculated due to <paramref name="hitEvents"/> being empty.
/// </returns>
public static double? CalculateUnstableRate(this IReadOnlyList<HitEvent> hitEvents)
public static UnstableRateCalculationResult? CalculateUnstableRate(this IReadOnlyList<HitEvent> hitEvents, UnstableRateCalculationResult? previousResult = null)
{
Debug.Assert(hitEvents.All(ev => ev.GameplayRate != null));
int count = 0;
double mean = 0;
double sumOfSquares = 0;
double mean = previousResult?.Mean ?? 0;
double sumOfSquares = previousResult?.SumOfSquares ?? 0;
foreach (var e in hitEvents)
for (int i = previousResult?.CalculatedHitEventsCount - 1 ?? 0; i < hitEvents.Count; i++)
{
HitEvent e = hitEvents[i];
if (!AffectsUnstableRate(e))
continue;
@ -45,7 +47,7 @@ namespace osu.Game.Rulesets.Scoring
if (count == 0)
return null;
return 10.0 * Math.Sqrt(sumOfSquares / count);
return new UnstableRateCalculationResult(hitEvents.Count, sumOfSquares, mean, 10.0 * Math.Sqrt(sumOfSquares / count));
}
/// <summary>
@ -66,5 +68,7 @@ namespace osu.Game.Rulesets.Scoring
}
public static bool AffectsUnstableRate(HitEvent e) => e.HitObject.HitWindows != HitWindows.Empty && e.Result.IsHit();
public record UnstableRateCalculationResult(int CalculatedHitEventsCount, double SumOfSquares, double Mean, double Result);
}
}

View File

@ -57,7 +57,7 @@ namespace osu.Game.Screens.Play.HUD
private void updateDisplay()
{
double? unstableRate = scoreProcessor.HitEvents.CalculateUnstableRate();
double? unstableRate = scoreProcessor.HitEvents.CalculateUnstableRate()?.Result;
valid.Value = unstableRate != null;
if (unstableRate != null)

View File

@ -18,7 +18,7 @@ namespace osu.Game.Screens.Ranking.Statistics
public UnstableRate(IReadOnlyList<HitEvent> hitEvents)
: base("Unstable Rate")
{
Value = hitEvents.CalculateUnstableRate();
Value = hitEvents.CalculateUnstableRate()?.Result;
}
protected override string DisplayValue(double? value) => value == null ? "(not available)" : value.Value.ToString(@"N2");