From 1fb19e712922871adca31db706afd27ebb0a247f Mon Sep 17 00:00:00 2001 From: Andrei Zavatski Date: Sat, 24 Feb 2024 20:18:30 +0300 Subject: [PATCH 1/3] Reduce allocations in DrawableSpinner --- .../Objects/Drawables/DrawableSpinner.cs | 32 ++++++++++++++++--- .../Objects/Drawables/DrawableHitObject.cs | 4 ++- 2 files changed, 30 insertions(+), 6 deletions(-) diff --git a/osu.Game.Rulesets.Osu/Objects/Drawables/DrawableSpinner.cs b/osu.Game.Rulesets.Osu/Objects/Drawables/DrawableSpinner.cs index 11120e49b5..8c21e6a6bc 100644 --- a/osu.Game.Rulesets.Osu/Objects/Drawables/DrawableSpinner.cs +++ b/osu.Game.Rulesets.Osu/Objects/Drawables/DrawableSpinner.cs @@ -279,10 +279,9 @@ protected override void Update() if (HandleUserInput) { bool isValidSpinningTime = Time.Current >= HitObject.StartTime && Time.Current <= HitObject.EndTime; - bool correctButtonPressed = (OsuActionInputManager?.PressedActions.Any(x => x == OsuAction.LeftButton || x == OsuAction.RightButton) ?? false); RotationTracker.Tracking = !Result.HasResult - && correctButtonPressed + && correctButtonPressed() && isValidSpinningTime; } @@ -292,11 +291,34 @@ protected override void Update() // Ticks can theoretically be judged at any point in the spinner's duration. // A tick must be alive to correctly play back samples, // but for performance reasons, we only want to keep the next tick alive. - var next = NestedHitObjects.FirstOrDefault(h => !h.Judged); + DrawableHitObject nextTick = null; + + foreach (var nested in NestedHitObjects) + { + if (!nested.Judged) + { + nextTick = nested; + break; + } + } // See default `LifetimeStart` as set in `DrawableSpinnerTick`. - if (next?.LifetimeStart == double.MaxValue) - next.LifetimeStart = HitObject.StartTime; + if (nextTick?.LifetimeStart == double.MaxValue) + nextTick.LifetimeStart = HitObject.StartTime; + } + + private bool correctButtonPressed() + { + if (OsuActionInputManager == null) + return false; + + foreach (var action in OsuActionInputManager.PressedActions) + { + if (action == OsuAction.LeftButton || action == OsuAction.RightButton) + return true; + } + + return false; } protected override void UpdateAfterChildren() diff --git a/osu.Game/Rulesets/Objects/Drawables/DrawableHitObject.cs b/osu.Game/Rulesets/Objects/Drawables/DrawableHitObject.cs index 16bd4b565c..de05219212 100644 --- a/osu.Game/Rulesets/Objects/Drawables/DrawableHitObject.cs +++ b/osu.Game/Rulesets/Objects/Drawables/DrawableHitObject.cs @@ -11,10 +11,12 @@ using JetBrains.Annotations; using osu.Framework.Allocation; using osu.Framework.Bindables; +using osu.Framework.Extensions.ListExtensions; using osu.Framework.Extensions.ObjectExtensions; using osu.Framework.Extensions.TypeExtensions; using osu.Framework.Graphics; using osu.Framework.Graphics.Primitives; +using osu.Framework.Lists; using osu.Framework.Threading; using osu.Framework.Utils; using osu.Game.Audio; @@ -65,7 +67,7 @@ public abstract partial class DrawableHitObject : PoolableDrawableWithLifetime GetSamples() => HitObject.Samples; private readonly List nestedHitObjects = new List(); - public IReadOnlyList NestedHitObjects => nestedHitObjects; + public SlimReadOnlyListWrapper NestedHitObjects => nestedHitObjects.AsSlimReadOnly(); /// /// Whether this object should handle any user input events. From 9e90f7fb0d731e06d36bce8e5bc269f362f307cd Mon Sep 17 00:00:00 2001 From: Andrei Zavatski Date: Sat, 24 Feb 2024 20:36:06 +0300 Subject: [PATCH 2/3] Store last enqueued RotationRecord in SpinnerSpmCalculator --- .../Skinning/Default/SpinnerSpmCalculator.cs | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/osu.Game.Rulesets.Osu/Skinning/Default/SpinnerSpmCalculator.cs b/osu.Game.Rulesets.Osu/Skinning/Default/SpinnerSpmCalculator.cs index 44962c8548..7986108fbd 100644 --- a/osu.Game.Rulesets.Osu/Skinning/Default/SpinnerSpmCalculator.cs +++ b/osu.Game.Rulesets.Osu/Skinning/Default/SpinnerSpmCalculator.cs @@ -2,7 +2,6 @@ // See the LICENCE file in the repository root for full licence text. using System.Collections.Generic; -using System.Linq; using osu.Framework.Allocation; using osu.Framework.Bindables; using osu.Framework.Extensions.ObjectExtensions; @@ -33,14 +32,16 @@ protected override void LoadComplete() drawableSpinner.HitObjectApplied += resetState; } + private RotationRecord lastRecord; + public void SetRotation(float currentRotation) { // If we've gone back in time, it's fine to work with a fresh set of records for now - if (records.Count > 0 && Time.Current < records.Last().Time) + if (records.Count > 0 && Time.Current < lastRecord.Time) records.Clear(); // Never calculate SPM by same time of record to avoid 0 / 0 = NaN or X / 0 = Infinity result. - if (records.Count > 0 && Precision.AlmostEquals(Time.Current, records.Last().Time)) + if (records.Count > 0 && Precision.AlmostEquals(Time.Current, lastRecord.Time)) return; if (records.Count > 0) @@ -52,7 +53,7 @@ record = records.Dequeue(); result.Value = (currentRotation - record.Rotation) / (Time.Current - record.Time) * 1000 * 60 / 360; } - records.Enqueue(new RotationRecord { Rotation = currentRotation, Time = Time.Current }); + records.Enqueue(lastRecord = new RotationRecord { Rotation = currentRotation, Time = Time.Current }); } private void resetState(DrawableHitObject hitObject) From e12f8c03eef09478b8c8f3823b60632d37713192 Mon Sep 17 00:00:00 2001 From: Dean Herbert Date: Sun, 25 Feb 2024 08:18:19 +0800 Subject: [PATCH 3/3] Reset `lastRecord` on `resetState` for good measure --- osu.Game.Rulesets.Osu/Skinning/Default/SpinnerSpmCalculator.cs | 1 + 1 file changed, 1 insertion(+) diff --git a/osu.Game.Rulesets.Osu/Skinning/Default/SpinnerSpmCalculator.cs b/osu.Game.Rulesets.Osu/Skinning/Default/SpinnerSpmCalculator.cs index 7986108fbd..3383989367 100644 --- a/osu.Game.Rulesets.Osu/Skinning/Default/SpinnerSpmCalculator.cs +++ b/osu.Game.Rulesets.Osu/Skinning/Default/SpinnerSpmCalculator.cs @@ -58,6 +58,7 @@ record = records.Dequeue(); private void resetState(DrawableHitObject hitObject) { + lastRecord = default; result.Value = 0; records.Clear(); }