Skip to content

Commit

Permalink
Delay append array creation until the array is really needed
Browse files Browse the repository at this point in the history
  • Loading branch information
quinmars committed Jul 5, 2018
1 parent 5e34467 commit fc5326a
Showing 1 changed file with 9 additions and 15 deletions.
24 changes: 9 additions & 15 deletions Rx.NET/Source/src/System.Reactive/Linq/Observable/AppendPrepend.cs
Original file line number Diff line number Diff line change
Expand Up @@ -185,10 +185,11 @@ public IAppendPrepend Prepend(TSource value)
internal sealed class _ : IdentitySink<TSource>
{
private readonly IObservable<TSource> _source;
private readonly TSource[] _appends;
private readonly Node<TSource> _appends;
private readonly IScheduler _scheduler;

private Node<TSource> _currentPrependNode;
private TSource[] _appendArray;
private int _currentAppendIndex;
private volatile bool _disposed;

Expand All @@ -198,11 +199,7 @@ public _(Recursive parent, IObserver<TSource> observer)
_source = parent._source;
_scheduler = parent.Scheduler;
_currentPrependNode = parent._prepends;

if (parent._appends != null)
{
_appends = parent._appends.ToReverseArray();
}
_appends = parent._appends;
}

public void Run()
Expand Down Expand Up @@ -230,6 +227,7 @@ public override void OnCompleted()
}
else
{
_appendArray = _appends.ToReverseArray();
//
// We never allow the scheduled work to be cancelled. Instead, the _disposed flag
// is used to have LoopRec bail out and perform proper clean-up of the
Expand Down Expand Up @@ -284,12 +282,12 @@ private IDisposable AppendValues(IScheduler scheduler)
return Disposable.Empty;
}

var current = _appends[_currentAppendIndex];
var current = _appendArray[_currentAppendIndex];
ForwardOnNext(current);

_currentAppendIndex++;

if (_currentAppendIndex == _appends.Length)
if (_currentAppendIndex == _appendArray.Length)
{
ForwardOnCompleted();
}
Expand Down Expand Up @@ -350,7 +348,7 @@ internal sealed class _ : IdentitySink<TSource>
{
private readonly IObservable<TSource> _source;
private readonly Node<TSource> _prepends;
private readonly TSource[] _appends;
private readonly Node<TSource> _appends;
private readonly ISchedulerLongRunning _scheduler;

private IDisposable _schedulerDisposable;
Expand All @@ -360,12 +358,8 @@ public _(LongRunning parent, IObserver<TSource> observer)
{
_source = parent._source;
_scheduler = parent._longRunningScheduler;

_prepends = parent._prepends;
if (parent._appends != null)
{
_appends = parent._appends.ToReverseArray();
}
_appends = parent._appends;
}

public void Run()
Expand Down Expand Up @@ -423,7 +417,7 @@ private void PrependValues(ICancelable cancel)

private void AppendValues(ICancelable cancel)
{
var array = _appends;
var array = _appends.ToReverseArray();
var i = 0;

while (!cancel.IsDisposed)
Expand Down

0 comments on commit fc5326a

Please sign in to comment.