Closed marcospb19 closed 2 years ago
@marcospb19 Thanks a lot for creating the issue!
As we use the --release
flag by default, does stripping the binary really give these size benefit? I'm unable to reproduce it on macOS.
We wouldn't want to regress on performance noticeably, so if it's slow enough we would need an opt-in flag as you say.
But perhaps we can use some default settings in the generated Cargo.toml
to reduce binary size by default, without hurting build times? I.e. debug symbols shouldn't probably be added by default, unless --debug
is specified.
Here's how I reproduced it:
Can you confirm it's being stripped in your system?
This may be easier now with Rust 1.58. https://github.com/rust-lang/cargo/pull/10088/ I didn't see difference on the compilation time for a hello world program, while seeing clear improvement on the binary size.
$ rust-script --clear-cache hello.rs
Hello world!
$ du $binary
3.5M /home/misawa/.cache/rust-script/binaries/release/hello_a747a1cd4da230aa9a6bb068
$ RUSTFLAGS="-C strip=symbols" rust-script --clear-cache hello.rs
Hello world!
$ du $binary
300K /home/misawa/.cache/rust-script/binaries/release/hello_a747a1cd4da230aa9a6bb068
$ hyperfine 'rust-script --clear-cache hello.rs' 'RUSTFLAGS="-C strip=symbols" rust-script --clear-cache hello.rs' --warmup 10
Benchmark 1: rust-script --clear-cache hello.rs
Time (mean ± σ): 593.7 ms ± 5.5 ms [User: 502.0 ms, System: 101.8 ms]
Range (min … max): 582.2 ms … 602.5 ms 10 runs
Benchmark 2: RUSTFLAGS="-C strip=symbols" rust-script --clear-cache hello.rs
Time (mean ± σ): 577.6 ms ± 10.2 ms [User: 485.7 ms, System: 100.3 ms]
Range (min … max): 566.7 ms … 597.7 ms 10 runs
Summary
'RUSTFLAGS="-C strip=symbols" rust-script --clear-cache hello.rs' ran
1.03 ± 0.02 times faster than 'rust-script --clear-cache hello.rs'
hyperfine 'rust-script --clear-cache hello.rs' --warmup 10 19.98s user 4.23s system 101% cpu 23.806 total
Actually maybe it'll be even easier when https://github.com/rust-lang/cargo/pull/10217 become usable, which allows us to specify rustflags
per build profile. In that future, we can just add the following to the Cargo.toml
template.
[profile.release]
rustflags = ["-C", "strip=symbols"]
Nice points.
Sorry, I know I previously said I was willing to implement it, but I have not found the time to dedicate to open source.
I'll pass this to anyone who wants to solve it.
The
--strip
flag would strip the binary right after compiling it, which increases the time of compiling but reduces the binary size.A
println!("Hello World!")
program, for example:(I'm willing to implement it.)