Login
You're viewing the functional.cafe public feed.
  • Sep 1, 2026, 12:55 AM

    Can someone explain why #Rust needs N *gigabytes* of target/debug disk space when it's compiling an N *megabyte* binary? That seems... insane? Why does it need THREE orders of magnitude more disk space? One, I can understand (I mean, not really, but it's small enough to ignore), but THREE ORDERS OF MAGNITUDE?!?!?

    And the crates dir is 283 *kilobytes*. Can someone please explain what is going on here?

    💬 4🔄 6⭐ 8

Replies

  • Sep 1, 2026, 1:28 AM

    @TheRealPomax
    I remember the first time I went to free up disk space and found one of the crates / build file folders being so big, it was quite a shock!

    For the crates at least, it seems each different version of each subdependency of a dependency end up with its own copy, that then ends up in that repository of crates... And then there are all the binaries / build files associated with those. But yeah, it all uses a lot more storage than I'd expect

    💬 0🔄 0⭐ 0
  • Sep 1, 2026, 5:20 AM

    @TheRealPomax the crate is the unit of compilation. Each crate gets compiled fully, and there may often be both debug and release versions. The crates ecosystem is large with lots of dependencies, even if many of those dependencies might only be used for a small part of its total functionality.

    Then when the program is linked, all the unused code is left out and the binary is much smaller.

    💬 1🔄 0⭐ 2
  • 💬 0🔄 0⭐ 0
  • Sep 1, 2026, 6:16 AM

    @TheRealPomax Yes, I can :3. I'm on the compiler team (though I haven't been super active recently) and spent some time looking into disk usage, so I know what's in the target dir. It can also vary by project of course, but this is what I'd say are the most important factors. They are factors in that they all multiply each other's effect.

    The first factor is that binaries are just a lot bigger than source code in Rust. With generics and inlining you can have a lot of abstraction in the source code that compiles down to straightline efficient code. This increases the relative size.

    The second big factor is debuginfo. By default, for dev builds, Cargo enables full debuginfo `-Cdebuginfo=2` which includes variable and inlining information. Because of the previous factor of complicated abstraction, this is huge! But the good news is that if you don't debug your binaries you can remove it: doc.rust-lang.org/cargo/refere

    Then, the third factor is incremental compilation. To speed up compilation after you change something, rustc saves intermediary results for each crate (not for dependencies from crates.io though). This includes one big file with results from its internal query system, and the object file (output from LLVM) from each codegen unit, of which there are dozens per crate (up to 256 by default). Due to generics and the `#[inline]` attribute, many functions get duplicated into many of these codegen units. You can also run off incremental compilation to save tons of disk space (see previous link) but that does lose you some amount of compilation speed.

    Overall, we can say that both when it comes to the language and default build system config, Rust chose the tradeoffs against build system disk use. This gives it other advantages, but it does make the disk use huge.

    💬 1🔄 6⭐ 13
  • Sep 1, 2026, 2:38 PM

    @noratrieb Cheers. That's still a crazy amount of extra data then, though. Because dozens and dozens of separately compiled crates is still just... Two orders of magnitude =P

    💬 0🔄 0⭐ 1
  • 💬 1🔄 0⭐ 0
  • Sep 1, 2026, 3:41 PM

    @esoteric_programmer that underestimates exactly how incredibly much bigger three orders of magnitude is. It's the difference between a day, and 3 years, or a postage stamp and a table. It is an *incredibly* larger amount. One order would be something you don't even think about, two orders would be "seems high, but also doesn't seem odd", three orders is "why do I need an entire airplane hanger to build a bike?" =D

    💬 0🔄 0⭐ 0
  • Sep 1, 2026, 4:44 PM

    @TheRealPomax woe, that much? maybe sort the target folder by biggest file and so on? The only time I got this issue is when I compiled something based on the matrix rust SDK, but I never actually dug in to see what's going on

    💬 0🔄 0⭐ 0