-
Notifications
You must be signed in to change notification settings - Fork 6
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
Strip packaged binaries to reduce buildpack size #319
Comments
Experimenting with this using Rust 1.59 beta, by running
And for non-release builds (using
During the build, this warning appears:
The build completes successfully however. What's interesting about this, is that the path referenced in the error message is under Trying again using a strip mode of
The Cargo docs mention a
We can take advantage of that by using: ...which prevents the warning, whilst still giving us the full output size reductions. It seems like perhaps this might be a Rust bug where it's not fully taking into account the strip options required for the host vs the target, but either way, there is an easy workaround for now :-) |
Filed: |
Now available in stable: |
The warnings seen when stripping using local path dependencies (ie when hacking on libcnb in this repo) will now be resolved in Rust 1.63, thanks to: That needn't block the work on enabling stripping in libcnb, since (a) they are only warnings, (b) they aren't shown when people use libcnb from crates.io (only local path based builds). |
As of Rust 1.59, Cargo now supports a `strip` option, which when enabled, causes the binaries output to stripped of all symbol information: https://blog.rust-lang.org/2022/02/24/Rust-1.59.0.html#creating-stripped-binaries https://doc.rust-lang.org/beta/cargo/reference/profiles.html#strip Doing this significantly reduces the size of the binaries, which: - Results in smaller overall production builder image sizes, which reduces push/pull times. - Helps speed up workflows where the otherwise large dev/debug builds were being used (such as integration tests or local buildpack development), since Docker/`pack build`'s transfer of files into the ephemeral builder image is very slow. - Helps reduce any end-user perception of `libcnb.rs` powered CNBs being bloated compared to say bash-based CNBs (and also keeps us competitive with any Go-based CNB frameworks). Before/after comparison: - `examples/basics`: - dev build: 22.01 MiB -> 3.26 MiB - release build: 5.04 MiB -> 1.29 MiB - `examples/exec.d`: - dev build: 37.40 MiB -> 4.10 MiB - release build: 9.02 MiB -> 1.78 MiB - `cargo test -- --ignored`: 5.5s -> 2.6s - `heroku/procfile`: - dev build: 43.40 MiB -> 5.16 MiB - release build: 6.27 MiB -> 2.17 MiB - `cargo test -- --ignored`: 9.1s -> 5.2s - `heroku/jvm`: - dev build: 116.55 MiB -> 15.52 MiB - release build: 24.40 MiB -> 7.38 MiB - `cargo test --test integration_tests`: 97s -> 82s Stripping the binaries does not result in any loss of useful backtraces, since with the cross-compilation and/or usage of MUSL, these did not work even in debug buildpack builds anyway. Both before and after this change, any panics still show the originating file/line number, which is all that's generally required when debugging. Lastly, even if backtraces had previously worked, they are a pain to enable given that the env has to be set outside of the buildpack invocation, so can't be used when `clear_env = true` is set etc. Since Cargo only supports the `strip` option as of Rust 1.59, the minimum Rust version has been bumped to match. Fixes #319. GUS-W-11324847.
As of Rust 1.59, Cargo now supports a `strip` option, which when enabled, causes the binaries output to stripped of all symbol information: https://blog.rust-lang.org/2022/02/24/Rust-1.59.0.html#creating-stripped-binaries https://doc.rust-lang.org/beta/cargo/reference/profiles.html#strip Doing this significantly reduces the size of the binaries, which: - Results in smaller overall production builder image sizes, which reduces push/pull times. - Helps speed up workflows where the otherwise large dev/debug builds were being used (such as integration tests or local buildpack development), since Docker/`pack build`'s transfer of files into the ephemeral builder image is very slow. - Helps reduce any end-user perception of `libcnb.rs` powered CNBs being bloated compared to say bash-based CNBs (and also keeps us competitive with any Go-based CNB frameworks). Before/after comparison: - `examples/basics`: - dev build: 22.01 MiB -> 3.26 MiB - release build: 5.04 MiB -> 1.29 MiB - `examples/exec.d`: - dev build: 37.40 MiB -> 4.10 MiB - release build: 9.02 MiB -> 1.78 MiB - `cargo test -- --ignored`: 5.5s -> 2.6s - `heroku/procfile`: - dev build: 43.40 MiB -> 5.16 MiB - release build: 6.27 MiB -> 2.17 MiB - `cargo test -- --ignored`: 9.1s -> 5.2s - `heroku/jvm`: - dev build: 116.55 MiB -> 15.52 MiB - release build: 24.40 MiB -> 7.38 MiB - `cargo test --test integration_tests`: 97s -> 82s Stripping the binaries does not result in any loss of useful backtraces, since with the cross-compilation and/or usage of MUSL, these did not work even in debug buildpack builds anyway. Both before and after this change, any panics still show the originating file/line number, which is all that's generally required when debugging. Lastly, even if backtraces had previously worked, they are a pain to enable given that the env has to be set outside of the buildpack invocation, so can't be used when `clear_env = true` is set etc. Since Cargo only supports the `strip` option as of Rust 1.59, the minimum Rust version has been bumped to match. Fixes #319. GUS-W-11324847.
Stripping the packaged binaries will reduce the buildpack size, which will help with builder image pull times, and also local development/integration test runtimes (the copy of local buildpacks into the ephemeral builder image when using
--buildpack
is surprisingly slow).Rust 1.58 stabilised the
-C strip
option:https://doc.rust-lang.org/stable/rustc/codegen-options/index.html#strip
However it's not until Rust 1.59 (currently in beta, due for release 2022-02-24) that corresponding support will be added to Cargo:
https://doc.rust-lang.org/beta/cargo/reference/profiles.html#strip
The feature can be enabled by adding this to
Cargo.toml
for the crate in question:However this would mean every project using libcnb would need to remember to do this, so I think it would make more sense if we configure it automatically in
libcnb-cargo
.I think we may want to enable it for non-release libcnb-cargo builds too, since it will speed up development use-cases as mentioned above. (And debug symbols don't exactly help much when the buildpack is run inside the container as part of the lifecycle, since no easy way to run via a debugger anyway.)
Profile options like this can't be passed via
cargo build
arguments:https://doc.rust-lang.org/beta/cargo/commands/cargo-build.html
...but we should be able to enable it via env var:
https://doc.rust-lang.org/beta/cargo/reference/config.html#environment-variables
For example:
GUS-W-11324847.
The text was updated successfully, but these errors were encountered: