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

Mono.then vs Mono.flatMap #376

Closed
mfinnern opened this issue Jan 18, 2017 · 5 comments
Closed

Mono.then vs Mono.flatMap #376

mfinnern opened this issue Jan 18, 2017 · 5 comments
Labels
for/stackoverflow Questions are best asked on SO or Gitter
Milestone

Comments

@mfinnern
Copy link

Hi,
I found an inconsistency, which is boggling my mind. The usage of the 'then' operator of Mono is (on my machine) 50% slower than the usage of flatmap. I used the following code:

    long start = System.nanoTime();
    for (int i = 0; i < 100000000; i++) {
        Mono.just("").then(x -> Mono.just(x)).block();
    }
    long middle = System.nanoTime();
    for (int i = 0; i < 100000000; i++) {
        Mono.just("").flatMap(x -> Mono.just(x)).blockLast();
    }
    long end = System.nanoTime();
    System.out.println((middle - start) / 1000000);
    System.out.println((end - middle) / 1000000);

I would understand if the flatmap-operator would be slower (since he is more powerful) but I see no rease why the 'then' is the slow one. The speed of 'then' is of no concerning level, but i was curios why there is a separate implementation, when it can substituted with flatmap.

Regards,
Malte

@smaldini
Copy link
Contributor

smaldini commented Jan 18, 2017

I think its down to the implementation of block which we don't try much to optimize. I'd recommend benching using subscribe first, but you can try to tweak the current code using flux().blockLast() on then.

@smaldini smaldini added the for/stackoverflow Questions are best asked on SO or Gitter label Jan 18, 2017
@akarnokd
Copy link
Contributor

This could be also a benchmark-method anomaly. Could you test it with JMH?

I get these:

Benchmark                          Mode  Cnt         Score        Error  Units
MonoThenFlatMap.monoFlatMapFirst  thrpt    5  22729126,809 �  285661,980  ops/s
MonoThenFlatMap.monoFlatMapLast   thrpt    5  24803867,918 �  488264,844  ops/s
MonoThenFlatMap.monoThen          thrpt    5  23197147,462 �  284239,712  ops/s

RxJava 2 for reference:

MonoThenFlatMap.maybeFlatMap      thrpt    5  22777173,379 �  965559,983  ops/s
MonoThenFlatMap.perhapsFlatMap    thrpt    5  13840042,279 � 1904139,288  ops/s
MonoThenFlatMap.singleFlatMap     thrpt    5  19609873,820 �  349511,301  ops/s
MonoThenFlatMap.soloFlatMap       thrpt    5  13921467,776 �  204683,680  ops/s

@mfinnern
Copy link
Author

Hi,
thanks for the responses! I changed the 'block' to 'subscribe' and ran it with JMH
I ran the loop inside a benchmark;

Benchmark                  Mode  Cnt         Score   Error  Units
TestBenchmark.monoFlatmap  avgt    2   5766933,689          us/op
TestBenchmark.monoThen     avgt    2  12872884,277          us/op

The behaviour looks even a bit worse. But when you get equivalent for flatmap and then results, it might be just an anomaly on my system/jvm/etc.

@akarnokd
Copy link
Contributor

Run it with block in JMH.

@mfinnern
Copy link
Author

mfinnern commented Jan 18, 2017

Hi,
i noticed that I was using an old jdk in my current workspace. I switched to the current one, and it was the cause for the difference:

//Old JDK

TestBenchmark.monoFlatmapBlockFirst  avgt    2  7631378,166          us/op
TestBenchmark.monoFlatmapBlockLast   avgt    2  7028633,904          us/op
TestBenchmark.monoThenBlock          avgt    2  8953092,195          us/op

// New JDK

TestBenchmark.monoFlatmapBlockFirst  avgt    2  6273282,912          us/op
TestBenchmark.monoFlatmapBlockLast   avgt    2  5506314,737          us/op
TestBenchmark.monoThenBlock          avgt    2  6254131,172          us/op

So now i get the same results. Thanks and sorry for the trouble!

Regards,
Malte

@smaldini smaldini modified the milestone: 3.0.5.RELEASE Feb 9, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
for/stackoverflow Questions are best asked on SO or Gitter
Projects
None yet
Development

No branches or pull requests

3 participants