8 ms·
Making Rust binaries smaller by default
- anonymoushn 3y agoSo what's in the remaining huge 415KB hello world program? Can we get rid of 90% of it a couple more times?
- kobzol 3y agoMostly the formatting machinery from stdlib. You can go sub 100 KiB or even less without it, but unless you target embedded, there's really a reason to do that, IMO.
- lifthrasiir 3y agoBuffered I/O and synchronization primitives to avoid corrupting the buffer. The underlying vector implementation and memory allocator. Panic support and backtrace printer, which includes a Rust-specific name demangler and a not-so-small DWARF parser in Linux. Path support because backtraces would include source file paths. Zlib-compatible decompressor because ELF allows compressed sections (!). Then you have several formatters that are often pretty large (e.g. f32 and f64 would add at least 30 KB of binary, and some depend on Unicode grapheme clusters). They are all essential for edge cases that can happen even for such a simple program.
- temac 3y agoWhy does it need to parse elf ?
- lifthrasiir 3y agoThe stack frame is a list of return addresses which have to be translated to a file name and line number. Such debug information is within a specific ELF section in the predefined format. In this simple case you may be able to hard-code the offset to the section (and guarantee that it was never compressed), but any additional C or Rust library will break this assumption, so a general parser has to be included.
- nsteel 3y agoSorry if this is dumb but if the idea is stripping debug stuff, why would a parser for translating return addresses into file names and line numbers be useful? EDIT: oh, ok, so I guess it's because strip is "debuginfo" here, rather than "true".
- kobzol 3y agoThere is some work underway to enable removing the backtrace generation/parsing from Rust binaries. It's hardcoded for now though.
- owl57 3y ago> several formatters that are often pretty large (e.g. f32 and f64 would add at least 30 KB of binary …) I know floats are full of scary corner cases, but… Assuming tens of bytes per an if statement, hundreds of corner cases just in formatting? Is it really that bad?
- lifthrasiir 3y agoA naive implementation would be small, but a correct implementation (with some concrete definition of "correct") will need a sizable data table. I know this because I wrote that part of code [1] and somehow it is still in the std even though other algorithms now exist [2]. [1] https://github.com/rust-lang/rust/pull/24612 https://github.com/rust-lang/rust/pull/24612 [2] https://github.com/rust-lang/rust/issues/52811 https://github.com/rust-lang/rust/issues/52811
- riquito 3y ago32Kb with the following configuration, on a linux target. It's not the default, you're using nightly, it's more complex to build and there are tradeoffs. You can even go less than that if that's your thing [profile.release] strip = true opt-level = "z" lto = true codegen-units = 1 panic = "abort" cargo +nightly build -Z build-std=std,panic_abort -Z build-std-features=panic_immediate_abort --target x86_64-unknown-linux-gnu --release As mentioned in another thread, I've simply followed https://github.com/johnthagen/min-sized-rust https://github.com/johnthagen/min-sized-rust
- flohofwoe 3y agoThese look like sensible defaults to me for release mode. What are the tradeoffs?
- rfoo 3y agoopt-level = "z" may be slightly slower than O2/O3. lto = true and lto-units = 1 makes the final linking unbearably slow for large programs. These are still okay, but then `panic = "abort"` shakes a lot of code off by making the final executable unable to print a nice stack trace (even without symbols) and instead immediately traps/abort()-s. Edit: and GP also used "panic-immediate-abort", which also removes the dependency to std::fmt::format!() because it now silently abort()s without even printing an error string.
- exDM69 3y agoAdditionally the panic = "abort" disables stack unwinding, and running destructors when panic occurs, so it's a big change to the semantics of the program. It's comparable to C++ land -fno-exceptions (not exactly, but similar).
- junon 3y agoAbort panic strategy is common in firmware and osdev, for anyone wondering if there are other use cases.
- 3y ago
- muzamil2002 3y ago[dead]
- Humphrey 3y ago> I can imagine a situation where a seasoned C or C++ programmer wants to try Rust, compiles a small program in release mode, notices the resulting binary size, and then immediately gives up on the language and goes to make fun of it on the forums. While I'm not a seasoned C or C++ programmer I have definitely done this to a few new languages when playing around with them. My thought was "If helloworld.rust is this large, it'll be huge once I've actually written more code".
- dataflow 3y agoYup same. Also I hate the "just strip it" advice. I don't know who on earth strips random binaries and has them actually work fine afterward 100% of the time. Stripping after linking frequently breaks binaries I try it on.
- champtar 3y agoAll binaries on a classic Linux distro are stripped (you can use 'file' to quickly check some), so your issues might be worth reporting
- dataflow 3y agoStripped via -s or via strip?
- kelnos 3y agoI never understood why strip's "--strip-unneeded" flag wasn't the default. Leaving that out can often enough give you a binary that just doesn't work.
- dataflow 3y agoYup that's exactly what I mean. Nobody ever mentions that flag when recommending stripping, and it's frustrating when you realize it's broken. It's been a few years, but even when I finally discovered and tried that flag, it still didn't quite do the right thing. Either it failed to strip stuff that -s stripped, or it still produced a binary that didn't work... I forget what exactly the issue was. I just remember failing to find an alternative to cc -s that was guaranteed to work.
- lionkor 3y agoInitial binary size, like of a hello world, is a great indicator of how much abstraction the language has that youre also paying for. For example, java has to set up a constants pool, parse it from the classfile, run init and cinit, resolve references, allocatea a frame, and so on, just to get to the entrypoint. Compared to C without stdlib which needs to basically just run a few bytes to syscall write(). There are obviously massive advantages to Java for which these steps are needed, but you do pay for it.
- frfl 3y agoI'm not trying to be rude, but what does your comment have to do with the actual article linked. I suspect you're commenting based solely on the title? If you read the article it mentions 90% reduction if you remove the debug symbols from the rust std lib that get bundled in the final hello world binary. A conversation about Java's size and abstraction is quite irrelevant to the article.
- kelnos 3y agoThe article talks about how "hello world" binary size can be part of the first impression someone new to your language has. Java is a nice illustration of that. Rust probably isn't, because many of Rust's abstractions are zero-cost, so it's not as obvious a comparison.
- lionkor 3y agoMy comment is pretty unrelated to the content of the article, but more has to do with what others said here in the comments
- nindalf 3y agoIt’s funny, because your claim that it’s a great indicator is contradicted by the article we’re discussing. Rust was widely considered to be “only pay for what you use” despite having 4MB hello world programs. And now that it’s 400KB that continues to be true. So in this case, 4MB wasn’t a good indicator was it?
- 3y ago
- eviks 3y ago> To reduce download bandwidth2, it does not come in two variants (with and without debug symbols), but only in the more general variant with debug symbols. Why not instead only without?
- kelnos 3y agoIf you have them, but don't want them, you can throw them out. If you don't have them, but want them (as does anyone who doesn't build in release mode when developing), you're out of luck. Sure, you can rebuild the standard library, but it's a lot simpler to strip the output than set things up so the first time you want debug symbols, it has to rebuild std, cache it somewhere, not rebuild it again on the next build, but be sure to invalidate that cache the next time std gets updated. And in general, not wanting debug symbols is the last step in development, before making your first release. Before then, you pretty much always want those debug symbols, except for when you're benchmarking binary size or something. So if they were going to ship std without debug symbols, they'd probably just be better off not shipping a prebuilt std at all, as pretty much everyone would end up having to build it on first run of 'cargo build' anyway. (Which is maybe fine, actually?)
- riquito 3y agoMy go to reference when I want to reduce rust binary size is the excellent https://github.com/johnthagen/min-sized-rust https://github.com/johnthagen/min-sized-rust, a set of guidelines on how to reduce size with explanations of the consequences
- eviks 3y ago[flagged]
- baq 3y agoLow priority issue easily fixed by googling first if you actually care. And it isn’t like release builds with debug symbols are a bad thing. Getting good bug reports matters, too.
- edflsafoiewq 3y agoAs the article points out, having debug symbols for only the stdlib is not very useful.
- eviks 3y agoExcept it is a bad thing, you just need to increase the low priority of reading the article
- kobzol 3y agoI have implemented the fix and got it approved in the span of about 3 weeks (and that was over the Christmas!). Doesn't seem that bad to me, given that it's a change that will affect pretty much any Rust user by default.
- eviks 3y agoEven if just focused on time alone, that's 3 weeks and all the years when you kept rediscovering it Then there is the fact that it required establishing the whole working group All for a simple fix that positively affects pretty much any Rust user
- kobzol 3y agoIt didn't really require establishing a working group :) That is just an effort to improve Rust binary sizes in general. In the end, changes in Rust can take a very long time (years) sometimes. It's maintained and developed by volunteers, and it has been growing exponentially these past few years, so not everything can move forward as fast as we would like.
- mischief6 3y agonot a rust programmer. can someone explain why this is in the final binary and not in a shared library?
- simonask 3y agoThe Rust compiler statically links everything into a single binary. This can be changed, but is generally discouraged as there are no ABI stability guarantees at this point.
- zozbot234 3y agoYou can use the C ABI if you want something stable, and there are custom crates that will help with translating from the Rust to the C ABI. (This also helps because the resulting shared objects can potentially be FFI'd with from any language, not just Rust. They might as well be plain C libraries as far as anyone is concerned, only with the usual Rust safety requirements.)
- tialaramex 3y agoAlso, even in "the" C ABI provided by Rust out of the box there's a certain amount of "Well, this is probably what your C compiler does here, but there's no requirement" rather than an actual hard ABI document. A lot of the "We're ABI stable" claims in C and C++ are "We daren't change anything or stuff breaks" which isn't so much an ABI as it is paralysis and Rust is confident it doesn't want to do that.
- Vogtinator 3y ago> A lot of the "We're ABI stable" claims in C and C++ are "We daren't change anything or stuff breaks" which isn't so much an ABI as it is paralysis For the C++ standary library maybe, but for pretty much all others which provide ABI compatibility it's a concious and properly followed decision.
- dhruvrajvanshi 3y ago
- raggi 3y agoIt'd be nice if the default was external symbols rather than none at all
- berkes 3y agoI'd find that a bit counterintuitive. To me, --release is an alternative for --debug. Debug implies debug stuff is in there, therefore the alternative removes it. But I do find it useful now and then to have debug symbols in production, so that monitoring and telemetry gets some context. But to me, it's rather logical that I need to add this to the build. I would, however, love it when it's trivial to get these symbols set up external.
- raggi 3y agoGNU binutils and LLVM both have support for debuginfod server topologies now, it would be great to see Rust tools get on the train. https://github.com/llvm/llvm-project/commit/36f01909a0e29c1014301ed6835687a84bf0e9fa https://github.com/llvm/llvm-project/commit/36f01909a0e29c10... And naturally we already have a port: https://crates.io/crates/debuginfod-rs https://crates.io/crates/debuginfod-rs
- 1vuio0pswjnm7 3y ago"Rust tries to appeal to programmers coming from many different backgrounds, and not everyone knows that something like stripping binaries even exists."
- cybrox 3y agoIt's not ideally written but it's safe to assume if someone compiles a quick test program for that purpose and it ends up being 4MB, they might just move on to other options instead of starting to think about how they can manually reduce the binary size. When evaluating languages, you rarely look foor good enough results to work around.
- Novosell 3y agoMy machine has 32GB of RAM, why should I care about binary size? For embedded, sure. But most people don't write for embedded. If it was like 1GB, sure. But it aint.
- pdimitar 3y agoBecause your employer might want the deployed final binary work fast on a 1 vCPU container with 1GB of RAM.
- cybrox 3y agoThe other replies cover "the why" adequately already but as a tangent: Every company makes some effort to be green and eco friendly these days, yet we're perfectly happy wasting billions of hours of compute time every second because "who cares" and "we can"... Same mindset.
- rkangel 3y agoYes. The statement I agree the most with in the blog post is "defaults matter".
- 1vuio0pswjnm7 3y agoAt this moment, I am compiling GCC 10.5 on a RPi Model B with 256M of RAM. The root filesystem is on a ramdisk embedded in the kernel. The SD card from which I booted is read-only. The compilation runs not from the SD card but from a chroot to a large external drive plugged into the RPi via USB. Once I have GCC and rest of the toolchain built, I then use it to compile custom kernels with embedded ramdisks. https://en.wikipedia.org/wiki/Self-hosting_(compilers) https://en.wikipedia.org/wiki/Self-hosting_(compilers) Wish I had a computer with 32GB RAM but I don't. I use strip -s every day.
- LAC-Tech 3y agoIt's great to see rust maturing. That people care about binary size and work to improve it is a good sign.
- flohofwoe 3y agoIt's not a great sign that this bug existed for 7 years, people noticed, asked about it, wrote tickets, everybody agreed that it should be fixed but then nobody took the time to actually do it (and the eventual fix is a rather crude hack by just stripping the output binary instead of preventing that debug symbols slip into a release binary in the first place). Looks more like a systemic issue in the Rust development process to me tbh. What's more shocking though is that even after a 90% size reduction, a vanilla hello world is still 415 KBytes. That's about 10..100x bigger than I would expect from a low level "systems programming language".
- lifthrasiir 3y agoA vast majority of that 415 KB is due to the backtrace support, which is amazingly complicated. (See my other comment for specifics.) It will depend on some more machinaries from std, and a "simple" hello world can always panic if stdout is closed or so, therefore that part of std cannot be easily removed unless you are fine with useless backtraces. Also, Rust has no direct platform support unlike C. So everything has to be statically linked to be portable. A statically linked glibc is indeed much larger than that (~800 KB in my machine). Conversely, you can sacrifice portability and link to `libstd*.so` dynamically to get a very small binary (~17 KB in my machine, both for C and Rust).
- flohofwoe 3y agoFor a statically linked libc I would expect that only the stdlib code that's actually used is included in the program (even without LTO). For instance a statically linked C hello world for Linux via MUSL (cross-compiled with `zig cc -Os -target x86_64-linux-musl hello.c -o hello` because I'm currently on a Mac) is just 5 KBytes.
- milianw 3y agoI applaud this initiative. yet I wonder - the ideal situation would be to not throw away the debug info (ever), but rather it should be put into an external file. even Linux supports this for many years and nowadays we even have direct support with dwarf5/dwo. is trust taken any action towards that direction? after all: without debug info (which you do not want to ship to customers necessarily), you cannot do profiling or debugging in any meaningful way...
- emi2k01 3y agoSplitting the debug info from the executable is supported on the 3-big-OSes but only enabled by default on Windows (and maybe macOS?)
- goku12 3y agoMany Linux distributions have debuginfod servers that supply split debugging symbols on demand. So one could argue that it's implemented by default on Linux too.
- lifthrasiir 3y agoCargo does support `split-debuginfo` [1] but it still has some rough edges in my experience. I do think that is the ultimate way to go in the future. [1] https://doc.rust-lang.org/cargo/reference/profiles.html#split-debuginfo https://doc.rust-lang.org/cargo/reference/profiles.html#spli...
- skor 3y ago> I can imagine a situation where a seasoned C or C++ programmer ... make fun of it on the forums Is this really a thing? Do C folks make fun of Rust?
- timschmidt 3y agoPeople can be quite tribal. Especially the ones who haven't taken the time to learn what the other tribe is up to and why.
- irdc 3y agoWhat is this “C programmer” you speak of? Anyone who’s been doing this for long enough is a polyglot.
- Voultapher 3y agoThat mirrors my experience. I've hardly ever met really experienced people that are hardstuck on language flaming. Programming languages are tools, with general and situational up and downsides.
- whstl 3y agoSame. Reddit gave me some interesting insights into people like this. Whenever I come across a very radical opinion, I check their whole profile. It is often someone without much if any experience, and clearly doing it just for the sake of tribalism.
- lieks 3y agoYes, we do, in some contexts. Because despite its complexity, slow compile times, lack of a specification or more than one viable implementation and bloated binary size, people still tout it as a C replacement. For application programming, Rust is fine, but for embedded and systems programming, nearly any of those on its own can be enough to eliminate Rust as an option, depending on the situation. Except for complexity, they are all solvable and being worked on, for this very reason. But Rust can never replace C completely, because it's not simple, and there is a sizeable portion of primarily C programmers who are minimalists.
- weinzierl 3y agoI was wondering if dead code elimination also removes the debug symbols related to the dead code? The ~ 4 MiB of debug symbols the article talks about are for the whole libstd or just the portions that actually ended up in the binary? I think the new default makes sense but I'd love to have the option to build a lean but debuggable release binary with just the needed symbols.
- kobzol 3y agoYou can't really use dead code elimination on debug symbols, because you don't know which symbols will you need. You would need to know where and how will your program crash or if the user will want to use a debugger on it.
- weinzierl 3y agoI don't understand. If a function isn't in the binary I will never need its name, so it does never have to be in the debug information. Likewise for variables.
- pornel 3y agoI think linkers don't go this far to rewrite the debug info. If libstd is used, libstd's debug info is used. The second problem is dynamic dispatch. The overhead in "hello world" is from printing and panicking machinery (it handles closed stdout), and these features use vtables, so it's even harder to precisely analyze what's actually used and what isn't.
- pdimitar 3y agoIt's really a shame that Rust includes the stdlib piecemeal in binary form, debug symbols and all, in every final binary. I do love Rust but binary sizes have always annoyed me greatly and I always had this nagging feeling that part of all programmers don't take Rust seriously because of it. And I actually have witnessed, several times in the last 2-ish years, older-school programmers berating and ignoring Rust on that basis alone (so the author is quite right to call this out as a factor). Looking at the https://github.com/johnthagen/min-sized-rust https://github.com/johnthagen/min-sized-rust repo, final binary size of 51 KB when compilation / linking / stripping takes stdlib into account (and not just blindly copy-pasting the 4MB binary blob) is acceptable and much more reasonable. I wouldn't care for further micro-optimizations e.g. going to 20KB or even 5KB (further down the README file). I also don't use nightly in my Rust work so I guess I'll have to wait several more years. :(
- lifthrasiir 3y agoBut why? I mean, I'm also obsessed of the byte size from time to time and I do often optimize for the size when it's doable, but in practice anything below 1 MB seems small enough that you don't need to optimize further. There are so many low-hanging fruits when it comes to the Rust binary size...
- bipson 3y agoDepends on what you're aiming for. If we talk about ultra-low-power platforms, e.g. energy-harvesting IoT devices, 1MB is still quite a lot. If we are going to argue that Rust can compete with C/C++, it needs to have similar performance, also regarding binary size.
- lifthrasiir 3y agoC/C++ standard library doesn't work in that environment anyway---you typically need a freestanding mode. Rust equvialent is `#[no_std]` which works well for popular boards.
- 3y ago
- nektro 3y ago> In fact, this new default will be used for any profile which does not enable `debuginfo` anywhere in its dependency chain, not just for the release profile. won't this destroy stack trace support?