I am learning Rust actively since a last 6 months, In order to learn I am building small personal projects to learn aspects of language. As I mentioned, I am still a newbie in Rust, so when Microsoft released edit
(vim like editor) for windows which is fully written in Rust, I decided to take a look at the code base (I love open-source) in order to learn from it.
First thing I looked at was Cargo.toml
and I was surprised by optimizations done for release build.
I decided to try those on my personal project. I created this small utility to parse parquet files using Rust (https://github.com/ninadmhatre/rupy/tree/main/pq). It works but problem is the binary size. Its massive 56 MB! I wanted to see how much reduction can be done on the binary size with this optimization/flags.
Default
Compilation Time: 2m 10s
Binary Size (MB): 56
Reduce codegen-units
For faster compilation code units are split, default is 256 (incremental) and 16. Higher the number quick the build but may produce slower code. Lets set it 1.
Docs: Here
Flags:
-
codegen-units=1
Compilation Time: 3m 10s
Binary Size (MB): 40
Change lto
Defers LTO optimizations to linker. Default is no
or off
.
Docs: Here
Flags:
codegen-units=1
-
lto= true
Compilation Time: 5m 16s
Binary Size (MB): 38
Change opt-level
Controls optimization levels. Default is no optiomization.
Docs: Here
Flags:
codegen-units=1
lto= true
-
opt-level = "s"
// optimize for size
Compilation Time: 4m 35s
Binary Size (MB): 28
Rest of the flags
Below flags are about debug and controlling behaviour, so instead of checking 1 by 1, I applied them together.
panic: abort
or unwind
on panic?
strip: Controlles stripping of debuginfo and other data from binary.
split-debuginfo: emitting debuginfo into separate file
Flags:
codegen-units=1
lto= true
-
opt-level = "s"
// optimize for size -
panic=abort
// do not unwind on panic -
strip="symbols"
// if you want crash reporting, then not recommended -
split-debuginfo="packed"
Compilation Time: 3m 56s
Binary Size (MB): 20
My repository is very small but depends on Polars. In conclusion, with codegen options/release flags, my compilation time increased to 3m 56s from 2m 10s -> +81% but my binary size reduced from 56M -> 20M -> -65%. You can play around with these options to figure out what works for you.
You can read more about all codegen options
Top comments (0)