ErnestV
ErnestV

Reputation: 137

Isn't stacking suspend'able functions a performance killer?

While diving into Android/Kotlin development, I learned that the suspend keyword transforms the function into a state machine that can be removed from its executing thread, put on hold, and resumed at some time in the future, by either the same thread or another.

As I see it, a state machine brings some overhead in code, and execution time. Which of course is necessary and not really a problem.

However, in some existing code I am looking into, I find that some suspend function is calling other functions that suspend, like here:

Coroutine:
override suspend fun doWork() : <T> { calls A::methodA }
class A { suspend fun methodA() : <T> { calls B::methodB }
class B { suspend fun methodB() : <T> { performs some network I/O }

If I am right this would create three suspendable state machines. Woudln't it be better to have the suspendkeyword only at the doWork() Coroutine implementation? Or would I lose some threading flexibility (which I doubt) ?

Upvotes: 1

Views: 38

Answers (0)

Related Questions