Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix performance issue of GTK DropDown when using an IEnumerable as a data store #2029

Merged
merged 1 commit into from
Sep 3, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 23 additions & 14 deletions src/Eto/CollectionChangedHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ protected virtual void OnUnregisterCollection(EventArgs e)
public virtual bool Register(TCollection collection)
{
if (Collection != null)
Unregister();
Unregister();
Collection = collection;

var notify = Collection as INotifyCollectionChanged;
Expand Down Expand Up @@ -101,9 +101,10 @@ public virtual int IndexOf(TItem item)
/// <param name="index">Index of the item to get.</param>
public virtual TItem ElementAt(int index)
{
var list = Collection as IList<TItem>;
if (list != null)
return list[index];
if (Collection is IList<TItem> listItem)
return listItem[index];
if (Collection is IList list)
return (TItem)list[index];
return InternalElementAt(index);
}

Expand Down Expand Up @@ -347,6 +348,9 @@ public abstract class EnumerableChangedHandler<TItem> : EnumerableChangedHandler
public abstract class EnumerableChangedHandler<TItem, TCollection> : CollectionChangedHandler<TItem, TCollection>
where TCollection: class, IEnumerable<TItem>
{
IList<TItem> _lookup;

IList<TItem> Lookup => _lookup ?? (_lookup = Collection?.ToList() ?? new List<TItem>());

/// <summary>
/// Implements the mechanism for finding the index of an item (the slow way)
Expand All @@ -359,7 +363,12 @@ public abstract class EnumerableChangedHandler<TItem, TCollection> : CollectionC
/// <returns>Index of the item, or -1 if not found</returns>
protected override int InternalIndexOf(TItem item)
{
return Collection?.IndexOf(item) ?? -1;
if (Collection is IList<TItem> list)
return list.IndexOf(item);
if (Collection is IList list2)
return list2.IndexOf(item);

return Lookup.IndexOf(item);
}

/// <summary>
Expand All @@ -371,10 +380,11 @@ public override int Count
get
{
// mono doesn't test for ICollection, causing performance issues
var coll = Collection as ICollection;
if (coll != null)
if (Collection is ICollection<TItem> coll)
return coll.Count;
return Collection != null ? Collection.Count() : 0;
if (Collection is ICollection coll2)
return coll2.Count;
return Lookup.Count;
}
}

Expand All @@ -386,13 +396,11 @@ public override int Count
/// <param name="index">Index of the item to get.</param>
protected override TItem InternalElementAt(int index)
{
var list = Collection as IList<TItem>;
if (list != null)
return (TItem)list[index];
var list2 = Collection as IList;
if (list2 != null)
if (Collection is IList<TItem> list)
return list[index];
if (Collection is IList list2)
return (TItem)list2[index];
return Collection.ElementAt(index);
return Lookup[index];
}

/// <summary>
Expand All @@ -411,6 +419,7 @@ protected virtual void InitializeCollection()
{
if (Collection != null)
AddRange(Collection);
_lookup = null;
}
}

Expand Down