Slide 1

Slide 1 text

WebAssembly Is it the future for the Web?

Slide 2

Slide 2 text

Bilal Çınarlı Sr. Software Engineer @Adidas @bcinarli

Slide 3

Slide 3 text

No content

Slide 4

Slide 4 text

HTML initially released in 1993

Slide 5

Slide 5 text

JavaScript is first appeared at the end of 1995

Slide 6

Slide 6 text

CSS initially released at the end of 1997

Slide 7

Slide 7 text

Slide 8

Slide 8 text

In the beginning Web was mainly used for displaying content

Slide 9

Slide 9 text

Then e-commerce came in to place. Web has started to be used for selling

Slide 10

Slide 10 text

We did not stop there!

Slide 11

Slide 11 text

We started to use it for playing Games, processing Images, processing Documents etc…

Slide 12

Slide 12 text

No content

Slide 13

Slide 13 text

JS limitations on performance

Slide 14

Slide 14 text

WebAssembly

Slide 15

Slide 15 text

WebAssembly (abbreviated Wasm) is a new type of code that can be run in modern web browsers and provides new features and major gains in performance.

Slide 16

Slide 16 text

It is a binary instruction format for a stack-based virtual machine

Slide 17

Slide 17 text

It works with inside and outside of the Browser

Slide 18

Slide 18 text

Slide 19

Slide 19 text

WebAssembly code can be executed at near-native speed across different platforms by taking advantage of common hardware capabilities.

Slide 20

Slide 20 text

No content

Slide 21

Slide 21 text

WebAssembly is a low-level assembly language, but it does have a human-readable text format that allows code to be written, viewed, and debugged by hand.

Slide 22

Slide 22 text

No content

Slide 23

Slide 23 text

WebAssembly is specified to be run in a safe, sandboxed execution environment. Like other web code, it will enforce the browser's same-origin and permissions policies

Slide 24

Slide 24 text

WebAssembly is designed so that it plays nicely with other web technologies and maintains backwards compatibility

Slide 25

Slide 25 text

How to Use?

Slide 26

Slide 26 text

Compiling your low-level code to WebAssembly

Slide 27

Slide 27 text

➡ Porting a C/C++ application with Emscripten ➡ Writing WebAssembly by hand ➡ Writing a Rust application to target it to WebAssembly ➡ AssemblyScript to compile it WebAssembly

Slide 28

Slide 28 text

// hello.c #include int main() { printf("hello, world!\n"); return 0; } Sample C++

Slide 29

Slide 29 text

(module (type $FUNCSIG$ii (func (param i32) (result i32))) (import "env" "puts" (func $puts (param i32) (result i32))) (table 0 anyfunc) (memory $0 1) (data (i32.const 16) "hello, world!\00") (export "memory" (memory $0)) (export "main" (func $main)) (func $main (; 1 ;) (result i32) (drop (call $puts (i32.const 16) ) ) (i32.const 0) ) ) Wat File

Slide 30

Slide 30 text

wasm-function[1]: sub rsp, 0x18 ; 0x000000 48 83 ec 18 cmp qword ptr [r14 + 0x28], rsp ; 0x000004 49 39 66 28 jae 0x56 ; 0x000008 0f 83 48 00 00 00 0x00000e: mov edi, 0x10 ; 0x00000e bf 10 00 00 00 mov qword ptr [rsp], r14 ; 0x000013 4c 89 34 24 mov rax, qword ptr [r14 + 0x30] ; 0x000017 49 8b 46 30 mov r14, qword ptr [r14 + 0x38] ; 0x00001b 4d 8b 76 38 mov r15, qword ptr [r14 + 0x18] ; 0x00001f 4d 8b 7e 18 call rax ; 0x000023 ff d0 mov r14, qword ptr [rsp] ; 0x000025 4c 8b 34 24 mov r15, qword ptr [r14 + 0x18] ; 0x000029 4d 8b 7e 18 xor eax, eax ; 0x00002d 33 c0 nop ; 0x00002f 66 90 add rsp, 0x18 ; 0x000031 48 83 c4 18 ret ; 0x000035 c3 wasm-function[0]: sub rsp, 0x18 ; 0x000000 48 83 ec 18 mov qword ptr [rsp], r14 ; 0x000004 4c 89 34 24 mov rax, qword ptr [r14 + 0x30] ; 0x000008 49 8b 46 30 mov r14, qword ptr [r14 + 0x38] ; 0x00000c 4d 8b 76 38 mov r15, qword ptr [r14 + 0x18] ; 0x000010 4d 8b 7e 18 call rax ; 0x000014 ff d0 mov r14, qword ptr [rsp] ; 0x000016 4c 8b 34 24 mov r15, qword ptr [r14 + 0x18] ; 0x00001a 4d 8b 7e 18 nop ; 0x00001e 66 90 add rsp, 0x18 ; 0x000020 48 83 c4 18 ret ; 0x000024 c3 Firefox WebAssembly

Slide 31

Slide 31 text

Wasm Module JavaScript Document Fetch Execute

Slide 32

Slide 32 text

fetch('/module.wasm') .then((response) =>" { return response.arrayBuffer(); }) .then((wasmBuffer) =>" { return WebAssembly.instantiate(wasmBuffer); }) .then(({ instance, module }) =>" { console.log(instance.exports.add(1, 2)); }); Loading Wasm to JS

Slide 33

Slide 33 text

Common Misconceptions

Slide 34

Slide 34 text

WebAssembly runs inside its own separate Virtual Machine In the browser, shared the same runtime with JS

Slide 35

Slide 35 text

WebAssembly runs inside a separate thread Uses the same execution thread as with JavaScript

Slide 36

Slide 36 text

WebAssembly directly runs on the processor It is platform agnostic. Runs through JS Engine

Slide 37

Slide 37 text

WebAssembly will replace JavaScript It provides to near-native performance for heavy processes. Not to created to replace but work along with JS

Slide 38

Slide 38 text

Resources: https://developer.mozilla.org/en-US/docs/WebAssembly/ Concepts https://webassembly.org/

Slide 39

Slide 39 text

Thank you