Upgrade to Pro — share decks privately, control downloads, hide ads and more …

WebAssembly in NodeJS

WebAssembly in NodeJS

My presentation about WASM in NodeJS @ NodeJS Meetup Berlin

E807477655ad7125c8731065178030e2?s=128

Willian Martins

October 17, 2017
Tweet

More Decks by Willian Martins

Other Decks in Technology

Transcript

  1. The Need for Speed and WebAssembly Willian Martins Autumn 2017

  2. None
  3. Timeline…

  4. How did we achieve it Moore law Pushed Tech industries

    to evolve until today Allowed huge computation power even on small devices
  5. All this power for what? Faster applications More immersive games

    Virtual reality Augmented reality Live video/image processing Big Data/Machine learning
  6. The languages over time

  7. But what about JS?

  8. JS - Early days Created in 1995 by Brendan Eich

    Was written in 11 days Designed to be easy and simple Designed to be a glue code for dynamic Web
  9. None
  10. JS - 2008 until now 2008 - JIT Compiler by

    google w/ Chrome Jump of performance Allowed more complex applications What is the next step?
  11. Willian Elia @wmsbill

  12. None
  13. WE’RE HIRING!!

  14. What is WebAssembly?

  15. What’s WebAssembly? New binary format Run compiled programs (C, C++,

    Rust) on a browser Works alongside Javascript Performance and flexibility API
  16. Why WebAssembly is faster?

  17. V8 - JiT in action

  18. V8 - JiT in action

  19. V8 - JiT in action

  20. V8 - JiT in action

  21. V8 - JiT in action

  22. V8 - JiT in action

  23. V8 - JiT in action

  24. JiT: Code life cycle in summary 1. Parse 2. Compile

    3. Optimize (de-optimize) 4. Execute 5. Garbage Collector
  25. WebAssembly

  26. WebAssembly is fast Parse Compile Optimize Execute GC Decode Compile

    + Optimize Execute JS WASM
  27. 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
  28. So WebAssembly looks fast, let’s see how to use it

  29. How to run WASM modules Current situation: not possible to

    run WASM modules on their own Need for some Javascript glue
  30. WebAssembly JS API 1. Fetch/Load the module binary 2. Instantiate

    it 3. Access exported functionalities
  31. fs.readFile(‘./module.wasm’, async function(err, file) => { const { instance }

    = await WebAssembly.instantiate(file); // Do something with the compiled results! });
  32. How to generate a WASM file

  33. Compile C to WASM + JS WASM emcc my-module.c -o

    my-module.js -s WASM=1
  34. Then we can simply import the generated JS code as

    a module
  35. Export functions to JS Keyword EMSCRIPTEN_KEEPALIVE EMSCRIPTEN_KEEPALIVE int sum(int x,

    int y) { return x + y; } Expose only the interface of the WASM module to JS
  36. What about WebAssembly memory? How can we access it?

  37. Memory management Emscripten provide three useful functions to manage WebAssembly

    memory _malloc(memoryNeeded) getValue(ptr, type) setValue(ptr, value, type)
  38. Elia @eliamain

  39. DEMO TIME

  40. The WASM implementation was still slower

  41. Why??

  42. JIT handover

  43. “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
  44. The future of WebAssembly

  45. 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

  46. Danke! Thank you!

  47. What about Native modules?