Browse Source
Merge pull request #3279 from ahopper/perf-reduce-allocation-in-lightweightobservablebase
Reduce calls to ToArray in LightweightObservableBase PublishNext
pull/3282/head
Benedikt Stebner
6 years ago
committed by
GitHub
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with
35 additions and
6 deletions
-
src/Avalonia.Base/Reactive/LightweightObservableBase.cs
-
tests/Avalonia.Benchmarks/Data/BindingsBenchmark.cs
|
|
|
@ -116,20 +116,33 @@ namespace Avalonia.Reactive |
|
|
|
{ |
|
|
|
if (Volatile.Read(ref _observers) != null) |
|
|
|
{ |
|
|
|
IObserver<T>[] observers; |
|
|
|
|
|
|
|
IObserver<T>[] observers = null; |
|
|
|
IObserver<T> singleObserver = null; |
|
|
|
lock (this) |
|
|
|
{ |
|
|
|
if (_observers == null) |
|
|
|
{ |
|
|
|
return; |
|
|
|
} |
|
|
|
observers = _observers.ToArray(); |
|
|
|
if (_observers.Count == 1) |
|
|
|
{ |
|
|
|
singleObserver = _observers[0]; |
|
|
|
} |
|
|
|
else |
|
|
|
{ |
|
|
|
observers = _observers.ToArray(); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
foreach (var observer in observers) |
|
|
|
if (singleObserver != null) |
|
|
|
{ |
|
|
|
observer.OnNext(value); |
|
|
|
singleObserver.OnNext(value); |
|
|
|
} |
|
|
|
else |
|
|
|
{ |
|
|
|
foreach (var observer in observers) |
|
|
|
{ |
|
|
|
observer.OnNext(value); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
@ -19,6 +19,22 @@ namespace Avalonia.Benchmarks.Data |
|
|
|
instance.Bind(TestClass.IntValueProperty, binding); |
|
|
|
} |
|
|
|
|
|
|
|
[Benchmark] |
|
|
|
public void UpdateTwoWayBinding_Via_Binding() |
|
|
|
{ |
|
|
|
var instance = new TestClass(); |
|
|
|
|
|
|
|
var binding = new Binding(nameof(TestClass.BoundValue), BindingMode.TwoWay) |
|
|
|
{ |
|
|
|
Source = instance |
|
|
|
}; |
|
|
|
|
|
|
|
instance.Bind(TestClass.IntValueProperty, binding); |
|
|
|
for (int i = 0; i < 60; i++) |
|
|
|
{ |
|
|
|
instance.IntValue = i; |
|
|
|
} |
|
|
|
} |
|
|
|
private class TestClass : AvaloniaObject |
|
|
|
{ |
|
|
|
public static readonly StyledProperty<int> IntValueProperty = |
|
|
|
|