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

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

    View Slide

  2. View Slide

  3. Timeline…

    View Slide

  4. How did we achieve it
    Moore law
    Pushed Tech industries to evolve until
    today
    Allowed huge computation power
    even on small devices

    View Slide

  5. All this power for what?
    Faster applications
    More immersive games
    Virtual reality
    Augmented reality
    Live video/image processing
    Big Data/Machine learning

    View Slide

  6. The languages over time

    View Slide

  7. But what about JS?

    View Slide

  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

    View Slide

  9. View Slide

  10. JS - 2008 until now
    2008 - JIT Compiler by google w/
    Chrome
    Jump of performance
    Allowed more complex applications
    What is the next step?

    View Slide

  11. Willian Elia
    @wmsbill

    View Slide

  12. View Slide

  13. WE’RE HIRING!!

    View Slide

  14. What is WebAssembly?

    View Slide

  15. What’s WebAssembly?
    New binary format
    Run compiled programs (C, C++, Rust) on
    a browser
    Works alongside Javascript
    Performance and flexibility
    API

    View Slide

  16. Why WebAssembly is
    faster?

    View Slide

  17. V8 - JiT in action

    View Slide

  18. V8 - JiT in action

    View Slide

  19. V8 - JiT in action

    View Slide

  20. V8 - JiT in action

    View Slide

  21. V8 - JiT in action

    View Slide

  22. V8 - JiT in action

    View Slide

  23. V8 - JiT in action

    View Slide

  24. JiT: Code life cycle in summary
    1. Parse
    2. Compile
    3. Optimize (de-optimize)
    4. Execute
    5. Garbage Collector

    View Slide

  25. WebAssembly

    View Slide

  26. WebAssembly is fast
    Parse Compile Optimize Execute GC
    Decode
    Compile
    +
    Optimize
    Execute
    JS
    WASM

    View Slide

  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

    View Slide

  28. So WebAssembly
    looks fast, let’s see
    how to use it

    View Slide

  29. How to run WASM
    modules
    Current situation: not possible to run
    WASM modules on their own
    Need for some Javascript glue

    View Slide

  30. WebAssembly JS API
    1. Fetch/Load the module binary
    2. Instantiate it
    3. Access exported functionalities

    View Slide

  31. fs.readFile(‘./module.wasm’, async function(err, file) => {
    const { instance } = await WebAssembly.instantiate(file);
    // Do something with the compiled results!
    });

    View Slide

  32. How to generate a
    WASM file

    View Slide

  33. Compile C to WASM
    +
    JS WASM
    emcc my-module.c -o my-module.js -s WASM=1

    View Slide

  34. Then we can simply
    import the generated
    JS code as a module

    View Slide

  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

    View Slide

  36. What about
    WebAssembly memory?
    How can we access it?

    View Slide

  37. Memory management
    Emscripten provide three useful
    functions to manage WebAssembly
    memory
    _malloc(memoryNeeded)
    getValue(ptr, type)
    setValue(ptr, value, type)

    View Slide

  38. Elia
    @eliamain

    View Slide

  39. DEMO TIME

    View Slide

  40. The WASM
    implementation was
    still slower

    View Slide

  41. Why??

    View Slide

  42. JIT handover

    View Slide

  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

    View Slide

  44. The future of
    WebAssembly

    View Slide

  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


    View Slide

  46. Danke!
    Thank you!

    View Slide

  47. What about Native
    modules?

    View Slide