Top Banner
http://localhost:8080/#1 1/67 • • •
67

BZR ZoRm - QCon 17/67 BZR ZoRm WebAssembly or wasm is a new portable, size-and load-time-efficient format suitable for compilation to the web. http ...

Jan 23, 2021

Download

Documents

dariahiddleston
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: BZR ZoRm - QCon 17/67 BZR ZoRm WebAssembly or wasm is a new portable, size-and load-time-efficient format suitable for compilation to the web. http ...

http://localhost:8080/#1 1/67

WebAssembly

and the death of JavaScript?

• • •

@ColinEberhardt, Scott Logic

Page 2: BZR ZoRm - QCon 17/67 BZR ZoRm WebAssembly or wasm is a new portable, size-and load-time-efficient format suitable for compilation to the web. http ...

http://localhost:8080/#1 2/67

A brief history of the web

Page 3: BZR ZoRm - QCon 17/67 BZR ZoRm WebAssembly or wasm is a new portable, size-and load-time-efficient format suitable for compilation to the web. http ...

http://localhost:8080/#1 3/67

JavaScript

(created in 10 days in May 1995, by Brendan Eich)

Page 4: BZR ZoRm - QCon 17/67 BZR ZoRm WebAssembly or wasm is a new portable, size-and load-time-efficient format suitable for compilation to the web. http ...

http://localhost:8080/#1 4/67

Java Applets

Page 5: BZR ZoRm - QCon 17/67 BZR ZoRm WebAssembly or wasm is a new portable, size-and load-time-efficient format suitable for compilation to the web. http ...

http://localhost:8080/#1 5/67

ActiveX

Page 6: BZR ZoRm - QCon 17/67 BZR ZoRm WebAssembly or wasm is a new portable, size-and load-time-efficient format suitable for compilation to the web. http ...

http://localhost:8080/#1 6/67

Flash

Page 7: BZR ZoRm - QCon 17/67 BZR ZoRm WebAssembly or wasm is a new portable, size-and load-time-efficient format suitable for compilation to the web. http ...

http://localhost:8080/#1 7/67

Silverlight

Page 8: BZR ZoRm - QCon 17/67 BZR ZoRm WebAssembly or wasm is a new portable, size-and load-time-efficient format suitable for compilation to the web. http ...

http://localhost:8080/#1 8/67

Dart

Page 9: BZR ZoRm - QCon 17/67 BZR ZoRm WebAssembly or wasm is a new portable, size-and load-time-efficient format suitable for compilation to the web. http ...

http://localhost:8080/#1 9/67

2018 - JavaScript (still!)

Page 10: BZR ZoRm - QCon 17/67 BZR ZoRm WebAssembly or wasm is a new portable, size-and load-time-efficient format suitable for compilation to the web. http ...

http://localhost:8080/#1 10/67

2018 - JavaScript (still!)

... but the way we are using it has changed

Page 11: BZR ZoRm - QCon 17/67 BZR ZoRm WebAssembly or wasm is a new portable, size-and load-time-efficient format suitable for compilation to the web. http ...

http://localhost:8080/#1 11/67

We are writing a lot of JavaScript

$ npx create-react-app my-app$ cd my-app$ find . -name '*.js' | xargs wc -l | tail -1 79905 total

Page 12: BZR ZoRm - QCon 17/67 BZR ZoRm WebAssembly or wasm is a new portable, size-and load-time-efficient format suitable for compilation to the web. http ...

http://localhost:8080/#1 12/67

JavaScript is the Assembly Language of the Web

Page 13: BZR ZoRm - QCon 17/67 BZR ZoRm WebAssembly or wasm is a new portable, size-and load-time-efficient format suitable for compilation to the web. http ...

http://localhost:8080/#1 13/67

JavaScript isn't a very good Assembly Language!

Page 14: BZR ZoRm - QCon 17/67 BZR ZoRm WebAssembly or wasm is a new portable, size-and load-time-efficient format suitable for compilation to the web. http ...

http://localhost:8080/#1 14/67

https://blog.mozilla.org/luke/2014/01/14/asm-js-aot-compilation-and-startup-performance/

Page 15: BZR ZoRm - QCon 17/67 BZR ZoRm WebAssembly or wasm is a new portable, size-and load-time-efficient format suitable for compilation to the web. http ...

http://localhost:8080/#1 15/67

What does JavaScript execution look like today?

https://hacks.mozilla.org/2017/02/a-cartoon-intro-to-webassembly/

Page 16: BZR ZoRm - QCon 17/67 BZR ZoRm WebAssembly or wasm is a new portable, size-and load-time-efficient format suitable for compilation to the web. http ...

http://localhost:8080/#1 16/67

the Web has become the most ubiquitousapplication platform ever, and yet by historical

accident the only natively supported programminglanguage for that platform is JavaScript!

Page 17: BZR ZoRm - QCon 17/67 BZR ZoRm WebAssembly or wasm is a new portable, size-and load-time-efficient format suitable for compilation to the web. http ...

http://localhost:8080/#1 17/67

WebAssembly

WebAssembly or wasm is a new portable, size-and load-time-efficient format suitable for

compilation to the web.

Page 18: BZR ZoRm - QCon 17/67 BZR ZoRm WebAssembly or wasm is a new portable, size-and load-time-efficient format suitable for compilation to the web. http ...

http://localhost:8080/#1 18/67

asm.js

Page 19: BZR ZoRm - QCon 17/67 BZR ZoRm WebAssembly or wasm is a new portable, size-and load-time-efficient format suitable for compilation to the web. http ...

http://localhost:8080/#1 19/67

float power(float number, int pow) { float res = number; for (int i = 0;i < pow - 1; i++) { res = res * number; } return res;}

emcc power.c -O3 -s ONLY_MY_CODE=1 -s EXPORTED_FUNCTIONS="['_power']"

"use asm";function X(a, b) { a = +a; b = b | 0; var c = 0.0, d = 0; d = b + -1 | 0; if ((b | 0) > 1) { b = 0; c = a; } else return +a; do { c = c * a; b = b + 1 | 0; } while ((b | 0) != (d | 0)); return +c;}

Page 20: BZR ZoRm - QCon 17/67 BZR ZoRm WebAssembly or wasm is a new portable, size-and load-time-efficient format suitable for compilation to the web. http ...

http://localhost:8080/#1 20/67

asm.js optimised execution

Page 21: BZR ZoRm - QCon 17/67 BZR ZoRm WebAssembly or wasm is a new portable, size-and load-time-efficient format suitable for compilation to the web. http ...

http://localhost:8080/#1 21/67

Page 22: BZR ZoRm - QCon 17/67 BZR ZoRm WebAssembly or wasm is a new portable, size-and load-time-efficient format suitable for compilation to the web. http ...

http://localhost:8080/#1 22/67

WebAssembly Roadmap

2015 - WebAssembly Community Group formed

2017 - WebAssembly MVP released

2018 - W3C public draft published

Page 23: BZR ZoRm - QCon 17/67 BZR ZoRm WebAssembly or wasm is a new portable, size-and load-time-efficient format suitable for compilation to the web. http ...

http://localhost:8080/#1 23/67

Page 24: BZR ZoRm - QCon 17/67 BZR ZoRm WebAssembly or wasm is a new portable, size-and load-time-efficient format suitable for compilation to the web. http ...

http://localhost:8080/#1 24/67

WebAssembly In Practice

Page 25: BZR ZoRm - QCon 17/67 BZR ZoRm WebAssembly or wasm is a new portable, size-and load-time-efficient format suitable for compilation to the web. http ...

http://localhost:8080/#1 25/67

WebAssembly In Practice

Page 26: BZR ZoRm - QCon 17/67 BZR ZoRm WebAssembly or wasm is a new portable, size-and load-time-efficient format suitable for compilation to the web. http ...

http://localhost:8080/#1 26/67

float power(float number, int pow) { float res = number; for (int i = 0;i < pow - 1; i++) { res = res * number; } return res;}

Page 27: BZR ZoRm - QCon 17/67 BZR ZoRm WebAssembly or wasm is a new portable, size-and load-time-efficient format suitable for compilation to the web. http ...

http://localhost:8080/#1 27/67

$ xxd add.wasm00000000: 0061 736d 0100 0000 0107 0160 027d 7f01 .asm.......`.}..00000010: 7d03 0201 0004 0401 7000 0005 0301 0001 }.......p.......00000020: 0712 0206 6d65 6d6f 7279 0200 0570 6f77 ....memory...pow00000030: 6572 0000 0a33 0131 0101 7d02 4020 0141 er...3.1..}.@ .A00000040: 0248 0d00 2001 417f 6a21 0120 0021 0203 .H.. .A.j!. .!..00000050: 4020 0220 0094 2102 2001 417f 6a22 010d @ . ..!. .A.j"..00000060: 000b 2002 0f0b 2000 0b .. ... ..

Page 28: BZR ZoRm - QCon 17/67 BZR ZoRm WebAssembly or wasm is a new portable, size-and load-time-efficient format suitable for compilation to the web. http ...

http://localhost:8080/#1 28/67

(module (table 0 anyfunc) (memory $0 1) (export "memory" (memory $0)) (export "power" (func $power)) (func $power (param $0 f32) (param $1 i32) (result f32) (local $2 f32) (block $label$0 (br_if $label$0 (i32.lt_s (get_local $1) (i32.const 2) ) ) (set_local $1 (i32.add (get_local $1) (i32.const -1) ) ) (set_local $2 (get_local $0) ) (loop $label$1 (set_local $2 (f32.mul (get_local $2) (get_local $0) ) ) (br_if $label$1 (tee_local $1 (i32.add (get_local $1) (i32.const -1) ) ) ) ) (return (get_local $2) ) ) (get_local $0) ))

Page 29: BZR ZoRm - QCon 17/67 BZR ZoRm WebAssembly or wasm is a new portable, size-and load-time-efficient format suitable for compilation to the web. http ...

http://localhost:8080/#1 29/67

// read the binary into a bufferconst fs = require("fs");const buf = fs.readFileSync("./add.wasm");

// create a wasm moduleconst wasmModule = new WebAssembly.Module(new Uint8Array(buf));

// construct an instance of the moduleconst wasmInstance = new WebAssembly.Instance(wasmModule);

// invoke the exported functionconst result = wasmInstance.exports.power(2, 3)console.log(result);

Page 30: BZR ZoRm - QCon 17/67 BZR ZoRm WebAssembly or wasm is a new portable, size-and load-time-efficient format suitable for compilation to the web. http ...

http://localhost:8080/#1 30/67

char *message = "hello wasm!";

char *getMessageRef(){ return message;}

Page 31: BZR ZoRm - QCon 17/67 BZR ZoRm WebAssembly or wasm is a new portable, size-and load-time-efficient format suitable for compilation to the web. http ...

http://localhost:8080/#1 31/67

const { TextDecoder } = require("util");

// read the binary into a bufferconst fs = require("fs");const buf = fs.readFileSync("./string.wasm");

// create a module and an instanceconst wasmModule = new WebAssembly.Module(new Uint8Array(buf));const wasmInstance = new WebAssembly.Instance(wasmModule);

// obtain a reference to linear and read the stringconst linearMemory = wasmInstance.exports.memory;const offset = wasmInstance.exports.getMessageRef();const buffer = new Uint8Array(linearMemory.buffer, offset, 12);

// decode and logconst str = new TextDecoder("utf-8").decode(buffer);console.log(str);

Page 32: BZR ZoRm - QCon 17/67 BZR ZoRm WebAssembly or wasm is a new portable, size-and load-time-efficient format suitable for compilation to the web. http ...

http://localhost:8080/#1 32/67

WebAssembly Architecture

A stack machine, 4 types, 67 instructions

Designed to support streaming compilation

Simple validation rules

Exports / imports functions

Linear memory is shared with JavaScript

Page 33: BZR ZoRm - QCon 17/67 BZR ZoRm WebAssembly or wasm is a new portable, size-and load-time-efficient format suitable for compilation to the web. http ...

http://localhost:8080/#1 33/67

WebAssembly Future

Garbage collector

Threads

Host bindings

SIMD

Exception handling

Page 34: BZR ZoRm - QCon 17/67 BZR ZoRm WebAssembly or wasm is a new portable, size-and load-time-efficient format suitable for compilation to the web. http ...

http://localhost:8080/#1 34/67

WebAssembly Language Support

(and what people are doing with it)

Page 35: BZR ZoRm - QCon 17/67 BZR ZoRm WebAssembly or wasm is a new portable, size-and load-time-efficient format suitable for compilation to the web. http ...

http://localhost:8080/#1 35/67

C / C++

Emscripten

Based on LLVM

Originally used to create asm.js

Page 36: BZR ZoRm - QCon 17/67 BZR ZoRm WebAssembly or wasm is a new portable, size-and load-time-efficient format suitable for compilation to the web. http ...

http://localhost:8080/#1 36/67

https://pspdfkit.com/blog/2017/webassembly-a-new-hope/

Page 37: BZR ZoRm - QCon 17/67 BZR ZoRm WebAssembly or wasm is a new portable, size-and load-time-efficient format suitable for compilation to the web. http ...

http://localhost:8080/#1 37/67

https://mbbill.github.io/JSC.js/

Page 38: BZR ZoRm - QCon 17/67 BZR ZoRm WebAssembly or wasm is a new portable, size-and load-time-efficient format suitable for compilation to the web. http ...

http://localhost:8080/#1 38/67

Page 39: BZR ZoRm - QCon 17/67 BZR ZoRm WebAssembly or wasm is a new portable, size-and load-time-efficient format suitable for compilation to the web. http ...

http://localhost:8080/#1 39/67

Java / C#

More challenging, these languages require a GC

Page 40: BZR ZoRm - QCon 17/67 BZR ZoRm WebAssembly or wasm is a new portable, size-and load-time-efficient format suitable for compilation to the web. http ...

http://localhost:8080/#1 40/67

Java / C#

More challenging, these languages require a GC

Blazor, experimental project, using Mono

Testing interpreted mode, vs. AOT (with runtime forGC etc …)

Blazor is an SPA framework

Page 41: BZR ZoRm - QCon 17/67 BZR ZoRm WebAssembly or wasm is a new portable, size-and load-time-efficient format suitable for compilation to the web. http ...

http://localhost:8080/#1 41/67

JavaScript

Needs a GC and isn’t statically typed

Page 42: BZR ZoRm - QCon 17/67 BZR ZoRm WebAssembly or wasm is a new portable, size-and load-time-efficient format suitable for compilation to the web. http ...

http://localhost:8080/#1 42/67

JavaScript

Needs a GC and isn’t statically typed

“Walt is a JavaScript-like syntax for WebAssembly textformat”

Page 43: BZR ZoRm - QCon 17/67 BZR ZoRm WebAssembly or wasm is a new portable, size-and load-time-efficient format suitable for compilation to the web. http ...

http://localhost:8080/#1 43/67

JavaScript

Needs a GC and isn’t statically typed

“Walt is a JavaScript-like syntax for WebAssembly textformat”

AssemblyScript - a TypeScript to WebAssembly compiler

Awaiting GC before it can really become powerful

Page 44: BZR ZoRm - QCon 17/67 BZR ZoRm WebAssembly or wasm is a new portable, size-and load-time-efficient format suitable for compilation to the web. http ...

http://localhost:8080/#1 44/67

https://bl.ocks.org/ColinEberhardt/6ceb7ca74aabac9c8534d7120d31b382

Page 45: BZR ZoRm - QCon 17/67 BZR ZoRm WebAssembly or wasm is a new portable, size-and load-time-efficient format suitable for compilation to the web. http ...

http://localhost:8080/#1 45/67

simulation = d3.forceSimulation() .force("link", d3.forceLink().id(function(d) { return d.id; })) .force("charge", d3.forceManyBody()) .force("center", d3.forceCenter(width / 2, height / 2));

function ticked() { simulation.tick(); link .attr('x1', d => d.source.x) .attr('y1', d => d.source.y) .attr('x2', d => d.target.x) .attr('y2', d => d.target.y);

node .attr('cx', d => d.x) .attr('cy', d => d.y);}

setInterval(ticked, 25);

Page 46: BZR ZoRm - QCon 17/67 BZR ZoRm WebAssembly or wasm is a new portable, size-and load-time-efficient format suitable for compilation to the web. http ...

http://localhost:8080/#1 46/67

Rust

Doesn’t require a GC

Originally used Emscripten, but moved to a simplertoolchain

We're poised to be THE language of choice forwasm.

Page 47: BZR ZoRm - QCon 17/67 BZR ZoRm WebAssembly or wasm is a new portable, size-and load-time-efficient format suitable for compilation to the web. http ...

http://localhost:8080/#1 47/67

http://blog.scottlogic.com/2017/12/13/chip8-emulator-webassembly-rust.html

Page 48: BZR ZoRm - QCon 17/67 BZR ZoRm WebAssembly or wasm is a new portable, size-and load-time-efficient format suitable for compilation to the web. http ...

http://localhost:8080/#1 48/67

https://hacks.mozilla.org/2018/01/oxidizing-source-maps-with-rust-and-webassembly/

Page 49: BZR ZoRm - QCon 17/67 BZR ZoRm WebAssembly or wasm is a new portable, size-and load-time-efficient format suitable for compilation to the web. http ...

http://localhost:8080/#1 49/67

Crystal Ball Gazing

����

Page 50: BZR ZoRm - QCon 17/67 BZR ZoRm WebAssembly or wasm is a new portable, size-and load-time-efficient format suitable for compilation to the web. http ...

http://localhost:8080/#1 50/67

2018

Rust, C, C++ used in production for performance critical,algorithmic tasks

Page 51: BZR ZoRm - QCon 17/67 BZR ZoRm WebAssembly or wasm is a new portable, size-and load-time-efficient format suitable for compilation to the web. http ...

http://localhost:8080/#1 51/67

2018

Rust, C, C++ used in production for performance critical,algorithmic tasks

Webpack

Page 52: BZR ZoRm - QCon 17/67 BZR ZoRm WebAssembly or wasm is a new portable, size-and load-time-efficient format suitable for compilation to the web. http ...

http://localhost:8080/#1 52/67

2018

Rust, C, C++ used in production for performance critical,algorithmic tasks

Webpack

Java, C#, Typescript lots of creative experiments / POCs

Page 53: BZR ZoRm - QCon 17/67 BZR ZoRm WebAssembly or wasm is a new portable, size-and load-time-efficient format suitable for compilation to the web. http ...

http://localhost:8080/#1 53/67

2018

Rust, C, C++ used in production for performance critical,algorithmic tasks

Webpack

Java, C#, Typescript lots of creative experiments / POCs

Native node modules

Page 54: BZR ZoRm - QCon 17/67 BZR ZoRm WebAssembly or wasm is a new portable, size-and load-time-efficient format suitable for compilation to the web. http ...

http://localhost:8080/#1 54/67

2018

Rust, C, C++ used in production for performance critical,algorithmic tasks

Webpack

Java, C#, Typescript lots of creative experiments / POCs

Native node modules

GC support

Page 55: BZR ZoRm - QCon 17/67 BZR ZoRm WebAssembly or wasm is a new portable, size-and load-time-efficient format suitable for compilation to the web. http ...

http://localhost:8080/#1 55/67

2019

Host bindings, SIMD, threading, ...

Page 56: BZR ZoRm - QCon 17/67 BZR ZoRm WebAssembly or wasm is a new portable, size-and load-time-efficient format suitable for compilation to the web. http ...

http://localhost:8080/#1 56/67

2019

Host bindings, SIMD, threading, ...

Java, C# become production ready

Page 57: BZR ZoRm - QCon 17/67 BZR ZoRm WebAssembly or wasm is a new portable, size-and load-time-efficient format suitable for compilation to the web. http ...

http://localhost:8080/#1 57/67

2019

Host bindings, SIMD, threading, ...

Java, C# become production ready

Another wave of mobile, desktop and server-side UIframeworks will re-target the web

write once, run everywhere

Page 58: BZR ZoRm - QCon 17/67 BZR ZoRm WebAssembly or wasm is a new portable, size-and load-time-efficient format suitable for compilation to the web. http ...

http://localhost:8080/#1 58/67

2019

Host bindings, SIMD, threading, ...

Java, C# become production ready

Another wave of mobile, desktop and server-side UIframeworks will re-target the web

write once, run everywhere

Performance gains fail to materialise, with backlashfrom early adopters

Page 59: BZR ZoRm - QCon 17/67 BZR ZoRm WebAssembly or wasm is a new portable, size-and load-time-efficient format suitable for compilation to the web. http ...

http://localhost:8080/#1 59/67

2019

Host bindings, SIMD, threading, ...

Java, C# become production ready

Another wave of mobile, desktop and server-side UIframeworks will re-target the web

write once, run everywhere

Performance gains fail to materialise, with backlashfrom early adopters

Heavyweight productivity tools start moving to the web(e.g. Photoshop, AutoCAD)

Page 60: BZR ZoRm - QCon 17/67 BZR ZoRm WebAssembly or wasm is a new portable, size-and load-time-efficient format suitable for compilation to the web. http ...

http://localhost:8080/#1 60/67

2020 - and beyond

JavaScript will compile directly to WebAssembly, "usewasm"

Page 61: BZR ZoRm - QCon 17/67 BZR ZoRm WebAssembly or wasm is a new portable, size-and load-time-efficient format suitable for compilation to the web. http ...

http://localhost:8080/#1 61/67

2020 - and beyond

JavaScript will compile directly to WebAssembly, "usewasm"

Native Android apps die-out in favour of ProgressiveWeb Apps (PWA) running on WebAssembly

Page 62: BZR ZoRm - QCon 17/67 BZR ZoRm WebAssembly or wasm is a new portable, size-and load-time-efficient format suitable for compilation to the web. http ...

http://localhost:8080/#1 62/67

2020 - and beyond

JavaScript will compile directly to WebAssembly, "usewasm"

Native Android apps die-out in favour of ProgressiveWeb Apps (PWA) running on WebAssembly

Windows store moves to PWA / WASM

Page 63: BZR ZoRm - QCon 17/67 BZR ZoRm WebAssembly or wasm is a new portable, size-and load-time-efficient format suitable for compilation to the web. http ...

http://localhost:8080/#1 63/67

2020 - and beyond

JavaScript will compile directly to WebAssembly, "usewasm"

Native Android apps die-out in favour of ProgressiveWeb Apps (PWA) running on WebAssembly

Windows store moves to PWA / WASM

A new DOM alternative will emerge?

Page 64: BZR ZoRm - QCon 17/67 BZR ZoRm WebAssembly or wasm is a new portable, size-and load-time-efficient format suitable for compilation to the web. http ...

http://localhost:8080/#1 64/67

2020 - and beyond

JavaScript will compile directly to WebAssembly, "usewasm"

Native Android apps die-out in favour of ProgressiveWeb Apps (PWA) running on WebAssembly

Windows store moves to PWA / WASM

A new DOM alternative will emerge?

JavaScript's monopoly will be lost, and it's popularitywill fade

Page 65: BZR ZoRm - QCon 17/67 BZR ZoRm WebAssembly or wasm is a new portable, size-and load-time-efficient format suitable for compilation to the web. http ...

http://localhost:8080/#1 65/67

2020 - and beyond

JavaScript will compile directly to WebAssembly, "usewasm"

Native Android apps die-out in favour of ProgressiveWeb Apps (PWA) running on WebAssembly

Windows store moves to PWA / WASM

A new DOM alternative will emerge?

JavaScript's monopoly will be lost, and it's popularitywill fade

The ubiquity of the web extends further still

Page 66: BZR ZoRm - QCon 17/67 BZR ZoRm WebAssembly or wasm is a new portable, size-and load-time-efficient format suitable for compilation to the web. http ...

http://localhost:8080/#1 66/67

WebAssembly

and the death of JavaScript?

• • •

Colin Eberhardt

Page 67: BZR ZoRm - QCon 17/67 BZR ZoRm WebAssembly or wasm is a new portable, size-and load-time-efficient format suitable for compilation to the web. http ...

http://localhost:8080/#1 67/67