mtpng icon indicating copy to clipboard operation
mtpng copied to clipboard

WASM experience?

Open tomByrer opened this issue 4 years ago • 5 comments

Does anyone have any experience in compiling this to WASM please?

tomByrer avatar Apr 18 '20 02:04 tomByrer

With the libz_sys and rayon dependencies I don't think it would build to wasm. You'd also need a threading API to make use of it, and a supporting browser or non-browser runtime, and none of that stuff is very standardized at this point.

bvibber avatar Jul 28 '20 04:07 bvibber

Hey, wasm threads just landed https://hacks.mozilla.org/2020/07/firefox-79/ btw, libz_sys might be replaced by miniz_oxide to get wasm support

zimond avatar Aug 03 '20 05:08 zimond

Wasm threading still hasn't landed in Safari, but is in Chrome on most platforms (not Android) and now in Firefox, as long as the web page opts in to certain HTTP headers.

It looks like wasm-bindgen now supports threads and can build rayon, so that's a good sign!

I think the stickiest point will be the C code in libz_sys if there's not suitable helper code for building C to wasm somehow.

You'll also lose some performance over native code in the filters, which are SIMD-optimized. Wasm's SIMD extension is still experimental and not widely enabled. But with the threading it may still be a win over asking the browser to compress an image.

bvibber avatar Aug 03 '20 17:08 bvibber

A while ago I did a quick test of using miniz_oxide: https://github.com/brion/mtpng/commits/miniz-oxide

The performance was not great, but that may be in part because there's no way to set the dictionary at the beginning of compression, so to prime each section we have to re-compress the last 32 KiB of the last section and then flush the data and throw it away before putting in the actual data for this chunk. miniz_oxide just doesn't seem to expose an interface to do that efficiently (or didn't at the time).

That could be fixed but would require coordinating a patch with miniz_oxide I guess?

(the hack does set it to 'no compression' so it's relatively cheap but maybe not ..... clean :D)

(anyway there's perf issues remaining compared to zlib, which could easily outweigh multithreading in many cases vs letting the browser do it with libpng)

bvibber avatar Aug 03 '20 17:08 bvibber

I don't know if this is helpful, but pcwalton has written a multi-threaded png decoder before: https://github.com/pcwalton/parng

And for the perf problem, I'm thinking that supporting WASM is already a big step forward. It could even be gated by cfg(target_arch) so it will not affect other targets

zimond avatar Aug 04 '20 02:08 zimond