7 ms·
Minimizing Rust Binary Size
- mkesper 6y agoNot all of these should be used except when striving for absolute minimal sizes.
- dijit 6y agoIs there an additional reason? I see some of them change behaviour or make things complex, but I don't see a performance impact or anything that indicates software will be more crashy.
- Sharlin 6y agoWell, not being able to use the standard library certainly makes it more difficult to write safe and performant programs.
- est31 6y agoMost of the options listed have a disadvantage which is why they aren't enabled by default. The LTO and codegen unit options increase compile time, removing std is worse for the development experience, using xargo (or cargo's build-std feature) requires a C compiler to be present and increases compile time (as std is being compiled).
- RockIslandLine 6y agoOptimizing for size versus performance often eliminates optimizations like unrolling loops. At the assembly level, it's sometimes true that a single instruction or smaller sequence of instructions takes more cpu cycles. Analyze the different instruction sequences here: https://www.nxp.com/docs/en/supporting-information/MC680X0OPTAPP.txt https://www.nxp.com/docs/en/supporting-information/MC680X0OP...
- tiborsaas 6y agoAnother post for size constraint coding in Rust: https://www.codeslow.com/2020/01/writing-4k-intro-in-rust.html https://www.codeslow.com/2020/01/writing-4k-intro-in-rust.ht...
- Hamuko 6y ago>Cargo defaults its optimization level to 3 for release builds, which optimizes the binary for speed. To instruct Cargo to optimize for minimal binary size, use the z optimization level in Cargo.toml In what scenarios is optimizing for binary size preferred over optimizing for speed?
- Fronzie 6y agoAny service or deamon that hardly takes any cpu time regardless of optimization.
- AprilArcus 6y agoif it makes the difference between fitting the whole binary in L1 cache or not, then the wins on memory latency could beat the losses on branch misprediction
- abjKT26nO8 6y agoEmbedded systems.
- speedgoose 6y agoIn WebAssembly for example.
- mrich 6y ago>In what scenarios is optimizing for binary size preferred over optimizing for speed? 64kb intros: https://www.reddit.com/r/rust/comments/597hhv/logicoma_elysian_1st_place_trsac_2016_pc_64kb/ https://www.reddit.com/r/rust/comments/597hhv/logicoma_elysi... https://www.pouet.net/prod.php?which=69658 https://www.pouet.net/prod.php?which=69658
- Sharlin 6y agoHowever, 8 kB (the binary size mentioned in the article) is still drastically too much overhead in a 64k intro, and 4k intros are obviously out of the question.
- trevyn 6y ago
- ahartmetz 6y agoI am surprised that dynamically linking to Rust libstd and other common libraries is not mentioned. Rust produces (as far as Rust code is concerned, not sure about libc if used) fully static binaries by default, right?
- est31 6y agoRust's dynamic linking story isn't really good because the ABI is highly unstable and Rust makes heavy use of non-erased generics.
- scns 6y agoReified Generics?
- est31 6y agoYeah that's the formal term. Thank you. Languages like Java have erased generics where you can't even put something of type T onto the stack because the generated code has to stay generic on the size of T. Super annoying to work with. On the other hand, reified generics increases the binary size. The advantage is that this is more digestible for the optimizer for inlining and specific optimization, which gives you more predictable performance than the alternative of devirtualization, but of course size can also have negative consequences, like for example slower compile time. One good example is this recent PR that improved compile time by making parts of the Vec implementation in the standard library non-generic: https://github.com/rust-lang/rust/pull/72013 https://github.com/rust-lang/rust/pull/72013
- 198608_ 6y agoPRO HACKERS HELPING PEOPLE +1302-648-5479 (text) Is your partner keeping secrets of lately and you want to know why? you feel your partner is cheating on you? Do you or someone you know have a police or court case and want the case CLEARED and forgotten by us hacking into FBI or government server and wiping off HISTORY of its existence? Did someone steal your money and you want the person found and your money recovered? Do you feel somebody is spying on you or bugging you and you want the person out of your way or exposed? Did you lost or forget password to your Facebook,Instagram,twitter,Gmail,Yahoomail,Hotmail etc and want them recovered? Do you wish to spy on somebody's computer or phone? Did you loose contact with someone(family member or old friend) and wish to know where they are and how to locate them for you all to reconnect? Did you lose a pet(dog,cat etc)and want them found? Are you a student and want your school grades UPGRADED? Do you have depts and want your credit scores cleared? You're welcome to our world. We're professional hackers and can invade devices(phones, emails,whasapp,text messages,Facebook,Instagram etc),hack out information you need and forward to you. Then you will stay happy. +13026485479 (texts only) globalhacker1986@gmail.com
- gutino 6y agoSadly there is not before after result example.
- brianm 6y agoI was curious about this, so applied the easy ones for an existing tool (https://github.com/brianm/wsf https://github.com/brianm/wsf). This was not an exhaustive test of optimization combinations, just a single stack, but the results are interesting! To read the table: default is no changes in the release profile, ie: [profile.release] # opt-level = 'z' # lto = true # panic = 'abort' # codegen-units = 1 After that each additional line gets uncommented and rebuilt, then sizes recorded before and after cargo-strip, so the final line is all four optimizations applied. Results: as generated after cargo-strip default 8675776 5069328 opt-level = 'z' 9023200 4676112 lto = true 5943312 3586584 panic = 'abort' 5062456 3135928 codegen-units = 1 4747000 3013048 Tests run on ubuntu 20.04 (Linux d2836c103a22 5.4.0-37-generic #41-Ubuntu SMP Wed Jun 3 18:57:02 UTC 2020 x86_64 GNU/Linux) with rustc 1.43.1 (8d69840ab 2020-05-04) cargo 1.43.0 (2cbe9048e 2020-05-03) cargo-strip - reduces the size of binaries using the `strip` command 0.2.2 Fascinatingly, opt-level='z' produced a LARGER binary than the default, before stripping. That was unexpected. -Brian
- naetius 6y agoI haven't been following Rust's development much lately, but I'm interested in understanding what's the state of ABI stability...
- martinhath 6y agoLike C++, they basically seem to take the stance of "no".
- steveklabnik 6y agoThere's some subtleties there, on both the C++ and Rust side. I won't speak to the C++ stuff, but on the Rust side, it's more "not yet, and we don't know when, and maybe never, we'll see" than it is "no."
- bluejekyll 6y agoIt would be accurate to say that the only stable ABI Rust currently supports is the C FFI ABI, right?
- steveklabnik 6y agoI would say that "the Rust ABI is not stable, but Rust also supports other ABIs." https://doc.rust-lang.org/stable/reference/items/external-blocks.html#abi https://doc.rust-lang.org/stable/reference/items/external-bl...
- saagarjha 6y agoDo you think that there will ever be reified generics on the ABI boundary?
- steveklabnik 6y agoNo idea, ABIs aren't my area of expertise.
- 6y ago
- wyldfire 6y agoWhat about `RUSTC_FLAGS=-Z opt=-Os` (this won't work, I'm sure -- it is a total mishmash of things I've seen before but -Os is the relevant part). building your executable, your dependencies and/or libstd w/-Os could really pay off -- especially if you already have LTO enabled.