Web Assembly - GOTO Conference · Web Assembly Nick Bray ncbray@google. Setting the stage. Native...

24
Web Assembly Nick Bray ncbray@google

Transcript of Web Assembly - GOTO Conference · Web Assembly Nick Bray ncbray@google. Setting the stage. Native...

Page 1: Web Assembly - GOTO Conference · Web Assembly Nick Bray ncbray@google. Setting the stage. Native code on the web - today Game engines: Unity / Unreal Languages: repl.it Emulators:

Web AssemblyNick Brayncbray@google

Page 2: Web Assembly - GOTO Conference · Web Assembly Nick Bray ncbray@google. Setting the stage. Native code on the web - today Game engines: Unity / Unreal Languages: repl.it Emulators:

Setting the stage

Page 3: Web Assembly - GOTO Conference · Web Assembly Nick Bray ncbray@google. Setting the stage. Native code on the web - today Game engines: Unity / Unreal Languages: repl.it Emulators:

Native code on the web - todayGame engines: Unity / Unreal

Languages: repl.it

Emulators: Dosbox, JS Linux, NaCl Development Environment

PDF Viewing

Media decoding

Page 4: Web Assembly - GOTO Conference · Web Assembly Nick Bray ncbray@google. Setting the stage. Native code on the web - today Game engines: Unity / Unreal Languages: repl.it Emulators:

Web != nativeAsynchronous

No threads with shared state

Inconsistent Performance

Porting code to the web is painful!

Page 5: Web Assembly - GOTO Conference · Web Assembly Nick Bray ncbray@google. Setting the stage. Native code on the web - today Game engines: Unity / Unreal Languages: repl.it Emulators:

Open

Secure

Portable

Ephemeral

… but the web is awesome

Page 6: Web Assembly - GOTO Conference · Web Assembly Nick Bray ncbray@google. Setting the stage. Native code on the web - today Game engines: Unity / Unreal Languages: repl.it Emulators:

In the beginningHTML (1991)

JavaScript (1995)

Plugins

NPAPI (1995-2015) - PDF documents

ActiveX (1996-2015) - “document embedding”

Shockwave / Flash (1996-??) - animations

Java Applets (1996-??) - applications

Page 7: Web Assembly - GOTO Conference · Web Assembly Nick Bray ncbray@google. Setting the stage. Native code on the web - today Game engines: Unity / Unreal Languages: repl.it Emulators:

Let’s try againJavaScript performance wars (2008)

Native Client / Portable Native Client (2008 / 2013)

Emscripten / asm.js (2010 / 2013)

Web Assembly (2016?)

Page 8: Web Assembly - GOTO Conference · Web Assembly Nick Bray ncbray@google. Setting the stage. Native code on the web - today Game engines: Unity / Unreal Languages: repl.it Emulators:

Year Secure Portable Ephemeral Cross Browser Shared Memory

JavaScript 1995 ✓ ✓ ✓ ✓ x

NPAPI 1995 x x x x ✓

ActiveX 1996 x x x x ✓

Flash 1996 ~ ✓ ✓ x x

Java Applets 1996 ~ / x ✓ ✓ x ✓

Native Client 2008 ✓ ~ ✓ x ✓

Emscripten 2010 ✓ ✓ ✓ ✓ x

asm.js 2013 ✓ ✓ ✓ ~ x

PNaCl 2013 ✓ ✓ ✓ x ✓

Web Assembly 2016 ? ✓ ✓ ✓ ✓ ✓

Page 9: Web Assembly - GOTO Conference · Web Assembly Nick Bray ncbray@google. Setting the stage. Native code on the web - today Game engines: Unity / Unreal Languages: repl.it Emulators:

WASM

C sources Compiler WASM binary

HTML

JavaScript sources

Data files

Browser

DOM

JS VM

WASM VM

Page 10: Web Assembly - GOTO Conference · Web Assembly Nick Bray ncbray@google. Setting the stage. Native code on the web - today Game engines: Unity / Unreal Languages: repl.it Emulators:

… but actually…

C sources Compiler WASM binary

HTML

JavaScript sources

Data files

Browser

DOM

JS+WASM VM

Page 11: Web Assembly - GOTO Conference · Web Assembly Nick Bray ncbray@google. Setting the stage. Native code on the web - today Game engines: Unity / Unreal Languages: repl.it Emulators:

const.int32 7

getlocal a

add.int32 ● ●

call foo ● ●

WASM Opsfoo(a+7, b)

getlocal b

Note: can statically infer all types

Page 12: Web Assembly - GOTO Conference · Web Assembly Nick Bray ncbray@google. Setting the stage. Native code on the web - today Game engines: Unity / Unreal Languages: repl.it Emulators:

WASM prototype vs Minified JS

66% smaller uncompressed

26% smaller compressed

23x faster to parse

Early estimates from Mozilla

Page 13: Web Assembly - GOTO Conference · Web Assembly Nick Bray ncbray@google. Setting the stage. Native code on the web - today Game engines: Unity / Unreal Languages: repl.it Emulators:

WASM Memory0

address_space_max

Load

Store

int8int16int32int64

float32float64

ptr = malloc(100);

100 bytes

Objects

Page 14: Web Assembly - GOTO Conference · Web Assembly Nick Bray ncbray@google. Setting the stage. Native code on the web - today Game engines: Unity / Unreal Languages: repl.it Emulators:

WASM FFI// FFI Interfaceexport int foo(int);import int bar();

// C compiled into WASMint foo(num int) { return num * bar(); // Call into JS}

// JavaScriptfunction bar() { return 3;}

function run(module) { var instance = WASM.createInstance( module, {bar: bar} ); return instance.foo(7); // Call into WASM}

foo (WASM)run (JS) bar (JS)

bar()

return 3

return 21

foo(7)

Page 15: Web Assembly - GOTO Conference · Web Assembly Nick Bray ncbray@google. Setting the stage. Native code on the web - today Game engines: Unity / Unreal Languages: repl.it Emulators:

WASM Polyfill

C sources Compiler

WASM binary

HTML

JavaScript sources

Data files

Browser

DOM

JS VM

Translator

asm.jsJavaScript

sources

Page 16: Web Assembly - GOTO Conference · Web Assembly Nick Bray ncbray@google. Setting the stage. Native code on the web - today Game engines: Unity / Unreal Languages: repl.it Emulators:

WASM Pollyfill

function _add($a, $b) { $a = $a | 0; $b = $b | 0; return (HEAP32[$b >> 2] | 0) + (HEAP32[$a >> 2] | 0) | 0;}

int add(int *a, int *b) { return *a + *b;}

int32 add(int32 a, int32 b) { return(add.int32(load.i32(getlocal(a)), load.i32(getlocal(b))))}

Page 18: Web Assembly - GOTO Conference · Web Assembly Nick Bray ncbray@google. Setting the stage. Native code on the web - today Game engines: Unity / Unreal Languages: repl.it Emulators:

Roadmap v1.0 (2016?)

Single thread w/ event loop. Loads fast, runs fast.

v 1.1 (2016?)

Threads! Blocking!

v1.2+ (2017?)

Exceptions, SIMD, dynamic linking, debugging, APIs

Page 19: Web Assembly - GOTO Conference · Web Assembly Nick Bray ncbray@google. Setting the stage. Native code on the web - today Game engines: Unity / Unreal Languages: repl.it Emulators:

Questions?Nick Brayncbray@google

https://github.com/WebAssembly

Page 20: Web Assembly - GOTO Conference · Web Assembly Nick Bray ncbray@google. Setting the stage. Native code on the web - today Game engines: Unity / Unreal Languages: repl.it Emulators:

Backup Slides

Page 21: Web Assembly - GOTO Conference · Web Assembly Nick Bray ncbray@google. Setting the stage. Native code on the web - today Game engines: Unity / Unreal Languages: repl.it Emulators:

call foo t1 c _

… as opposed the the status quo.foo(a+7, b)

~ is simplified ~

t0 = 7t1 = a+t0foo(t1, c)

add.int32 a t0 t1

const.int32 7 t0

Page 22: Web Assembly - GOTO Conference · Web Assembly Nick Bray ncbray@google. Setting the stage. Native code on the web - today Game engines: Unity / Unreal Languages: repl.it Emulators:

… but this is really about control flow.while (a < 10) { a = a + 1;}

loop ● block 2 ● ●

if ● ●

ge.int32 ● ●

getlocal a

const.int32 10

break 1

setlocal a ● add.int32 ● ●

getlocal a

const.int32 1

Page 23: Web Assembly - GOTO Conference · Web Assembly Nick Bray ncbray@google. Setting the stage. Native code on the web - today Game engines: Unity / Unreal Languages: repl.it Emulators:

… as opposed the the status quo.

add.int32 a t0 a

const.int32 1 t0

lt.int32 a t0 t1

const.int32 10 t0

while (a < 10) { a = a + 1;}

t1

T

F

Page 24: Web Assembly - GOTO Conference · Web Assembly Nick Bray ncbray@google. Setting the stage. Native code on the web - today Game engines: Unity / Unreal Languages: repl.it Emulators:

Process

Competing for Address Space

JSCode

Array Buffers

DOM

WASMMemory

WASMCode

JS Heap