torrust-tracker
torrust-tracker copied to clipboard
Reduce release binary size
Current release profile come with debug info and lto=fat, which is quite unnecessary for release build.
this cause cargo build --release
generated 60M+ binary file, and is not ideal for docker image or small footprint devices.
[profile.release]
debug = 1
opt-level = 3
lto = "fat"
result:
root@debian-dev:~/torrust-tracker# ls -lah target/release/torrust-tracker
-rwxr-xr-x 2 root root 64M May 1 23:55 target/release/torrust-tracker
root@debian-dev:~/torrust-tracker# file target/release/torrust-tracker
target/release/torrust-tracker: ELF 64-bit LSB pie executable, x86-64, version 1 (SYSV), dynamically linked, interpreter /lib64/ld-linux-x86-64.so.2, BuildID[sha1]=somebuild-sha1-hash, for GNU/Linux 3.2.0, with debug_info, not stripped
when change the release profile to
[profile.release]
strip = true
opt-level = 3
lto = true
codegen-units = 1
panic = "abort"
result:
root@debian-dev:~/torrust-tracker# ls -lah target/release/torrust-tracker
-rwxr-xr-x 2 root root 11M May 1 23:58 target/release/torrust-tracker
root@debian-dev:~/torrust-tracker# file target/release/torrust-tracker
target/release/torrust-tracker: ELF 64-bit LSB pie executable, x86-64, version 1 (SYSV), dynamically linked, interpreter /lib64/ld-linux-x86-64.so.2, BuildID[sha1]=somebuild-sha1-hash, for GNU/Linux 3.2.0, stripped
The details are in link
Thank you, @nyacat! Regarding the strip=true
option, I removed it some months ago because of this https://github.com/torrust/torrust-tracker/commit/3098ed2c59c420167797b3a6f697c697d440c0f2. I think that's the flag that makes the binary bigger. If that's the case, it's a trade-off between image size and compilation time.
Maybe we could dynamically add it only when we are building docker images for releases, so we have faster builds for each commit and smaller docker images for releases.
Regarding the other options. I don't know how they affect the final binary size. I'll check them, if they do not have any other undesirable side effects, we can change them.
I can confirm that strip = true
is the option that reduces the size the most:
With strip = true
:
$ ls -lah target/release/torrust-tracker
-rwxrwxr-x 2 josecelano josecelano 11M May 2 10:07 target/release/torrust-tracker
With strip = false
:
$ ls -lah target/release/torrust-tracker
-rwxrwxr-x 2 josecelano josecelano 65M May 2 10:06 target/release/torrust-tracker
On the other hand, in the Dockerfile:
FROM chef AS builder
...
# Build dependencies
COPY --from=planner /app/recipe.json recipe.json
RUN cargo chef cook --release --target x86_64-unknown-linux-musl --recipe-path recipe.json
# Build the application
COPY . .
RUN cargo build --release --target x86_64-unknown-linux-musl --bin torrust-tracker
# Strip the binary
# More info: https://github.com/LukeMathWalker/cargo-chef/issues/149
RUN strip /app/target/x86_64-unknown-linux-musl/release/torrust-tracker
You can see this line:
RUN strip /app/target/x86_64-unknown-linux-musl/release/torrust-tracker
With that line, you can "strip" the binary later so that the docker image will contain the smaller binary.
See: https://github.com/LukeMathWalker/cargo-chef/issues/149
But I see your point. If you are not using docker, you can either change the strip
option to true before building or try to overwrite it:
cargo --config strip=true build --release
For some reason, this second option is not working for me.
Maybe we could do the opposite; we could add the strip = true
option and remove it when we are building the docker image.
Thank you very much for testing the way I describe, and I didn't test with strip = true
and strip later after build, this is my fault not strict enough, and I'm very glad you tested this for this project and let things getting better.
anyway I hope to be able to put in the release branch soon