with webassembly
play

with WebAssembly Boyan Mihaylov @boyanio boyan.io - PowerPoint PPT Presentation

In-depth hands-on experience with WebAssembly Boyan Mihaylov @boyanio boyan.io https://github.com/boyanio/wasm-class/ @boyanio WebAssembly ( WASM ) is compiler target for programs on the Web @boyanio function add(a, b) { return a + b; }


  1. In-depth hands-on experience with WebAssembly Boyan Mihaylov @boyanio boyan.io

  2. https://github.com/boyanio/wasm-class/ @boyanio

  3. WebAssembly ( WASM ) is compiler target for programs on the Web @boyanio

  4. function add(a, b) { return a + b; } @boyanio

  5. JavaScript performance over the years ? Node.js Browser wars: the introduction of JIT 1995 2008 2017 @boyanio

  6. WebAssembly is a typed language It supports 32 and 64-bit integers (i32, i64) and floating points (f32, f64) @boyanio

  7. Binary representation (.wasm) 0061 736d 0100 0000 0187 8080 8000 0160 027f 7f01 7f03 8280 8080 0001 0004 8480 8080 0001 7000 0005 8380 8080 0001 0001 0681 8080 8000 0007 9080 8080 0002 066d 656d 6f72 7902 0003 6164 6400 000a 8d80 8080 0001 8780 8080 0000 2001 2000 6a0b @boyanio

  8. Textual representation (.wat) (module (table 0 anyfunc) (memory $0 1) (export “memory” (memory $0)) (export “add” ( func $add)) (func $add (; 0 ;) (param $0 i32) (param $1 i32) (result i32) (i32.add (get_local $1) (get_local $0) ) ) ) @boyanio

  9. Compile Re- Fetch Parse + Execute GC optimize optimize Compile Fetch Decode + Execute optimize Fetch / Decode / Compile + optimize Execute (streaming API) @boyanio

  10. WebAssembly provides consistent, predictable performance @boyanio

  11. WebAssembly enables code reusability between native and Web @boyanio

  12. Traditional multi-target compilation x64 Windows x86 x64 .cpp MAC x86 Source code x64 ARM v7 Linux x86 ARM v8 @boyanio

  13. Multi-target compilation with WebAssembly .cpp .wasm Source code WebAssembly module @boyanio

  14. Where is WebAssembly useful? Gaming industry Libraries Unreal, Unity OpenCV, Box2D, LibSass Multimedia Platform simulation / emulation AV1, FLIF, BPG DOSBox, MAME, QEMU @boyanio

  15. How to produce WebAssembly load with execute compile JavaScript .cpp .wasm .html Source WASM module HTML page Browser @boyanio

  16. WasmFiddle https://wasdk.github.io/WasmFiddle/ @boyanio

  17. Open Source LLVM to JavaScript compiler https://kripken.github.io/emscripten-site/ @boyanio

  18. emcc index.c – s WASM=1 – o index.js @boyanio

  19. How compilers work Frontend Backend Source Intermediate Assembly or program Representation (IR) machine code Static analysis Code generation Semantic analysis Code optimization @boyanio

  20. How Emscripten works Fastcomp clang (LLVM Backend) C / C++ LLVM IR JavaScript @boyanio

  21. asm.js is an extraordinarily optimizable, low-level subset of JavaScript @boyanio

  22. asm.js function () { “use asm ”; function add(a, b) { a = a | 0; b = b | 0; return a + b | 0; } return { add: add }; } @boyanio

  23. asm.js performance @boyanio http://kripken.github.io/mloc_emscripten_talk/#/28

  24. WebAssembly = asm.js done right @boyanio

  25. From asm.js to WebAssembly clang Fastcomp asm2wasm C / C++ LLVM IR asm.js WASM @boyanio

  26. New WebAssembly Backend (unstable) LLVM WASM clang Backend C / C++ LLVM IR WASM @boyanio

  27. The distributable, loadable, and executable unit of code in WebAssembly is called a module . @boyanio https://github.com/WebAssembly/design/blob/master/Modules.md https://github.com/WebAssembly/design/blob/master/Modules.md

  28. Module imports & exports const imports = { const exports = module.exports; “name”: { exports.printName(); “first”: “Anna”, exports.reverseName(); “last”: “Nanna” }, “print”: function (what) { console.log(what); } }; @boyanio

  29. Linear memory const imports = { “ env ”: { “memory”: new WebAssembly.Memory({ initial: 10, maximum: 100}), … } }; 0 1 2 3 4 5 6 00010011 00100100 10011100 11100011 00101111 00010000 01001110 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 @boyanio

  30. Linear memory What WebAssembly sees 0 1 2 3 4 5 6 00010011 00100100 10011100 11100011 00101111 00010000 01001110 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 What JavaScript sees @boyanio

  31. Loading WebAssembly // Traditional approach fetch(‘ app.wasm ’) .then(result => result.arrayBuffer()) .then(buffer => WebAssembly.instantiate(buffer, imports)) .then(({ module, instance }) => { instance.exports.main(); }); // Using the streaming API WebAssembly.instantiateStreaming (fetch(‘ app.wasm ’), imports) .then(({ module, instance }) => { instance.exports.main(); }); @boyanio

  32. How secure is WebAssembly? @boyanio

  33. WebAssembly runs in a memory-safe sandboxed environment @boyanio

  34. Resource <script> HTML, CSS, SVG, … VM APIs JavaScript WebAssembly Rendering DOM + CSSOM WebAudio WebGL IndexedDB GC Compiler … WebSockets Service Workers … Sandbox, Same- origin policy, … Network Audio … Storage Graphics @boyanio

  35. @boyanio http://caniuse.com/#search=WebAssembly

  36. What about browsers not supporting WebAssembly? Fallback to asm.js @boyanio

  37. From version 1.0 to next @boyanio

  38. Direct access to WebAPIs today everything goes through JavaScript @boyanio

  39. Integration with browser’s garbage collection for better interop with JavaScript & WebAPIs @boyanio

  40. Multi-threading ??? low-level buildings blocks for shared memory between threads, atomics and futexes @boyanio

  41. What languages can be compiled to WebAssembly? https://boyan.io/wasm-wheel/ @boyanio

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend