~/talk

Coroutines Internals: How The Sausage is Made

Talkat droidcon New York 2024 · 2024-09-19..09-20· [kotlin, concurrency]
speakerdeck:pbreault
slides:none on file

Kotlin suspendable functions compile by adding a Continuation parameter and becoming a suspend functions → state machine that returns Any? (or the sentinel COROUTINE_SUSPENDED) and stores per-state locals inside a generated continuation; continuations wrap caller continuations to continuations wrap to form call stack so resumptions call resumeWith down the chain. Dispatching is a continuation interceptor implemented by dispatchers: main uses an Android Handler, default and IO are backed by the same thread pool (IO starts at 64 threads) and support limited parallelism; examples include reimplementing delay with ScheduledThreadPool and using suspendCancellableCoroutine; compiler optimizations can elide locals and duplicate try/catch across states.

AI-written summary · about

  1. 0:00Introduction
  2. 1:45What makes coroutines lightweight
  3. 3:09Callback and threading problems
  4. 5:15Coroutines vs callbacks
  5. 6:19Delay with continuations
  6. 8:51Compiler transforms suspendable functions
  7. 10:37State machines and continuations
  8. 14:27Preserving state across suspensions
  9. 16:13Debugging and optimizations
  10. 17:35Loops in state machines
  11. 20:04Try-catch in suspendable functions
  12. 20:47Call stack with continuations
  13. 24:40Threading with interceptors
  14. 27:07Dispatchers implementation
  15. 29:37Main dispatcher on Android
  16. 32:50Default and IO dispatchers
  17. 35:20Shared thread pool behavior
  18. 37:04Recap and key takeaways
~/search
type to search 6,249 conferences, 138,987 speakers and 185,172 talks
~/help
cmdctrl k
search every conference, speaker and series
/
focus the filter search
h
home
c
conferences
s
speakers
e
series
t
light / dark — the status bar button does the same
?
this help
esc
close this, then clear every filter — or just the search, while you are typing in it

Ignored while you are typing. Pages without a filter rail have no search to focus.