Hands-on-lab: Hands on WebAssembly

A presentation at SophiaConf in June 2021 in Sophia Antipolis, France by Horacio Gonzalez

Slide 1

Slide 1

Hands-on WebAssembly Horacio Gonzalez 2021-06-24

Slide 2

Slide 2

Who are we? Introducing myself and introducing OVH OVHcloud

Slide 3

Slide 3

Horacio Gonzalez @LostInBrittany Spaniard lost in Brittany, developer, dreamer and all-around geek Flutter

Slide 4

Slide 4

OVHcloud: A global leader Web Cloud & Telcom 30 Data Centers in 12 locations 1 Million+ Servers produced since 1999 Private Cloud 34 Points of Presence on a 20 TBPS Bandwidth Network 1.5 Million Customers across 132 countries Public Cloud 2200 Employees worldwide 3.8 Million Websites hosting Storage 115K Private Cloud VMS running 1.5 Billion Euros Invested since 2016 300K Public Cloud instances running P.U.E. 1.09 Energy efficiency indicator 380K Physical Servers running in our data centers 20+ Years in Business Disrupting since 1999 Network & Security

Slide 5

Slide 5

How is the codelab structured? What are we coding today?

Slide 6

Slide 6

A GitHub repository https://github.com/LostInBrittany/wasm-codelab

Slide 7

Slide 7

Nothing to install Using WebAssembly Explorer and WebAssembly Studio

Slide 8

Slide 8

Only additional tool: a web server Because of the browser security model

Slide 9

Slide 9

Procedure: follow the steps Step by step

Slide 10

Slide 10

But before coding, let’s speak What’s this WebAssembly thing?

Slide 11

Slide 11

Did I say WebAssembly? Wasm for friends…

Slide 12

Slide 12

WebAssembly, what’s that?

Slide 13

Slide 13

A low-level binary format Not a programming language, a compilation target

Slide 14

Slide 14

That runs on a stack-based virtual machine A portable binary format that runs on all modern browsers… but also on NodeJS and elsewhere!!

Slide 15

Slide 15

With several key advantages

Slide 16

Slide 16

But above all… Wasm is not meant to replace JavaScript

Slide 17

Slide 17

Who is using WebAssembly today? And many more others…

Slide 18

Slide 18

A bit of history Remembering the past to better understand the present

Slide 19

Slide 19

Executing other languages in the browser A long story, with many failures…

Slide 20

Slide 20

2012 - From C to JS: enter emscripten Passing by LLVM pivot

Slide 21

Slide 21

2013 - Generated JS is slow… Let’s use only a strict subset of JS: asm.js Only features adapted to AOT optimization

Slide 22

Slide 22

WebAssembly project Joint effort

Slide 23

Slide 23

Hello W(asm)orld My first WebAssembly program

Slide 24

Slide 24

I don’t want to install a compiler now… Let’s use Wasm Explorer https://mbebenita.github.io/WasmExplorer/

Slide 25

Slide 25

Let’s begin with the a simple function WAT: WebAssembly Text Format Human readable version of the .wasm binary

Slide 26

Slide 26

Download the binary .wasm file Now we need to call it from JS…

Slide 27

Slide 27

Instantiating the Wasm 1. Get the .wasm binary file into an array buffer 2. Compile the bytes into a WebAssembly module 3. Instantiate the WebAssembly module

Slide 28

Slide 28

Instantiating the WASM

Slide 29

Slide 29

Loading the squarer function

Slide 30

Slide 30

Using it! Directly from the browser console (it’s a simple demo…)

Slide 31

Slide 31

You sold us a codelab! Stop speaking and let us code

Slide 32

Slide 32

You can do steps 01 and 02 now Let’s code, mates!

Slide 33

Slide 33

Some use cases What can I do with it?

Slide 34

Slide 34

Tapping into other languages ecosystems Don’t rewrite libs anymore

Slide 35

Slide 35

Replacing problematic JS bits Predictable performance Same peak performance, but less variation

Slide 36

Slide 36

Features of Wasm Why is everybody looking at it?

Slide 37

Slide 37

Near native speed https://medium.com/wasmer/benchmarking-webassembly-runtimes-18497ce0d76e

Slide 38

Slide 38

Highly portable It can be run almost everywhere…

Slide 39

Slide 39

Readable and debuggable Each .wasm file with it .wat companion file

Slide 40

Slide 40

Memory safe & secure Running in a fully sandboxed environment

Slide 41

Slide 41

Accepting many source languages And more and more…

Slide 42

Slide 42

Some constraints Still a young platform…

Slide 43

Slide 43

Native WASM types are limited WASM currently has four available types: ● i32: 32-bit integer ● i64: 64-bit integer ● f32: 32-bit float ● f64: 64-bit float Types from languages compiled to WASM are mapped to these types

Slide 44

Slide 44

How can we share data? Using the same data in WASM and JS? Shared linear memory between them, and serializing the data to one Wasm types

Slide 45

Slide 45

Solution is coming: Interface types Beautiful description at: https://hacks.mozilla.org/2019/08/webassembly-interface-types

Slide 46

Slide 46

No outside access By design, communication is done using the shared linear memory only

Slide 47

Slide 47

Solution exists: WASI

Slide 48

Slide 48

Mono-thread and scalar operations only Not the most efficient way…

Slide 49

Slide 49

Solution exists: SIMD

Slide 50

Slide 50

Solutions are coming too: Wasm Threads Threads on Web Workers with shared linear memory

Slide 51

Slide 51

Incoming proposals: Garbage collector And exception handling

Slide 52

Slide 52

You can do steps 03 and 04 now Let’s code, mates!

Slide 53

Slide 53

AssemblyScript Writing WASM without learning a new language

Slide 54

Slide 54

TypeScript subset compiled to WASM Why would I want to compile TypeScript to WASM?

Slide 55

Slide 55

Ahead of Time compiled TypeScript More predictable performance

Slide 56

Slide 56

Avoiding the dynamicness of JavaScript More specific integer and floating point types

Slide 57

Slide 57

Objects cannot flow in and out of WASM yet Using a loader to write/read them to/from memory

Slide 58

Slide 58

No direct access to DOM Glue code using exports/imports to/from JavaScript

Slide 59

Slide 59

You can do step 05 now Let’s code, mates!

Slide 60

Slide 60

WebAssembly ❤ Web Components How to hide the complexity and remove friction

Slide 61

Slide 61

The 3 minutes context What the heck are web component?

Slide 62

Slide 62

Web Components Web standard W3C

Slide 63

Slide 63

Web Components Available in all modern browsers: Firefox, Safari, Chrome

Slide 64

Slide 64

Web Components Create your own HTML tags Encapsulating look and behavior

Slide 65

Slide 65

Web Components Fully interoperable With other web components, with any framework

Slide 66

Slide 66

Web Components CUSTOM ELEMENTS SHADOW DOM TEMPLATES

Slide 67

Slide 67

Custom Element To define your own HTML tag <body> … <script> window.customElements.define(‘my-element’, class extends HTMLElement {…}); </script> <my-element></my-element> </body>

Slide 68

Slide 68

Shadow DOM To encapsulate subtree and style in an element <button>Hello, world!</button> <script> var host = document.querySelector(‘button’); const shadowRoot = host.attachShadow({mode:’open’}); shadowRoot.textContent = ‘こんにちは、影の世界!’; </script>

Slide 69

Slide 69

Template To have clonable document template <template id=”mytemplate”> <img src=”” alt=”great image”> <div class=”comment”></div> </template> var t = document.querySelector(‘#mytemplate’); // Populate the src at runtime. t.content.querySelector(‘img’).src = ‘logo.png’; var clone = document.importNode(t.content, true); document.body.appendChild(clone);

Slide 70

Slide 70

But in fact, it’s just an element… ● ● ● ● Attributes Properties Methods Events

Slide 71

Slide 71

You can do step 06 and 07 now

Slide 72

Slide 72

That’s all, folks! Thank you all!