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

Optimize WeakDelegateReference by introducing TargetEquals, which tak… #1793

Merged
merged 2 commits into from
May 24, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
Optimize WeakDelegateReference by introducing TargetEquals, which tak…
…es 60% less time than comparing with Target if it's still alive, and makes WeakDelegate.RemoveListener take 90% less time in normal usage.
  • Loading branch information
hermestobias committed May 10, 2019
commit 886ffcff25fda9d8b7c65cac8d2db24d1522e19b
47 changes: 47 additions & 0 deletions Source/Prism.Tests/Events/DelegateReferenceFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,53 @@ public void WeakDelegateWorksWithStaticMethodDelegates()
Assert.NotNull(action.Target);
}

[Fact]
public void TargetEqualsActionShouldReturnTrue()
{
var classHandler = new SomeClassHandler();
Action<string> myAction = new Action<string>(classHandler.MyAction);

var weakAction = new DelegateReference(myAction, false);

Assert.True(weakAction.TargetEquals(new Action<string>(classHandler.MyAction)));
}

[Fact]
public void TargetEqualsNullShouldReturnTrueIfTargetNotAlive()
{
SomeClassHandler handler = new SomeClassHandler();
var weakHandlerRef = new WeakReference(handler);

var action = new DelegateReference((Action<string>)handler.DoEvent, false);

handler = null;
GC.Collect();
Assert.False(weakHandlerRef.IsAlive);

Assert.True(action.TargetEquals(null));
}

[Fact]
public void TargetEqualsNullShouldReturnFalseIfTargetAlive()
{
SomeClassHandler handler = new SomeClassHandler();
var weakHandlerRef = new WeakReference(handler);

var action = new DelegateReference((Action<string>)handler.DoEvent, false);

Assert.False(action.TargetEquals(null));
Assert.True(weakHandlerRef.IsAlive);
GC.KeepAlive(handler);
}

[Fact]
public void TargetEqualsWorksWithStaticMethodDelegates()
{
var action = new DelegateReference((Action)SomeClassHandler.StaticMethod, false);

Assert.True(action.TargetEquals((Action)SomeClassHandler.StaticMethod));
}

//todo: fix
//[Fact]
//public void NullDelegateThrows()
Expand Down
23 changes: 21 additions & 2 deletions Source/Prism/Events/DelegateReference.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public DelegateReference(Delegate @delegate, bool keepReferenceAlive)
_delegateType = @delegate.GetType();
}
}

/// <summary>
/// Gets the <see cref="Delegate" /> (the target) referenced by the current <see cref="DelegateReference"/> object.
/// </summary>
Expand All @@ -59,7 +59,26 @@ public Delegate Target
}
}
}


/// <summary>
/// Checks if the <see cref="Delegate" /> (the target) referenced by the current <see cref="DelegateReference"/> object are equal to another <see cref="Delegate" />.
/// This is equivalent with comparing <see cref="Target"/> with <paramref name="delegate"/>, only more efficient.
/// </summary>
/// <param name="delegate">The other delegate to compare with.</param>
/// <returns>True if the target referenced by the current object are equal to <paramref name="delegate"/>.</returns>
public bool TargetEquals(Delegate @delegate)
{
if (_delegate != null)
{
return _delegate == @delegate;
}
if (@delegate == null)
{
return !_method.IsStatic && !this._weakReference.IsAlive;
}
return _weakReference.Target == @delegate.Target && Equals(this._method, @delegate.GetMethodInfo());
}

private Delegate TryGetDelegate()
{
if (_method.IsStatic)
Expand Down
12 changes: 4 additions & 8 deletions Source/Wpf/Prism.Wpf/Events/WeakDelegatesManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,15 @@ public void AddListener(Delegate listener)

public void RemoveListener(Delegate listener)
{
this.listeners.RemoveAll(reference =>
{
//Remove the listener, and prune collected listeners
Delegate target = reference.Target;
return listener.Equals(target) || target == null;
});
//Remove the listener, and prune collected listeners
this.listeners.RemoveAll(reference => reference.TargetEquals(null) || reference.TargetEquals(listener));
}

public void Raise(params object[] args)
{
this.listeners.RemoveAll(listener => listener.Target == null);
this.listeners.RemoveAll(listener => listener.TargetEquals(null));

foreach (Delegate handler in this.listeners.ToList().Select(listener => listener.Target).Where(listener => listener != null))
foreach (Delegate handler in this.listeners.Select(listener => listener.Target).Where(listener => listener != null).ToList())
{
handler.DynamicInvoke(args);
}
Expand Down