vaugham hong - embedding javascript v8

37
How Javascript V8 Works

Upload: allen-pike

Post on 08-Jan-2017

64 views

Category:

Technology


2 download

TRANSCRIPT

Page 1: Vaugham Hong - Embedding JavaScript V8

How Javascript V8 Works

Page 2: Vaugham Hong - Embedding JavaScript V8

Who am I?

• Vaugham Hong

• Electronic Arts - rendering / systems

• BigPark / Microsoft - gameplay / scripting

• ConquerMobile, AlkanAir, Colligo , nGrain…etc. - full stack

• uForis VR - full stack

Page 3: Vaugham Hong - Embedding JavaScript V8

! web_dev

Page 4: Vaugham Hong - Embedding JavaScript V8

VR x V8• Explored 360 panoramas /

videos and 3D spaces

• Early 2014 - Custom markup to create static connected 3D spaces

• eg. Model, Portal, Video, Image elements

• Early 2015 - V8 JS integration

• Dynamic spaces

Page 5: Vaugham Hong - Embedding JavaScript V8

VR x V8• Shader Toys

• Youtube

Page 6: Vaugham Hong - Embedding JavaScript V8

VR x V8• Chromium

• Emulator

Page 7: Vaugham Hong - Embedding JavaScript V8

How Javascript V8 Works

Page 8: Vaugham Hong - Embedding JavaScript V8

What is V8?

• V8 is Google’s high performance Javascript engine

• Desktop Chrome September, 2008 (55% MS 2016)

• Android Chrome September, 2012 (80% MS 2016)

• Node.JS

Page 9: Vaugham Hong - Embedding JavaScript V8

Embedding V8App

Load / Run Script

JS to NativeFunction Call

Initialize

Native to JSFunction Call

Destroy

V8

Page 10: Vaugham Hong - Embedding JavaScript V8

Embedding V8

INCLUDED NOT INCLUDED

Array Math Date Number Object …

DOM WebGL require

Page 11: Vaugham Hong - Embedding JavaScript V8

Today

• JIT

• Full compiler

• Optimized compiler

• Garbage collection

• New space - scavenge

• Old space - mark-sweep / mark-compact

Page 12: Vaugham Hong - Embedding JavaScript V8

V8 JIT Overview• Compile one function at a time - as they are

encountered

• Constant startup latency

• Never compile code that is unreachable

• Two compilers

• Full compiler

• Optimization compiler (CrankShaft)

Page 13: Vaugham Hong - Embedding JavaScript V8

Full Compiler

• Goal - Spit out machine code as quickly as possible

• Don’t worry too much about optimizations

• No intermediate language / No byte code

• Javascript => Machine Code

• Profile for hot functions

Javascript MachineCodeParse JS Code-GenAST

Page 14: Vaugham Hong - Embedding JavaScript V8

Hot-ness

• Counter based profiling

• Decrement counter exiting functions

• Flag for optimization when reaching 0

• Deterministic

Page 15: Vaugham Hong - Embedding JavaScript V8

Optimized Compiler

• Goal - spit out optimized machine code

• Hydrogen IL - includes type info, scope info for optimizations (Inlining, dead code elimination, range analysis, static type inference, …etc.)

• Lithium IL - optimized ready for machine code generation

Javascript

MachineCode

Parse JS ScopeAnalysisAST Graph

Gen

L. ILOptimizationH. IL CodeGen

Page 16: Vaugham Hong - Embedding JavaScript V8

De-optimization

• V8 could decide to go back to un-optimized code

• Adding / deleting properties may void optimization contract

• Too many de-optimizations and V8 may stop optimizing entirely

• Constructs like try / catch will make code ineligible for optimizations

• https://github.com/vhf/v8-bailout-reasons

• Un-optimized code will run GC a little hotter

Un-Optimized Optimized

Page 17: Vaugham Hong - Embedding JavaScript V8

V8 GC Overview

• Automatic lifetime management

• How do you allocate objects?

• How / when do you deallocate objects?

• Generational GC

• Young / old objects

Page 18: Vaugham Hong - Embedding JavaScript V8

GC “Spaces”

NEW SPACE

OLD SPACE

OBJECT TYPE Young Old

OBJECT SIZE Small Small - Large

COLLECTION FREQUENCY High LowCOLLECTION

TIME Short LongHEAP SIZE 1-8 MB Sky’s the limit

Each space contains a set of 1MB pages

Page 19: Vaugham Hong - Embedding JavaScript V8

New Space

• Stack based allocator

• Collect when full

• Uses Cheney’s “Stop-and-copy” algorithm to reclaim memory

NEW SPACE

OBJECT TYPE Young

OBJECT SIZE Small

COLLECTION FREQ High

COLLECTION TIME Short

SIZE 1-8 MB

Page 20: Vaugham Hong - Embedding JavaScript V8

New Space Scavenge

Empty

To Space

From Space

Page 21: Vaugham Hong - Embedding JavaScript V8

New Space Scavenge

Allocate A, B, C

To Space

From Space

A CB

Page 22: Vaugham Hong - Embedding JavaScript V8

New Space Scavenge

Not enough space allocating D Pause GC and scavenge

To Space

From Space

A CB D

Page 23: Vaugham Hong - Embedding JavaScript V8

New Space Scavenge

Swap To and From Space

To Space

From Space

A CB

Page 24: Vaugham Hong - Embedding JavaScript V8

New Space Scavenge

To Space

From Space

A C

Retain Live Objects

Page 25: Vaugham Hong - Embedding JavaScript V8

New Space Scavenge

Allocate D

To Space

From Space

A C D

Page 26: Vaugham Hong - Embedding JavaScript V8

Pointer Discovery

• Pointers

• Data (eg. strings, numbers)

0x2144AC800x3426D1000x057SCC100x31054AD0

0x00A2FF11

0x122AD100

0x00A2FFA1

0x00A2FA11

Page 27: Vaugham Hong - Embedding JavaScript V8

Pointer Discovery Tagged Pointers

• 32-bit V8 allocations are 4 byte aligned

• Last two bits of an address are always zero

• Use last two bits to encode hidden data

• 00 - data

• 01 - pointer

0x04

0x00

0x08

0x0C

0x10

0x1C 11100

10000

01100

01000

00100

00000Hex Binary

0x2144AC800x3426D1000x057SCC100x31054AD0

0x00A2FF11

0x122AD100

0x00A2FFA1

0x00A2FA11

Page 28: Vaugham Hong - Embedding JavaScript V8

Pointer Discovery Tagged Pointers

0x31054AD0 0x31054AD0Data

0x00A2FF11 0x00A2FF10Pointer

Page 29: Vaugham Hong - Embedding JavaScript V8

Pointer Discovery Tagged Pointers

data

pointerdatapointerpointer

0x2144AC800x3426D1000x057SCC100x31054AD0

0x00A2FF11

0x122AD100

0x00A2FFA1

0x00A2FA11

Page 30: Vaugham Hong - Embedding JavaScript V8

Old Space

• Free-list based

• Collect on threshold size

• Mark-Sweep to reclaim memory

• Mark-Compact to reclaim pages

OLD SPACE

OBJECT TYPE Old

OBJECT SIZE Any

COLLECTION FREQ Low

COLLECTION TIME Long

SIZE Sky’s the limit

Page 31: Vaugham Hong - Embedding JavaScript V8

Old Space Mark Phase

data

pointer

datapointer

pointer

0

0

0

0

0

00

data

pointer

datapointer

pointer

1

0

0

1

1

11

Page 32: Vaugham Hong - Embedding JavaScript V8

Old Space Sweep

DEFG

ABC

1000

110

FG

Page Free List

DEFG

ABC

1000

110

FG

Page Free List

CE

Page 33: Vaugham Hong - Embedding JavaScript V8

Old Space Compact

pointer

data

datadata

pointer

data

pointerpointerdata

datadatadata

Page 1

Page 2

Page 1

Page 2

Reclaim

Page 34: Vaugham Hong - Embedding JavaScript V8

Wrap Up

• Cooperate with the JIT

• Write scripts that stay optimized

• De-optimization puts pressure on GC

• Pre allocate as much as possible

• Don’t let allocations pile up and force collection cycles

Page 35: Vaugham Hong - Embedding JavaScript V8

uForis + V8• Live editing with Javascript

• Natively backed scene management, particle systems, physics, rendering, material systems, async I/O, …etc.

• CommonJS compliant => NPM module support

• 1000 FPS!

• Platforms

• Oculus, Vive, Cardboard, GearVR

• Win32, OSX, iOS, Android

Page 36: Vaugham Hong - Embedding JavaScript V8

Questions?

• LinkedIn - https://ca.linkedin.com/in/vaughamhong

• E-mail - [email protected]

• Twitter - vaughamhong

Page 37: Vaugham Hong - Embedding JavaScript V8

We are hiring!

• Interested in joining the virtual reality industry?

• Want to find ways to help apply virtual reality in practical business problems?

• Highly organized, efficient, and excited about how virtual reality software development works?

• We'd love to hear from you!

• http://www.uforis.com/careers