Skip to content

Commit

Permalink
Merge pull request #6356 from smoogipoo/revert-bindablelist-hashset
Browse files Browse the repository at this point in the history
Revert BindableList GC reduction change
  • Loading branch information
peppy authored Aug 9, 2024
2 parents 55b2291 + 7a3d4bd commit 7bb0e9e
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 28 deletions.
7 changes: 0 additions & 7 deletions osu.Framework.Benchmarks/BenchmarkBindableList.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,6 @@ public void GlobalSetup()
list.Add(i);
}

[Benchmark]
public void Add()
{
for (int i = 0; i < 10; i++)
list.Add(i);
}

[Benchmark]
public int Enumerate()
{
Expand Down
32 changes: 11 additions & 21 deletions osu.Framework/Bindables/BindableList.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public BindableList(IEnumerable<T> items = null)
public T this[int index]
{
get => collection[index];
set => setIndex(index, value, getRecursionList());
set => setIndex(index, value, new HashSet<BindableList<T>>());
}

private void setIndex(int index, T item, HashSet<BindableList<T>> appliedInstances)
Expand Down Expand Up @@ -85,7 +85,7 @@ private void setIndex(int index, T item, HashSet<BindableList<T>> appliedInstanc
/// <param name="item">The item to be added.</param>
/// <exception cref="InvalidOperationException">Thrown when this <see cref="BindableList{T}"/> is <see cref="Disabled"/>.</exception>
public void Add(T item)
=> add(item, getRecursionList());
=> add(item, new HashSet<BindableList<T>>());

private void add(T item, HashSet<BindableList<T>> appliedInstances)
{
Expand Down Expand Up @@ -118,7 +118,7 @@ private void add(T item, HashSet<BindableList<T>> appliedInstances)
/// <param name="item">The item to insert.</param>
/// <exception cref="InvalidOperationException">Thrown when this <see cref="BindableList{T}"/> is <see cref="Disabled"/>.</exception>
public void Insert(int index, T item)
=> insert(index, item, getRecursionList());
=> insert(index, item, new HashSet<BindableList<T>>());

private void insert(int index, T item, HashSet<BindableList<T>> appliedInstances)
{
Expand All @@ -142,7 +142,7 @@ private void insert(int index, T item, HashSet<BindableList<T>> appliedInstances
/// </summary>
/// <exception cref="InvalidOperationException">Thrown when this <see cref="BindableList{T}"/> is <see cref="Disabled"/>.</exception>
public void Clear()
=> clear(getRecursionList());
=> clear(new HashSet<BindableList<T>>());

private void clear(HashSet<BindableList<T>> appliedInstances)
{
Expand Down Expand Up @@ -182,7 +182,7 @@ public bool Contains(T item)
/// <returns><code>true</code> if the removal was successful.</returns>
/// <exception cref="InvalidOperationException">Thrown if this <see cref="BindableList{T}"/> is <see cref="Disabled"/>.</exception>
public bool Remove(T item)
=> remove(item, getRecursionList());
=> remove(item, new HashSet<BindableList<T>>());

private bool remove(T item, HashSet<BindableList<T>> appliedInstances)
{
Expand Down Expand Up @@ -224,7 +224,7 @@ private bool remove(T item, HashSet<BindableList<T>> appliedInstances)
/// <param name="count">The count of items to be removed.</param>
public void RemoveRange(int index, int count)
{
removeRange(index, count, getRecursionList());
removeRange(index, count, new HashSet<BindableList<T>>());
}

private void removeRange(int index, int count, HashSet<BindableList<T>> appliedInstances)
Expand Down Expand Up @@ -259,7 +259,7 @@ private void removeRange(int index, int count, HashSet<BindableList<T>> appliedI
/// <param name="index">The index of the item to remove.</param>
/// <exception cref="InvalidOperationException">Thrown if this <see cref="BindableList{T}"/> is <see cref="Disabled"/>.</exception>
public void RemoveAt(int index)
=> removeAt(index, getRecursionList());
=> removeAt(index, new HashSet<BindableList<T>>());

private void removeAt(int index, HashSet<BindableList<T>> appliedInstances)
{
Expand All @@ -285,7 +285,7 @@ private void removeAt(int index, HashSet<BindableList<T>> appliedInstances)
/// </summary>
/// <param name="match">The predicate.</param>
public int RemoveAll(Predicate<T> match)
=> removeAll(match, getRecursionList());
=> removeAll(match, new HashSet<BindableList<T>>());

private int removeAll(Predicate<T> match, HashSet<BindableList<T>> appliedInstances)
{
Expand Down Expand Up @@ -319,7 +319,7 @@ private int removeAll(Predicate<T> match, HashSet<BindableList<T>> appliedInstan
/// <param name="count">The count of items to be removed.</param>
/// <param name="newItems">The items to replace the removed items with.</param>
public void ReplaceRange(int index, int count, IEnumerable<T> newItems)
=> replaceRange(index, count, newItems as IList ?? newItems.ToArray(), getRecursionList());
=> replaceRange(index, count, newItems as IList ?? newItems.ToArray(), new HashSet<BindableList<T>>());

private void replaceRange(int index, int count, IList newItems, HashSet<BindableList<T>> appliedInstances)
{
Expand Down Expand Up @@ -542,7 +542,7 @@ public virtual void UnbindFrom(IUnbindable them)
/// <param name="items">The collection whose items should be added to this collection.</param>
/// <exception cref="InvalidOperationException">Thrown if this collection is <see cref="Disabled"/></exception>
public void AddRange(IEnumerable<T> items)
=> addRange(items as IList ?? items.ToArray(), getRecursionList());
=> addRange(items as IList ?? items.ToArray(), new HashSet<BindableList<T>>());

private void addRange(IList items, HashSet<BindableList<T>> appliedInstances)
{
Expand All @@ -567,7 +567,7 @@ private void addRange(IList items, HashSet<BindableList<T>> appliedInstances)
/// <param name="oldIndex">The index of the item to move.</param>
/// <param name="newIndex">The index specifying the new location of the item.</param>
public void Move(int oldIndex, int newIndex)
=> move(oldIndex, newIndex, getRecursionList());
=> move(oldIndex, newIndex, new HashSet<BindableList<T>>());

private void move(int oldIndex, int newIndex, HashSet<BindableList<T>> appliedInstances)
{
Expand Down Expand Up @@ -691,15 +691,5 @@ private void ensureMutationAllowed()
public bool IsDefault => Count == 0;

string IFormattable.ToString(string format, IFormatProvider formatProvider) => ((FormattableString)$"{GetType().ReadableName()}({nameof(Count)}={Count})").ToString(formatProvider);

[ThreadStatic]
private static HashSet<BindableList<T>> recursionList;

private static HashSet<BindableList<T>> getRecursionList()
{
recursionList ??= new HashSet<BindableList<T>>();
recursionList.Clear();
return recursionList;
}
}
}

0 comments on commit 7bb0e9e

Please sign in to comment.