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

OperatorSingle should request exactly what it needs #2860

Merged

Conversation

davidmoten
Copy link
Collaborator

I noticed that observable.single().subscribe() requested Long.MAX_VALUE from observable which is more than it needs to come up with one item or an error. This PR sets a Producer for OperatorSingle so that it requests no more or less than it needs.

// should request at most 2 in total to complete with no
// items, emit the single item or decide that there are
// too many items.
long numToRequest = Math.min(2 - requested.get(), n);
Copy link
Member

Choose a reason for hiding this comment

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

Why not request(2) once in this request()? I.e.,

public void request(long n) {
  if (n > 0 && once.compareAndSet(false, true)) {
    parent.requestMore(2);
  }
}

@davidmoten
Copy link
Collaborator Author

yep, indeed. I got a bit caught up preserving the existing logic. I'll clean it up.

@davidmoten davidmoten force-pushed the OperatorSingle-request-accounting branch from a73044a to f8cee0a Compare April 7, 2015 11:04
@davidmoten davidmoten force-pushed the OperatorSingle-request-accounting branch from f8cee0a to d51763d Compare April 7, 2015 11:16
@davidmoten
Copy link
Collaborator Author

there's a bit of a cleanup, have another look, thanks.

@akarnokd
Copy link
Member

akarnokd commented Apr 7, 2015

Thanks.

akarnokd added a commit that referenced this pull request Apr 7, 2015
…nting

OperatorSingle should request exactly what it needs
@akarnokd akarnokd merged commit 3a7dca9 into ReactiveX:1.x Apr 7, 2015
@benjchristensen benjchristensen mentioned this pull request Apr 9, 2015
@davidmoten davidmoten deleted the OperatorSingle-request-accounting branch April 10, 2015 04:34
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants