News for hackers. Stuff that matters.
Re:Making Rust binaries smaller by default by lifthrasiir on Wednesday January 24, 2024 @06:17AM (#39116000)
But 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...
Re:Making Rust binaries smaller by default by 0x000xca0xfe on Wednesday January 24, 2024 @07:39AM (#39116551)
I work in embedded software and we fight for every megabyte of NAND flash. Multiple smaller executables add up, too.

Reducing the binary size is usually more important than performance (and often more important than memory safety, if we are totally honest...).

Personally, I hate bloated software just as much as slow software. It's crazy what you can do with 64 kilobytes, let the demoscene blow your mind: https://www.youtube.com/watch?v=ZfuierUvx1A

Would be awesome if compilers could do the same :)

Re:Making Rust binaries smaller by default by lifthrasiir on Wednesday January 24, 2024 @08:19AM (#39116936)
While I'm not a demoscener, I know enough about how they are generally made and have my own share of award-winning small programs. So I'm confident that they don't make a good example of non-bloated software.

Every demo is really amazing when you encounter them for the first time, but AFAIK 64K demos were hottest around 2000 when Farbrausch revolutionalized the scene with .fr-08: .the .product [1]. Since then, 64K turned out to be too large because most 64K demos can be divided into multiple parts---engine, data and compressor---and each part can be individually developed. There are many 64K demos but far less engines and only a handful number of compressors in this level. It also means that there are only a handful number of people that can actually make engines and compressors. It's not a fit software, it's rather an unhealthily thin software. They are still awesome but they can't be a model.

[1] http://www.theproduct.de/

Re:Making Rust binaries smaller by default by MrJohz on Wednesday January 24, 2024 @08:04AM (#39116781)
In fairness, this is just doing something automatically that you could have done manually anyway. It's a good change because defaults matter, but it's always been possible to strip debug symbols from Rustc-generated binaries, and all the guides I've seen to reducing binary stuff have mentioned this already.

It's also important to remember that in embedded software, you're probably working in a no_std environment anyway, which means that the Rust standard library doesn't need to be included in the binary, which will already significantly reduce the size of your binary.

A lot of this is about tradeoffs, and I think the article does a good job of explaining what tradeoffs are relevant here. Yes, binaries should be as small as possible, but shipping multiple compiled standard libraries is also not ideal. The Rust team seem to have gone for a good default for people who are beginning, with lots of ways to tailor the build process for people who don't need debugging information, or who are willing to accept longer install/compile times in exchange for smaller, more optimised binaries.

Re:Making Rust binaries smaller by default by 0x000xca0xfe on Wednesday January 24, 2024 @08:16AM (#39116899)
I'm not criticizing Rust, I'm criticizing statements that doubt whether the effort to reduce the binary size is worth it.

We currently have some nasty C++/boost monstrosities, if Rust can deliver a better development experience AND reliable stack traces AND smaller binaries, it would be HUGE for embedded.

Re:Making Rust binaries smaller by default by pdimitar on Wednesday January 24, 2024 @08:45AM (#39117257)
Stripping debug stuff has always been possible, yes, but that doesn't entirely address the problem of the stdlib being included piecemeal. I want a compiler who throws away everything that's not used in the final binary.

If that means that 90% of the stdlib is unused (which is likely true for many small projects) then it should not be included in the binary.

Re:Making Rust binaries smaller by default by stefs on Wednesday January 24, 2024 @09:17AM (#39117580)
Isn't that the linkers job? To throw away unused functions?

I remember this being the standard decades ago. It might not make sense in certain situations (i.e. with reflection), but that shouldn't be the default case anyway. What happened?

Re:Making Rust binaries smaller by default by pdimitar on Wednesday January 24, 2024 @09:18AM (#39117593)
Yes, but I was left with the impression that including the stdlib in its entirety prevents proper tree-shaking / dead code elimination. Could be wrong.
Re:Making Rust binaries smaller by default by timeon on Wednesday January 24, 2024 @08:05AM (#39116796)
> I work in embedded software

But embedded Rust usually does not use stdlib. This thread started with:

> It's really a shame that Rust includes the stdlib piecemeal in binary form, debug symbols and all, in every final binary.

Which is not case for embedded.

Re:Making Rust binaries smaller by default by 0x000xca0xfe on Wednesday January 24, 2024 @08:12AM (#39116855)
We are shipping a full blown custom Linux distribution, embedded software is more than just microcontrollers. Binary size is still just as relevant.
Re:Making Rust binaries smaller by default by LoganDark on Wednesday January 24, 2024 @08:14AM (#39116873)
Are you confusing "embedded" for "microcontrollers without an operating system"? Because there are plenty of embedded CPUs that can run Linux. Buildroot even exists to make it easier to create distributions for these types of devices. For example, I would consider the reMarkable 2 to be an embedded device, but it runs full-blown Linux (to the point of having a bloated Qt stack).

That's probably an extreme case though, as nobody in their right mind would actually try to run Qt on an embedded device with any reasonable limit on resources. I'm sure rM only does it because they can afford to be wasteful - they have hundreds of megabytes of space for the operating system, and all the latency-sensitive stuff is definitely not built on Qt.

Re:Making Rust binaries smaller by default by deschutes on Wednesday January 24, 2024 @11:20AM (#39119167)
You can't be everything to everyone. Packaging considerations for niche use cases rightfully take a back seat to debuggability and packaging considerations in modern web deployments and consumer devices.
Re:Making Rust binaries smaller by default by Aissen on Wednesday January 24, 2024 @12:12PM (#39119802)
You can always do a multi-call binary (busybox-style) so that you only get one copy of the stdlib. Coupled with build-std (and other min-size things), it should bring rust code size to a manageable level.
Re:Making Rust binaries smaller by default by gred on Wednesday January 24, 2024 @07:01AM (#39116297)
I don't have a horse in this race, but a programming language that prides itself on "zero-cost abstractions" and then generates a Hello World program that is 90% wasted space doesn't leave a great impression.
Re:Making Rust binaries smaller by default by lifthrasiir on Wednesday January 24, 2024 @07:08AM (#39116342)
Most debugging informations are "useless" until they are desparately needed, and this "wasted space" was no exception. So as always, it's another trade-off: will you "waste" some bytes to ease yourself in the future? Cargo already has decided that uninformed people don't want them for the release mode, but it was found that this decision was not uniformly enforced and some debuginfo was still left there. So a new option was added and the default was changed. Happy ending!
Re:Making Rust binaries smaller by default by gred on Wednesday January 24, 2024 @07:12AM (#39116359)
True, but it sounds (nearly?) "useless" based on the description provided by the author:

> For example, one thing that was noted is that if we strip the debug symbols by default, then backtraces of release builds will... not contain any debug info, such as line numbers. That is indeed true, but my claim is that these have not been useful anyway. If you have a binary that only has debug symbols for the standard library, but not for your own code, then even though the backtrace will contain some line numbers from stdlib, it will not really give you any useful context [...]

Re:Making Rust binaries smaller by default by MrJohz on Wednesday January 24, 2024 @08:05AM (#39116793)
That's true, which is why it's now being stripped.
Re:Making Rust binaries smaller by default by oblio on Wednesday January 24, 2024 @07:14AM (#39116371)
> "zero-cost abstractions"

Zero cost in terms of CPU and RAM usage during execution time.

Re:Making Rust binaries smaller by default by 0x000xca0xfe on Wednesday January 24, 2024 @07:50AM (#39116649)
Program size affects disk/file system load time, relocation time, cache utilization, zram speed, and probably many things more.

Nothing is zero-cost if you look closely enough.

Re:Making Rust binaries smaller by default by pdimitar on Wednesday January 24, 2024 @08:47AM (#39117279)
I don't disagree with your premise but bigger binaries lead to significantly more CPU cache churn. (EDIT: or not, see elsewhere in this thread for clarifications)
Re:Making Rust binaries smaller by default by pdimitar on Wednesday January 24, 2024 @08:48AM (#39117295)
I don't judge by this but I have met programmers who do (as mentioned upthread) so yeah, I agree. It's a signal that is useful to many, and that signal should start being optimized sooner rather than later.
Re:Making Rust binaries smaller by default by elteto on Wednesday January 24, 2024 @01:55PM (#39121225)
I don’t see how zero cost abstraction has anything to do with binary size. It’s about runtime efficiency of language features.

Optimizing binary size is a worthy and important goal because some people have uses cases where it matters. If you are not one of those people why fret about it?

Re:Making Rust binaries smaller by default by JohnFen on Wednesday January 24, 2024 @06:13PM (#39123924)
> I don’t see how zero cost abstraction has anything to do with binary size.

But binary size is absolutely a cost. Whether or not it's a cost that matters to you is a different question, but it's still a cost.

Re:Making Rust binaries smaller by default by zer00eyz on Wednesday January 24, 2024 @06:32AM (#39116099)
Out in javascript land no one cares about file size or wire time any more (but should) --

Over in C, plenty of people work on things where file size matters. It is a big deal. System constraints (embedded) wire constraints (far away systems where internet is slow and ephemeral), legacy systems where everything is going to be slow even moving that fat file around and you have to be present to update (ATM, ticket, vending machines)... The list goes on.

So it may NOT matter for the desk top, or mobile or servers, but that's a tiny fraction of the computing out there.

Re:Making Rust binaries smaller by default by lifthrasiir on Wednesday January 24, 2024 @06:39AM (#39116139)
I do have a lower threshold for JS apps, which is about 200 KB. This difference accounts for the fact that websites can be streamed while executables can't. I'm not saying that we shouldn't optimize for the size---rather I'm saying the size optimization is not worthwhile under some threshold.
Re:Making Rust binaries smaller by default by flohofwoe on Wednesday January 24, 2024 @06:46AM (#39116182)
Many MMOs do actually download updated client executables on the fly in the launcher, and that shouldn't make the player wait more than necessary. Even the Steam client has such an automatic update process (and at 200 MBytes this is already in "very annoying" territory IMHO).
Re:Making Rust binaries smaller by default by lifthrasiir on Wednesday January 24, 2024 @06:56AM (#39116248)
I mean, yes! Games are on the opposite end of the spectrum, where uncompressed assets fly around and 100 MB sounds like very small. These are what I refer to "low-hanging fruits"---I expect a moderate effort can halve the total size for most cases. (I did work on asset file formats back when I was a gamedev.) 400 KB executable is not really a big deal compared to that.
Re:Making Rust binaries smaller by default by pdimitar on Wednesday January 24, 2024 @08:51AM (#39117320)
I agree with the threshold argument but I guess it differs for all of us. To me 4MB Hello World program is hilariously bad. I want Rust to compete with C. Not be better or even equal to C when it comes to binary sizes, but I want it to be close.
Re:Making Rust binaries smaller by default by lifthrasiir on Wednesday January 24, 2024 @08:56AM (#39117379)
I agree that 4 MB is bad, but I felt 400 KB is fine given all circumstances.

If Rust really wants to minimize any overhead in spite of the necessity of backtrace supports, there are indeed many ways to minimize the cold section of executable. Even a simple compression will work---especially given that we already have a copy of miniz-oxide there! So try that if you are motivated, and I would more than welcome that effort, but Rust has way, way more important things to do than that.

Re:Making Rust binaries smaller by default by pdimitar on Wednesday January 24, 2024 @08:59AM (#39117412)
I am 100% sure there are much important issues right now and I sympathize with the lack of bandwidth. The one thing I am claiming is that some lower-hanging fruit (if it is indeed that; might be super complex in fact) carries a significant positive public relations / image potential which should IMO be picked / harvested.

(EDIT: I am talking about the `build-std` work here, not the default strip debug info flag.)

Re:Making Rust binaries smaller by default by zer00eyz on Wednesday January 24, 2024 @10:28AM (#39118457)
I dont have a hard limit for JS...

What I have is a wall for functionality.

Loading 700kb react blob (compressed mind you) so I can read a web page is a hard no. You want to give me a rich GUI to do data editing in a browser, bring it on, I'll take the down load.

I know that rust and tiny go swap code here and there, binary sizes getting smaller on rust might make me give it a poke for some of the more "cute" embedded/iot things I like to play with... (another place where small matters!)

Re:Making Rust binaries smaller by default by edflsafoiewq on Wednesday January 24, 2024 @01:33PM (#39120937)
What? JS is the only ecosystem I know of that really cares about file size, to the point that it's expected to see the size of a library in release form (minified, gzipped) on its homepage. That's vanishing rare with any other language, including C.
Re:Making Rust binaries smaller by default by JohnFen on Wednesday January 24, 2024 @06:19PM (#39123981)
> Out in javascript land no one cares about file size or wire time any more

Which is one of the reasons why I tend to leave JS disabled in my browser. Too many web devs have no care or concern for these sorts of things, which often makes JS a real resource drain.

Re:Making Rust binaries smaller by default by bipson on Wednesday January 24, 2024 @06:26AM (#39116060)
Depends 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.

Re:Making Rust binaries smaller by default by lifthrasiir on Wednesday January 24, 2024 @06:41AM (#39116149)
C/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.
Re:Making Rust binaries smaller by default by tialaramex on Wednesday January 24, 2024 @08:25AM (#39117013)
To be technical, Rust deliberately splits out three elements: core (which is necessary for the Rust language and is always provided) alloc (which needs a heap allocator, not applicable if you don't have or don't want one) and then std (which expects an operating system, with features like files and sockets and threads and knowing what the time is)

C++ just labels a few specific features as available in a "freestanding" C++ standard library if you have one (on an embedded platform presumably you do).

This makes it very easy to know what you're getting in Rust's stdlib in #[no_std] because it's all of core, so e.g.

https://doc.rust-lang.org/core/primitive.slice.html#method.s... vs. https://doc.rust-lang.org/std/primitive.slice.html#method.so...

At first glance those are identical, but no, std is re-exporting every feature core had, but it also gains features, for example the stable sort function family only exist in std because they're using a temporary allocation whereas the unstable (ie equivalent items may be re-ordered) sort provided even in core doesn't do that.

Figuring out what you get in your stdlib with C++ often comes down to suck it and see.

Re:Making Rust binaries smaller by default by formerly_proven on Wednesday January 24, 2024 @06:27AM (#39116069)
Adds up with larger applications using a bunch of distinct binaries or many libraries.
Re:Making Rust binaries smaller by default by pdimitar on Wednesday January 24, 2024 @08:44AM (#39117235)
Mostly because CPU caches matter a lot. Executing 7-8 different Rust binaries, in a loop, that are each north of 10MB+ is bound to be less performant than corresponding smaller programs.

Never had the time or dedication to actually verify this but I've been bitten by programs and OS-es that trash the cache too much and I've seen humanly perceivable lags because of it. But maybe in this case I am overreacting.

Re:Making Rust binaries smaller by default by kramerger on Wednesday January 24, 2024 @09:47AM (#39117916)
I think most debug info is in its own section. On a real OS the pages are loaded lazily so no (significant) performance hit.

Still, I hate big Rust binaries as much as the next guy.

Re:Making Rust binaries smaller by default by nindalf on Wednesday January 24, 2024 @08:58AM (#39117408)
Would debug symbols ever be loaded into CPU cache? They'd be in RAM, sure. But why would the CPU ever load them into cache? The way I'm imagining this, and I could be wrong, is that the debug symbols are to one end of the binary. They wouldn't be loaded into cache unless they're going to be used imminently.

Another way of saying this is that the change to strip automatically is a small win for for disk space and RAM consumption, but I don't think it's going to improve performance dramatically? I could be wrong about this though.

Re:Making Rust binaries smaller by default by pdimitar on Wednesday January 24, 2024 @09:02AM (#39117443)
No, it's more likely that you are correct because it has been a long time since I worked that close to the metal and my info is outdated or downright wrong at this point.

Thanks for the nuance. If debug symbols indeed never go into the CPU cache then my remark is completely irrelevant.

Re:Making Rust binaries smaller by default by JohnFen on Wednesday January 24, 2024 @06:11PM (#39123912)
> in practice anything below 1 MB seems small enough that you don't need to optimize further.

I think that it depends on what sort of machine you're aiming for the binary to run on. I develop for a few platforms where a 1MB executable size would be completely unacceptable.

Re:Making Rust binaries smaller by default by pjmlp on Thursday January 25, 2024 @03:50AM (#39127549)
That won't fit into a ESP32, Arduino or embedded hardware with similar restrictions, leaving the space for C and C++, or the Basic, Pascal compiler vendors still enjoying that space like Mikroe.