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

perf: use shared, static observables to reduce allocations #1289

Merged
merged 2 commits into from
Feb 24, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
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
12 changes: 6 additions & 6 deletions src/ReactiveUI.Tests/AutoPersistHelperTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public void AutoPersistDoesntWorkOnNonDataContractClasses()

bool shouldDie = true;
try {
fixture.AutoPersist(x => Observable.Return(Unit.Default));
fixture.AutoPersist(x => Observables.Unit);
} catch (Exception) {
shouldDie = false;
}
Expand All @@ -37,7 +37,7 @@ public void AutoPersistHelperShouldntTriggerOnNonPersistableProperties()
var manualSave = new Subject<Unit>();

int timesSaved = 0;
fixture.AutoPersist(x => { timesSaved++; return Observable.Return(Unit.Default); }, manualSave, TimeSpan.FromMilliseconds(100));
fixture.AutoPersist(x => { timesSaved++; return Observables.Unit; }, manualSave, TimeSpan.FromMilliseconds(100));

// No changes = no saving
sched.AdvanceByMs(2 * 100);
Expand All @@ -58,7 +58,7 @@ public void AutoPersistHelperSavesOnInterval()
var manualSave = new Subject<Unit>();

int timesSaved = 0;
fixture.AutoPersist(x => { timesSaved++; return Observable.Return(Unit.Default); }, manualSave, TimeSpan.FromMilliseconds(100));
fixture.AutoPersist(x => { timesSaved++; return Observables.Unit; }, manualSave, TimeSpan.FromMilliseconds(100));

// No changes = no saving
sched.AdvanceByMs(2 * 100);
Expand Down Expand Up @@ -91,7 +91,7 @@ public void AutoPersistHelperDisconnects()
var manualSave = new Subject<Unit>();

int timesSaved = 0;
var disp = fixture.AutoPersist(x => { timesSaved++; return Observable.Return(Unit.Default); }, manualSave, TimeSpan.FromMilliseconds(100));
var disp = fixture.AutoPersist(x => { timesSaved++; return Observables.Unit; }, manualSave, TimeSpan.FromMilliseconds(100));

// No changes = no saving
sched.AdvanceByMs(2 * 100);
Expand Down Expand Up @@ -129,7 +129,7 @@ public void AutoPersistCollectionSmokeTest()
var fixture = new ReactiveList<TestFixture> { item };

int timesSaved = 0;
fixture.AutoPersistCollection(x => { timesSaved++; return Observable.Return(Unit.Default); }, manualSave, TimeSpan.FromMilliseconds(100));
fixture.AutoPersistCollection(x => { timesSaved++; return Observables.Unit; }, manualSave, TimeSpan.FromMilliseconds(100));

sched.AdvanceByMs(2 * 100);
Assert.Equal(0, timesSaved);
Expand Down Expand Up @@ -181,7 +181,7 @@ public void AutoPersistCollectionDisconnectsOnDispose()
var fixture = new ReactiveList<TestFixture> { item };

int timesSaved = 0;
var disp = fixture.AutoPersistCollection(x => { timesSaved++; return Observable.Return(Unit.Default); }, manualSave, TimeSpan.FromMilliseconds(100));
var disp = fixture.AutoPersistCollection(x => { timesSaved++; return Observables.Unit; }, manualSave, TimeSpan.FromMilliseconds(100));

sched.AdvanceByMs(2 * 100);
Assert.Equal(0, timesSaved);
Expand Down
4 changes: 2 additions & 2 deletions src/ReactiveUI.Tests/InteractionsTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -160,8 +160,8 @@ public void HandlersCanContainAsynchronousCode()
});
var handler1B = interaction.RegisterHandler(
x =>
Observable
.Return(Unit.Default)
Observables
.Unit
.Delay(TimeSpan.FromSeconds(1), scheduler)
.Do(_ => x.SetOutput("B")));

Expand Down
2 changes: 1 addition & 1 deletion src/ReactiveUI.Tests/MessageBusTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public void MessageBusSmokeTest()
public void ExplicitSendMessageShouldWorkEvenAfterRegisteringSource()
{
var fixture = new MessageBus();
fixture.RegisterMessageSource(Observable.Never<int>());
fixture.RegisterMessageSource(Observable<int>.Never);

bool messageReceived = false;
fixture.Listen<int>().Subscribe(_ => messageReceived = true);
Expand Down
6 changes: 3 additions & 3 deletions src/ReactiveUI.Tests/ObservableAsPropertyHelperTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ public void OAPHShouldProvideInitialValueImmediatelyRegardlessOfScheduler()
var output = new List<int>();

(new TestScheduler()).With(sched => {
var fixture = new ObservableAsPropertyHelper<int>(Observable.Never<int>(),
var fixture = new ObservableAsPropertyHelper<int>(Observable<int>.Never,
x => output.Add(x), 32);

Assert.Equal(32, fixture.Value);
Expand Down Expand Up @@ -301,8 +301,8 @@ public RaceConditionFixture()
// This triggers the property change firing
// upon subscription in the ObservableAsPropertyHelper
// constructor.
Observable
.Return(true)
Observables
.True
.Do(_ => Count++)
.ToProperty(this, x => x.A, out _A);
}
Expand Down
2 changes: 1 addition & 1 deletion src/ReactiveUI.Tests/ReactiveCollectionTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1719,7 +1719,7 @@ public void TestDelayNotifications()

data = makeAsyncCollection(maxSize);

Observable.Delay(Observable.Return(Unit.Default), TimeSpan.FromMilliseconds(100))
Observable.Delay(Observables.Unit, TimeSpan.FromMilliseconds(100))
.ObserveOn(RxApp.MainThreadScheduler)
.Subscribe(_ => {
using (list.SuppressChangeNotifications()) {
Expand Down
56 changes: 28 additions & 28 deletions src/ReactiveUI.Tests/ReactiveCommandTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public void CreateThrowsIfExecutionParameterIsNull()
[Fact]
public void CanExecuteIsBehavioral()
{
var fixture = ReactiveCommand.Create(() => Observable.Return(Unit.Default));
var fixture = ReactiveCommand.Create(() => Observables.Unit);
var canExecute = fixture
.CanExecute
.CreateCollection();
Expand All @@ -42,7 +42,7 @@ public void CanExecuteIsBehavioral()
public void CanExecuteOnlyTicksDistinctValues()
{
var canExecuteSubject = new Subject<bool>();
var fixture = ReactiveCommand.Create(() => Observable.Return(Unit.Default), canExecuteSubject);
var fixture = ReactiveCommand.Create(() => Observables.Unit, canExecuteSubject);
var canExecute = fixture
.CanExecute
.CreateCollection();
Expand All @@ -63,7 +63,7 @@ public void CanExecuteOnlyTicksDistinctValues()
public void CanExecuteIsFalseIfCallerDictatesAsSuch()
{
var canExecuteSubject = new Subject<bool>();
var fixture = ReactiveCommand.Create(() => Observable.Return(Unit.Default), canExecuteSubject);
var fixture = ReactiveCommand.Create(() => Observables.Unit, canExecuteSubject);
var canExecute = fixture
.CanExecute
.CreateCollection();
Expand All @@ -81,7 +81,7 @@ public void CanExecuteIsFalseIfCallerDictatesAsSuch()
public void CanExecuteIsFalseIfAlreadyExecuting()
{
(new TestScheduler()).With(sched => {
var execute = Observable.Return(Unit.Default).Delay(TimeSpan.FromSeconds(1), sched);
var execute = Observables.Unit.Delay(TimeSpan.FromSeconds(1), sched);
var fixture = ReactiveCommand.CreateFromObservable(() => execute, outputScheduler: sched);
var canExecute = fixture
.CanExecute
Expand All @@ -104,7 +104,7 @@ public void CanExecuteIsFalseIfAlreadyExecuting()
public void CanExecuteIsUnsubscribedAfterCommandDisposal()
{
var canExecuteSubject = new Subject<bool>();
var fixture = ReactiveCommand.Create(() => Observable.Return(Unit.Default), canExecuteSubject);
var fixture = ReactiveCommand.Create(() => Observables.Unit, canExecuteSubject);

Assert.True(canExecuteSubject.HasObservers);

Expand All @@ -117,7 +117,7 @@ public void CanExecuteIsUnsubscribedAfterCommandDisposal()
public void CanExecuteTicksFailuresThroughThrownExceptions()
{
var canExecuteSubject = new Subject<bool>();
var fixture = ReactiveCommand.Create(() => Observable.Return(Unit.Default), canExecuteSubject);
var fixture = ReactiveCommand.Create(() => Observables.Unit, canExecuteSubject);
var thrownExceptions = fixture
.ThrownExceptions
.CreateCollection();
Expand All @@ -132,7 +132,7 @@ public void CanExecuteTicksFailuresThroughThrownExceptions()
public void CanExecuteIsAvailableViaICommand()
{
var canExecuteSubject = new Subject<bool>();
ICommand fixture = ReactiveCommand.Create(() => Observable.Return(Unit.Default), canExecuteSubject);
ICommand fixture = ReactiveCommand.Create(() => Observables.Unit, canExecuteSubject);

Assert.False(fixture.CanExecute(null));

Expand All @@ -147,7 +147,7 @@ public void CanExecuteIsAvailableViaICommand()
public void CanExecuteChangedIsAvailableViaICommand()
{
var canExecuteSubject = new Subject<bool>();
ICommand fixture = ReactiveCommand.Create(() => Observable.Return(Unit.Default), canExecuteSubject);
ICommand fixture = ReactiveCommand.Create(() => Observables.Unit, canExecuteSubject);
var canExecuteChanged = new List<bool>();
fixture.CanExecuteChanged += (s, e) => canExecuteChanged.Add(fixture.CanExecute(null));

Expand All @@ -162,7 +162,7 @@ public void CanExecuteChangedIsAvailableViaICommand()
[Fact]
public void IsExecutingIsBehavioral()
{
var fixture = ReactiveCommand.Create(() => Observable.Return(Unit.Default));
var fixture = ReactiveCommand.Create(() => Observables.Unit);
var isExecuting = fixture
.IsExecuting
.CreateCollection();
Expand All @@ -175,7 +175,7 @@ public void IsExecutingIsBehavioral()
public void IsExecutingTicksAsExecutionsProgress()
{
(new TestScheduler()).With(sched => {
var execute = Observable.Return(Unit.Default).Delay(TimeSpan.FromSeconds(1), sched);
var execute = Observables.Unit.Delay(TimeSpan.FromSeconds(1), sched);
var fixture = ReactiveCommand.CreateFromObservable(() => execute, outputScheduler: sched);
var isExecuting = fixture
.IsExecuting
Expand Down Expand Up @@ -297,7 +297,7 @@ public void ExecutePassesThroughParameter()
var parameters = new List<int>();
var fixture = ReactiveCommand.CreateFromObservable<int, Unit>(param => {
parameters.Add(param);
return Observable.Return(Unit.Default);
return Observables.Unit;
});

fixture.Execute(1).Subscribe();
Expand All @@ -314,7 +314,7 @@ public void ExecutePassesThroughParameter()
public void ExecuteExecutesOnTheSpecifiedScheduler()
{
(new TestScheduler()).With(sched => {
var execute = Observable.Return(Unit.Default).Delay(TimeSpan.FromSeconds(1), sched);
var execute = Observables.Unit.Delay(TimeSpan.FromSeconds(1), sched);
var fixture = ReactiveCommand.CreateFromObservable(() => execute, outputScheduler: sched);
var isExecuting = fixture
.IsExecuting
Expand Down Expand Up @@ -410,7 +410,7 @@ public void ExecuteTicksAnyLambdaException()
public void ExecuteCanBeCancelled()
{
(new TestScheduler()).With(sched => {
var execute = Observable.Return(Unit.Default).Delay(TimeSpan.FromSeconds(1), sched);
var execute = Observables.Unit.Delay(TimeSpan.FromSeconds(1), sched);
var fixture = ReactiveCommand.CreateFromObservable(() => execute, outputScheduler: sched);
var executed = fixture
.CreateCollection();
Expand Down Expand Up @@ -456,7 +456,7 @@ public void ExecuteIsAvailableViaICommand()
var executed = false;
ICommand fixture = ReactiveCommand.Create(() => {
executed = true;
return Observable.Return(Unit.Default);
return Observables.Unit;
});

fixture.Execute(null);
Expand Down Expand Up @@ -494,7 +494,7 @@ public void ExecuteViaICommandThrowsIfParameterTypeIsIncorrect()
public void ResultIsTickedThroughSpecifiedScheduler()
{
(new TestScheduler()).With(sched => {
var fixture = ReactiveCommand.Create(() => Observable.Return(Unit.Default), outputScheduler: sched);
var fixture = ReactiveCommand.Create(() => Observables.Unit, outputScheduler: sched);
var results = fixture
.CreateCollection();

Expand Down Expand Up @@ -988,8 +988,8 @@ public class CombinedReactiveCommandTest
[Fact]
public void CanExecuteIsFalseIfAnyChildCannotExecute()
{
var child1 = ReactiveCommand.Create(() => Observable.Return(Unit.Default));
var child2 = ReactiveCommand.Create(() => Observable.Return(Unit.Default), Observable.Return(false));
var child1 = ReactiveCommand.Create(() => Observables.Unit);
var child2 = ReactiveCommand.Create(() => Observables.Unit, Observables.False);
var childCommands = new[] { child1, child2 };
var fixture = ReactiveCommand.CreateCombined(childCommands);
var canExecute = fixture
Expand All @@ -1003,10 +1003,10 @@ public void CanExecuteIsFalseIfAnyChildCannotExecute()
[Fact]
public void CanExecuteIsFalseIfParentCanExecuteIsFalse()
{
var child1 = ReactiveCommand.Create(() => Observable.Return(Unit.Default));
var child2 = ReactiveCommand.Create(() => Observable.Return(Unit.Default));
var child1 = ReactiveCommand.Create(() => Observables.Unit);
var child2 = ReactiveCommand.Create(() => Observables.Unit);
var childCommands = new[] { child1, child2 };
var fixture = ReactiveCommand.CreateCombined(childCommands, Observable.Return(false));
var fixture = ReactiveCommand.CreateCombined(childCommands, Observables.False);
var canExecute = fixture
.CanExecute
.CreateCollection();
Expand All @@ -1019,8 +1019,8 @@ public void CanExecuteIsFalseIfParentCanExecuteIsFalse()
public void CanExecuteTicksFailuresThroughThrownExceptions()
{
var canExecuteSubject = new Subject<bool>();
var child1 = ReactiveCommand.Create(() => Observable.Return(Unit.Default));
var child2 = ReactiveCommand.Create(() => Observable.Return(Unit.Default));
var child1 = ReactiveCommand.Create(() => Observables.Unit);
var child2 = ReactiveCommand.Create(() => Observables.Unit);
var childCommands = new[] { child1, child2 };
var fixture = ReactiveCommand.CreateCombined(childCommands, canExecuteSubject);
var thrownExceptions = fixture
Expand All @@ -1037,8 +1037,8 @@ public void CanExecuteTicksFailuresThroughThrownExceptions()
public void CanExecuteTicksFailuresInChildCanExecuteThroughThrownExceptions()
{
var canExecuteSubject = new Subject<bool>();
var child1 = ReactiveCommand.Create(() => Observable.Return(Unit.Default));
var child2 = ReactiveCommand.Create(() => Observable.Return(Unit.Default), canExecuteSubject);
var child1 = ReactiveCommand.Create(() => Observables.Unit);
var child2 = ReactiveCommand.Create(() => Observables.Unit, canExecuteSubject);
var childCommands = new[] { child1, child2 };
var fixture = ReactiveCommand.CreateCombined(childCommands);
var thrownExceptions = fixture
Expand All @@ -1054,9 +1054,9 @@ public void CanExecuteTicksFailuresInChildCanExecuteThroughThrownExceptions()
[Fact]
public void ExecuteExecutesAllChildCommands()
{
var child1 = ReactiveCommand.Create(() => Observable.Return(Unit.Default));
var child2 = ReactiveCommand.Create(() => Observable.Return(Unit.Default));
var child3 = ReactiveCommand.Create(() => Observable.Return(Unit.Default));
var child1 = ReactiveCommand.Create(() => Observables.Unit);
var child2 = ReactiveCommand.Create(() => Observables.Unit);
var child3 = ReactiveCommand.Create(() => Observables.Unit);
var childCommands = new[] { child1, child2, child3 };
var fixture = ReactiveCommand.CreateCombined(childCommands);

Expand Down Expand Up @@ -1137,7 +1137,7 @@ public void ResultIsTickedThroughSpecifiedScheduler()
[Fact]
public void ExecuteTicksErrorsInAnyChildCommandThroughThrownExceptions()
{
var child1 = ReactiveCommand.CreateFromObservable(() => Observable.Return(Unit.Default));
var child1 = ReactiveCommand.CreateFromObservable(() => Observables.Unit);
var child2 = ReactiveCommand.CreateFromObservable(() => Observable.Throw<Unit>(new InvalidOperationException("oops")));
var childCommands = new[] { child1, child2 };
var fixture = ReactiveCommand.CreateCombined(childCommands);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public IObservable<bool> GetActivationForView(IActivatable view)
if (control == null) {
// Show a friendly warning in the log that this view will never be activated
this.Log().Warn("Expected a view of type System.Windows.Forms.Control but it is {0}.\r\nYou need to implement your own IActivationForViewFetcher for {0}.", view.GetType());
return Observable.Empty<bool>();
return Observable<bool>.Empty;
}

// Create an observable stream of booleans
Expand Down
2 changes: 1 addition & 1 deletion src/ReactiveUI.Winforms/Winforms/RoutedViewHost.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public RoutedControlHost()
}
}));

this.ViewContractObservable = Observable.Return(default(string));
this.ViewContractObservable = Observable<string>.Default;

var vmAndContract =
this.WhenAnyObservable(x => x.Router.CurrentViewModel)
Expand Down
2 changes: 1 addition & 1 deletion src/ReactiveUI.Winforms/Winforms/ViewModelViewHost.cs
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ IEnumerable<IDisposable> setupBindings()
}
});

this.ViewContractObservable = Observable.Return(default(string));
this.ViewContractObservable = Observable<string>.Default;

var vmAndContract =
this.WhenAny(x => x.ViewModel, x => x.Value)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public IObservable<bool> GetActivationForView(IActivatable view)
GetActivationFor(view as Page) ??
GetActivationFor(view as View) ??
GetActivationFor(view as Cell) ??
Observable.Never<bool>();
Observable<bool>.Never;

return activation.DistinctUntilChanged();
}
Expand Down
2 changes: 1 addition & 1 deletion src/ReactiveUI.XamForms/XamForms/RoutedViewHost.cs
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ public RoutedViewHost()

protected IObservable<Page> PageForViewModel(IRoutableViewModel vm)
{
if (vm == null) return Observable.Empty<Page>();
if (vm == null) return Observable<Page>.Empty;

var ret = ViewLocator.Current.ResolveView(vm);
if (ret == null) {
Expand Down
4 changes: 2 additions & 2 deletions src/ReactiveUI.XamForms/XamForms/ViewModelViewHost.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public IObservable<string> ViewContractObservable {
nameof(ViewContractObservable),
typeof(IObservable<string>),
typeof(ViewModelViewHost),
Observable.Never<string>(),
Observable<string>.Never,
BindingMode.OneWay);

public IViewLocator ViewLocator { get; set; }
Expand All @@ -60,7 +60,7 @@ public ViewModelViewHost()
{
// NB: InUnitTestRunner also returns true in Design Mode
if (ModeDetector.InUnitTestRunner()) {
ViewContractObservable = Observable.Never<string>();
ViewContractObservable = Observable<string>.Never;
return;
}

Expand Down
Loading