WebAssembly
The journey
Elia Maino - Willian Silva
BrazilJS Conf 2017
Who are we?
Willian Elia
@wmsbill @eliamain
Senior Junior
WE’RE HIRING!!
What is WebAssembly?
What’s WebAssembly?
New binary format
Run compiled programs (C, C++, Rust) on a
browser
Works alongside Javascript
Performance and flexibility
API
Looks interesting, right?
The Journey
BrazilJS 2015
+
JSConfEU 2017
+
+
The journey begun
Our POC: John
Conway’s game of life
Game of life
ZERO PLAYER game
Matrix, each cell can be
either DEAD or ALIVE
The only external input
is the INITIAL STATE
Alive Dead
Game of life
CURRENT STATUS NEIGHBOURS COUNT NEXT STATUS
<
( )
>
A good example if we
use big matrix
Our algorithm
1.Create a big matrix randomly filled with 0
and 1
2.Render the initial state and send it to the
environment component
3.Generate and render the next state (loop)
Three approaches
}
Same algorithm
O(n*m) complexity
Render part in common
VANILLA JS
WEBASSEMBLY
WEB WORKERS
Vanilla JS
A lot of functions
function getNextState(currentState, width, height) {
…
}
function getLineCount(currentState, column, bounds) {
…
}
function createBounds(width, height) {
…
}
…
Results
Vanilla JS results
Next state calculation between 9ms and 4ms
Why so many variations
in the results?
JIT
JIT: just in time compiler
Cold code -> Interpreter
Warm code -> baseline compiler
Hot code -> optimising compiler
JIT: just in time compiler
Cold code -> Interpreter
Warm code -> baseline compiler
Hot code -> optimising compiler
JIT: just in time compiler
Cold code -> Interpreter
Warm code -> baseline compiler
Hot code -> optimising compiler
JIT: Code life cycle
1. Parse
2. Compile
3. Optimize (de-optimize)
4. Execute
5. Garbage Collector
WebAssembly
WebAssembly is fast
Parse Compile Optimize Execute GC
Decode
Compile
+
Optimize
Execute
JS
WASM
WebAssembly is fast
WASM is more compact -> Faster FETCH of the source
WASM is closer to machine code -> Faster DECODING,
COMPILING and OPTIMISING
No need to RE-OPTIMISE
No garbage collection
So WebAssembly
sounds fast, let’s see
how to use it
How to run WASM modules
Current situation: not possible to run WASM
modules on their own
Need for some Javascript glue
WebAssembly JS API
1. Fetch the module binary
2. Instantiate it
3. Access exported functionalities
fetch('module.wasm').then(response =>
 response.arrayBuffer()
).then(bytes =>
 WebAssembly.instantiate(bytes, importObject)
).then(results => {
 // Do something with the compiled results!
});
How to generate a
WASM file
Compile C to WASM
+JS WASM
v 1.37
emcc environment.c -o environment.js -s WASM=1
Then we can simply
import the generated
JS code as a module
Export functions to JS
Keyword EMSCRIPTEN_KEEPALIVE
EMSCRIPTEN_KEEPALIVE
void getNextState(int width, int
height) {
…
}
Expose only the interface of the WASM module to JS
What about
WebAssembly memory?
How can we access it?
We want to reproduce
the same logic of our
Vanilla JS
implementation in C
The C environment has
to be initialised with the
first state of the game
Memory management
Emscripten provide three useful functions
to manage WebAssembly memory
_malloc(memoryNeeded)
getValue(ptr, type)
setValue(ptr, value, type)
Our JS code has a
reference to the C
memory containing the
next state
Write
Allocate
Read
Write
Too many memory
access from JS
Reduce memory access
One memory allocation on loading
One memory write on loading
One memory read on each iteration
We measured the
performance again…
The WASM
implementation was
still slower
WebAssembly performance
Still slightly slower than Vanilla JS
Why??
Why was WASM slower?
Further investigation
Simple counter test
Results confirmed
JIT handover
“Currently, calling a WebAssembly
function in JS code is slower than it
needs to be. The JIT doesn’t know how
to deal directly with WebAssembly, so it
has to route the WebAssembly to
something that does.
…
This can be up to 100x slower than it
would be if the JIT knew how to handle it
directly.
”
Lin Clark
JIT handover
Web Workers
Parallel threads
Promise.all()
Results
Web workers results
Next state calculation between 3ms and 2ms
Shared Array Buffer?
The future of
WebAssembly
Browser support
SHIPPED 11 SOON
Future features
Formal Specification

Threads supports

SIMD acronym to Single Instruction Multiple Data (back to Stage 3 on TC39) 

Exception handling

Garbage collection

Direct DOM access

ES6 Module integration

Learn more
https://github.com/eliamaino-fp/webassembly-js
https://braziljs.org/blog/iniciando-com-webassembly-parte-1/
http://devnaestrada.com.br/2017/07/07/web-assembly.html
https://github.com/mbasso/awesome-wasm/
twitter.com/wmsbill
twitter.com/eliamain
Takeaways
“Learn one, do one, teach
one”
Obrigado!
Grazie!
Thank you!
WE’RE HIRING!!

WebAssembly - The Journey (BrazilJS 2017 talk)