24

Several rust utilities include binaries for linux with musl and gnu suffixes, leaving me to wonder which one should I use, and why?

Examples:

balupton
  • 585

3 Answers3

17

Specifically for Rust, my advice would be to use gnu/glibc when possible if performance matters as musl binaries can be factors of 10 slower especially when multi-threading is important as musl's standard standard allocator is very bad in that case.

For Rust, there are known performance issues when using musl binaries. This was reported in 2020 but still seems to be an issue in 2023. It might be fixable by not using the default allocator, but in general, things like multi-threading seem to suffer a lot for musl compared to glibc.

In one stark case I reported here, the musl flavor does not parallelize at all (it even gets slower in real time, the more threads you use), while the glibc flavor parallelizes almost perfectly (i.e. 10x number of threads reduces wall clock by almost 10x). In this case, this means musl is more than 30 times slower than the glibc version!

11

The most compelling reason to choose Musl is that it's simple and small and holds the title for the go to in the container space, or with static linked executables (like Rust and Go). There are technical reasons for this

GNU C Library tends to have much more complexity mostly around assembly optimizations for platforms and backwards compatibility. It's the solution most Linux distributions use.

As for the example you've given, with sd

  • On sizes,

    • The GNU version is 979 KB
    • The Musl version is 1020 KB
  • Linkage,

    • GNU: If you look at sd-v1.0.0-x86_64-unknown-linux-gnu.tar.gz with ldd you'll see that it's dynamically linked to libc.so.6.
      ❯ ldd sd
          linux-vdso.so.1 (0x00007ffc8efcf000)
          libgcc_s.so.1 => /lib/x86_64-linux gnu/libgcc_s.so.1 (0x00007f1cad12b000)
          libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007f1cacc1f000)
          /lib64/ld-linux-x86-64.so.2 (0x00007f1cad170000)
      
    • Musl, if you look at sd-v1.0.0-x86_64-unknown-linux-gnu.tar.gz with ldd you'll see "statically linked" which effectively means it doesn't need libc at all, nor a dynamic linker.
      ❯ ldd ./sd
          statically linked
      

So for a difference of 41 KB, you can avoid having to package the shared library, /lib/x86_64-linux-gnu/libc.so.6. which on my system is 1.9 MB

  • 1020 KB total for statically-linked Musl distribution
  • 979 KB + 1922 KB (for libc) in GNU distribution = 2901 KB total
Evan Carroll
  • 9,518
1

GNU C Library (glibc) is the standard library used on many distributions. musl libc (muslc) is a new C library aiming to be lightweight, fast, simple, free, (annoyingly) correct. Musl is used on some distributions like Alpine Linux.

The first one provides binaries linked with glibc and musl. The second provides binaries linked with musl (Linux), the GNU version perhaps is compiled using MinGW. Use the muslc version on Linux. (That's my opinion because any software have an option for binaries linked with a newer, better libc should be prefered.)