genish.js icon indicating copy to clipboard operation
genish.js copied to clipboard

Wasm

Open charlieroberts opened this issue 3 years ago • 7 comments

charlieroberts avatar Jun 08 '21 19:06 charlieroberts

Hey Charlie, if you link in wabt.js (wat2wasm) you can generate wasm on the fly and assemble it, instead of just calling wasm functions. The overhead of popping in an out of wasm is often quite high.

abramhindle avatar Jun 08 '21 20:06 abramhindle

Hey Abram,

Hope all is well with you! There's only one call to wasm per callback block... I don't think it can get any lower than this :) The wat2wasm happens during the gulp build... which I just realized I haven't committed. Will do so now!

charlieroberts avatar Jun 08 '21 20:06 charlieroberts

here's the gulpfile (such as it is): https://github.com/charlieroberts/genish.js/blob/wasm/gulpfile.js

... and here's the relevant part of the audioworklet: https://github.com/charlieroberts/genish.js/blob/wasm/src/module.js#L70

It's gonna take me a bit to clean up this branch, I probably should have just used a new repo.

charlieroberts avatar Jun 08 '21 20:06 charlieroberts

last but not least I updated the README in the wasm branch to include a bit more description / motivation: https://github.com/charlieroberts/genish.js/blob/wasm/README.md

There's also a link to a wasm-enabled playground there if you want to try it out.

charlieroberts avatar Jun 08 '21 20:06 charlieroberts

I just found this fantastic essay on js > wasm (and vice-versa) in Firefox from a few years back... turns out it's not as slow as it was originally: https://hacks.mozilla.org/2018/10/calls-between-javascript-and-webassembly-are-finally-fast-🎉/

charlieroberts avatar Jun 09 '21 15:06 charlieroberts

That's a really interesting FF article about the performance of wasm. Yeah wasm is pretty close to the JIT'd code in FF. FF does wasm well. Last year when I tested Wasm versus JS, the gains or similar performance was mostly apparent when you stayed in wasm long enough or used floating point operations. https://github.com/abramhindle/impractical-wasm (video: https://www.youtube.com/watch?v=VkpBLEif6B8 ). I'm glad to see that you're having a lot of success porting it.

abramhindle avatar Jun 09 '21 16:06 abramhindle

oh nice, that's a much better guide to doing this than most of the ones I looked at when I was trying to figure out how this all worked. thanks for sharing!

charlieroberts avatar Jun 09 '21 17:06 charlieroberts