by vanderZwan · 6y ago · 0 comments
For example, last week me and a few other users on Observable have been experimenting with various JS ports of one particular permuted congruential random number generator. Using assemblyscript I managed to write a decent-enough WASM version, and it's slower than implementing 64 bit arithmetic with four 32 bit numbers, which involves a lot more multiplications and additions:
https://observablehq.com/d/ce811886e1071d69
There are two possible explanations: either 64 bit integer arithmetic is still really slow in WASM, or the call overhead is really, really significant.
This fits my earlier experience with trying to implement fast log approximation functions for data-viz purposes (since the rounding errors would not be visible at sub-pixel levels). They weren't much faster than the proper Math.log function (whereas in pure C or C++ that difference would be much larger).
I suspect that if this call overhead were reduced, it would be much easier to gradually add more WASM to a Web App and reap the benefits of it.
Calling overhead between WASM and JS is significant though, but it has already been optimized massively [1], I doubt that this can be improved much further). Calling a very small function across language borders is amost always a bad idea, not just in the specific JS=>WASM context.
[1] https://hacks.mozilla.org/2018/10/calls-between-javascript-a...
PS: also contrary to popular belief, JS and WASM have similar performance for simple number crunching tasks because in that situation JS is pretty fast, Javascript only becomes slow when objects and properties come into play.
> There’s only one case where an optimized call from JavaScript » WebAssembly is not faster than JavaScript » JavaScript. That is when JavaScript has in-lined a function.
Combined with your remark that 64 bit arithmetic is fast, I'd say this pretty much confirms the suspicion that a lack of inlining is what makes this particular WASM function execute slower
For what it's worth, (scalar) numerical operations in JS are actually quite close to C. It's not like python.
The problem with writing the loop on the WASM side is that passing the data back and forth between JS and WASM involves typed arrays and array buffers. That has significant overhead of its own. Still, good suggestion, I should try that with a pixel buffer!
> For what it's worth, (scalar) numerical operations in JS are actually quite close to C. It's not like python.
Indeed, and I am generally very impressed and happy with the performance of JavaScript these days! In my experience it only takes a little bit of effort to hint at the compiler if we're working with doubles or 32 bit integers, and then it goes really fast. In this particular case however we needed 64 bit integer arithmetic, so that's a bit of an edge-case.
``` const eps: f64 = 2.3283064365386963e-10; const m: u64 = 6364136223846793005; const inc: u64 = 1442695040888963407;
let state: u64 = 1;
export function random(): f64 { let oldstate = state * 6364136223846793005 + 1442695040888963407; state = oldstate; const xorshifted = u32(((oldstate >>> 18) ^ oldstate) >> 27); const out_int = rotr(xorshifted, u32(oldstate >> 59)); return eps * out_int; } ```
I recommend return f64 instead f32. This has slightly faster interop
(also, as you might have noticed, HN doesn't support MD proper. To get code blocks you have to indent it with four spaces)
const eps: f64 = 2.3283064365386963e-10;
const m: u64 = 6364136223846793005;
const inc: u64 = 1442695040888963407;
let state: u64 = 1;
export function random(): f64 {
let oldstate = state * 6364136223846793005 + 1442695040888963407;
state = oldstate;
const xorshifted = u32(((oldstate >>> 18) ^ oldstate) >> 27);
const out_int = rotr(xorshifted, u32(oldstate >> 59));
return eps * out_int;
}I'll keep that in mind in next time. Thanks for the tip!
WASM shines for numeric computation like encoding, decoding, encryption.
In the benchmarks I've seen, image encoding is 10-20X faster in WASM. Crypto is about the same speed as WebCrypto which isn't available in all browsers and runs native code.
There's an unmistakable performance advantage for compute heavy workloads.
I think it's inevitable for WASM to replace JS eventually. We're just in the early days.
WASM has 2-5X performance advantage for "most" workloads. It also parses much faster and is more compact than minified JS. Eventually the performance advantages will be too large to ignore
Do you think front-end developers will start writing C/C++/Rust instead of JavaScript? Or do we need other languages that can compile to WASM before it can replace JS?