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

4.x: Use Schedule calls with state #558

Merged
merged 5 commits into from
Jun 1, 2018

Conversation

akarnokd
Copy link
Collaborator

This PR changes most of the no state argument Schedule calls into the state-aware Schedule calls. This should help avoid instantiating a new delegate because of the variable capture.

@@ -56,9 +56,11 @@ protected override IDisposable SubscribeCore(IObserver<TSource> observer)
var d = new SerialDisposable();
d.Disposable = m;

m.Disposable = scheduler.Schedule(() =>
m.Disposable = scheduler.Schedule((scheduler, source, observer, d),
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you are using this overload, the scheduler will be passed. Convention throughout the rest of the code is to name it 'self' (now '_'). You can then omit it from the state-tuple.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was an odd case, wrapping the outer scheduler. I'm not sure if the inner scheduler would be the correct one in this situation.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is generally just the executing one.

@@ -414,7 +414,7 @@ private void DrainQueue(ICancelable cancel)
if (shouldWait)
{
var timer = new ManualResetEventSlim();
_scheduler.Schedule(waitTime, () => { timer.Set(); });
_scheduler.Schedule(timer, waitTime, (_, state) => { state.Set(); return Disposable.Empty; });
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider using the extension I recently added. You may omit the scheduler parameter (_), won't have to return Disposable.Empty everywhere.

Also, please give the inner variable a revealing name. I used to go for 'closureXYZ', e.g. closureTimer in this case. Just 'state' is not revealing.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The problem with that overload is that the parameters are after the lambda, which I find harder to read.

Naming it like closureTimer makes little sense as these are not captured values by the lambda like before, they are now arguments.

Copy link
Collaborator

@danielcweber danielcweber Jun 1, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changing the signature creates ambiguities unfortunately.

If not 'closureTimer', at least something more revealing than 'state'.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll rename these. You could introduce an internal extension under a different name but with the same ordering of arguments.

Copy link
Collaborator

@danielcweber danielcweber Jun 1, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can rename it in a subsequent PR and let ReSharper do the reordering. Would you then use the Action-extension for now?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I find the ordering of the parameters distracting with those Action overloads. I'd prefer this ordering and a renamed internal schedule method.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok I will file a PR with a renamed method and then we can get the Disposable.Empty out of that?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure.

@@ -284,12 +284,19 @@ public IDisposable Connect(IObserver<TArgs> observer)
{
if (--_count == 0)
{
_parent._scheduler.Schedule(_removeHandler.Dispose);
_parent._scheduler.Schedule(_removeHandler, (_, self) => RemoveHandlerDispose(self));
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

'self', throughout calls to Schedule, has bee 'traditionally' used to denote the executing scheduler. If 'this' is passed as state, I like using @this as a name.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll inline the method call back here.

}

private void Invoke()
private IDisposable Invoke()
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As above, consider the new extension and don't repeat returning Disposable.Empty.

@danielcweber danielcweber merged commit 196b172 into dotnet:master Jun 1, 2018
@akarnokd akarnokd deleted the AvoidStatelessSchedule branch June 1, 2018 20:59
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants