How suspend Functions Actually Work

Article · 2:00

Continuation-passing style, state machines, and why suspension costs almost nothing.

There is no magic in suspend, and knowing what the compiler does removes most of the confusion around coroutines. A suspend function is compiled into an ordinary function with one extra parameter: a Continuation, which is the callback to invoke when the work resumes. This transformation is called continuation-passing style.

The body is then rewritten as a state machine. Every point where the function can suspend becomes a labelled state. On entry, the generated code switches on the current label and jumps to the right place, so resuming means re-entering the same function and continuing from the stored label rather than unwinding and rebuilding a call stack. Local variables that survive a suspension point are stored as fields on the continuation object.

Two consequences follow. First, a suspension is cheap - no thread is parked, no context is switched at the operating system level, and the cost is roughly one object allocation plus a virtual call. That is why hundreds of thousands of coroutines are practical where hundreds of threads are not.

Second, a suspend function that never actually suspends costs essentially nothing beyond the extra parameter. The generated code returns its value directly rather than returning the COROUTINE_SUSPENDED marker, so the fast path stays fast. This is why marking a function suspend "just in case" is not the performance problem people assume.

// What you write:
suspend fun loadProfile(id: Int): String {
    val user = fetchUser(id)          // suspension point 1
    val photo = fetchPhoto(user)      // suspension point 2
    return "$user / $photo"
}

// Roughly what the compiler produces (simplified pseudo-Kotlin):
//
// fun loadProfile(id: Int, continuation: Continuation<String>): Any? {
//     val state = continuation as? ProfileStateMachine ?: ProfileStateMachine(continuation)
//     when (state.label) {
//         0 -> {
//             state.label = 1
//             val result = fetchUser(id, state)
//             if (result == COROUTINE_SUSPENDED) return COROUTINE_SUSPENDED
//             state.user = result as String          // survives suspension
//         }
//         1 -> { ... resume here, read state.user ... }
//     }
// }

suspend fun fetchUser(id: Int): String = "user-$id"
suspend fun fetchPhoto(user: String): String = "photo-of-$user"

Discussion

No questions on this topic yet. Sign in to ask the first one.