Continuation on heap vs thread context switch

I've just had a look at Swift concurrency: Behind the scenes - WWDC21 - Videos - Apple Developer and after reading about the mechanism for continuation, i've got a question :

From what in understood (things are still a bit blurry, i may be confused) :

  • GCD works with thread context switching, which IIRC, push & pop thread contexts to and from the heap whenever a switch occurs.

  • async/await working with continuations also saves data to & from the heap (into the thread stack) at every suspension points.

My question is this : what makes the second mechanism inherently more efficient ?

I am not a Darwin kernel engineer, so take my answer with a grain of salt.

Thread Switching involves the kernel, which requires costly context switches and a bunch of busywork which may or may not be needed, but has to be done.
Async/Await Tasks are scheduled & switched without having to hop into the kernel, and therefore are much faster & efficient in terms of CPU cycles & memory usage.

1 Like