Skip to content

Add --strip flag #36

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
marcospb19 opened this issue Oct 5, 2021 · 5 comments · Fixed by #48
Closed

Add --strip flag #36

marcospb19 opened this issue Oct 5, 2021 · 5 comments · Fixed by #48
Labels
enhancement New feature or request

Comments

@marcospb19
Copy link

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:

  • 3.3M after compiling.
  • 268KB after stripping.

(I'm willing to implement it.)

@fornwall fornwall added the enhancement New feature or request label Nov 10, 2021
@fornwall
Copy link
Owner

@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.

@marcospb19
Copy link
Author

Here's how I reproduced it:

image

Can you confirm it's being stripped in your system?

@MiSawa
Copy link
Contributor

MiSawa commented Mar 8, 2022

This may be easier now with Rust 1.58. rust-lang/cargo#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

@MiSawa
Copy link
Contributor

MiSawa commented Mar 8, 2022

Actually maybe it'll be even easier when rust-lang/cargo#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"]

@marcospb19
Copy link
Author

marcospb19 commented Mar 8, 2022

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants