Slide 1

Slide 1 text

Do things faster & better with WebAssembly @sendilkumarn

Slide 2

Slide 2 text

Sendil Kumar • Full stack developer @Xebialabs • Core dev team member @JHipster • Team member @webpack • Part of rust-wasm Working Group • Big open source lover & enthusiast

Slide 3

Slide 3 text

@sendilkumarn You can reach me at

Slide 4

Slide 4 text

Fullstack devs ?

Slide 5

Slide 5 text

C / C++ devs ?

Slide 6

Slide 6 text

Rust devs ?

Slide 7

Slide 7 text

Faster More control Safety Types Closer to machine

Slide 8

Slide 8 text

! JS devs ?

Slide 9

Slide 9 text

What you do to increase the Performance of your App ?

Slide 10

Slide 10 text

Minify Async & Defer Reduce dom manipulation Profile JS Engine based optimisations Immutability Type system Code splitting Service worker

Slide 11

Slide 11 text

Let us do a performance question

Slide 12

Slide 12 text

Which has higher performance? function test() { let arr = []; arr.push(1); arr.push(2); arr.push(3); } function test() { let arr = new Array(3); arr.push(1); arr.push(2); arr.push(3); }

Slide 13

Slide 13 text

This has higher performance function test() { let arr = []; arr.push(1); arr.push(2); arr.push(3); } function test() { let arr = new Array(3); arr.push(1); arr.push(2); arr.push(3); }

Slide 14

Slide 14 text

But why ? Packed & Holey Array https://v8project.blogspot.nl/2017/09/elements-kinds-in-v8.html

Slide 15

Slide 15 text

No content

Slide 16

Slide 16 text

Uncertainties

Slide 17

Slide 17 text

No content

Slide 18

Slide 18 text

Most of the profiling, needs you to know more about the internals of JS engine.

Slide 19

Slide 19 text

Optional Arguments Packed/Holey Array Monomorphisation Micro- benchmarking Engine based tunings Known engine shortcomings Time- consuming

Slide 20

Slide 20 text

⚙ Maintainability ⚙

Slide 21

Slide 21 text

Tradeoff between Uncertainty & Performance

Slide 22

Slide 22 text

How better will it be to have the consistency & speed of native code in browsers?

Slide 23

Slide 23 text

WebAssembly Binary encoded - Stack machine

Slide 24

Slide 24 text

In other words, you can compile your native code and make them run in your browser.

Slide 25

Slide 25 text

No content

Slide 26

Slide 26 text

Fast & Efficient

Slide 27

Slide 27 text

Fast & Efficient Safe

Slide 28

Slide 28 text

Fast & Efficient Safe Debuggable

Slide 29

Slide 29 text

Fast & Efficient Safe Debuggable Open web platform

Slide 30

Slide 30 text

“WebAssembly is not a replacement of JAVASCRIPT” integrating well and being symbiotic

Slide 31

Slide 31 text

How JS works now inside JS Engine

Slide 32

Slide 32 text

PARSE COMPILE OPTIMIZE Re-OPTIMIZE EXECUTE GC Javascript AST

Slide 33

Slide 33 text

PARSE COMPILE OPTIMIZE Re-OPTIMIZE EXECUTE GC Profiler COMPILE 0101010101010

Slide 34

Slide 34 text

PARSE COMPILE OPTIMIZE Re-OPTIMIZE EXECUTE GC Profiler

Slide 35

Slide 35 text

PARSE COMPILE OPTIMIZE Re-OPTIMIZE EXECUTE GC Profiler

Slide 36

Slide 36 text

PARSE COMPILE OPTIMIZE Re-OPTIMIZE EXECUTE GC Profiler

Slide 37

Slide 37 text

How WASM Works?

Slide 38

Slide 38 text

DECODE COMPILE EXECUTE 0101010101010 0101010101010

Slide 39

Slide 39 text

DECODE COMPILE EXECUTE

Slide 40

Slide 40 text

DECODE COMPILE EXECUTE

Slide 41

Slide 41 text

Simple Example in Rust

Slide 42

Slide 42 text

#[no_mangle] pub extern fn add_one(x: i32) -> i32{ x+1 } RUST

Slide 43

Slide 43 text

rustc +nightly \ — —target wasm32-unknown-unknown \ -O test.rs

Slide 44

Slide 44 text

WASM Raw binary format

Slide 45

Slide 45 text

WASM MODULE IMPORT TABLE MEMORY GLOBAL EXPORT ELEMENT DATA START FUNCTION & CODE

Slide 46

Slide 46 text

WASM IMPORT EXPORT FUNCTION & CODE (import …) (export …) (func …)

Slide 47

Slide 47 text

WASM MEMORY GLOBAL START (start $start_function) The first function to be loaded in the module (global type mutability expr) (memory init-size max-size(opt))

Slide 48

Slide 48 text

WASM TABLE ELEMENT DATA Data -> linear array of memory indexes GC references, raw OS handles, or native pointers Allows module to initialise the elements

Slide 49

Slide 49 text

(module (type $t0 (func (param i32) (result i32))) (func $add_one (export “add_one”) (type $0) (param $p0 i32) (result i32) get_local $p0 i32.const 1 i32.add ) (table $T0 0 anyfunc) (memory $memory (export “memory”) 17) (data (i32.const 4) “\10\00\10\00”)) WAST

Slide 50

Slide 50 text

(module ) Module

Slide 51

Slide 51 text

(type $t0 (func (param i32) (result i32) ) ) Type

Slide 52

Slide 52 text

(func $add_one (export “add_one”) (type $0) (param $p0 i32) (result i32) get_local $p0 i32.const 1 i32.add ) Actual function

Slide 53

Slide 53 text

(table $T0 0 anyfunc) Table

Slide 54

Slide 54 text

(memory $memory (export “memory”) 17) Memory

Slide 55

Slide 55 text

(data (i32.const 4) “\10\00\10\00”) ) Data

Slide 56

Slide 56 text

How to load them?

Slide 57

Slide 57 text

Using fetch…

Slide 58

Slide 58 text

Using fetch… fetch('test.wasm').then(response => response.arrayBuffer() ).then(bytes => WebAssembly.instantiate(bytes, importObject) ).then(result => result.instance.exports.add_one() );

Slide 59

Slide 59 text

Using compile streaming…

Slide 60

Slide 60 text

Using compile streaming… WebAssembly.compileStreaming((fetch(‘test.wasm’)) .then(module => WebAssembly.instantiate(module, importObject) ).then(result => result.instance.exports.add_one() );

Slide 61

Slide 61 text

Using instantiate streaming… ⚠Few browsers yet to support

Slide 62

Slide 62 text

Using instantiate streaming… WebAssembly.instantiateStreaming( (fetch(‘test.wasm’), importObj) .then(result => result.instance.exports.add_one() ); ⚠Few browsers yet to support

Slide 63

Slide 63 text

Streaming compilation speeds up the wasm performance by 10-15 times Streaming compilation will be coming to JS…

Slide 64

Slide 64 text

ESM spec for WASM ⚠Still in very early stages, and will be implemented

Slide 65

Slide 65 text

How to include them in your App? Demo time >>>

Slide 66

Slide 66 text

Future Garbage Collection DOM Manipulation Bulk memory operations Threads Exception handling

Slide 67

Slide 67 text

Questions?

Slide 68

Slide 68 text

No content

Slide 69

Slide 69 text

Happy Hacking