No video

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

  Рет қаралды 84,272

JetBrains

JetBrains

Күн бұрын

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.

Пікірлер: 33
@zhou7yuan
@zhou7yuan 5 жыл бұрын
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 github.com/Kotlin/kotlinx.coroutines/blob/master/coroutines-guide.md
@RobertBMenke
@RobertBMenke 6 жыл бұрын
This is a fantastic talk. Roman is super smart and is good at explaining things simply.
@as4yt
@as4yt 5 жыл бұрын
I liked this talk. The lecturer explains not only how to program, but also how things work, and in a step by step manner.
@bruuhhhhhhhhhhh
@bruuhhhhhhhhhhh 6 жыл бұрын
Very well done, seems that with Kotlin the official explanation is usually the simplest and best :)
@mehtazsazid9398
@mehtazsazid9398 Жыл бұрын
My feeling while watching this video: The world is full of genius people. It's an amazing feeling to live with them.
@WaitButHow
@WaitButHow 6 жыл бұрын
Excellent talk! Presents complex topics in an accessible manner, with well thought out transitions from concept to concept.
@webdrizzled
@webdrizzled Жыл бұрын
Roman Elizarov.....This was awesome discussion...i enjoyed every little explaination of CPS and coroutines internal working.
@AlanDarkworld
@AlanDarkworld 6 жыл бұрын
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.
@Warper4Lineage2
@Warper4Lineage2 6 жыл бұрын
1) As far as I know, it's in Kotlin coroutines library. 2) If there is something to do, executor can put your coroutine aside and do something for another coroutine. 3) There are non-blocking counterparts for IO operations en.wikipedia.org/wiki/Non-blocking_I/O_(Java)
@damienstanton
@damienstanton 5 жыл бұрын
Check the doc here: github.com/Kotlin/KEEP/blob/master/proposals/coroutines.md To me, Kotlin's coroutine design models Go's very closely (I am a Go developer).
@mangatraimodi
@mangatraimodi 5 жыл бұрын
@Alan You are right on 3. The thread will be blocked. Coroutines will not help. And this is same in any other library. Coroutines just gives you an easy syntax to compose multiple asynchronous tasks together.
@minhnghia8613
@minhnghia8613 3 жыл бұрын
By "hooked up somewhere", did you mean executing somewhere, like JS message queue? In 12:35, he says that state/closure object is reused. The state instance is passed around. So it doesn't need a central queue to process. I agree that the stack will grow very very quickly, but the actual memory used is not that much, just references. Full disclosure: I'm like 20min into coroutine, and 1-2 month into JS.
@roxferesr
@roxferesr 3 жыл бұрын
2) I think that the implementations on how `delay` works is provided by the underlying Dispatcher. Dispatchers.IO might use a thread pool (of a specific size). Same for Dispatchers.Default, Dispatchers.Main (main thread in android) will probably just post something into the event queue/loop with a delay. A single threaded dispatchers might do something similar (event queue)
@vipullal7689
@vipullal7689 3 жыл бұрын
Brilliant talk ! Loved it ! Makes it all very clear
@SuperCatt0-Boss
@SuperCatt0-Boss 11 ай бұрын
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.
@sergiomendes7727
@sergiomendes7727 Жыл бұрын
Amazing, it makes things clearer to me. Sometimes we don't have idea how things works behind the hood.
@user-fg6ng7ej6w
@user-fg6ng7ej6w 2 жыл бұрын
insightful and very easy to comprehend talk. thanks
@9Blurke9
@9Blurke9 5 ай бұрын
Legendary talk
@AlexeiFando
@AlexeiFando 26 күн бұрын
Another valuable resource for learning about how coroutines work in-depth, is the talk from its co-creator, Roman Elizarov from 2017. kzfaq.info/get/bejne/j9iiiKaEzp2WdWw.html
@StephanOudmaijer
@StephanOudmaijer 3 жыл бұрын
Great explanation. Thanks!
@video-tm4qx
@video-tm4qx 2 жыл бұрын
good explanation 👍
@zekininadresi
@zekininadresi 2 жыл бұрын
I think `cancel` should be called before `join`? at 29:50
@mhdunknown
@mhdunknown Жыл бұрын
No, launch() returns immediately, and the next line will be executed after it returns, which would be cancel() if it was called first, which would cancel the asynchronous work that was started in the coroutine. By calling join() first, we wait until the coroutine is finished before we cancel it.
@whiteyoghurt
@whiteyoghurt Ай бұрын
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 :)
@zekininadresi
@zekininadresi 2 жыл бұрын
good stuff
@robhamk
@robhamk 6 жыл бұрын
where to find the slides
@maxkorotkov
@maxkorotkov 6 жыл бұрын
Try this link: www.slideshare.net/elizarov/deep-dive-into-coroutines-on-jvm-kotlinconf-2017
@LouisCognault
@LouisCognault 6 жыл бұрын
They are all linked on kotlinconf.com/talks/
@SanalDersaneLeventYadrga
@SanalDersaneLeventYadrga Жыл бұрын
37:27
@crazieeez
@crazieeez 6 жыл бұрын
kotlin coroutine has dumb naming. "suspend" and then "await()". Hella dumb.
@savagebp0
@savagebp0 4 жыл бұрын
"suspend" and "await" are two different things. Suspend is modifying your function to be capable of suspending its execution via continuations. Await, on the other hand, describes that a detached asynchronous process is being awaited for by my code. For example, I launched multiple network requests in parallel but my function needs to (a)wait for their values before it can continue so, therefore, my function SUSPENDS and AWAITs for the asynchronous values to become available before resuming execution. They may be new to those that don't have much experience in asynchronous execution paradigms, but they describe exactly what's going on. These keywords weren't invented by the Kotlin language team, just used to accurately describe what the code is doing. Good reads on why "suspend" is accurate: > Coroutines are computer program components that generalize subroutines for non-preemptive multitasking, by allowing execution to be SUSPENDED and resumed. en.wikipedia.org/wiki/Coroutine Good reads on why "await" is accurate: > While await and wait are similar in terms of meaning, they differ in usage. > • Wait can be used without an object, like in the sentence I am waiting. > • Await, meanwhile, requires an object. writingexplained.org/awaiting-vs-waiting-difference > In computer programming, the async/await pattern is a syntactic feature of many programming languages that allows an asynchronous, non-blocking function to be structured in a way similar to an ordinary synchronous function. en.wikipedia.org/wiki/Async/await
@SanalDersaneLeventYadrga
@SanalDersaneLeventYadrga Жыл бұрын
45:17
KotlinConf 2017 - Introduction to Coroutines by Roman Elizarov
45:32
Fortunately, Ultraman protects me  #shorts #ultraman #ultramantiga #liveaction
00:10
奧特羅羅 Ultraman
Рет қаралды 4,4 МЛН
Schoolboy - Часть 2
00:12
⚡️КАН АНДРЕЙ⚡️
Рет қаралды 16 МЛН
Sunglasses Didn't Cover For Me! 🫢
00:12
Polar Reacts
Рет қаралды 5 МЛН
Роман Елизаров - Корутины в Kotlin
59:04
JPoint, Joker и JUG ru
Рет қаралды 63 М.
Andrew Kelley   Practical Data Oriented Design (DoD)
46:40
ChimiChanga
Рет қаралды 74 М.
Coroutines and Loom behind the scenes by Roman Elizarov
45:22
Kotlin by JetBrains
Рет қаралды 31 М.
Understand Kotlin Coroutines on Android (Google I/O'19)
37:49
Android Developers
Рет қаралды 174 М.
Compilers, How They Work, And Writing Them From Scratch
23:53
Adam McDaniel
Рет қаралды 143 М.
The Greenwich Meridian is in the wrong place
25:07
Stand-up Maths
Рет қаралды 707 М.
Fortunately, Ultraman protects me  #shorts #ultraman #ultramantiga #liveaction
00:10
奧特羅羅 Ultraman
Рет қаралды 4,4 МЛН