Large WebAssembly builds with Rust and regex
I wanted to share a few functions between a Rust server and web app client, so I decided to finally try out WebAssembly (WASM). After a few tweaks to get data marshalled between the Javascript/WASM worlds, it actually worked really well⊠except the final .wasm
file size was about 720K for a ~100 line function (plus dependencies, of course).
Working through the code size guide eventually made it clear that the regex crate was contributing a lot to the total size. Removing it brought the size down to ~24K. And I got to learn about parser combinators with nom!
This appears to be one of the fundamental points of friction when using WASM. Either compile in everything you need (increasing filesize) or inject it from the host environment (increasing complexity). In fact, one of the reasons Rust is so well suited to WASM is that doesnât need to bring an entire garbage collector.
This is extra unfortunate since regex support is available even in Javascriptâs rudimentary âstandardâ lib. I imagine some sort of Rust crate that automatically defers to the hostâs regex implementation could exist (initial searches didnât turn anything up). Until then, Iâll avoid using regex when I intend to compile into WASM.