https://kotlinlang.org logo
#coroutines
Title
# coroutines
t

thana

03/26/2020, 10:02 AM
i feel that coroutines have the great disadvantage, that ehy cmopletly spoil the stack traces. so currenyly im profiling an application and all i see is that
invokeSuspend
takes a considerbale amount of time. How do you cope with this situation? can we expect the situation will improve when the jvm itself supports suspendable methods?
k

kevinherron

03/26/2020, 12:08 PM
t

thana

03/26/2020, 12:55 PM
yes, but all it does in my eys is slightly improving the situation by glueing together some stack traces. In fact they are still spoiled because the whole logic is rewritten by coroutines
d

Dico

03/26/2020, 7:10 PM
Are you suggesting that this problem is unique to coroutines?
t

thana

03/27/2020, 8:08 AM
yes
g

gildor

03/29/2020, 1:03 PM
Project Loom and JVM support of coroutines most probably will be indifferent for Kotlin Coroutines, Kotlin Coroutines are compile time transformation, so there is no runtime magic. But for sure some optimization may be done in future It's indeed problem how to profile dispatching of asynchronous code on coroutines, but you should try to investigate and report your case if you see some particular problem in implementation The easiest solution is to use blocking code for hot spots of your app, do less dispatching, it's also true for any asynchronous or multithread code, do less dispatching for critical parts of the code, less context switching makes it a lot more efficient
e

elizarov

03/30/2020, 7:54 AM
@thana Can you provide a self-contained benchmark that demonstrates your problem? So far, our experience was quite positive in figure out how to optimize when we were benchmarking our code.
t

thana

03/30/2020, 8:19 AM
@elizarov most probably i'm just confused by the code, the compiler generates in regard with coroutines. so in the end it might be a problem one gets used with. so i think i am hoping for answers like "yes that's confusing but if you put a filter on methods X and Y everything gets much cleaner" or something similar 😉
e

elizarov

03/30/2020, 8:20 AM
If you can share the code, I'm sure we'll be able to come with an advise on how to improve it. So far your topic-starter question is non-actionable.
t

thana

03/30/2020, 8:38 AM
im afraid the actual code doesnt exist any more. in our case we were iterating a
Sequence
and doing some havy stuff on the items. The profiler revealed there was a huhge amount of time spent in
invokeSuspend
and we were struggeling wether it was really a problem in the
Sequence
itself or just coincidence
e

elizarov

03/30/2020, 8:56 AM
What kind of profiler you were using?
t

thana

03/30/2020, 9:04 AM
jprofiler's cpu profiler
e

elizarov

03/30/2020, 9:05 AM
It is highly likely that it was just a coincidence (google for "safepoint bias")
t

thana

03/30/2020, 9:21 AM
thank you!
7 Views