FAQ - Help and questions

Why are Rust executables large, and how do I make them smaller?

#cargo#performance#deploymentpublished 0 replies0 views
ReplyWatch
Share
Hacker NewsRedditLinkedInE-mail

Short answer

A Rust program carries its own standard library and generic code compiled for your types, and debug builds also carry symbols. A release profile with lto, codegen-units = 1, panic = "abort" and opt-level = "z" shrank a small test program from 175,104 to 144,896 bytes on Windows; larger programs gain more.

Checked with rustc 1.93.0, Windows 11, MSVC toolchain

Frequently asked question, written and answered by the Ruststudio team. Replies are open: corrections are welcome.

#1

Answer#

A Rust executable is statically linked: the parts of the standard library it uses, the code of every dependency, and generic functions compiled separately for each type they are used with are all inside the file. A debug build is much larger still, because it has no optimisation and (on Linux, for example) carries debug symbols in the file.

The release profile in Cargo.toml controls how much of that is removed. These settings go under [profile.release]:

SettingEffectTrade-off
lto = trueOptimises across crate boundaries and removes unused codeSlower release builds
codegen-units = 1Lets the optimiser see the whole crate at onceSlower release builds, no parallel code generation
panic = "abort"A panic ends the process; the unwinding tables are not includedcatch_unwind no longer works; destructors do not run on a panic
opt-level = "z"Optimise for size instead of speedThe program may be slower
strip = trueRemoves symbol and debug information from the fileNo effect when the symbols are already in a separate .pdb file (see the result below)

How to do it#

  1. Measure first. This is the test program: it counts words from its command line arguments (it uses HashMap, sorting and formatting, so it is not an empty program).
src/main.rsrust
   use std::collections::HashMap;

   fn main() {
       let text = std::env::args().skip(1).collect::<Vec<_>>().join(" ");
       let mut counts: HashMap<String, usize> = HashMap::new();
       for w in text.split_whitespace() {
           *counts.entry(w.to_lowercase()).or_default() += 1;
       }
       let mut v: Vec<_> = counts.into_iter().collect();
       v.sort();
       for (w, n) in v {
           println!("{w}: {n}");
       }
   }
  1. Add the profile, one setting at a time, and run cargo build --release after each:
Cargo.tomltoml
   [profile.release]
   strip = true
   lto = true
   codegen-units = 1
   panic = "abort"
   opt-level = "z"

Measured result#

Machine and versions: Windows 11 (10.0.26300), rustc 1.93.0, the MSVC toolchain, one run per row, size of target\release\size.exe.

Settings in [profile.release]Size in bytes
none (the default release profile)175,104
strip = true175,104
plus lto = true, codegen-units = 1163,328
plus panic = "abort"154,112
plus opt-level = "z"144,896

That is about 17 percent smaller for a program this small. Two honest observations: strip = true changed nothing on this Windows build, because the MSVC toolchain keeps debug information in a separate .pdb file rather than in the executable (on Linux it can matter much more); and for a tiny program the standard library dominates the size, so the savings are largest on bigger programs with many dependencies. Measure your own program rather than expecting these numbers.

Common mistakes#

  • Using opt-level = "z" on code where speed matters, and then not measuring the speed.
  • Using panic = "abort" in a library or a server that relies on catching panics.
  • Compressing the file with a packer to hide the size. It can trigger antivirus warnings and does not reduce what is loaded at run time.

Why is the debug build so much bigger than the release build?#

The debug profile does no optimisation and keeps checks such as integer overflow detection, so the compiled code is larger and slower. Use cargo build --release for anything you measure or ship.

Does cargo build --release produce the same file everywhere?#

No. The result depends on the target (operating system and processor), the Rust version and the profile. Build on or for each target you ship.

Sources#

    QuoteReply
    Share
    Hacker NewsRedditLinkedInE-mail

    No replies yet