
The battle of the event loops Ujjwal Sharma (@ryzokuken) featuring Olga Kobets (@homyasusina) 1 Ujjwal Sharma (he/him) • Compilers Hacker at Igalia • Node.js Core Collaborator • TC39 Delegate • Work on V8 and Cranelift (Spidermonkey/wasmtime) • Student • Speaker 2 @ryzokuken ft. @homyasusina Барсик B1000 3 @ryzokuken ft. @homyasusina The event loop has to be one of the most talked about subjects in JavaScript 4 @ryzokuken ft. @homyasusina Let’s dig a little deeper. 5 @ryzokuken ft. @homyasusina Section I Concurrency 6 @ryzokuken ft. @homyasusina Concurrency vs Parallelism Concurrency 7 @ryzokuken ft. @homyasusina Concurrency /kənˈkʌr(ə)nsi/ noun When two or more tasks can start, run, and complete in overlapping time periods. Concurrency 8 @ryzokuken ft. @homyasusina Example: multitasking on a single-core machine Concurrency 9 @ryzokuken ft. @homyasusina Concurrency 10 @ryzokuken ft. @homyasusina Parallelism /ˈparəlɛlɪzəm/ noun The state of being parallel or of corresponding in some way. Concurrency 11 @ryzokuken ft. @homyasusina Example: the Greek thinkers used to believe in the parallelism of microcosm and macrocosm Concurrency 12 @ryzokuken ft. @homyasusina � Concurrency 13 @ryzokuken ft. @homyasusina Parallelism /ˈparəlɛlɪzəm/ noun When tasks literally run at the same time. Concurrency 14 @ryzokuken ft. @homyasusina Example: a multicore processor Concurrency 15 @ryzokuken ft. @homyasusina Concurrency 16 @ryzokuken ft. @homyasusina If computation is said to be concurrent, then it doesn't necessarily dictate how the concurrency is achieved under the hood. Concurrency 17 @ryzokuken ft. @homyasusina JavaScript is single-threaded Concurrency 18 @ryzokuken ft. @homyasusina V8 is single-threaded Concurrency 19 @ryzokuken ft. @homyasusina “There are many who pretend to despise and belittle that which is beyond their reach.” – Aesop (Aesop’s Fables) Concurrency 20 @ryzokuken ft. @homyasusina JavaScript does not need multithreading Concurrency 21 @ryzokuken ft. @homyasusina Two main reasons for operations to be time-consuming: 1. Operations that perform heavy computation. 2. Operations that depend on something. Concurrency 22 @ryzokuken ft. @homyasusina Two main reasons for operations to be time-consuming: 1. Operations that require CPU time. 1. Operations that perform heavy computation. 2. Operations that wait for something. 2. Operations that depend on something. Concurrency 23 @ryzokuken ft. @homyasusina 99% of all applications do nothing 99% of the time Concurrency 24 @ryzokuken ft. @homyasusina Multithreading is useful when 1. Significant CPU time is required. 2. Need to call an awkward synchronous (blocking) API. Concurrency 25 @ryzokuken ft. @homyasusina Node.js const cluster = require("cluster") const workers = require("worker_threads") Concurrency 26 @ryzokuken ft. @homyasusina Deno const worker = new Worker(...) Concurrency 27 @ryzokuken ft. @homyasusina So how do you use single-threaded concurrency in the real world? Concurrency 28 @ryzokuken ft. @homyasusina Section II Asynchronous Programming 29 @ryzokuken ft. @homyasusina Asynchrony /eɪˈsɪŋ krəˌni/ noun The occurrence of events independent of the main program flow and ways to deal with such events. Asynchronous Programming 30 @ryzokuken ft. @homyasusina Event-driven programming is by far the most popular paradigm to achieve asynchrony Asynchronous Programming 31 @ryzokuken ft. @homyasusina Green Threads is a popular alternative Asynchronous Programming 32 @ryzokuken ft. @homyasusina We’re not the first ones to use event-driven systems to build web servers Asynchronous Programming 33 @ryzokuken ft. @homyasusina • .NET (C#) • Spark (Java) • Twisted (Python) • Express (JavaScript) • Vapor (Swift) • Rocket (Rust) Asynchronous Programming 34 @ryzokuken ft. @homyasusina JavaScript has a concurrency model based on an event loop Asynchronous Programming 35 @ryzokuken ft. @homyasusina Asynchronous Programming 36 @ryzokuken ft. @homyasusina What the heck is the event loop anyway? | Philip Roberts | JSConf EU Asynchronous Programming 37 @ryzokuken ft. @homyasusina Иван Тулуп: асинхронщина в JS под капотом / Михаил Башуров (Luxoft) Asynchronous Programming 38 @ryzokuken ft. @homyasusina But the “event loop” is a theoretical model Asynchronous Programming 39 @ryzokuken ft. @homyasusina Section III Event Loops 40 @ryzokuken ft. @homyasusina poll and select • History: Introduced in the ~80s-90s (old). • Functionality: More or less the same (boring). • Speed: Perform similarly on benchmarks (slow). 1983 • Portability: Everywhere (nice). • Complexity: As simple as it gets (neat). Event Loops 41 @ryzokuken ft. @homyasusina Result: libevent 2000 Event Loops 42 @ryzokuken ft. @homyasusina People loved poll • epoll • /dev/poll • kqueue • pollset • inotify Event Loops 43 @ryzokuken ft. @homyasusina Result: libevent 2003 Event Loops 44 @ryzokuken ft. @homyasusina Result: libevent* 2005 * Slightly Faster Event Loops 45 @ryzokuken ft. @homyasusina Problem: libevent is too… Event Loops 46 @ryzokuken ft. @homyasusina Problem: libevent is too… bloated Event Loops 47 @ryzokuken ft. @homyasusina Result: libev 2007 Event Loops 48 @ryzokuken ft. @homyasusina Event Loops 49 @ryzokuken ft. @homyasusina Node Standard Library Node Bindings Thread Pool Event Loop V8 (libeio) (libev) Event Loops 50 @ryzokuken ft. @homyasusina Narrator: There was a problem. Event Loops 51 @ryzokuken ft. @homyasusina Event Loops 52 @ryzokuken ft. @homyasusina Enter the Dragon Event Loops 53 @ryzokuken ft. @homyasusina Enter the Unicorn Velociraptor 2013 Event Loops 54 @ryzokuken ft. @homyasusina Event Loops 55 @ryzokuken ft. @homyasusina LXJS 2012 - Bert Belder - libuv Event Loops 56 @ryzokuken ft. @homyasusina Node Standard Library Node Bindings Thread Pool Event Loop V8 (libeio) (libev) Event Loops 57 @ryzokuken ft. @homyasusina Node Standard Library Node Bindings V8 libuv Event Loops 58 @ryzokuken ft. @homyasusina Event Loops 59 @ryzokuken ft. @homyasusina Section IV Into the boxing ring 60 @ryzokuken ft. @homyasusina The lifesaver: autocannon • wrk and wrk2 • “It’s just JavaScript” • “It just works” • mcollina is a legend • TCP? • Fake TCP? Into the boxing ring 61 @ryzokuken ft. @homyasusina Introducing gandiva Into the boxing ring 62 @ryzokuken ft. @homyasusina Let the benchmarking begin! Into the boxing ring 63 @ryzokuken ft. @homyasusina Section V Conclusion 64 @ryzokuken ft. @homyasusina Conclusion 1: tokio is slow Conclusion 65 @ryzokuken ft. @homyasusina Conclusion 66 @ryzokuken ft. @homyasusina Conclusion 2: deno is slow Conclusion 67 @ryzokuken ft. @homyasusina Deno's design is different than Node's in that all native calls are done through zero-copy message passing. This allows for a more uniform bindings, where we have centralised understanding of all calls being made out of the VM. – Ryan Dahl Conclusion 68 @ryzokuken ft. @homyasusina Ultimately we expect this design to result in better performance, but we're not there yet. Deno's networking is about 50% the speed of Node v13. Follow our progress at https:// deno.land/benchmarks – Ryan Dahl Conclusion 69 @ryzokuken ft. @homyasusina Conclusion 3: people are still reluctant Conclusion 70 @ryzokuken ft. @homyasusina Conclusion 71 @ryzokuken ft. @homyasusina But it’s getting there! Conclusion 72 @ryzokuken ft. @homyasusina Introducing Deno 1.0 Conclusion 73 @ryzokuken ft. @homyasusina Special Thanks • Artem Kobzar • Ryan Dahl • Olga Kobets • Kamil Mysliwiec Conclusion 74 @ryzokuken ft. @homyasusina спасибо! Conclusion 75 @ryzokuken ft. @homyasusina.
Details
-
File Typepdf
-
Upload Time-
-
Content LanguagesEnglish
-
Upload UserAnonymous/Not logged-in
-
File Pages75 Page
-
File Size-