I did some algorithm performance comparision with js, but wasm built are no faster than js.
Same nqueen algorithm, wasm version took 4450 ms, js version took 4633ms.
rust native version only took round 2sec.
There's some other algorithm I took from https://benchmarksgame-team.pages.debian.net/benchmarksgame/faster/rust.html.
wasm fannkuch: 5579ms
js fannkuch: 4322ms
wasm should be faster than js version
git clone https://github.com/gliheng/wasm-perf
cd wasm-perf
wasm-pack build wasm --release
npm install
npm run dev
Open http://localhost:8080, see the console print
Include the relevant details of your environment.
wasm-pack version: 0.6.0
rustc version: 1.32.0 (9fda7c223 2019-01-16)
Thanks for the report! Always a good idea to keep a close eye on performance. One thing I'd recommend initially is that the default template optimizes for code size, but in your use case it looks like you're optimizing for speed. This difference has historically caused significant slowdown, so I'd recommend deleting that [profile.release] section or even removing opt-level and turning on lto = true. In general we don't have a lot of guides yet of tweaking compile settings for speed, but it'd be good to have them!
Once I made that change and ran the benchmarks myself locally in Firefox Nightly on a mac I got:
wasm binary tree: 573ms
js binary tree: 1269ms
wasm regex: 11ms
js regex: 7ms
wasm fannkuch: 8165ms
js fannkuch: 3177ms
wasm nqueen: 2629ms
js nqueen: 3382ms
wasm fibonacci: 51ms
js fibonacci: 135ms
which I think shows a significant improvement across the board from when I was looking at opt-level=s!
It still shows that wasm isn't so great at fannkuch with respect to the JS implementation. Taking a look at a profile in Gecko it doesn't look like anything is too awry, it may just be a missing optimization in either LLVM or the wasm engine itself.
I'm not too familiar with the various engines here, but I'll try passing this along to some Gecko folks to see if they have ideas about optimizations
Alex, Thanks for looking into it. Your method works. With opt-level=3 and lto=true, binary size went from 810377 to 868998. Not much increase in size I think.
It would be nice if rustwasm book have a Optimize for speed section to make it clear, in case people don't understand. If speed of wasm is not fast, why would people use it? : )
A good point! I've opened up https://github.com/rustwasm/book/issues/154 to track that for addition to the book itself. With that I think this issue is largely solved, so I'm going to close this.
Most helpful comment
Thanks for the report! Always a good idea to keep a close eye on performance. One thing I'd recommend initially is that the default template optimizes for code size, but in your use case it looks like you're optimizing for speed. This difference has historically caused significant slowdown, so I'd recommend deleting that
[profile.release]section or even removingopt-leveland turning onlto = true. In general we don't have a lot of guides yet of tweaking compile settings for speed, but it'd be good to have them!Once I made that change and ran the benchmarks myself locally in Firefox Nightly on a mac I got:
which I think shows a significant improvement across the board from when I was looking at
opt-level=s!It still shows that wasm isn't so great at fannkuch with respect to the JS implementation. Taking a look at a profile in Gecko it doesn't look like anything is too awry, it may just be a missing optimization in either LLVM or the wasm engine itself.
I'm not too familiar with the various engines here, but I'll try passing this along to some Gecko folks to see if they have ideas about optimizations