-
Notifications
You must be signed in to change notification settings - Fork 751
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
4.x: Improve the performance of Range() #684
Conversation
public _(Range parent, IObserver<int> observer) | ||
int _index; | ||
|
||
IDisposable _task; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Unfortunately, the _upstream
is not accessible so it can't be used to replace a previous task.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I forgot to dispose this. I'll update the PR shortly with a fresh benchmark as well.
_start = parent._start; | ||
_count = parent._count; | ||
_index = start; | ||
_end = start + count; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Calculate the end index once (exclusive).
var longRunning = scheduler.AsLongRunning(); | ||
if (longRunning != null) | ||
var first = scheduler.Schedule(this, (innerScheduler, @this) => @this.LoopRec(innerScheduler)); | ||
Disposable.TrySetSingle(ref _task, first); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When the first schedule returns, a subsequent schedule might be underway with _task
set on it. This will avoid overwriting that newer IDisposable
.
Disposable.TrySetSingle(ref _task, first); | ||
} | ||
|
||
private IDisposable LoopRec(IScheduler scheduler) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No need to pass around an index state as it can be safely stored in a field. The inner schedulers guarantee there is only one LoopRec
accessing it at a time.
else | ||
_index = idx + 1; | ||
ForwardOnNext(idx); | ||
var next = scheduler.Schedule(this, (innerScheduler, @this) => @this.LoopRec(innerScheduler)); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why not return next
and use Schedule(TState, Func<IScheduler, TState, IDisposable>)
in run? There is a surprising behavior associated with the default CurrentThreadScheduler
. Each schedule call will create a ScheduledItem
which links to a previous ScheduledItem
through its _disposable
field due to recursion. This can create a very long chain of linked ScheduledItem
s. When the range ends, the Dispose()
will then try to walk that linked list which ends up overflowing the call stack.
This approach in the PR will keep reference only to the latest scheduled task while not linking the internal ScheduledItem
s together. I believe the original Action<Action<TState>>
-based version did this basically with the help of that particular extension method, but with more overhead.
Improvements:
|
This PR improves the performance and reduces allocations in
Range()
as well as splits the operator to recursive and non-recursive variants due to a workaround required in the recursive version. See further explanations in the diff.The PR also adds a benchmark for verifying the gains: