-3

I'm implementing a Mandelbrot Set visualization using Rust with WebAssmbly, where my goal is to make it using multi-threading.

I've implemented the Mandelbrot Set both in Javascript (using Typescript) and in Rust single-threaded so far. I've made some benchmarks and the Rust implementation is about x17 time slower, and I'm completely lost here, I don't know why I'm getting this bad performance.

Here is the repo, at master the implementation that uses Rust, and in js-implementation the one with Rust.

https://github.com/DanielRamosAcosta/mandlerbot-set-webassembly

Thanks in advance.

Daniel Ramos
  • 2,115
  • 3
  • 18
  • 28
  • 1
    The JavaScript engines were optimized since more than 10 Years, the WASM engines are just a year old. – Jonas Wilms Nov 30 '18 at 19:31
  • 2
    @JonasWilms The whole point of Wasm is that it is meant to run a lot faster than JavaScript. This is from the first few sentences of the [Wikipedia article](https://en.wikipedia.org/wiki/WebAssembly): "[Wasm] is meant to enable executing code nearly as quickly as running native machine code. It was envisioned to complement JavaScript to speed up performance-critical parts of web applications and later on to enable web development in languages other than JavaScript." – Sven Marnach Nov 30 '18 at 19:35
  • @sven just because it could be faster doesn't mean that it necessarily run faster in practice. V8 compiles heavily used functions in JS down to bytecode (if it is able to deduce the types and resolve references statically), not sure how far the WASM development is yet. – Jonas Wilms Nov 30 '18 at 19:39
  • Possible [duplicate](https://stackoverflow.com/questions/48173979/why-is-webassembly-function-almost-300-time-slower-than-same-js-function). What Daniel did was a 'micro benchmark'. A lot more optimizations are needed. – clamentjohn Nov 30 '18 at 19:43
  • 3
    @daniel-ramos can you provide some instructions of how to build the source code? I'm getting an error about trying to build it and that makes it difficult to try to reproduce the measurements! Otherwise one problem I can see so far is that you're not using `cargo build --release` (note how you're running `wasm-bindgen` over the `debug` folder). The Rust and WebAssembly book has a [great section](https://rustwasm.github.io/book/game-of-life/time-profiling.html) on time profiling if you're interested as well! – alexcrichton Nov 30 '18 at 20:15
  • @alexcrichton It should be as easy as an `npm start`, and in order to build a `make` should be enough. I'll try the `--release` option! – Daniel Ramos Nov 30 '18 at 20:53
  • @alexcrichton Thanks you a lot!!! The `--realease` – Daniel Ramos Nov 30 '18 at 21:13
  • 2
    @JonasWilms Basically the main point of Wasm is that it actually _does_ run faster in practice. Much of it runs at near-native speed, and even the best optimised JavaScript JIT won't be able to compete with it. I recommend reading [this nice article by Lin Clark](https://hacks.mozilla.org/2017/02/what-makes-webassembly-fast/) as an introduction. – Sven Marnach Nov 30 '18 at 22:40
  • 2
    Possible duplicate of [Why is my Rust program slower than the equivalent Java program?](https://stackoverflow.com/questions/25255736/why-is-my-rust-program-slower-than-the-equivalent-java-program) – hellow Dec 03 '18 at 11:39
  • Yes @hellow, it's duplicated – Daniel Ramos Dec 10 '18 at 12:03

1 Answers1

3

Remember to use the --release flag when building.

Daniel Ramos
  • 2,115
  • 3
  • 18
  • 28