From 3aefc919675a855f6d13385d4d69ed326db2bc4f Mon Sep 17 00:00:00 2001 From: Andrei Zavatski Date: Wed, 31 Jan 2024 07:54:07 +0300 Subject: [PATCH 1/5] Make AliveDrawableMap public --- .../PooledDrawableWithLifetimeContainer.cs | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/osu.Game/Rulesets/Objects/Pooling/PooledDrawableWithLifetimeContainer.cs b/osu.Game/Rulesets/Objects/Pooling/PooledDrawableWithLifetimeContainer.cs index 1b0176cae5..1ebdf48ae8 100644 --- a/osu.Game/Rulesets/Objects/Pooling/PooledDrawableWithLifetimeContainer.cs +++ b/osu.Game/Rulesets/Objects/Pooling/PooledDrawableWithLifetimeContainer.cs @@ -35,7 +35,7 @@ namespace osu.Game.Rulesets.Objects.Pooling /// /// The enumeration order is undefined. /// - public IEnumerable<(TEntry Entry, TDrawable Drawable)> AliveEntries => aliveDrawableMap.Select(x => (x.Key, x.Value)); + public IEnumerable<(TEntry Entry, TDrawable Drawable)> AliveEntries => AliveDrawableMap.Select(x => (x.Key, x.Value)); /// /// Whether to remove an entry when clock goes backward and crossed its . @@ -53,7 +53,7 @@ namespace osu.Game.Rulesets.Objects.Pooling /// internal double FutureLifetimeExtension { get; set; } - private readonly Dictionary aliveDrawableMap = new Dictionary(); + public readonly Dictionary AliveDrawableMap = new Dictionary(); private readonly HashSet allEntries = new HashSet(); private readonly LifetimeEntryManager lifetimeManager = new LifetimeEntryManager(); @@ -101,10 +101,10 @@ namespace osu.Game.Rulesets.Objects.Pooling private void entryBecameAlive(LifetimeEntry lifetimeEntry) { var entry = (TEntry)lifetimeEntry; - Debug.Assert(!aliveDrawableMap.ContainsKey(entry)); + Debug.Assert(!AliveDrawableMap.ContainsKey(entry)); TDrawable drawable = GetDrawable(entry); - aliveDrawableMap[entry] = drawable; + AliveDrawableMap[entry] = drawable; AddDrawable(entry, drawable); } @@ -119,10 +119,10 @@ namespace osu.Game.Rulesets.Objects.Pooling private void entryBecameDead(LifetimeEntry lifetimeEntry) { var entry = (TEntry)lifetimeEntry; - Debug.Assert(aliveDrawableMap.ContainsKey(entry)); + Debug.Assert(AliveDrawableMap.ContainsKey(entry)); - TDrawable drawable = aliveDrawableMap[entry]; - aliveDrawableMap.Remove(entry); + TDrawable drawable = AliveDrawableMap[entry]; + AliveDrawableMap.Remove(entry); RemoveDrawable(entry, drawable); } @@ -148,7 +148,7 @@ namespace osu.Game.Rulesets.Objects.Pooling foreach (var entry in Entries.ToArray()) Remove(entry); - Debug.Assert(aliveDrawableMap.Count == 0); + Debug.Assert(AliveDrawableMap.Count == 0); } protected override bool CheckChildrenLife() From 6b1de5446ad5dfd21a481ee8f484b92b42851be5 Mon Sep 17 00:00:00 2001 From: Andrei Zavatski Date: Wed, 31 Jan 2024 07:54:28 +0300 Subject: [PATCH 2/5] Reduce allocaations in ScrollingHitObjectContainer --- osu.Game/Rulesets/UI/Scrolling/ScrollingHitObjectContainer.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/osu.Game/Rulesets/UI/Scrolling/ScrollingHitObjectContainer.cs b/osu.Game/Rulesets/UI/Scrolling/ScrollingHitObjectContainer.cs index 39ddb5c753..23ac4378e4 100644 --- a/osu.Game/Rulesets/UI/Scrolling/ScrollingHitObjectContainer.cs +++ b/osu.Game/Rulesets/UI/Scrolling/ScrollingHitObjectContainer.cs @@ -186,9 +186,9 @@ namespace osu.Game.Rulesets.UI.Scrolling // to prevent hit objects displayed in a wrong position for one frame. // Only AliveEntries need to be considered for layout (reduces overhead in the case of scroll speed changes). // We are not using AliveObjects directly to avoid selection/sorting overhead since we don't care about the order at which positions will be updated. - foreach (var entry in AliveEntries) + foreach (var entry in AliveDrawableMap) { - var obj = entry.Drawable; + var obj = entry.Value; updatePosition(obj, Time.Current); From 0642d740149b2daf8876d76b3b55c15709cd6144 Mon Sep 17 00:00:00 2001 From: Dan Balasescu Date: Wed, 31 Jan 2024 22:52:57 +0900 Subject: [PATCH 3/5] Expose as ReadOnlyDictionary --- .../PooledDrawableWithLifetimeContainer.cs | 19 +++++++++++-------- .../UI/GameplaySampleTriggerSource.cs | 2 +- osu.Game/Rulesets/UI/HitObjectContainer.cs | 2 +- .../Scrolling/ScrollingHitObjectContainer.cs | 2 +- 4 files changed, 14 insertions(+), 11 deletions(-) diff --git a/osu.Game/Rulesets/Objects/Pooling/PooledDrawableWithLifetimeContainer.cs b/osu.Game/Rulesets/Objects/Pooling/PooledDrawableWithLifetimeContainer.cs index 1ebdf48ae8..aed608cf8f 100644 --- a/osu.Game/Rulesets/Objects/Pooling/PooledDrawableWithLifetimeContainer.cs +++ b/osu.Game/Rulesets/Objects/Pooling/PooledDrawableWithLifetimeContainer.cs @@ -2,6 +2,7 @@ // See the LICENCE file in the repository root for full licence text. using System.Collections.Generic; +using System.Collections.ObjectModel; using System.Diagnostics; using System.Linq; using osu.Framework.Graphics; @@ -35,7 +36,7 @@ namespace osu.Game.Rulesets.Objects.Pooling /// /// The enumeration order is undefined. /// - public IEnumerable<(TEntry Entry, TDrawable Drawable)> AliveEntries => AliveDrawableMap.Select(x => (x.Key, x.Value)); + public readonly ReadOnlyDictionary AliveEntries; /// /// Whether to remove an entry when clock goes backward and crossed its . @@ -53,7 +54,7 @@ namespace osu.Game.Rulesets.Objects.Pooling /// internal double FutureLifetimeExtension { get; set; } - public readonly Dictionary AliveDrawableMap = new Dictionary(); + private readonly Dictionary aliveDrawableMap = new Dictionary(); private readonly HashSet allEntries = new HashSet(); private readonly LifetimeEntryManager lifetimeManager = new LifetimeEntryManager(); @@ -63,6 +64,8 @@ namespace osu.Game.Rulesets.Objects.Pooling lifetimeManager.EntryBecameAlive += entryBecameAlive; lifetimeManager.EntryBecameDead += entryBecameDead; lifetimeManager.EntryCrossedBoundary += entryCrossedBoundary; + + AliveEntries = new ReadOnlyDictionary(aliveDrawableMap); } /// @@ -101,10 +104,10 @@ namespace osu.Game.Rulesets.Objects.Pooling private void entryBecameAlive(LifetimeEntry lifetimeEntry) { var entry = (TEntry)lifetimeEntry; - Debug.Assert(!AliveDrawableMap.ContainsKey(entry)); + Debug.Assert(!aliveDrawableMap.ContainsKey(entry)); TDrawable drawable = GetDrawable(entry); - AliveDrawableMap[entry] = drawable; + aliveDrawableMap[entry] = drawable; AddDrawable(entry, drawable); } @@ -119,10 +122,10 @@ namespace osu.Game.Rulesets.Objects.Pooling private void entryBecameDead(LifetimeEntry lifetimeEntry) { var entry = (TEntry)lifetimeEntry; - Debug.Assert(AliveDrawableMap.ContainsKey(entry)); + Debug.Assert(aliveDrawableMap.ContainsKey(entry)); - TDrawable drawable = AliveDrawableMap[entry]; - AliveDrawableMap.Remove(entry); + TDrawable drawable = aliveDrawableMap[entry]; + aliveDrawableMap.Remove(entry); RemoveDrawable(entry, drawable); } @@ -148,7 +151,7 @@ namespace osu.Game.Rulesets.Objects.Pooling foreach (var entry in Entries.ToArray()) Remove(entry); - Debug.Assert(AliveDrawableMap.Count == 0); + Debug.Assert(aliveDrawableMap.Count == 0); } protected override bool CheckChildrenLife() diff --git a/osu.Game/Rulesets/UI/GameplaySampleTriggerSource.cs b/osu.Game/Rulesets/UI/GameplaySampleTriggerSource.cs index b61e8d9674..177520f28f 100644 --- a/osu.Game/Rulesets/UI/GameplaySampleTriggerSource.cs +++ b/osu.Game/Rulesets/UI/GameplaySampleTriggerSource.cs @@ -105,7 +105,7 @@ namespace osu.Game.Rulesets.UI // If required, we can make this lookup more efficient by adding support to get next-future-entry in LifetimeEntryManager. var candidate = // Use alive entries first as an optimisation. - hitObjectContainer.AliveEntries.Select(tuple => tuple.Entry).Where(e => !isAlreadyHit(e)).MinBy(e => e.HitObject.StartTime) + hitObjectContainer.AliveEntries.Keys.Where(e => !isAlreadyHit(e)).MinBy(e => e.HitObject.StartTime) ?? hitObjectContainer.Entries.Where(e => !isAlreadyHit(e)).MinBy(e => e.HitObject.StartTime); // In the case there are no non-judged objects, the last hit object should be used instead. diff --git a/osu.Game/Rulesets/UI/HitObjectContainer.cs b/osu.Game/Rulesets/UI/HitObjectContainer.cs index 099be486b3..c2879e6d87 100644 --- a/osu.Game/Rulesets/UI/HitObjectContainer.cs +++ b/osu.Game/Rulesets/UI/HitObjectContainer.cs @@ -21,7 +21,7 @@ namespace osu.Game.Rulesets.UI { public IEnumerable Objects => InternalChildren.Cast().OrderBy(h => h.HitObject.StartTime); - public IEnumerable AliveObjects => AliveEntries.Select(pair => pair.Drawable).OrderBy(h => h.HitObject.StartTime); + public IEnumerable AliveObjects => AliveEntries.Values.OrderBy(h => h.HitObject.StartTime); /// /// Invoked when a is judged. diff --git a/osu.Game/Rulesets/UI/Scrolling/ScrollingHitObjectContainer.cs b/osu.Game/Rulesets/UI/Scrolling/ScrollingHitObjectContainer.cs index 23ac4378e4..4e72291b9c 100644 --- a/osu.Game/Rulesets/UI/Scrolling/ScrollingHitObjectContainer.cs +++ b/osu.Game/Rulesets/UI/Scrolling/ScrollingHitObjectContainer.cs @@ -186,7 +186,7 @@ namespace osu.Game.Rulesets.UI.Scrolling // to prevent hit objects displayed in a wrong position for one frame. // Only AliveEntries need to be considered for layout (reduces overhead in the case of scroll speed changes). // We are not using AliveObjects directly to avoid selection/sorting overhead since we don't care about the order at which positions will be updated. - foreach (var entry in AliveDrawableMap) + foreach (var entry in AliveEntries) { var obj = entry.Value; From 4aa27482a9b95ce8049d2a6cac1b1ded0945bc8f Mon Sep 17 00:00:00 2001 From: Andrei Zavatski Date: Sat, 3 Feb 2024 19:52:40 +0300 Subject: [PATCH 4/5] Use SlimReadOnlyDictionaryWrapper for AliveEntries --- .../Objects/Pooling/PooledDrawableWithLifetimeContainer.cs | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/osu.Game/Rulesets/Objects/Pooling/PooledDrawableWithLifetimeContainer.cs b/osu.Game/Rulesets/Objects/Pooling/PooledDrawableWithLifetimeContainer.cs index aed608cf8f..efc10f26e1 100644 --- a/osu.Game/Rulesets/Objects/Pooling/PooledDrawableWithLifetimeContainer.cs +++ b/osu.Game/Rulesets/Objects/Pooling/PooledDrawableWithLifetimeContainer.cs @@ -2,12 +2,13 @@ // See the LICENCE file in the repository root for full licence text. using System.Collections.Generic; -using System.Collections.ObjectModel; using System.Diagnostics; using System.Linq; +using osu.Framework.Extensions.ListExtensions; using osu.Framework.Graphics; using osu.Framework.Graphics.Containers; using osu.Framework.Graphics.Performance; +using osu.Framework.Lists; namespace osu.Game.Rulesets.Objects.Pooling { @@ -36,7 +37,7 @@ namespace osu.Game.Rulesets.Objects.Pooling /// /// The enumeration order is undefined. /// - public readonly ReadOnlyDictionary AliveEntries; + public readonly SlimReadOnlyDictionaryWrapper AliveEntries; /// /// Whether to remove an entry when clock goes backward and crossed its . @@ -65,7 +66,7 @@ namespace osu.Game.Rulesets.Objects.Pooling lifetimeManager.EntryBecameDead += entryBecameDead; lifetimeManager.EntryCrossedBoundary += entryCrossedBoundary; - AliveEntries = new ReadOnlyDictionary(aliveDrawableMap); + AliveEntries = aliveDrawableMap.AsSlimReadOnly(); } /// From e2e3c61c9c9e1a13434a6db6d919299ee459c78e Mon Sep 17 00:00:00 2001 From: Andrei Zavatski Date: Sat, 3 Feb 2024 19:54:04 +0300 Subject: [PATCH 5/5] Use AliveEntries where we don't need startTime order --- osu.Game.Rulesets.Osu/Mods/OsuModDepth.cs | 4 +++- osu.Game.Rulesets.Osu/Mods/OsuModMagnetised.cs | 4 +++- osu.Game.Rulesets.Osu/Mods/OsuModRepel.cs | 4 +++- osu.Game/Screens/Play/FailAnimationContainer.cs | 4 +++- 4 files changed, 12 insertions(+), 4 deletions(-) diff --git a/osu.Game.Rulesets.Osu/Mods/OsuModDepth.cs b/osu.Game.Rulesets.Osu/Mods/OsuModDepth.cs index b70d607ca1..a9111eec1f 100644 --- a/osu.Game.Rulesets.Osu/Mods/OsuModDepth.cs +++ b/osu.Game.Rulesets.Osu/Mods/OsuModDepth.cs @@ -75,8 +75,10 @@ namespace osu.Game.Rulesets.Osu.Mods { double time = playfield.Time.Current; - foreach (var drawable in playfield.HitObjectContainer.AliveObjects) + foreach (var entry in playfield.HitObjectContainer.AliveEntries) { + var drawable = entry.Value; + switch (drawable) { case DrawableHitCircle circle: diff --git a/osu.Game.Rulesets.Osu/Mods/OsuModMagnetised.cs b/osu.Game.Rulesets.Osu/Mods/OsuModMagnetised.cs index befee4af5a..b49fb931d1 100644 --- a/osu.Game.Rulesets.Osu/Mods/OsuModMagnetised.cs +++ b/osu.Game.Rulesets.Osu/Mods/OsuModMagnetised.cs @@ -49,8 +49,10 @@ namespace osu.Game.Rulesets.Osu.Mods { var cursorPos = playfield.Cursor.AsNonNull().ActiveCursor.DrawPosition; - foreach (var drawable in playfield.HitObjectContainer.AliveObjects) + foreach (var entry in playfield.HitObjectContainer.AliveEntries) { + var drawable = entry.Value; + switch (drawable) { case DrawableHitCircle circle: diff --git a/osu.Game.Rulesets.Osu/Mods/OsuModRepel.cs b/osu.Game.Rulesets.Osu/Mods/OsuModRepel.cs index 91feb33931..ced98f0cd5 100644 --- a/osu.Game.Rulesets.Osu/Mods/OsuModRepel.cs +++ b/osu.Game.Rulesets.Osu/Mods/OsuModRepel.cs @@ -48,8 +48,10 @@ namespace osu.Game.Rulesets.Osu.Mods { var cursorPos = playfield.Cursor.AsNonNull().ActiveCursor.DrawPosition; - foreach (var drawable in playfield.HitObjectContainer.AliveObjects) + foreach (var entry in playfield.HitObjectContainer.AliveEntries) { + var drawable = entry.Value; + var destination = Vector2.Clamp(2 * drawable.Position - cursorPos, Vector2.Zero, OsuPlayfield.BASE_SIZE); if (drawable.HitObject is Slider thisSlider) diff --git a/osu.Game/Screens/Play/FailAnimationContainer.cs b/osu.Game/Screens/Play/FailAnimationContainer.cs index 821c67e3cb..ebb0d77726 100644 --- a/osu.Game/Screens/Play/FailAnimationContainer.cs +++ b/osu.Game/Screens/Play/FailAnimationContainer.cs @@ -198,8 +198,10 @@ namespace osu.Game.Screens.Play foreach (var nested in playfield.NestedPlayfields) applyToPlayfield(nested); - foreach (DrawableHitObject obj in playfield.HitObjectContainer.AliveObjects) + foreach (var entry in playfield.HitObjectContainer.AliveEntries) { + var obj = entry.Value; + if (appliedObjects.Contains(obj)) continue;