We are usually significantly smaller than emscripten for the same code.
We are usually significantly smaller than emscripten for the same code.
https://hacks.mozilla.org/2018/01/shrinking-webassembly-and-...
But yeah, as you and others said, while both Rust and C/C++ can do that, there's a lot more going on in the general case. Once you add a malloc/free implementation, or string operations, or anything else, the size increases.
The fundamental issue is that Rust and C/C++ were not designed for this use case. In principle, a new language could do a lot better. If such a language were GC based, it would avoid shipping a malloc/free (once wasm gets GC, that will be "free" for that language). And if such a language had the same string types and a compatible "standard library" with the Web, so that it would just call directly into existing Web APIs, then it would avoid shipping a lot of other code that Rust and C/C++ currently need.
TypeScript, for example, could become such a language. That would probably be the optimal path for creating tiny wasm binaries.
I’m not as convinced as you regarding such a language, for example, once host bindings lands, you’ll be able to call into every API with no overhead, and the bindgen stuff we’re doing will transparently just shrink overnight. But we’ll see! It’s exciting times.
If you really want to go down in size, you either need to compile without the standard library and/or use wasm-gc and wasmopt to remove all the useless code that have been added to your wasm binary.
With this, you can generate wasm binary that weight only a few hundreds octets.
See here: https://rustwasm.github.io/book/game-of-life/code-size.html
In the future, it will do more of the right things by default, but for now, you have to do some configuration if you want the tiniest possible binary. Early days!
char *m = NULL;
m[1] = 0xFF;
For m[0] the compiler exchanges the code with the abort function :-)
I wonder, if something similar is possible in Rust.