0
votes

So, I did a lot of experimentation with Emscripten. And what I noticed pretty much is that the musl-libc that is being used is impressively huge...like, really huge. A simple "Hello, World!" printing app is way above the 10kb...which is by far not practical.

Now how can I reduce this? So far, I figured that I could use -Os and --closure 1. But the latter only really works over JavaScript, so it does not affect the Wasm output.

What else can I do? Use a different libc implementation, maybe? I looked at uClibc and dietlibc as well as Metallic. I also thought about looing for a cheap - filesize-wise - deflate function so I could transfer a gzipped version of the Wasm binary. But so far, that is all I got.

Any suggestions? My last try was:

emcc test.cpp -o test.wasm -s WASM=1 -Os --closure 1

where test.cpp is just a printf("Hello, world");.

1
You could try with -Oz too, but Emscripten is maybe not the good toolset to target embedded systems. It is a toolset to target web browsers for now. - Gabriel Cuvillier

1 Answers

2
votes

Simple answer: If you care about the binary size, never use printf(), consider using puts().

In my experiment, printf() version of "Hello world" is whopping 13KB, and puts() version is as small as 2KB.

You say "A simple "Hello, World!" printing app", but it is actually not true in the wider perspective because printf() is by far the biggest part of the C standard library in terms of the binary size. Actually I am a bit surprised that they managed reducing it to 13KB. This is mainly because of the % formatting. Not only printf() deals with simple formats such as %c, %d, and %ld, but it has to deal with complex formats such as %4.2f, %+.0e, %E, and %*d.

Apart from the web application, this is a common problem in the embedded systems where you should statically link against stdlib and the available program memory size is often as small as 10kb. Just google "printf size in embedded system", you will easily see a lot of hates around printf() and a lot of efforts to reduce size of it.

Switching stdlib like uClibc and dietlibc won't help much because they still are libraries for the POSIX system which implements a full specification of printf(). You should look for stdlibs for bare-metal embedded systems such as newlib-nano that implements a subset of printf() specification to reduce the binary size. Moreover, I don't think switching stdlib for Emscripten is almost an impossible job because Emscripten uses significantly hacked version of musl-libc for their use case.