Google V8 engine

hitamu 500 views 35 slides Dec 09, 2019
Slide 1
Slide 1 of 35
Slide 1
1
Slide 2
2
Slide 3
3
Slide 4
4
Slide 5
5
Slide 6
6
Slide 7
7
Slide 8
8
Slide 9
9
Slide 10
10
Slide 11
11
Slide 12
12
Slide 13
13
Slide 14
14
Slide 15
15
Slide 16
16
Slide 17
17
Slide 18
18
Slide 19
19
Slide 20
20
Slide 21
21
Slide 22
22
Slide 23
23
Slide 24
24
Slide 25
25
Slide 26
26
Slide 27
27
Slide 28
28
Slide 29
29
Slide 30
30
Slide 31
31
Slide 32
32
Slide 33
33
Slide 34
34
Slide 35
35

About This Presentation

Understand Google V8 engine for writing better Javascript code


Slide Content

V8 Engine Septeni Technology 2018

Hello! I am ThuNx Programming & Photography Find out more: @hitamu

AGENDA Overview Javascript Engine V8 Engine Inside V8 Compiler Pipeline Optimization Concepts Conclusions

1. Overview Javascript Engine

Browser at high-level

List of Javascript Engine V8 (Google) Chrome Node.js Chakra (Microsoft) IE Edge Spidermonkey (Mozilla) Firefox JavaScriptCore (Apple) Safari

Chrome V8 Engine V8 Engine V8 is Google's open source. V8 is written in C++ and is used in Google Chrome V8 is embed in Node.js

V8 in Chrome & Node.js V8 Engine V8 Engine core library Rendering Engine Networking libuv

V8’s Performance Fast Property Access Dynamic Machine Code Generation * Efficient Garbage Collection

2 . Inside V8 Engine Compiler Pipeline Optimization Concepts

2. 1 Compiler Pipeline A brief history of time...

Compiler pipeline (2010) Full- Codegen Unoptimized Code Crankshaft Optimized Code Parser Abstract Syntax Tree JavaScript Source Code Optimized Baseline Optimize Deoptimize

Compiler pipeline (2014) Full- Codegen Unoptimized Code Crankshaft Optimized Code TurboFan Parser Abstract Syntax Tree JavaScript Source Code Optimized Baseline Optimize Deoptimize

Compiler pipeline (2016) Full- Codegen Unoptimized Code Crankshaft Optimized Code TurboFan Parser Abstract Syntax Tree Ignition Bytecode JavaScript Source Code Optimized Baseline Interpreted Optimize Deoptimize Baseline (Low-end Android)

Compiler pipeline (2017 and future) Optimized Code TurboFan Parser Abstract Syntax Tree Ignition Bytecode JavaScript Source Code Optimized Interpreted Deoptimize Optimize

2.2 Optimization Concepts Hidden Class Inlining Cache

Hidden Class

Hidden Class Javascript Prototype based o0.__proto__.__proto__ Change type at runtime => Hard to track object & variables Hidden class Internal representation of the type system => Fast property access

function Point (x, y) { this .x = x; this .y = y; } const p1 = new Point( 12 , 3 ); const p2 = new Point( 5 , 9 ); const p3 = new Point( 12 ); const hero = { name : 'Superman' }; Javascript Code

// The JavaScript engine's dictionary // storing the objects const allObjects = { o0 : { x: 12, y: 3, __proto__: 'p6a1251' }, //p1 o1 : { x: 5, y: 9, __proto__: 'p6a1251' }, //p2 o2 : { x: 12 , __proto__: 'p6a1251' }, //p3 // Object{ // constructor: Point(x, y), // __proto__: Object{constructor: Object()} // } o3 : { name: ‘Superman’, __proto__: 'p419ecc' }, // Object{constructor: Object()} //hero } } Dynamic lookup

Hidden Class function Point (x, y) { this .x = x; this .y = y; }

Hidden Class const p1 = new Point( 13 , 37 ); => this.x = 13

Hidden Class const p1 = new Point( 13 , 37 ); => this.y = 37

Hidden Class const p5 = new Point( 13 , 37 ); p5.a = 'a' ; const p6 = new Point( 13 , 37 ); p6.b = 'b' ;

Hidden Class /* C2 */ const p1 = new Point( 13 , 37 ); /* C1 */ const p2 = new Point( 2 ); /* C0 */ const p3 = new Point(); /* C2 */ const p4 = new Point( 4 , 2 );

Optimization tips Avoid using delete Won’t be backed by hidden class Init object in the same order Same hidden classes, same transitions Avoid adding properties after the object creation If need: Adding in order of props

Inline Caching

Inline Caching /* C2 */ const p1 = new Point( 13 , 37 ); /* C1 */ const p2 = new Point( 2 ); /* C0 */ const p3 = new Point(); /* C2 */ const p4 = new Point( 4 , 2 );

Inline Cache Monomorphic single shape fastest possible IC Polymorphic more than one shape linear search among cached entries Megamorphic too many shapes slowest ICs, hitting global cache

Optimization tips Prefer Monomorphism than Polymorphism Same method repeatedly over many different method only once

Conclusion

Optimization tips Avoid using delete Init object in the same order Avoid adding properties after the object creation Prefer Monomorphism than Polymorphism Same method repeatedly over many different method only once

Digging Deeper Deeper in Inline Caching Type Feedback, Type Guard Intermediate Representation, Deoptimization Garbage Collection

References http://mrale.ph/blog/2015/01/11/whats-up-with-monomorphism.html http://mrale.ph/blog/2012/06/03/explaining-js-vms-in-js-inline-caches.html https://draft.li/blog/2016/12/22/javascript-engines-hidden-classes/ https://github.com/v8/v8/wiki/Design%20Elements Google I/O 2016, 2017 https://v8project.blogspot.com/

THANK YOU