Author: Jase Batchelor
Build configuration and optimisations
The notes below are based on a Youtube video on the Let’s Get Rusty channel.
These tips comprise of simple build flags and amendments that should be made to Config.toml
in order to optimise binaries.
1. Don’t forget the --release
flag
Firstly, don’t forget to add the --release
flag (even though warned, it will be regularly forgotten 😿)
cargo build --release
2. Config.toml
settings
Secondly, add the following to Config.toml
# IMPORTANT: 'strip = true ' will break WASM builds
[profile.release]
strip = true # Automatically strip symbols from the binary
opt-level = 3 # Read the docs on this. Eg 'opt-level = "s"' optimises for size _over_ runtime performance
lto = true # Enable link time optimisation
codegen-units = 1 # Maximise size reduction optimisations
3. Analyse dependency size using cargo-bloat
Install cargo-bloat
# IMPORTANT: 'strip = true' must NOT be used
cargo install cargo-bloat --no-default-features
‘strip = true ’ will break WASM builds.
https://github.com/rust-lang/rust/issues/93294
Ensure that strip = true
is NOT set in Config.toml
and run the following. Once completed then check listed dependencies to see what can be optimised.
cargo bloat --release --crates
Additional notes
The target-cpu=native
flag was mentioned in the video comments, and also here https://stackoverflow.com/a/72752601 and here https://users.rust-lang.org/t/how-to-best-ensure-target-cpu-native/53167
Note, this refers to ~/.cargo/config.toml
[build]
rustflags = ["-C", "target-cpu=native"]
[target.wasm32-unknown-unknown]
rustflags = ["-C", "target-feature=+simd128"]
[target.wasm32-wasi]
rustflags = ["-C", "target-feature=+simd128"]
The following was also mentioned in the comments, but yet to be tested:
# Requires nightly: " -Z build-std=std --target your_target" flags
cargo build -Z build-std=std --target nightly-aarch64-apple-darwin --release