Slide 1

Slide 1 text

Dissecting Node.js Understanding the most popular JS runtime... To the bone

Slide 2

Slide 2 text

whoami /khaosdoctor @_staticvoid lsantos.dev /khaosdoctor cloud advocate_ [email protected]

Slide 3

Slide 3 text

obarra.co trainingcenter.io

Slide 4

Slide 4 text

abcdevelopers.org

Slide 5

Slide 5 text

agenda_ - What is JavaScript - What is Node.js - Our little function - How JavaScript works - V8 and its sorceries - V8 compiling pipelines - Putting it all together

Slide 6

Slide 6 text

what is JavaScript?

Slide 7

Slide 7 text

JavaScript The language everyone loves to hate - Prototype-based - Dynamically-typed - Interpreted language - Created in 1995 as a scripting language for Netscape - Created by Brendan Eich - Conforms with ECMA262 (ECMAScript) - Defined/Maintained by TC39

Slide 8

Slide 8 text

what is Node.js?

Slide 9

Slide 9 text

Node.js The angel that brought JS to the world again - JavaScript server-side runtime - Created in 2009 by Ryan Dahl - Later sponsored by Joyent - Event loop - Non blocking I/O - Uses V8

Slide 10

Slide 10 text

No content

Slide 11

Slide 11 text

elements of Node.js_

Slide 12

Slide 12 text

our little function_

Slide 13

Slide 13 text

No content

Slide 14

Slide 14 text

fs is a C++ binding_

Slide 15

Slide 15 text

No content

Slide 16

Slide 16 text

No content

Slide 17

Slide 17 text

the real Node.js_

Slide 18

Slide 18 text

No content

Slide 19

Slide 19 text

No content

Slide 20

Slide 20 text

how JavaScript works?

Slide 21

Slide 21 text

the JS engine_

Slide 22

Slide 22 text

What is an engine? - Runs all JS code we write - Responsible for - parsing and interpreting - memory allocation - the call stack - garbage collection - code optimizations

Slide 23

Slide 23 text

There are many - V8 -> made by Google for Chrome and some Chromium-based browsers - Chakra -> made by Microsoft for Microsoft Edge - SpiderMonkey -> made by Netscape, now runs on Firefox - Rhino -> made by Mozilla, in Java - Nitro -> made by Apple for Safari - JerryScript, Espruino -> IoT - And many others...

Slide 24

Slide 24 text

Simplified diagram of an engine

Slide 25

Slide 25 text

JavaScript is single-threaded_

Slide 26

Slide 26 text

what means to be single-threaded? - We only have one call stack - We can only run one thing at a time

Slide 27

Slide 27 text

call stack?

Slide 28

Slide 28 text

call stack - Not part of JS - Provided by V8 - Data structure - Ordered as LIFO - Keeps track of function calls - Made of stack frames

Slide 29

Slide 29 text

No content

Slide 30

Slide 30 text

Call Stack Step 0

Slide 31

Slide 31 text

Call Stack Step 1 printSquare(5)

Slide 32

Slide 32 text

Call Stack Step 2 printSquare(5) multiply(x, x)

Slide 33

Slide 33 text

Call Stack Step 2 printSquare(5)

Slide 34

Slide 34 text

Call Stack Step 3 printSquare(5) console.log(s)

Slide 35

Slide 35 text

Call Stack Step 4 printSquare(5)

Slide 36

Slide 36 text

Call Stack Step 5

Slide 37

Slide 37 text

stack overflow

Slide 38

Slide 38 text

Call Stack f() f() f() f() f() f() f() f() f()

Slide 39

Slide 39 text

the hosting environment_

Slide 40

Slide 40 text

hosting environment - Wherever place the code is running on - Provides external APIs - Handles async queues

Slide 41

Slide 41 text

the real diagram

Slide 42

Slide 42 text

event loop

Slide 43

Slide 43 text

callbacks

Slide 44

Slide 44 text

event loop - Handle async code - Puts queued callbacks onto the call stack - In Node, provided by libuv - Not part of V8 - Not part of JS

Slide 45

Slide 45 text

libuv

Slide 46

Slide 46 text

libuv more than an event loop - Open source - Created for node - Wrapper around libev - Provides - Event loop - TCP & UDP - DNS Resolution - FS Events - IPC and Child processes - Thread pool - More...

Slide 47

Slide 47 text

No content

Slide 48

Slide 48 text

callback queues_

Slide 49

Slide 49 text

No content

Slide 50

Slide 50 text

async callback function

Slide 51

Slide 51 text

Call Stack Ext. APIs CB queue

Slide 52

Slide 52 text

Call Stack Ext. APIs CB queue console.log('Starting')

Slide 53

Slide 53 text

Call Stack Ext. APIs CB queue console.log('Starting') Starting

Slide 54

Slide 54 text

Call Stack Ext. APIs CB queue

Slide 55

Slide 55 text

Call Stack Ext. APIs CB queue fs.readFile(fp, cb)

Slide 56

Slide 56 text

Call Stack Ext. APIs CB queue fs.readFile(fp, cb) file cb

Slide 57

Slide 57 text

Call Stack Ext. APIs CB queue file cb

Slide 58

Slide 58 text

Call Stack Ext. APIs CB queue file cb console.log('Ending')

Slide 59

Slide 59 text

Call Stack Ext. APIs CB queue console.log('Ending') Starting Ending cb

Slide 60

Slide 60 text

later does not mean "later"

Slide 61

Slide 61 text

Call Stack Ext. APIs CB queue cb

Slide 62

Slide 62 text

Call Stack Ext. APIs CB queue cb(err, data)

Slide 63

Slide 63 text

Call Stack Ext. APIs CB queue cb(err, data) console.log(data.toS())

Slide 64

Slide 64 text

Call Stack Ext. APIs CB queue Starting Ending The file contents cb(err, data) console.log(data.toS())

Slide 65

Slide 65 text

Call Stack Ext. APIs CB queue cb(err, data)

Slide 66

Slide 66 text

Call Stack Ext. APIs CB queue

Slide 67

Slide 67 text

microtasks and macrotasks_

Slide 68

Slide 68 text

microtask queue

Slide 69

Slide 69 text

microtasks - process.nextTick - Promises - Object.observe

Slide 70

Slide 70 text

macrotask queue

Slide 71

Slide 71 text

macrotasks - setTimeout - setInterval - setImmediate - any I/O operation

Slide 72

Slide 72 text

what would happen?

Slide 73

Slide 73 text

Call Stack Ext. APIs CB queue Job queue console.log('start')

Slide 74

Slide 74 text

Call Stack Ext. APIs CB queue Job queue console.log('start') start

Slide 75

Slide 75 text

Call Stack Ext. APIs CB queue Job queue

Slide 76

Slide 76 text

Call Stack Ext. APIs CB queue Job queue setTimeout('t1',0)

Slide 77

Slide 77 text

Call Stack Ext. APIs CB queue Job queue setTimeout('t1',0) timer cb1

Slide 78

Slide 78 text

Call Stack Ext. APIs CB queue Job queue timer cb1

Slide 79

Slide 79 text

Call Stack Ext. APIs CB queue Job queue setTimeout('t2',0) timer cb1

Slide 80

Slide 80 text

Call Stack Ext. APIs CB queue Job queue setTimeout('t2',0) timer cb1 timer cb2

Slide 81

Slide 81 text

Call Stack Ext. APIs CB queue Job queue timer cb1 timer cb2

Slide 82

Slide 82 text

Call Stack Ext. APIs CB queue Job queue timer cb2 .then(console.log) cb1

Slide 83

Slide 83 text

Call Stack Ext. APIs CB queue Job queue handle setTimeout('t3',0) cb1 cb2

Slide 84

Slide 84 text

Call Stack Ext. APIs CB queue Job queue timer cb3 handle setTimeout('t3',0) cb1 cb2

Slide 85

Slide 85 text

Call Stack Ext. APIs CB queue Job queue handle .then(fn) cb1 cb2 cb3

Slide 86

Slide 86 text

Call Stack Ext. APIs CB queue Job queue handle .then(fn) fn cb1 cb2 cb3

Slide 87

Slide 87 text

Call Stack Ext. APIs CB queue Job queue handle fn cb1 cb2 cb3

Slide 88

Slide 88 text

Call Stack Ext. APIs CB queue Job queue handle fn cb1 cb2 cb3

Slide 89

Slide 89 text

Call Stack Ext. APIs CB queue Job queue handle fn setTimeout('tp',0) cb1 cb2 cb3

Slide 90

Slide 90 text

Call Stack Ext. APIs CB queue Job queue timer cb4 handle fn setTimeout('tp',0) cb1 cb2 cb3

Slide 91

Slide 91 text

Call Stack Ext. APIs CB queue Job queue timer cb4 handle fn cb1 cb2 cb3

Slide 92

Slide 92 text

Call Stack Ext. APIs CB queue Job queue timer cb4 fn cb1 cb2 cb3

Slide 93

Slide 93 text

Call Stack Ext. APIs CB queue Job queue fn cb1 cb2 cb3 cb4

Slide 94

Slide 94 text

Call Stack Ext. APIs CB queue Job queue fn console.log('promise') cb1 cb2 cb3 cb4

Slide 95

Slide 95 text

Call Stack Ext. APIs CB queue Job queue fn console.log('promise') start promise cb1 cb2 cb3 cb4

Slide 96

Slide 96 text

Call Stack Ext. APIs CB queue Job queue fn cb1 cb2 cb3 cb4

Slide 97

Slide 97 text

Call Stack Ext. APIs CB queue Job queue cb1 cb2 cb3 cb4

Slide 98

Slide 98 text

Call Stack Ext. APIs CB queue Job queue cb2 cb3 cb4 cb1

Slide 99

Slide 99 text

Call Stack Ext. APIs CB queue Job queue cb2 cb3 cb4 cb1 console.log('timeout')

Slide 100

Slide 100 text

Call Stack Ext. APIs CB queue Job queue cb2 cb3 cb4 cb1 console.log('timeout') start promise timeout

Slide 101

Slide 101 text

Call Stack Ext. APIs CB queue Job queue cb2 cb3 cb4 cb1

Slide 102

Slide 102 text

Call Stack Ext. APIs CB queue Job queue cb2 cb3 cb4

Slide 103

Slide 103 text

Call Stack Ext. APIs CB queue Job queue cb3 cb4 cb2

Slide 104

Slide 104 text

Call Stack Ext. APIs CB queue Job queue cb3 cb4 cb2 console.log('timeout2')

Slide 105

Slide 105 text

Call Stack Ext. APIs CB queue Job queue cb3 cb4 cb2 console.log('timeout2') start promise timeout timeout2

Slide 106

Slide 106 text

Call Stack Ext. APIs CB queue Job queue cb3 cb4 cb2

Slide 107

Slide 107 text

Call Stack Ext. APIs CB queue Job queue cb3 cb4

Slide 108

Slide 108 text

Call Stack Ext. APIs CB queue Job queue cb4 cb3

Slide 109

Slide 109 text

Call Stack Ext. APIs CB queue Job queue cb4 cb3 console.log('timeout3')

Slide 110

Slide 110 text

Call Stack Ext. APIs CB queue Job queue cb4 cb3 console.log('timeout3') start promise timeout timeout2 timeout3

Slide 111

Slide 111 text

Call Stack Ext. APIs CB queue Job queue cb4 cb3

Slide 112

Slide 112 text

Call Stack Ext. APIs CB queue Job queue cb4

Slide 113

Slide 113 text

Call Stack Ext. APIs CB queue Job queue cb4

Slide 114

Slide 114 text

Call Stack Ext. APIs CB queue Job queue cb4 console.log('p timeout')

Slide 115

Slide 115 text

Call Stack Ext. APIs CB queue Job queue cb4 console.log('p timeout') start promise timeout timeout2 timeout3 promise timeout

Slide 116

Slide 116 text

Call Stack Ext. APIs CB queue Job queue cb4

Slide 117

Slide 117 text

Call Stack Ext. APIs CB queue Job queue

Slide 118

Slide 118 text

V8

Slide 119

Slide 119 text

V8 the golden compiler - Made by Google - open source high-performance - JS and WebAsm engine - Written in C++ - Implements ES fully - Can be standalone

Slide 120

Slide 120 text

abstract syntax tree_

Slide 121

Slide 121 text

AST of readFile (part)

Slide 122

Slide 122 text

hidden classes_

Slide 123

Slide 123 text

No content

Slide 124

Slide 124 text

No content

Slide 125

Slide 125 text

No content

Slide 126

Slide 126 text

C2

Slide 127

Slide 127 text

property order matters_

Slide 128

Slide 128 text

V8 pipelines_

Slide 129

Slide 129 text

AoT vs JiT

Slide 130

Slide 130 text

old compiler pipeline_

Slide 131

Slide 131 text

Full-Codegen

Slide 132

Slide 132 text

FCG compiler fast, but not that good - First step of the pipeline - Simple and fast - Not-optimised, relatively slow - Handles type-feedback, finds hot functions - Takes the AST and generate generic native code

Slide 133

Slide 133 text

Crankshaft

Slide 134

Slide 134 text

Crankshaft compiler good, but not that fast - Takes type-feedback from FCG - Optimises code accordingly - Replaces not-opt code with optimised code using OSR - Does not implement all ES - Made of two components - Hydrogen - Lithium

Slide 135

Slide 135 text

new compiler pipeline_

Slide 136

Slide 136 text

new compiler pipeline

Slide 137

Slide 137 text

TurboFan

Slide 138

Slide 138 text

TurboFan Crankshaft, the right way - Started as a secondary compiler for ES6 - Designed to be a webasm compiler - Uses a sea of nodes - Receives bytecodes directly - Divided in layers - Frontend - Optimising Layer - Backend - Fixed deoptimisation cliffs

Slide 139

Slide 139 text

mixed old pipeline

Slide 140

Slide 140 text

current pipeline

Slide 141

Slide 141 text

Ignition

Slide 142

Slide 142 text

Ignition an interpreter to rule them all - JS Interpreter - Generates optimised bytecode - Created to reduce memory usage - Focused on low memory devices (cell phones) - NOT a parser - AST not source of truth anymore - Uses Turbofan's backend

Slide 143

Slide 143 text

ignition pipeline_

Slide 144

Slide 144 text

No content

Slide 145

Slide 145 text

bytecodes

Slide 146

Slide 146 text

bytecodes are middle-class representations

Slide 147

Slide 147 text

node --print-bytecode --print-bytecode-filter=f index.js

Slide 148

Slide 148 text

putting it all together_

Slide 149

Slide 149 text

No content

Slide 150

Slide 150 text

generated bytecode for readFileAsync

Slide 151

Slide 151 text

Goes through this pipeline

Slide 152

Slide 152 text

Call Stack Ext. APIs CB queue Job queue

Slide 153

Slide 153 text

Call Stack Ext. APIs CB queue Job queue path.resolve

Slide 154

Slide 154 text

Call Stack Ext. APIs CB queue Job queue

Slide 155

Slide 155 text

Call Stack Ext. APIs CB queue Job queue IIFE

Slide 156

Slide 156 text

Call Stack Ext. APIs CB queue Job queue IIFE readFileAsync

Slide 157

Slide 157 text

Call Stack Ext. APIs CB queue Job queue handle IIFE

Slide 158

Slide 158 text

Call Stack Ext. APIs CB queue Job queue handle

Slide 159

Slide 159 text

Call Stack Ext. APIs CB queue Job queue handle

Slide 160

Slide 160 text

Call Stack Ext. APIs CB queue Job queue handle readFile

Slide 161

Slide 161 text

Call Stack Ext. APIs CB queue Job queue handle file cb

Slide 162

Slide 162 text

Call Stack Ext. APIs CB queue Job queue cb

Slide 163

Slide 163 text

Call Stack Ext. APIs CB queue Job queue cb

Slide 164

Slide 164 text

Call Stack Ext. APIs CB queue Job queue cb resolve

Slide 165

Slide 165 text

Call Stack Ext. APIs CB queue Job queue cb resolve callback

Slide 166

Slide 166 text

Call Stack Ext. APIs CB queue Job queue cb resolve callback toString()

Slide 167

Slide 167 text

Call Stack Ext. APIs CB queue Job queue cb resolve callback

Slide 168

Slide 168 text

Call Stack Ext. APIs CB queue Job queue cb resolve

Slide 169

Slide 169 text

Call Stack Ext. APIs CB queue Job queue cb

Slide 170

Slide 170 text

Call Stack Ext. APIs CB queue Job queue cb console.log

Slide 171

Slide 171 text

Call Stack Ext. APIs CB queue Job queue cb

Slide 172

Slide 172 text

Call Stack Ext. APIs CB queue Job queue

Slide 173

Slide 173 text

references_ - All references are in this guide: bit.ly/node-under-the-hood

Slide 174

Slide 174 text

references_ - https://dev.to/khaosdoctor/node-js-under-the-hood-1-getting-to-know-our-tools-1465 - https://dev.to/khaosdoctor/node-js-under-the-hood-2-understanding-javascript-48cn - https://dev.to/khaosdoctor/node-js-under-the-hood-3-deep-dive-into-the-event-loop-135d - https://dev.to/khaosdoctor/node-js-under-the-hood-4-let-s-talk-about-v8-1eol - https://dev.to/khaosdoctor/node-js-under-the-hood-5-hidden-classes-variable-allocations-1244 - https://dev.to/khaosdoctor/node-js-under-the-hood-6-the-old-v8-34hm - https://dev.to/khaosdoctor/node-js-under-the-hood-7-the-new-v8-4gd6 - https://dev.to/khaosdoctor/node-js-under-the-hood-8-oh-the-bytecodes-1p6p - https://dev.to/khaosdoctor/node-js-under-the-hood-9-collecting-the-garbage-772

Slide 175

Slide 175 text

thank you :D /khaosdoctor @_staticvoid lsantos.dev /khaosdoctor