Skip to content

Commit

Permalink
Reduce WeakReference allocs in AggregateBindable
Browse files Browse the repository at this point in the history
  • Loading branch information
smoogipoo committed Feb 26, 2024
1 parent 082f699 commit 840d401
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 3 deletions.
6 changes: 3 additions & 3 deletions osu.Framework/Bindables/AggregateBindable.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public void AddSource(IBindable<T> bindable)
return;

var boundCopy = bindable.GetBoundCopy();
sourceMapping.Add(new WeakRefPair(new WeakReference<IBindable<T>>(bindable), boundCopy));
sourceMapping.Add(new WeakRefPair(bindable.GetWeakReference(), boundCopy));
boundCopy.BindValueChanged(recalculateAggregate, true);
}
}
Expand Down Expand Up @@ -114,10 +114,10 @@ public void RemoveAllSources()

private class WeakRefPair
{
public readonly WeakReference<IBindable<T>> WeakReference;
public readonly WeakReference<Bindable<T>> WeakReference;
public readonly IBindable<T> BoundCopy;

public WeakRefPair(WeakReference<IBindable<T>> weakReference, IBindable<T> boundCopy)
public WeakRefPair(WeakReference<Bindable<T>> weakReference, IBindable<T> boundCopy)
{
WeakReference = weakReference;
BoundCopy = boundCopy;
Expand Down
2 changes: 2 additions & 0 deletions osu.Framework/Bindables/Bindable.cs
Original file line number Diff line number Diff line change
Expand Up @@ -440,6 +440,8 @@ public Bindable<T> GetUnboundCopy()

IBindable IBindable.GetBoundCopy() => GetBoundCopy();

public WeakReference<Bindable<T>> GetWeakReference() => weakReference;

IBindable<T> IBindable<T>.GetBoundCopy() => GetBoundCopy();

/// <inheritdoc cref="IBindable{T}.GetBoundCopy"/>
Expand Down
5 changes: 5 additions & 0 deletions osu.Framework/Bindables/IBindable.cs
Original file line number Diff line number Diff line change
Expand Up @@ -111,5 +111,10 @@ IBindable<T> BindTarget

/// <inheritdoc cref="IBindable.GetBoundCopy"/>
IBindable<T> GetBoundCopy();

/// <summary>
/// Retrieves a weak reference to this bindable.
/// </summary>
internal WeakReference<Bindable<T>> GetWeakReference();
}
}

0 comments on commit 840d401

Please sign in to comment.