KotlinConf 2017 - Deep Dive into Coroutines on JVM by Roman Elizarov

preview_player
Показать описание
In this talk, we perform a deep dive into the design and implementation of Kotlin coroutines for those who like to understand it down to the bottom.

What were the design goals of the Kotlin coroutines and how are they implemented on JVM? What is the difference between CPS and CSP? What concept stands behind the suspending functions in Kotlin and how are they represented on JVM? What exactly happens when you launch coroutines and how much actual overhead do they have? Can you use Kotlin coroutines from Java?

This talk answers these and other coroutine-related questions, showing and explaining the corresponding byte code that is produced by the Kotlin compiler with code samples and benchmarks.

Roman Elizarov is a professional software developer with more than 16 years experience. He started his career at Devexperts, where he designed and developed high-performance trading software for leading brokerage firms and market data delivery services that routinely handle millions of events per second. He is an expert in Java and JVM, particularly in real-time data processing, algorithms and performance optimizations for modern architectures.

Roman currently works on the Kotlin language at JetBrains. Having worked on very large systems comprising of many modules written in different languages, he has a dream of a single language that can be used to write all parts of a very large distributed system, to share and reuse data models and algorithms without friction. Kotlin with JVM and JS backends and the upcoming native compiler is the ideal candidate to realize this dream.

In 2000 Roman had graduated from St. Petersburg ITMO. He now teaches a course on concurrent and distributed programming in ITMO. During his undergraduate study, he participated at ACM International Collegiate Programming Contest (ICPC). Since 1997 and until now Roman serves as a Chief Judge of Northeastern European Regional Programming Contest (NEERC) of ACM ICPC.
Рекомендации по теме
Комментарии
Автор

Continuation Passing Style (CPS) [2:06]
Direct Style [2:33]
Continuation-Passing Style [3:08]
CPS == Callbacks
Coroutines Direct Style [3:52]
How does it work? [4:15]
Kotlin suspending functions [4:27]
CPS Transformation [4:36]
Continuation [5:03]

Direct to CPS [6:16]
Direct code [6:28]
Continuations [6:34]
Callbacks? [7:01]
Labels [7:21]
State [8:11]
CPS Transform [8:38]
Save state [9:15]
Callback [9:44]
Restore state [10:53]
Continue [11:18]
State Machine vs Callbacks [11:39]
SM: Reuse closure/state object
CB: Create new closure
(C#, JavaScript do it in state machines)
SM: Easy loops and higher-order functions

Integration [13:37]
Zoo of futures on JVM [13:55]
Callbacks everywhere [15:44]
Install callback [18:05]
Analyze response [18:25]
Out-of-the box integrations [19:14]

Coroutine context [20:23]
What thread it resumes on? [21:14]
Continuation Interceptor [22:42]
Dispatched continuation [23:45]

Starting coroutines [24:36]
Coroutine builder [25:00]

Job cancellation [28:17]
Launch coroutine builder [29:24]
Launching coroutine [29:38]
Job [29:58]
Using coroutine context [30:07]
Timeouts [30:53]
Cooperative cancellation [31:21]
(Java Thread.stop() story ~[33:15])
(example) [33:29]
while (isActive)
delay()
Cancellable suspension [34:32]
Cancellable continuation [35:08]
Completion handler [35:15]

Communicating Sequential Processes (CSP) [36:32]
Shared Mutable State [37:29]
The choice [37:47]
Example [38:53]
(Channel)
Demo [41:06] (failed)

Actors [42:19]
The choice [42:34]
CSP -> named channels
Actor -> named coroutine & inbox channel
Example [43:26]

References [44:44]
Guide to kotlinx.coroutines by example

zhouyuan
Автор

This is a fantastic talk. Roman is super smart and is good at explaining things simply.

RobertBMenke
Автор

Very well done, seems that with Kotlin the official explanation is usually the simplest and best :)

bruuhhhhhhhhhhh
Автор

I liked this talk. The lecturer explains not only how to program, but also how things work, and in a step by step manner.

asyt
Автор

Roman was awesome discussion...i enjoyed every little explaination of CPS and coroutines internal working.

shubhamsonicse
Автор

Excellent talk! Presents complex topics in an accessible manner, with well thought out transitions from concept to concept.

WaitButHow
Автор

Nice talk. Unfortunately it leaves me with more questions than answers. Just to name a few:
1) Coroutines need to be "hooked up" somewhere. In JavaScript, it's the browser itself. In Kotlin, I would assume that there is some static "context holder"object to which every new coroutine is passed, such that it can be triggered from there once the continuation condition is met. Where is this central static place?
2) How exactly does the "delay(...)" function work? To my knowledge, you cannot delay anything in Java per se, except by either burning CPU cycles with busy waiting, using Thread.sleep() or Object.wait() / Object.notify(). How exactly does this work?
3) If you invoke an inherently blocking operation from a given API (e.g. load text from file), how do you do so without "sacrificing" a thread that is being blocked? The coroutines claim that they can do hundreds of thousands of such operations, without each of them requiring a thread. Which bears the question: how?

Roman said that there is no magic involved. It still looks like plenty of magic to me.

AlanDarkworld
Автор

My feeling while watching this video:
The world is full of genius people. It's an amazing feeling to live with them.

mehtazsazid
Автор

Brilliant talk ! Loved it ! Makes it all very clear

vipullal
Автор

I think `cancel` should be called before `join`? at 29:50

zekininadresi
Автор

Like almost 15 years ago, we created tools to convert the diagrams logic to c++ using this context/label approach to mimic the multi-threads to handle the concurrent calls in the telecommunication systems.

SuperCatt-Boss
Автор

Amazing, it makes things clearer to me.
Sometimes we don't have idea how things works behind the hood.

sergiomendes
Автор

insightful and very easy to comprehend talk. thanks

fgngejw
Автор

I don't like it, seems like overengineering to me. It looks the same as the other approaches - async/await, Futures, callbacks, ...done over again in a more confusing way :S In his other presentation, he said asynchronous code needs to be explicit to be understood and handled well. All I get from coroutines is a headache. I would much rather create my own thread pool manually and submit Runnables to it, catching exceptions by myself. That or use the C# style with async and await, that looks much simpler :)

whiteyoghurt
Автор

kotlin coroutine has dumb naming. "suspend" and then "await()". Hella dumb.

crazieeez