-
Notifications
You must be signed in to change notification settings - Fork 7.6k
1.x: Optimizate single just #3642
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
Conversation
public void call(final SingleSubscriber<? super R> child) { | ||
|
||
Single<? extends R> o = func.call(value); | ||
if (o.getClass() == ScalarSynchronousSingle.class) { |
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.
This can be instanceof
as well.
Could you also squash the commits? |
In the master, there is a new class |
Ok, I will try to do it. |
@akarnokd I can't find class SingleSourcePerf in master, do you mean SinglePerfBaseline? |
👍 |
rebased |
if (o instanceof ScalarSynchronousSingle) { | ||
child.onSuccess(((ScalarSynchronousSingle<? extends R>) o).value); | ||
} else { | ||
o.unsafeSubscribe(new Subscriber<R>() { |
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.
You need to put this new Subscriber
to a new variable, and call child.add(...)
to maintain the subscription chain.
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.
You can use this unit test to verify the behavior:
final Action0 unsubscribe = mock(Action0.class);
Single<Integer> s = Single.create(new OnSubscribe<Integer>() {
@Override
public void call(SingleSubscriber<? super Integer> subscriber) {
subscriber.add(Subscriptions.create(unsubscribe));
}
});
Subscription subscription = Single.merge(Single.just(s)).subscribe();
subscription.unsubscribe();
verify(unsubscribe, times(1)).call();
@Chaoba could you rebase against the master and fix the minor subscription issue? Thanks! |
@zsxwing Thanks for your comment. Should I squash the commits? |
The commits has been squashed |
👍 ping @akarnokd to take a final look and pull in :) |
👍 @artem-zinnatullin I'd like your opinion on this |
@@ -1212,4 +1212,18 @@ public void iterableToArrayShouldConvertSet() { | |||
assertSame(s1, singlesArray[0]); | |||
assertSame(s2, singlesArray[1]); | |||
} | |||
|
|||
@Test | |||
public void testScalarFlatMap() { |
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.
test
prefix is not needed :)
@Chaoba please add tests for: Otherwise looks great, will be happy to see it in next release! |
👍 |
Single.just(1).flatMap(new Func1<Integer, Single<Integer>>() { | ||
@Override | ||
public Single<Integer> call(Integer v) { | ||
return Single.just(v); |
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.
Please return another result, at the moment test can pass even if flatMap
returns same Single
, you can also change type from Integer
to let's say String
during flatMap
:)
Great set of tests! Just few comments left, @Chaoba please ping again once you fix them! (you can squash commits right away so we could merge it ASAP) |
ping @artem-zinnatullin to review again please. |
👍 thanks a lot, @Chaoba! (Looks like we can merge it now) |
👍 |
This PR add similar optimization as ScalarSynchronousObservable to Single.just method.