Skip to content

Commit

Permalink
Further reduce allocs
Browse files Browse the repository at this point in the history
  • Loading branch information
smoogipoo committed Feb 26, 2024
1 parent 840d401 commit 7a33912
Showing 1 changed file with 14 additions and 19 deletions.
33 changes: 14 additions & 19 deletions osu.Framework/Bindables/AggregateBindable.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@

using System;
using System.Collections.Generic;
using System.Linq;

namespace osu.Framework.Bindables
{
Expand Down Expand Up @@ -65,20 +64,26 @@ public void RemoveSource(IBindable<T> bindable)
{
lock (sourceMapping)
{
var weak = findExistingPair(bindable);

if (weak != null)
if (findExistingPair(bindable) is WeakRefPair pair)
{
weak.BoundCopy.UnbindAll();
sourceMapping.Remove(weak);
pair.BoundCopy.UnbindAll();
sourceMapping.Remove(pair);
}

recalculateAggregate();
}
}

private WeakRefPair findExistingPair(IBindable<T> bindable) =>
sourceMapping.FirstOrDefault(p => p.WeakReference.TryGetTarget(out var target) && target == bindable);
private WeakRefPair? findExistingPair(IBindable<T> bindable)
{
foreach (var p in sourceMapping)
{
if (p.WeakReference.TryGetTarget(out var target) && target == bindable)
return p;
}

return null;
}

private void recalculateAggregate(ValueChangedEvent<T> obj = null)
{
Expand Down Expand Up @@ -112,16 +117,6 @@ public void RemoveAllSources()
}
}

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

public WeakRefPair(WeakReference<Bindable<T>> weakReference, IBindable<T> boundCopy)
{
WeakReference = weakReference;
BoundCopy = boundCopy;
}
}
private readonly record struct WeakRefPair(WeakReference<Bindable<T>> WeakReference, IBindable<T> BoundCopy);
}
}

0 comments on commit 7a33912

Please sign in to comment.