Tech News
← Back to articles

Default musl allocator considered harmful to performance

read original related products more articles

TLDR: In a real world benchmark, the default musl allocator caused a 7x slowdown compared to other allocators. I recommend all Rust projects immediately add the following lines to their application’s main.rs :

// Avoid musl's default allocator due to lackluster performance // https://nickb.dev/blog/default-musl-allocator-considered-harmful-to-performance #[cfg(target_env = "musl" )] #[global_allocator] static GLOBAL : mimalloc ::MiMalloc = mimalloc::MiMalloc;

And Cargo.toml

[ target . 'cfg(target_env = "musl")' . dependencies ] mimalloc = "0.1.43"

The root cause is the contention between multiple threads when allocating memory, so the problem worsens as more threads or allocations are created.

I recommend swapping the allocator even if musl is not a compilation target today or if the program is single threaded. This is something you simply don’t want to forget.

Reader’s choice on what allocator they want to sub in. The code snippets use mimalloc, but jemalloc is also good.

Also reader’s choice if allocation substitution should only be restricted to musl environments (as shown) or if it should be done globally. I don’t mind conditionally compiling dependencies here as it serves as another form of documentation.

Why musl?

If I feel so strongly about avoiding the default musl allocator, why even use musl in the first place?

... continue reading