Skip to content

Commit fc5326a

Browse files
committed
Delay append array creation until the array is really needed
1 parent 5e34467 commit fc5326a

File tree

1 file changed

+9
-15
lines changed

1 file changed

+9
-15
lines changed

Rx.NET/Source/src/System.Reactive/Linq/Observable/AppendPrepend.cs

Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -185,10 +185,11 @@ public IAppendPrepend Prepend(TSource value)
185185
internal sealed class _ : IdentitySink<TSource>
186186
{
187187
private readonly IObservable<TSource> _source;
188-
private readonly TSource[] _appends;
188+
private readonly Node<TSource> _appends;
189189
private readonly IScheduler _scheduler;
190190

191191
private Node<TSource> _currentPrependNode;
192+
private TSource[] _appendArray;
192193
private int _currentAppendIndex;
193194
private volatile bool _disposed;
194195

@@ -198,11 +199,7 @@ public _(Recursive parent, IObserver<TSource> observer)
198199
_source = parent._source;
199200
_scheduler = parent.Scheduler;
200201
_currentPrependNode = parent._prepends;
201-
202-
if (parent._appends != null)
203-
{
204-
_appends = parent._appends.ToReverseArray();
205-
}
202+
_appends = parent._appends;
206203
}
207204

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

287-
var current = _appends[_currentAppendIndex];
285+
var current = _appendArray[_currentAppendIndex];
288286
ForwardOnNext(current);
289287

290288
_currentAppendIndex++;
291289

292-
if (_currentAppendIndex == _appends.Length)
290+
if (_currentAppendIndex == _appendArray.Length)
293291
{
294292
ForwardOnCompleted();
295293
}
@@ -350,7 +348,7 @@ internal sealed class _ : IdentitySink<TSource>
350348
{
351349
private readonly IObservable<TSource> _source;
352350
private readonly Node<TSource> _prepends;
353-
private readonly TSource[] _appends;
351+
private readonly Node<TSource> _appends;
354352
private readonly ISchedulerLongRunning _scheduler;
355353

356354
private IDisposable _schedulerDisposable;
@@ -360,12 +358,8 @@ public _(LongRunning parent, IObserver<TSource> observer)
360358
{
361359
_source = parent._source;
362360
_scheduler = parent._longRunningScheduler;
363-
364361
_prepends = parent._prepends;
365-
if (parent._appends != null)
366-
{
367-
_appends = parent._appends.ToReverseArray();
368-
}
362+
_appends = parent._appends;
369363
}
370364

371365
public void Run()
@@ -423,7 +417,7 @@ private void PrependValues(ICancelable cancel)
423417

424418
private void AppendValues(ICancelable cancel)
425419
{
426-
var array = _appends;
420+
var array = _appends.ToReverseArray();
427421
var i = 0;
428422

429423
while (!cancel.IsDisposed)

0 commit comments

Comments
 (0)