1
0
mirror of https://github.com/ppy/osu.git synced 2025-01-12 17:23:22 +08:00

Merge pull request #16542 from peppy/carousel-search-performance-less-simple

Avoid performing full filter when updating carousel beatmap sets
This commit is contained in:
Dan Balasescu 2022-01-21 15:05:30 +09:00 committed by GitHub
commit c4f24ef96c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 34 additions and 9 deletions

View File

@ -300,16 +300,18 @@ namespace osu.Game.Screens.Select
root.AddChild(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. // check if we can/need to maintain our current selection.
if (previouslySelectedID != null) if (previouslySelectedID != null)
select((CarouselItem)newSet.Beatmaps.FirstOrDefault(b => b.BeatmapInfo.ID == previouslySelectedID) ?? newSet); select((CarouselItem)newSet.Beatmaps.FirstOrDefault(b => b.BeatmapInfo.ID == previouslySelectedID) ?? newSet);
itemsCache.Invalidate(); itemsCache.Invalidate();
Schedule(() => BeatmapSetsChanged?.Invoke()); Schedule(() =>
{
if (!Scroll.UserScrolling)
ScrollToSelected(true);
BeatmapSetsChanged?.Invoke();
});
}); });
/// <summary> /// <summary>

View File

@ -4,6 +4,8 @@
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
#nullable enable
namespace osu.Game.Screens.Select.Carousel namespace osu.Game.Screens.Select.Carousel
{ {
/// <summary> /// <summary>
@ -11,7 +13,7 @@ namespace osu.Game.Screens.Select.Carousel
/// </summary> /// </summary>
public class CarouselGroup : CarouselItem public class CarouselGroup : CarouselItem
{ {
public override DrawableCarouselItem CreateDrawableRepresentation() => null; public override DrawableCarouselItem? CreateDrawableRepresentation() => null;
public IReadOnlyList<CarouselItem> Children => InternalChildren; public IReadOnlyList<CarouselItem> Children => InternalChildren;
@ -23,6 +25,10 @@ namespace osu.Game.Screens.Select.Carousel
/// </summary> /// </summary>
private ulong currentChildID; private ulong currentChildID;
private Comparer<CarouselItem>? criteriaComparer;
private FilterCriteria? lastCriteria;
public virtual void RemoveChild(CarouselItem i) public virtual void RemoveChild(CarouselItem i)
{ {
InternalChildren.Remove(i); InternalChildren.Remove(i);
@ -36,10 +42,24 @@ namespace osu.Game.Screens.Select.Carousel
{ {
i.State.ValueChanged += state => ChildItemStateChanged(i, state.NewValue); i.State.ValueChanged += state => ChildItemStateChanged(i, state.NewValue);
i.ChildID = ++currentChildID; i.ChildID = ++currentChildID;
InternalChildren.Add(i);
if (lastCriteria != null)
{
i.Filter(lastCriteria);
int index = InternalChildren.BinarySearch(i, criteriaComparer);
if (index < 0) index = ~index; // BinarySearch hacks multiple return values with 2's complement.
InternalChildren.Insert(index, i);
}
else
{
// criteria may be null for initial population. the filtering will be applied post-add.
InternalChildren.Add(i);
}
} }
public CarouselGroup(List<CarouselItem> items = null) public CarouselGroup(List<CarouselItem>? items = null)
{ {
if (items != null) InternalChildren = items; if (items != null) InternalChildren = items;
@ -67,9 +87,12 @@ namespace osu.Game.Screens.Select.Carousel
base.Filter(criteria); base.Filter(criteria);
InternalChildren.ForEach(c => c.Filter(criteria)); InternalChildren.ForEach(c => c.Filter(criteria));
// IEnumerable<T>.OrderBy() is used instead of List<T>.Sort() to ensure sorting stability // IEnumerable<T>.OrderBy() is used instead of List<T>.Sort() to ensure sorting stability
var criteriaComparer = Comparer<CarouselItem>.Create((x, y) => x.CompareTo(criteria, y)); criteriaComparer = Comparer<CarouselItem>.Create((x, y) => x.CompareTo(criteria, y));
InternalChildren = InternalChildren.OrderBy(c => c, criteriaComparer).ToList(); InternalChildren = InternalChildren.OrderBy(c => c, criteriaComparer).ToList();
lastCriteria = criteria;
} }
protected virtual void ChildItemStateChanged(CarouselItem item, CarouselItemState value) protected virtual void ChildItemStateChanged(CarouselItem item, CarouselItemState value)