containers / youki

A container runtime written in Rust
https://containers.github.io/youki/
Apache License 2.0
6.27k stars 341 forks source link

Optimize binaries size #1992

Open utam0k opened 1 year ago

utam0k commented 1 year ago

The goal is to drop the current 8 MB to 5 MB at the time of the latest release.

$ cargo bloat --crates --release 
[...]
 File  .text     Size Crate
 9.9%  22.7% 823.1KiB libcontainer
 7.5%  17.3% 627.3KiB std
 3.3%   7.6% 274.5KiB libcgroups
 3.3%   7.5% 271.5KiB clap_builder
 2.2%   5.0% 182.2KiB serde
 2.1%   4.7% 171.7KiB regex_syntax
 2.0%   4.6% 167.3KiB youki
 2.0%   4.6% 165.7KiB regex
 1.4%   3.3% 119.2KiB aho_corasick
 1.3%   3.0% 110.4KiB liboci_cli
 1.3%   3.0% 110.1KiB serde_json
 1.0%   2.4%  87.0KiB tracing_subscriber
 1.0%   2.3%  82.5KiB clap_complete
 0.8%   1.8%  65.0KiB oci_spec
 0.7%   1.6%  59.3KiB procfs
 0.5%   1.2%  44.3KiB hashbrown
 0.5%   1.2%  43.6KiB chrono
 0.4%   0.8%  29.3KiB rust_criu
 0.2%   0.5%  16.4KiB nix
 0.2%   0.4%  14.8KiB memchr
 1.5%   3.5% 126.0KiB And 32 more crates. Use -n N to show more.
43.7% 100.0%   3.5MiB .text section size, the file size is 8.1MiB

Note: numbers above are a result of guesswork. They are not 100% correct and never will be.

Reference

YJDoc2 commented 1 year ago

Just a Note :

Not sure if stripping away the symbols an stuff is a good idea or not though.

tsturzl commented 8 months ago

You can strip symbols out into a separate file. This is pretty common in C/C++ where you'll often find "dbg" packages for most software, which basically just installs the debug symbols for the respective package. This means you have the option whether or not you want to have the symbols or not. The symbols are generally only useful for sorting through a core dump, which would be a good reason to keep the symbols around, but not ship them by default in the release binary.

I'm curious what the space-time trade off is between optimization 2 and 3. I think it's worth considering adding this to the build steps. I'm familiar with how to do this.