Skip to content

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

Merged
merged 1 commit into from
Feb 3, 2016
Merged

1.x: Optimizate single just #3642

merged 1 commit into from
Feb 3, 2016

Conversation

Chaoba
Copy link

@Chaoba Chaoba commented Jan 25, 2016

This PR add similar optimization as ScalarSynchronousObservable to Single.just method.

@Chaoba Chaoba changed the title Optimizate single just 1.x: Optimizate single just Jan 25, 2016
public void call(final SingleSubscriber<? super R> child) {

Single<? extends R> o = func.call(value);
if (o.getClass() == ScalarSynchronousSingle.class) {
Copy link
Member

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.

@akarnokd
Copy link
Member

Could you also squash the commits?

@akarnokd
Copy link
Member

In the master, there is a new class SingleSourcePerf. Could you run it before and after to see how well the optimizations work?

@Chaoba
Copy link
Author

Chaoba commented Jan 25, 2016

Ok, I will try to do it.

@Chaoba
Copy link
Author

Chaoba commented Jan 26, 2016

@akarnokd I can't find class SingleSourcePerf in master, do you mean SinglePerfBaseline?

@akarnokd
Copy link
Member

@Chaoba
Copy link
Author

Chaoba commented Jan 26, 2016

I have run the benchmark on 1.x and this PR(Intel Core i5-2400 @3.10GHz, Windows 7 x64, Java 1.8.0_45-b15), the results are as follows:

1.x

20160126163611

this PR

20160126163953

The results show that most of scores in this PR is higher than 1.x, especially that flatmap and flatmapConnst have a great ascension.

@akarnokd
Copy link
Member

👍

@Chaoba
Copy link
Author

Chaoba commented Jan 27, 2016

rebased

if (o instanceof ScalarSynchronousSingle) {
child.onSuccess(((ScalarSynchronousSingle<? extends R>) o).value);
} else {
o.unsafeSubscribe(new Subscriber<R>() {
Copy link
Member

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.

Copy link
Member

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();

@zsxwing
Copy link
Member

zsxwing commented Jan 31, 2016

@Chaoba could you rebase against the master and fix the minor subscription issue? Thanks!

@Chaoba
Copy link
Author

Chaoba commented Feb 1, 2016

@zsxwing Thanks for your comment. Should I squash the commits?

@zsxwing
Copy link
Member

zsxwing commented Feb 1, 2016

@zsxwing Thanks for your comment. Should I squash the commits?

@Chaoba yes, please.

@Chaoba
Copy link
Author

Chaoba commented Feb 1, 2016

The commits has been squashed

@zsxwing
Copy link
Member

zsxwing commented Feb 1, 2016

👍 ping @akarnokd to take a final look and pull in :)

@akarnokd
Copy link
Member

akarnokd commented Feb 1, 2016

👍

@artem-zinnatullin I'd like your opinion on this Single matter.

@@ -1212,4 +1212,18 @@ public void iterableToArrayShouldConvertSet() {
assertSame(s1, singlesArray[0]);
assertSame(s2, singlesArray[1]);
}

@Test
public void testScalarFlatMap() {
Copy link
Contributor

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 :)

@artem-zinnatullin
Copy link
Contributor

@Chaoba please add tests for: scalarMerge, scalarObserveOn and scalarSubscribeOn and squash PR into one commit.

Otherwise looks great, will be happy to see it in next release!

@Chaoba
Copy link
Author

Chaoba commented Feb 2, 2016

add some new testcases, now the coverage is as follows:
20160202112639

@akarnokd
Copy link
Member

akarnokd commented Feb 2, 2016

👍

Single.just(1).flatMap(new Func1<Integer, Single<Integer>>() {
@Override
public Single<Integer> call(Integer v) {
return Single.just(v);
Copy link
Contributor

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 :)

@artem-zinnatullin
Copy link
Contributor

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)

@Chaoba
Copy link
Author

Chaoba commented Feb 3, 2016

ping @artem-zinnatullin to review again please.

@artem-zinnatullin
Copy link
Contributor

👍 thanks a lot, @Chaoba! (Looks like we can merge it now)

akarnokd added a commit that referenced this pull request Feb 3, 2016
@akarnokd akarnokd merged commit 6dc04f1 into ReactiveX:1.x Feb 3, 2016
@stevegury
Copy link
Member

👍

@Chaoba Chaoba deleted the optimizate_single_just branch February 4, 2016 01:58
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.

5 participants