12 comments

  • matherial56 minutes ago
    If your algorithm does a ton of small allocations to the point where the allocator is the bottleneck, you&#x27;re already doing it wrong. The allocator necessarily comes with a lot of overhead because it needs to accommodate diverse use cases, avoid fragmentation, and ideally, implement a variety of security checks. If you&#x27;re doing something alloc-intensive, you&#x27;re probably allocating and freeing a lot of identical structures and you&#x27;d be better off grabbing some continuous memory and managing that yourself in a task-specific way.<p>But the reality is that almost no one actually cares about performance because compute is cheaper than expertise and labor, at least in the short haul. Everything is getting more bloated and slower and we just compensate by adding CPU cores, gigabytes and gigahertz.
    • haolez18 minutes ago
      That&#x27;s an interesting viewpoint, but then, will the allocator&#x27;s performance never matter for any use case that is not &quot;wrong&quot;? It doesn&#x27;t feel right.
      • gumby7 minutes ago
        Think about it this way because the issue isn’t specific to allocators: it’s pretty good in general but can often be beaten if you have special understanding of what you need to do. That’s OK.<p>You can buy cars and trucks that are optimized for driving on freeways and residential streets carrying stuff people often carry. But then there are special vehicles like fork lifts and such that are kinds of large special cases. And then there are weirdo specialised vehicles that have four wheels but are rare and their users can’t live without them.<p>Languages like C++ let you plug in special allocators if you want. But most people don’t. Some, like HFT people do crazy headstands to avoid slow allocations. I don’t ever want to do that but if they want to, why not. I don’t think they complain that the default case doesn’t fit their needs!
    • weinzierl5 minutes ago
      <i>&quot;But the reality is that almost no one actually cares about performance because compute is cheaper than expertise and labor, at least in the short haul.&quot;</i><p>Doesn&#x27;t have to stay that way, with hardware prices soaring and development cost allegedly in free fall.
  • tombert5 minutes ago
    I feel with Rust I try and avoid re-allocations in most cases anyway, so I&#x27;m not sure that musl&#x27;s allocator being slow would significantly affect performance (though I haven&#x27;t benchmarked it). I feel like part of the appeal of Rust is that you can do imperatively-style mutation-heavy code comparatively risk-free, so despite me normally being the &quot;Functional Programming Nerd&quot;, I generally write Rust in a style that&#x27;s a bit closer to C.<p>I use musl for my Rust stuff because I <i>have</i> noticed that for the stuff I write it appears to have a lower memory footprint; since a lot of what I do is IO-bound anyway, I care more about using less memory than raw performance.
  • marssaxman2 hours ago
    People have such different perspectives. 26% slower does not sound &quot;terrible&quot; to me; it sounds like quite a reasonable price one might choose to pay for the convenience musl offers. If musl&#x27;s allocator were 2.6x slower, I might call that &quot;not so great&quot;... but in order to qualify as &quot;terrible&quot; I think the difference would have to be an order of magnitude!
    • loeg2 hours ago
      The 26% number at the top of the article is from using mimalloc (which is a high performance allocator, at least as fast as the glibc allocator) + musl for some task, and the slowdown is coming from (probably) slow musl implementations of memcpy&#x2F;memset. The musl allocator is even worse.
      • hibikir1 hour ago
        Yeah, doing compute-heavy work a couple of jobs ago, we tried small images with musl, and the default allocator was a catastrophe: 75%+ slowdowns for our real life tasks. Even with a better allocator, we were way better off with the larger image.
      • Joker_vD1 hour ago
        &gt; the slowdown is coming from (probably) slow musl implementations of memcpy&#x2F;memset.<p>It&#x27;s wild that such a fundamental piece of code (you can&#x27;t really implement operation on structs without those) is library-supplied. I wish compilers would just have something like __builtin_memcpy and __builtin_memset, and provided some highly optimized, specialist-crafted assembly in those, instead of having to inline the library code and hopefully be able to optimize it.
        • compiler-guy1 hour ago
          Clang and GCC do provide these, and automatically use them in many situations (particularly small copies). But c-libraries can actually do it better in many cases, especially for large copies.<p>Glibc, for example, has perhaps ten different implementations of memcpy <i>just for x86</i>. The compiler certainly could provide all that, but the next step is harder:<p>glibc automatically dispatches to the proper one at runtime based on the actual microarchitecture that the binary is running on. You pay the extra dispatch cost once, but all of non-inline function call cost every time. This is what allows distros to compile to a nice baseline architecture, but still get near-optimal memcpy performance on many more architectures than a single inline instance could possibly give. These differences matter.<p>And it does it for not just memcpy, but half-a-dozen other extremely performance sensitive library functions, like strcpy and so on.<p>Inlining works very much against this strategy. If you can guarantee that the target microarch never changes, then it isn&#x27;t a good one. But that is somewhat unusual for everyone but those who build their own binaries to run on a single class of machines forever.<p>Worse, inlining the really high performance versions of these ends up being terrible from a code size perspective, because they are often hundreds of instructions, which can have bad caching effects. And once you amortize the function-call cost over many iterations of the loop, it isn&#x27;t so expensive to call out to the library.<p>Anyway, just some additional considerations to think about.
        • fweimer5 minutes ago
          These builtins of course exist, it&#x27;s how compilers keep track of the behavior of these functions.<p>For GCC, there is -minline-all-stringops:<p><a href="https:&#x2F;&#x2F;gcc.gnu.org&#x2F;onlinedocs&#x2F;gcc-16.2.0&#x2F;gcc&#x2F;x86-Options.html#index-minline-all-stringops" rel="nofollow">https:&#x2F;&#x2F;gcc.gnu.org&#x2F;onlinedocs&#x2F;gcc-16.2.0&#x2F;gcc&#x2F;x86-Options.ht...</a><p>It does what it says, but the results may not be what you expect.
        • SkiFire1357 minutes ago
          &gt; I wish compilers would just have something like __builtin_memcpy and __builtin_memset<p>The ones provided by the compilers are simply the libc ones.<p>LLVM will even go as far as detect attempts to rewrite memcpy and replace them with a call to the libc one!
          • wren699150 minutes ago
            Even if the attempt is inside of a function called memcpy() which contains no code other than your copy loop, and links with priority over the libc implementation! (as all embedded firmware engineers learn at some point in their journey)
        • kvuj1 hour ago
          Maybe you&#x27;re being sarcastic, but I&#x27;m pretty sure clang + gcc do offer these.<p>The problems at first glance :<p>- Not having control over the implementation detail of the interface that your library provides is probably not wise. Sounds like a lot of bad bug reports and edge cases that you have no control over.<p>- Not all compilers may provide these.
    • SkiFire1359 minutes ago
      The 26% slower appears to be for their whole application, not just the allocator. For some parts of the application to make the whole this much slower it must mean that those parts are quite a lot slower, likely much more than 2x.<p>Moreover the 26% is with mimalloc, with musl&#x27;s allocator it&#x27;s 144%, so there are likely other parts that are slower (likely the memcpy implementation)
    • otterley25 minutes ago
      I&#x27;m curious. What convenience, specifically, are people benefiting from by using musl?
      • plorkyeran4 minutes ago
        If you want to ship a prebuilt binary that&#x27;ll run on any linux distro you need to statically link libc and musl is by far the easiest way to do that.
      • sombragris19 minutes ago
        The convenience (?) of not having to comply with GPL terms. Some people really hate copyleft.
    • stackskipton1 hour ago
      Ops here, I think if you NEED that convenience, sure, rock with MUSL BUT I also see a ton of devs crowing about using MUSL on my 128GB x86 Kubernetes hosts. I have plenty of Disk Space, you can ship glibc based container.
    • fhn38 minutes ago
      Tell your employer a 26% pay decrease for you is acceptable.
  • bloppe2 hours ago
    This is only one aspect of &quot;performance&quot;. glibc&#x27;s allocator may be faster, but it also uses more memory.<p>For a much more technical discussion, see <a href="https:&#x2F;&#x2F;github.com&#x2F;sharkdp&#x2F;fd&#x2F;issues&#x2F;710" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;sharkdp&#x2F;fd&#x2F;issues&#x2F;710</a>
  • mattrighetti2 hours ago
    Posted this the other day but the whole musl allocator thing seems to be well known [0]<p>[0]: <a href="https:&#x2F;&#x2F;news.ycombinator.com&#x2F;item?id=45143347">https:&#x2F;&#x2F;news.ycombinator.com&#x2F;item?id=45143347</a>
    • masklinn1 hour ago
      Musl&#x27;s allocator being awful is pretty well known, though mostly in that it&#x27;s absolutely awful in multithreaded context. TFA points out that musl has a bunch of other noticeably slower functions, which is less well known (though they&#x27;re also slower by a smaller factor, and they don&#x27;t worsen as your parallelism increases).
  • grep_it2 hours ago
    I think the size of linked binaries and simplicity were always the main features?
    • Joker_vD2 hours ago
      Yeah... I&#x27;ve recently had a chance to compare how fgets is implemented in both GNU libc and musl, and, well. With glibc, it was a challenge to even find where the fgets&#x27;s code actually is.
    • jdc-pub1 hour ago
      I haven’t dug into why, but for unknown-linux builds on x86, Rust binaries have been substantially smaller on musl than standard dynamic linking to glibc, for me. No idea if I’m doing something wrong or if the handful of cases I tried were all special in some way.
  • OptionOfT2 hours ago
    Are there other options if I want to ship a &#x27;FROM scratch&#x27; image with just a single Rust executable, and everything compiled in?<p>That to me is the main driver for MUSL.
    • kccqzy54 minutes ago
      You can do FROM scratch, and use still glibc; it’s just that you need to copy more than one file. I don’t really understand if you are already dealing with images why you still need the image to contain a single file.
    • masklinn1 hour ago
      The best option would be the x86_64-unknown-linux-none target (<a href="https:&#x2F;&#x2F;doc.rust-lang.org&#x2F;nightly&#x2F;rustc&#x2F;platform-support&#x2F;x86_64-unknown-linux-none.html" rel="nofollow">https:&#x2F;&#x2F;doc.rust-lang.org&#x2F;nightly&#x2F;rustc&#x2F;platform-support&#x2F;x86...</a>) however it currently a tier 3 with a single maintainer (so it is technically available but unsupported).<p>If this specific use case is of high interest to you and you have some available bandwidth, contributing to it, maybe becoming a maintainer, and eventually organising a tier 2 MCP would definitely be a good idea.
      • VorpalWay58 minutes ago
        Note that this is no-std no-alloc target, with all the limitations that leads to.<p>You could add alloc with a custom global allocator, but I don&#x27;t even know what high perf global allocator you could use that wouldn&#x27;t need libc. Jemalloc and mimalloc are out. Some embedded allocators would work (but those are rarely high performance, instead being optimised for small code and data footprints).<p>That said, with enough effort (quite a lot!) it would be possible to add support for alloc and std without libc on Linux specifically (since it has a stable syscall ABI).<p>What might be more realistic though is looking at relibc (a rust implementation of libc, made for Redox OS but from what I read it also supports Linux). But I haven&#x27;t tried it and I don&#x27;t know the state (or goal) of it.
        • masklinn5 minutes ago
          &gt; it would be possible to add support for alloc and std without libc on Linux specifically (since it has a stable syscall ABI).<p>Well yes that’s a Linux specific target so that’s kinda the point.<p>Technically you could do libcless on a few other platforms which are not <i>actively hostile</i> to it (yet) like freebsd, but that would have no chance of getting to tier 2 if it was even accepted.
    • nazgulsenpai2 hours ago
      Same usecase here, I use musl for compiling self contained Nim utilities I use on containers and servers without having to deal with glibc hell.
  • up2isomorphism1 hour ago
    Funny thing is that the major reason most people use musl is because glibc make it (artificially) hard to do completely static linking.
  • desdenova2 hours ago
    Most of musl&#x27;s performance issues come from their allocator. Using it with a third party high performance allocator allows you to benefit from static linking with very little performance loss.
    • loeg2 hours ago
      &gt; Most of musl&#x27;s performance issues come from their allocator. Using it with a third party high performance allocator allows you to benefit from static linking with very little performance loss.<p>This is addressed and disputed very early in the article. The very first benchmark presented shows a 26% regression using musl + mimalloc, a high-performance 3rd party allocator.
      • masklinn1 hour ago
        It&#x27;s not <i>really</i> disputed since musl <i>without</i> mimalloc has a 144% overhead, so most of the performance issues do indeed come from the allocator, by a pretty large margin (~85% of it). Not only that, but some of the &quot;other code&quot; performance hit might still come from the allocator: when you set a global allocator on the Rust side, musl still uses its own allocator internally (as demonstrated by <a href="https:&#x2F;&#x2F;github.com&#x2F;BurntSushi&#x2F;ripgrep&#x2F;issues&#x2F;3494" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;BurntSushi&#x2F;ripgrep&#x2F;issues&#x2F;3494</a>).<p>And the compounding issue is that the allocator issues get significantly worse as parallelism increases, as the allocator is serial, so as concurrency increases so does the impact of the allocator, which is not the case for most of the &quot;regular slow&quot; code (of musl), those have a relatively constant overhead per thread.
        • loeg54 minutes ago
          &quot;Using it with a third party high performance allocator allows you to benefit from static linking with very little performance loss&quot; is disputed; 26% is not &quot;very little,&quot; even if 144% is worse.
  • delduca2 hours ago
    Also musl is not a complete runtime
  • dchest19 minutes ago
    [dead]
  • legastenigga2 hours ago
    [dead]