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

d.medina

06/01/2020, 3:45 PM
I am getting a but confused about how suspend works, if i call a none suspend function from a suspend function is it suspendable or do all the functions need to be marked as suspend?
z

zak.taccardi

06/01/2020, 3:46 PM
a non suspend function can be called from a suspending or non-suspending context
d

d.medina

06/01/2020, 3:51 PM
I understand that What i am wondering is is there a performance difference from calling a suspend fn that calls non-suspending functions inside of it vs it having suspending functions. from what i understand there is not, but a colleague of mine with c# background disagrees.
z

Zach Klippenstein (he/him) [MOD]

06/01/2020, 3:55 PM
I believe a suspend function that makes zero suspend calls will not have a state machine class generated for it. If it makes at least one suspending call, the state machine will be generated. But the state machine code is pretty efficient, unlikely to have any significant performance impact in most cases.
e

elizarov

06/01/2020, 3:58 PM
As a matter of style you don’t have to mark all your functions as
suspend
You only mark those functions as
suspend
that perform some long-running things and wait for them (like sending requests to server, etc). This way, your code and architecture because easier to read and to understand as there is a clear separation between fast local and potentially very slow remote computations.
K 8
❤️ 7
👍🏼 12
3 Views