1
0
mirror of https://github.com/ppy/osu.git synced 2024-12-15 15:12:57 +08:00

Add <Guid,CarouselBeatmapSet> dictionary to speed up update operations in carousel

This commit is contained in:
Dean Herbert 2022-01-20 21:58:16 +09:00
parent 9a864267d2
commit 0b93f3c88f

View File

@ -95,7 +95,7 @@ namespace osu.Game.Screens.Select
protected readonly CarouselScrollContainer Scroll; protected readonly CarouselScrollContainer Scroll;
private IEnumerable<CarouselBeatmapSet> beatmapSets => root.Children.OfType<CarouselBeatmapSet>(); private IEnumerable<CarouselBeatmapSet> beatmapSets => root.BeatmapSetsByID.Values;
// todo: only used for testing, maybe remove. // todo: only used for testing, maybe remove.
private bool loadedTestBeatmaps; private bool loadedTestBeatmaps;
@ -117,6 +117,7 @@ namespace osu.Game.Screens.Select
newRoot.AddChildren(beatmapSets.Select(s => createCarouselSet(s.Detach())).Where(g => g != null)); newRoot.AddChildren(beatmapSets.Select(s => createCarouselSet(s.Detach())).Where(g => g != null));
root = newRoot; root = newRoot;
if (selectedBeatmapSet != null && !beatmapSets.Contains(selectedBeatmapSet.BeatmapSet)) if (selectedBeatmapSet != null && !beatmapSets.Contains(selectedBeatmapSet.BeatmapSet))
selectedBeatmapSet = null; selectedBeatmapSet = null;
@ -209,7 +210,7 @@ namespace osu.Game.Screens.Select
return; return;
foreach (int i in changes.InsertedIndices) foreach (int i in changes.InsertedIndices)
RemoveBeatmapSet(sender[i].Detach()); removeBeatmapSet(sender[i].ID);
} }
private void beatmapSetsChanged(IRealmCollection<BeatmapSetInfo> sender, ChangeSet changes, Exception error) private void beatmapSetsChanged(IRealmCollection<BeatmapSetInfo> sender, ChangeSet changes, Exception error)
@ -223,24 +224,20 @@ namespace osu.Game.Screens.Select
// During initial population, we must manually account for the fact that our original query was done on an async thread. // During initial population, we must manually account for the fact that our original query was done on an async thread.
// Since then, there may have been imports or deletions. // Since then, there may have been imports or deletions.
// Here we manually catch up on any changes. // Here we manually catch up on any changes.
var populatedSets = new HashSet<Guid>();
foreach (var s in beatmapSets)
populatedSets.Add(s.BeatmapSet.ID);
var realmSets = new HashSet<Guid>(); var realmSets = new HashSet<Guid>();
foreach (var s in sender) foreach (var s in sender)
realmSets.Add(s.ID); realmSets.Add(s.ID);
foreach (var s in realmSets) foreach (var id in realmSets)
{ {
if (!populatedSets.Contains(s)) if (!root.BeatmapSetsByID.ContainsKey(id))
UpdateBeatmapSet(realmFactory.Context.Find<BeatmapSetInfo>(s)); UpdateBeatmapSet(realmFactory.Context.Find<BeatmapSetInfo>(id).Detach());
} }
foreach (var s in populatedSets) foreach (var id in root.BeatmapSetsByID.Keys)
{ {
if (!realmSets.Contains(s)) if (!realmSets.Contains(id))
RemoveBeatmapSet(realmFactory.Context.Find<BeatmapSetInfo>(s)); removeBeatmapSet(id);
} }
signalBeatmapsLoaded(); signalBeatmapsLoaded();
@ -261,16 +258,30 @@ namespace osu.Game.Screens.Select
return; return;
foreach (int i in changes.InsertedIndices) foreach (int i in changes.InsertedIndices)
UpdateBeatmapSet(sender[i].BeatmapSet?.Detach()); {
var beatmapInfo = sender[i];
var beatmapSet = beatmapInfo.BeatmapSet;
Debug.Assert(beatmapSet != null);
// Only require to action here if the beatmap is missing.
// This avoids processing these events unnecessarily when new beatmaps are imported, for example.
if (root.BeatmapSetsByID.TryGetValue(beatmapSet.ID, out var existingSet)
&& existingSet.BeatmapSet.Beatmaps.All(b => b.ID != beatmapInfo.ID))
{
UpdateBeatmapSet(beatmapSet.Detach());
}
}
} }
private IRealmCollection<BeatmapSetInfo> getBeatmapSets(Realm realm) => realm.All<BeatmapSetInfo>().Where(s => !s.DeletePending && !s.Protected).AsRealmCollection(); private IRealmCollection<BeatmapSetInfo> getBeatmapSets(Realm realm) => realm.All<BeatmapSetInfo>().Where(s => !s.DeletePending && !s.Protected).AsRealmCollection();
public void RemoveBeatmapSet(BeatmapSetInfo beatmapSet) => Schedule(() => public void RemoveBeatmapSet(BeatmapSetInfo beatmapSet) =>
{ removeBeatmapSet(beatmapSet.ID);
var existingSet = beatmapSets.FirstOrDefault(b => b.BeatmapSet.Equals(beatmapSet));
if (existingSet == null) private void removeBeatmapSet(Guid beatmapSetID) => Schedule(() =>
{
if (!root.BeatmapSetsByID.TryGetValue(beatmapSetID, out var existingSet))
return; return;
root.RemoveChild(existingSet); root.RemoveChild(existingSet);
@ -281,33 +292,27 @@ namespace osu.Game.Screens.Select
{ {
Guid? previouslySelectedID = null; Guid? previouslySelectedID = null;
CarouselBeatmapSet existingSet = beatmapSets.FirstOrDefault(b => b.BeatmapSet.Equals(beatmapSet));
// If the selected beatmap is about to be removed, store its ID so it can be re-selected if required // If the selected beatmap is about to be removed, store its ID so it can be re-selected if required
if (existingSet?.State?.Value == CarouselItemState.Selected) if (selectedBeatmapSet?.BeatmapSet.ID == beatmapSet.ID)
previouslySelectedID = selectedBeatmap?.BeatmapInfo.ID; previouslySelectedID = selectedBeatmap?.BeatmapInfo.ID;
var newSet = createCarouselSet(beatmapSet); var newSet = createCarouselSet(beatmapSet);
if (existingSet != null) root.RemoveChild(beatmapSet.ID);
root.RemoveChild(existingSet);
if (newSet == null) if (newSet != null)
{ {
itemsCache.Invalidate(); root.AddChild(newSet);
return;
// only reset scroll position if already near the scroll target.
// without this, during a large beatmap import it is impossible to navigate the carousel.
applyActiveCriteria(false, alwaysResetScrollPosition: false);
// check if we can/need to maintain our current selection.
if (previouslySelectedID != null)
select((CarouselItem)newSet.Beatmaps.FirstOrDefault(b => b.BeatmapInfo.ID == previouslySelectedID) ?? newSet);
} }
root.AddChild(newSet);
// only reset scroll position if already near the scroll target.
// without this, during a large beatmap import it is impossible to navigate the carousel.
applyActiveCriteria(false, alwaysResetScrollPosition: false);
// check if we can/need to maintain our current selection.
if (previouslySelectedID != null)
select((CarouselItem)newSet.Beatmaps.FirstOrDefault(b => b.BeatmapInfo.ID == previouslySelectedID) ?? newSet);
itemsCache.Invalidate(); itemsCache.Invalidate();
Schedule(() => BeatmapSetsChanged?.Invoke()); Schedule(() => BeatmapSetsChanged?.Invoke());
}); });
@ -911,6 +916,8 @@ namespace osu.Game.Screens.Select
{ {
private readonly BeatmapCarousel carousel; private readonly BeatmapCarousel carousel;
public readonly Dictionary<Guid, CarouselBeatmapSet> BeatmapSetsByID = new Dictionary<Guid, CarouselBeatmapSet>();
public CarouselRoot(BeatmapCarousel carousel) public CarouselRoot(BeatmapCarousel carousel)
{ {
// root should always remain selected. if not, PerformSelection will not be called. // root should always remain selected. if not, PerformSelection will not be called.
@ -920,6 +927,28 @@ namespace osu.Game.Screens.Select
this.carousel = carousel; this.carousel = carousel;
} }
public override void AddChild(CarouselItem i)
{
CarouselBeatmapSet set = (CarouselBeatmapSet)i;
BeatmapSetsByID[set.BeatmapSet.ID] = set;
base.AddChild(i);
}
public void RemoveChild(Guid beatmapSetID)
{
if (BeatmapSetsByID.TryGetValue(beatmapSetID, out var carouselBeatmapSet))
RemoveChild(carouselBeatmapSet);
}
public override void RemoveChild(CarouselItem i)
{
CarouselBeatmapSet set = (CarouselBeatmapSet)i;
BeatmapSetsByID.Remove(set.BeatmapSet.ID);
base.RemoveChild(i);
}
protected override void PerformSelection() protected override void PerformSelection()
{ {
if (LastSelected == null || LastSelected.Filtered.Value) if (LastSelected == null || LastSelected.Filtered.Value)