Slide 1

Slide 1 text

No content

Slide 2

Slide 2 text

V8 internals for JavaScript developers @mathias

Slide 3

Slide 3 text

Elements kinds in V8

Slide 4

Slide 4 text

const array = [1, 2, 3];

Slide 5

Slide 5 text

const array = [1, 2, 3]; // elements kind: PACKED_SMI_ELEMENTS

Slide 6

Slide 6 text

const array = [1, 2, 3]; // elements kind: PACKED_SMI_ELEMENTS array.push(4.56);

Slide 7

Slide 7 text

const array = [1, 2, 3]; // elements kind: PACKED_SMI_ELEMENTS array.push(4.56); // elements kind: PACKED_DOUBLE_ELEMENTS

Slide 8

Slide 8 text

const array = [1, 2, 3]; // elements kind: PACKED_SMI_ELEMENTS array.push(4.56); // elements kind: PACKED_DOUBLE_ELEMENTS array.push('x');

Slide 9

Slide 9 text

const array = [1, 2, 3]; // elements kind: PACKED_SMI_ELEMENTS array.push(4.56); // elements kind: PACKED_DOUBLE_ELEMENTS array.push('x'); // elements kind: PACKED_ELEMENTS

Slide 10

Slide 10 text

Smi Doubles Regular elements Elements kinds

Slide 11

Slide 11 text

const array = [1, 2, 3]; // elements kind: PACKED_SMI_ELEMENTS array.push(4.56); // elements kind: PACKED_DOUBLE_ELEMENTS array.push('x'); // elements kind: PACKED_ELEMENTS

Slide 12

Slide 12 text

array.length; // 5 index 0 1 2 3 4 value 1 2 3 4.56 'x'

Slide 13

Slide 13 text

array.length; // 5 array[9] = 1; // array[5] until array[8] are now holes index 0 1 2 3 4 5 6 7 8 9 value 1 2 3 4.56 'x' 1

Slide 14

Slide 14 text

array.length; // 5 array[9] = 1; // array[5] until array[8] are now holes // elements kind: HOLEY_ELEMENTS index 0 1 2 3 4 5 6 7 8 9 value 1 2 3 4.56 'x' 1

Slide 15

Slide 15 text

array[8]; // → ??? index 0 1 2 3 4 5 6 7 8 9 value 1 2 3 4.56 'x' 1

Slide 16

Slide 16 text

array[8]; // → ??? ❌ index 0 1 2 3 4 5 6 7 8 9 value 1 2 3 4.56 'x' 1

Slide 17

Slide 17 text

array[8]; // → ??? ❌ 8 >= 0 && 8 < array.length; // bounds check // → true index 0 1 2 3 4 5 6 7 8 9 value 1 2 3 4.56 'x' 1

Slide 18

Slide 18 text

array[8]; // → ??? ❌ 8 >= 0 && 8 < array.length; // bounds check // → true ❌ index 0 1 2 3 4 5 6 7 8 9 value 1 2 3 4.56 'x' 1

Slide 19

Slide 19 text

array[8]; // → ??? ❌ 8 >= 0 && 8 < array.length; // bounds check // → true ❌ hasOwnProperty(array, '8'); // → false index 0 1 2 3 4 5 6 7 8 9 value 1 2 3 4.56 'x' 1

Slide 20

Slide 20 text

index 0 1 2 3 4 5 6 7 8 9 value 1 2 3 4.56 'x' 1 array[8]; // → ??? ❌ 8 >= 0 && 8 < array.length; // bounds check // → true ❌ hasOwnProperty(array, '8'); // → false ❌

Slide 21

Slide 21 text

array[8]; // → ??? ❌ 8 >= 0 && 8 < array.length; // bounds check // → true ❌ hasOwnProperty(array, '8'); // → false ❌ hasOwnProperty(Array.prototype, '8'); // → false

Slide 22

Slide 22 text

array[8]; // → ??? ❌ 8 >= 0 && 8 < array.length; // bounds check // → true ❌ hasOwnProperty(array, '8'); // → false ❌ hasOwnProperty(Array.prototype, '8'); // → false ❌

Slide 23

Slide 23 text

array[8]; // → ??? ❌ 8 >= 0 && 8 < array.length; // bounds check // → true ❌ hasOwnProperty(array, '8'); // → false ❌ hasOwnProperty(Array.prototype, '8'); // → false ❌ hasOwnProperty(Object.prototype, '8'); // → false

Slide 24

Slide 24 text

array[8]; // → ??? ❌ 8 >= 0 && 8 < array.length; // bounds check // → true ❌ hasOwnProperty(array, '8'); // → false ❌ hasOwnProperty(Array.prototype, '8'); // → false ❌ hasOwnProperty(Object.prototype, '8'); // → false ✅

Slide 25

Slide 25 text

array[8]; // → undefined ✅ 8 >= 0 && 8 < array.length; // bounds check // → true hasOwnProperty(array, '8'); // → false hasOwnProperty(Array.prototype, '8'); // → false hasOwnProperty(Object.prototype, '8'); // → false ✅

Slide 26

Slide 26 text

packedArray[8]; // → undefined ✅ 8 >= 0 && 8 < packedArray.length; // bounds check // → true ✅ hasOwnProperty(packedArray, '8'); // → true ✅ hasOwnProperty(Array.prototype, '8'); // → false ✅ hasOwnProperty(Object.prototype, '8'); // → false ✅

Slide 27

Slide 27 text

packedArray[8]; // → undefined ✅ 8 >= 0 && 8 < packedArray.length; // bounds check // → true ✅ hasOwnProperty(packedArray, '8'); // → true ✅ hasOwnProperty(Array.prototype, '8'); // → false ✅ hasOwnProperty(Object.prototype, '8'); // → false ✅

Slide 28

Slide 28 text

array[0]; // → ???

Slide 29

Slide 29 text

array[0]; // → ??? ❌

Slide 30

Slide 30 text

array[0]; // → ??? ❌ 0 >= 0 && 0 < array.length; // bounds check // → true

Slide 31

Slide 31 text

array[0]; // → ??? ❌ 0 >= 0 && 0 < array.length; // bounds check // → true ❌

Slide 32

Slide 32 text

array[0]; // → ??? ❌ 0 >= 0 && 0 < array.length; // bounds check // → true ❌ hasOwnProperty(array, '0'); // → true

Slide 33

Slide 33 text

array[0]; // → ??? ❌ 0 >= 0 && 0 < array.length; // bounds check // → true ❌ hasOwnProperty(array, '0'); // → true ✅

Slide 34

Slide 34 text

array[0]; // → 1 ✅ 0 >= 0 && 0 < array.length; // bounds check // → true hasOwnProperty(array, '0'); // → true ✅

Slide 35

Slide 35 text

PACKED > HOLEY

Slide 36

Slide 36 text

PACKED > HOLEY

Slide 37

Slide 37 text

Smi Doubles Regular elements Elements kinds

Slide 38

Slide 38 text

Smi, packed Doubles, packed Regular elements, packed Smi, holey Doubles, holey Regular elements, holey

Slide 39

Slide 39 text

lattice

Slide 40

Slide 40 text

PACKED_SMI_ELEMENTS HOLEY_SMI_ELEMENTS PACKED_DOUBLE_ELEMENTS HOLEY_DOUBLE_ELEMENTS PACKED_ELEMENTS HOLEY_ELEMENTS

Slide 41

Slide 41 text

const array = new Array(3);

Slide 42

Slide 42 text

const array = new Array(3); index 0 1 2 value

Slide 43

Slide 43 text

const array = new Array(3); // HOLEY_SMI_ELEMENTS index 0 1 2 value

Slide 44

Slide 44 text

const array = new Array(3); // HOLEY_SMI_ELEMENTS array[0] = 'a'; index 0 1 2 value 'a'

Slide 45

Slide 45 text

const array = new Array(3); // HOLEY_SMI_ELEMENTS array[0] = 'a'; // HOLEY_ELEMENTS index 0 1 2 value 'a'

Slide 46

Slide 46 text

const array = new Array(3); // HOLEY_SMI_ELEMENTS array[0] = 'a'; // HOLEY_ELEMENTS array[1] = 'b'; index 0 1 2 value 'a' 'b'

Slide 47

Slide 47 text

const array = new Array(3); // HOLEY_SMI_ELEMENTS array[0] = 'a'; // HOLEY_ELEMENTS array[1] = 'b'; array[2] = 'c'; index 0 1 2 value 'a' 'b' 'c' now packed!

Slide 48

Slide 48 text

const array = new Array(3); // HOLEY_SMI_ELEMENTS array[0] = 'a'; // HOLEY_ELEMENTS array[1] = 'b'; array[2] = 'c'; // HOLEY_ELEMENTS (still!) now packed! but it’s too late index 0 1 2 value 'a' 'b' 'c'

Slide 49

Slide 49 text

const array = ['a', 'b', 'c']; // elements kind: PACKED_ELEMENTS

Slide 50

Slide 50 text

const array = ['a', 'b', 'c']; // elements kind: PACKED_ELEMENTS // … array.push(someValue); array.push(someOtherValue);

Slide 51

Slide 51 text

Avoid holes! #ProTip Avoid holes

Slide 52

Slide 52 text

for (let i = 0, item; (item = items[i]) != null; i++) { doSomething(item); }

Slide 53

Slide 53 text

for (let i = 0, item; (item = items[i]) != null; i++) { doSomething(item); }

Slide 54

Slide 54 text

for (let i = 0, item; (item = items[i]) != null; i++) { doSomething(item); } for (let index = 0; index < items.length; index++) { doSomething(item); }

Slide 55

Slide 55 text

for (const item of items) { doSomething(item); }

Slide 56

Slide 56 text

Avoid holes! #ProTip Avoid out-of-bounds reads

Slide 57

Slide 57 text

[3, 2, 1, +0]; // PACKED_SMI_ELEMENTS

Slide 58

Slide 58 text

[3, 2, 1, +0]; // PACKED_SMI_ELEMENTS [3, 2, 1, -0]; // PACKED_DOUBLE_ELEMENTS

Slide 59

Slide 59 text

[3, 2, 1, +0]; // PACKED_SMI_ELEMENTS [3, 2, 1, -0]; // PACKED_DOUBLE_ELEMENTS [3, 2, 1, NaN, Infinity]; // PACKED_DOUBLE_ELEMENTS

Slide 60

Slide 60 text

Avoid holes! #ProTip Avoid elements kind transitions

Slide 61

Slide 61 text

const arrayLike = {}; arrayLike[0] = 'a'; arrayLike[1] = 'b'; arrayLike[2] = 'c'; arrayLike.length = 3;

Slide 62

Slide 62 text

Array.prototype.forEach.call(arrayLike, (value, index) => { console.log(`${ index }: ${ value }`); }); // This logs '0: a', then '1: b', and finally '2: c'.

Slide 63

Slide 63 text

const actualArray = Array.prototype.slice.call(arrayLike, 0); actualArray.forEach((value, index) => { console.log(`${ index }: ${ value }`); }); // This logs '0: a', then '1: b', and finally '2: c'.

Slide 64

Slide 64 text

const logArgs = function() { Array.prototype.forEach.call(arguments, (value, index) => { console.log(`${ index }: ${ value }`); }); }; logArgs('a', 'b', 'c'); // This logs '0: a', then '1: b', and finally '2: c'.

Slide 65

Slide 65 text

const logArgs = (...args) => { args.forEach((value, index) => { console.log(`${ index }: ${ value }`); }); }; logArgs('a', 'b', 'c'); // This logs '0: a', then '1: b', and finally '2: c'.

Slide 66

Slide 66 text

Avoid holes! #ProTip Prefer arrays over array-like objects

Slide 67

Slide 67 text

$

Slide 68

Slide 68 text

$ rlwrap ~/projects/v8/out.gn/x64.debug/d8

Slide 69

Slide 69 text

$ rlwrap ~/projects/v8/out.gn/x64.debug/d8 --allow-natives-syntax

Slide 70

Slide 70 text

$ rlwrap ~/projects/v8/out.gn/x64.debug/d8 --allow-natives-syntax V8 version 6.2.0 (candidate) d8>

Slide 71

Slide 71 text

$ rlwrap ~/projects/v8/out.gn/x64.debug/d8 --allow-natives-syntax V8 version 6.2.0 (candidate) d8> const array = [1, 2, 3];

Slide 72

Slide 72 text

$ rlwrap ~/projects/v8/out.gn/x64.debug/d8 --allow-natives-syntax V8 version 6.2.0 (candidate) d8> const array = [1, 2, 3]; %DebugPrint(array);

Slide 73

Slide 73 text

$ rlwrap ~/projects/v8/out.gn/x64.debug/d8 --allow-natives-syntax V8 version 6.2.0 (candidate) d8> const array = [1, 2, 3]; %DebugPrint(array); DebugPrint: 0x313389e0e551: [JSArray] - map = 0x3133e0582889 [FastProperties] - prototype = 0x313360387f81 - elements = 0x313389e0e4c9 [PACKED_SMI_ELEMENTS (COW)] - length = 3 - properties = 0x3133dae02241 { #length: 0x31336c242839 (const accessor descriptor) } …

Slide 74

Slide 74 text

$ rlwrap ~/projects/v8/out.gn/x64.debug/d8 --allow-natives-syntax V8 version 6.2.0 (candidate) d8> const array = [1, 2, 3]; %DebugPrint(array); DebugPrint: 0x313389e0e551: [JSArray] - map = 0x3133e0582889 [FastProperties] - prototype = 0x313360387f81 - elements = 0x313389e0e4c9 [PACKED_SMI_ELEMENTS (COW)] - length = 3 - properties = 0x3133dae02241 { #length: 0x31336c242839 (const accessor descriptor) } …

Slide 75

Slide 75 text

No content

Slide 76

Slide 76 text

Avoid holes. — J.K. Rowling

Slide 77

Slide 77 text

Avoid holes. Avoid out-of-bounds reads. — ancient Chinese proverb

Slide 78

Slide 78 text

Avoid holes. Avoid out-of-bounds reads. Avoid elements kind transitions. — Justin Bieber

Slide 79

Slide 79 text

Avoid holes. Avoid out-of-bounds reads. Avoid elements kind transitions. Prefer arrays over array-like objects. — Albert Einstein

Slide 80

Slide 80 text

Avoid holes. Avoid out-of-bounds reads. Avoid elements kind transitions. Prefer arrays over array-like objects. Eat your vegetables. — this slide, just now

Slide 81

Slide 81 text

Thank you! @mathias