WebAssembly is a new compilation target and virtual machine that is designed to run compiled code nearly as fast as native machine code. It aims to be a universal compilation target that runs across browsers and other environments. WebAssembly code is compiled to a binary format that is faster to parse and run than JavaScript. It integrates well with JavaScript and the web while providing better performance than asm.js for tasks like games, 3D graphics, and data parallel computation. WebAssembly is currently supported in all major browsers and provides a compilation target for C/C++ and other languages via compilers like Emscripten.
10. JavaScript conquers the world
@bmihaylov | Voxxed Days Thessaloniki 2016 10
source: https://www.codementor.io/learn-programming/beginner-programming-language-job-salary-community
102.3K
280.4K
431.7K
444.4K
512.1K
791.9K
953.9K
962.4K
1,699.7K
1,964.5K
Swift
Objective-C
C#
C
C++
PHP
Python
Ruby
Java
JavaScript
Total GitHub Projects
(as of February 2016)
20.9K
31.6K
50.5K
67.7K
90.3K
112.0K
117.9K
125.1K
191.9K
200.1K
Swift
Ruby
SQL
C
C++
Python
C#
PHP
Java
JavaScript
StackOverflow Tag Followers
(as of February 2016)
14. Issues with JavaScript
Just-In-Time compilation
Types are inferred on runtime
Variables can change types
Automated garbage collection
@bmihaylov | Voxxed Days Thessaloniki 2016 14
15. Meet asm.js
@bmihaylov | Voxxed Days Thessaloniki 2016 15
Started by Mozilla in 2013
It defines a subset of JavaScript
Avoids potential slowdowns in code
e.g., no variables with mixed type
It does only low-level assembly-like computation
exactly what compiled C/C++ needs
16. @bmihaylov | Voxxed Days Thessaloniki 2016 16
C/C++ Emscripten
.js
.js + .html
Node.js
Web Browser
17. Hello, world
@bmihaylov | Voxxed Days Thessaloniki 2016 17
…
function _main() {
var $0 = 0, $vararg_buffer = 0, label = 0, sp = 0;
sp = STACKTOP;
STACKTOP = STACKTOP + 16|0;
if ((STACKTOP|0) >= (STACK_MAX|0))
abort();
$vararg_buffer = sp; $0 = 0;
(_printf(672,$vararg_buffer)|0);
STACKTOP = sp;
return 0;
}
…
#include<stdio.h>
int main() {
printf(“Hello, Thessaloniki“);;
return 0;
}
C program (1KB) asm.js output (479KB)
18. asm.js example
@bmihaylov | Codemotion Milan 2015 18
asm.js module definition Usage in JavaScript
function CircleModule(stdlib, foreign, heap) {
"use asm";
var pi = +stdlib.Math.PI;
function area(r) {
r = r | 0;
return +(pi * r * r);
}
return { area: area };
}
// create and initialize the heap (64k)
var heap = new ArrayBuffer(0x10000);
init(heap, START, END);
// produce exports object,
// linked to AOT-compiled code
var circle = CircleModule(window, null, heap);
// calculate the area of a circle
circle.area(31);
20. Mozilla and Epic ported Unreal Engine 3 to the Web in 4 days
https://www.youtube.com/watch?v=XsyogXtyU9o
@bmihaylov | Voxxed Days Thessaloniki 2016 20
21. How compilers work
@bmihaylov | Voxxed Days Thessaloniki 2016 21
Source
program
Intermediate
Representation
Assembly or
machine
code
Frontend Backend
Static analysis
Semantic analysis
Code generation
Code optimization
25. аsm.js +++
@bmihaylov | Voxxed Days Thessaloniki 2016 25
Not a new language
Code is a subset of JavaScript
so it already runs in all browsers, if special support is missing
Code runs 2X slower than native
comparable to C# and Java
26. аsm.js ---
@bmihaylov | Voxxed Days Thessaloniki 2016 26
The parser becomes the bottleneck
once VMs optimize for it
We may want to do some different things
than what JavaScript allows us to do today
It is backed-up only by Mozilla
so far
29. What is WebAssembly
@bmihaylov | Voxxed Days Thessaloniki 2016 29
A new language
short name is wasm
Improvement to JavaScript
and the browser
Compilation target from other languages
Modular structure
Integrates with ES6 modules
30. Abstract syntax tree
@bmihaylov | Voxxed Days Thessaloniki 2016 30
WebAssembly describes an AST
e.g., it supports branches and loops
It is possible to produce it directly
or compile it from another language
Binary & textual representation
31. Textual representation
@bmihaylov | Voxxed Days Thessaloniki 2016 31
Main purpose is human consumption
No official text representation yet
most examples use s-expressions
Used in browser developer tools
when no source maps exist
32. Example textual representation
@bmihaylov | Voxxed Days Thessaloniki 2016 32
asm.js module s-expressions format
function () {
"use asm";
function add(x, y) {
x = x | 0;
y = y | 0;
return x + y | 0;
}
return { add: add };
}
(module
(export $add “add” $add)
(func $add (param $x i32) (param $y i32) (result i32)
(i32.add
(get_local $x)
(get_local $y)
)
)
)
33. Binary representation
@bmihaylov | Voxxed Days Thessaloniki 2016 33
Reduction in payload size
about 20-30% smaller than the textual representation
Faster than parsing text
about 23x faster in the tests
Allows many optimization for the code
both size and decoding speed
The main format used by the browsers
34. How to use WebAssembly
@bmihaylov | Voxxed Days Thessaloniki 2016 34
1. Write code in any language
that can be compiled to WebAssembly
2. Compile the code
3. Load the output in the browser
36. Meet Binaryen
@bmihaylov | Voxxed Days Thessaloniki 2016 36
Compiler and toolchain infrastructure library for
WebAssembly
Parse & emit WebAssembly
Represent & process WebAssembly
Interpret WebAssembly
37. Binaryen
@bmihaylov | Voxxed Days Thessaloniki 2016 37
WebAssembly is a binary format and
we integrate it with Emscripten -> hence the name
Pronounced as bi-NAIR-ee-in
Rhymes with Targaryen
Game of Thrones
38. What comes with Binaryen
@bmihaylov | Voxxed Days Thessaloniki 2016 38
The Binary shell
It can load a WebAssembly module, transform it, execute it in an interpreter, etc.
asm2wasm / wasm2asm
Compiles asm.js to WebAssembly and vice versa
s2wasm
Compiles .s files, in the format of the new LLVM backend, to WebAssembly
wasm.js
A port of Binaryen itself to JavaScript
41. Compile from other languages
@bmihaylov | Voxxed Days Thessaloniki 2016 41
C/C++ Browser
WebAssembly
X Emscripten
Y
LLVM bitcode
42. WebAssembly is not bytecode
@bmihaylov | Voxxed Days Thessaloniki 2016 42
Bytecode is linear and stack-, register-, or SSA-based
WebAssembly is binary representation of an AST
WebAssembly is not versioned
It follows the same evolution cycle as JavaScript
WebAssembly will probably lead to universal VM
44. WebAssembly vs. asm.js
@bmihaylov | Voxxed Days Thessaloniki 2016 44
Better load times
Binary format is much faster to parse than the textual representation of asm.js
Full threading support
SIMD
Single Instruction, Multiple Data
Integrated garbage collection
47. What’s next
@bmihaylov | Voxxed Days Thessaloniki 2016 47
Define the official text representation
Further reduction in binary format size
Updates on the WebAssembly JavaScript API
Better documentation
for compiler writers, tool authors, hackers, and students
49. WebAssembly and JavaScript
@bmihaylov | Voxxed Days Thessaloniki 2016 49
WebAssembly JavaScript
Games,
video & image
decoders, etc.
External libraries
(e.g., C/C++)
50. @bmihaylov | Voxxed Days Thessaloniki 2016 50
WebAssembly fills in the gaps that would
be awkward to fill with JavaScript.
Eric Elliott
“
”
photo: www.adafruit.com