rust-lang / backtrace-rs

Backtraces in Rust
https://docs.rs/backtrace
Other
524 stars 240 forks source link

Track binary size impact on Rust executables #541

Closed workingjubilee closed 1 year ago

workingjubilee commented 1 year ago

Rust's backtrace implementation is a significant factor in how large every Rust binary is. We should be testing to see how much commits here regress binary size for stdlib-using binaries, before we land them. We may be able to bait invite some people working on rustc-perf to help us investigate this issue. It is very likely a size increase due to recent commits created a notable regression, although one that was probably worth the price if so.

workingjubilee commented 1 year ago

[9:30 PM] detly: one really, truly fun factor in all of this is how much of an impact rust versions actually make. I might be missing something, but doing nothing other than switching to nightly changes the post-strip binary size from 358,324 B to 407,484 B [9:31 PM] detly: (this is without even using the build-std feature, just the same build --release --target etc. command) [9:53 PM] detly: so... building with nightly-2023-07-03 and build-std, I get: 342,000 B. Same thing with nightly-2023-07-01, I get: 313,328 B. That's kind of wild. If I'd done that experiment today, I'd have figured it didn't help much. Fortunately I did it three days ago as well, and remembered that it had a noticeable impact.

So approximately 30KiB.

Repo of interest with further information: https://gitlab.com/detly/prod-self-test

detly commented 1 year ago

Thanks for reporting this! If it helps, here's a breakdown from cargo-bloat. Note RUSTC_TARGET_ARCH="mipsel-unknown-linux-musl", which tends to have slightly bigger code size overall from x86_64.

----
Stripped size: 313328
----

buildbot@3156e9e8fc34:/crate$ cargo +nightly-2023-07-01 bloat -Z build-std=std,panic_abort --release --target $RUSTC_TARGET_ARCH
    Finished release [optimized] target(s) in 0.03s
    Analyzing target/mipsel-unknown-linux-musl/release/prod-self-test

 File  .text     Size          Crate Name
 5.0%   7.2%  19.0KiB            std addr2line::ResDwarf<R>::parse
 4.4%   6.3%  16.6KiB            std std::backtrace_rs::symbolize::gimli::resolve
 2.3%   3.3%   8.8KiB            std std::backtrace_rs::symbolize::gimli::elf::decompress_zlib
 2.2%   3.2%   8.4KiB            std gimli::read::unit::parse_attribute
 2.1%   3.0%   7.8KiB            std addr2line::Lines::parse
 1.2%   1.7%   4.4KiB            std gimli::read::abbrev::DebugAbbrev<R>::abbreviations
 1.1%   1.6%   4.3KiB            std std::backtrace_rs::symbolize::gimli::libs_dl_iterate_phd...
 1.1%   1.6%   4.2KiB            std gimli::read::line::parse_attribute
 1.1%   1.5%   4.0KiB rustc_demangle rustc_demangle::v0::Printer::print_const
 1.0%   1.5%   3.9KiB            std core::slice::sort::recurse
 0.9%   1.4%   3.6KiB            std addr2line::function::Function<R>::parse_children
 0.9%   1.3%   3.5KiB            std std::rt::lang_start
 0.9%   1.3%   3.4KiB            std gimli::read::unit::Attribute<R>::value
 0.8%   1.2%   3.2KiB rustc_demangle <&T as core::fmt::Display>::fmt
 0.8%   1.2%   3.1KiB rustc_demangle rustc_demangle::v0::Printer::print_path
 0.8%   1.1%   3.0KiB rustc_demangle rustc_demangle::v0::Printer::print_type
 0.8%   1.1%   2.9KiB            std gimli::read::rnglists::RngListIter<R>::next
 0.6%   0.9%   2.5KiB      gpio_cdev <&T as core::fmt::Display>::fmt
 0.6%   0.9%   2.4KiB            std std::backtrace_rs::symbolize::SymbolName::new
 0.5%   0.8%   2.1KiB            std std::backtrace_rs::symbolize::gimli::Context::new
39.8%  57.6% 151.4KiB                And 728 smaller methods. Use -n N to show more.
69.1% 100.0% 262.8KiB                .text section size, the file size is 380.6KiB

----
Stripped size: 342000
----

buildbot@3156e9e8fc34:/crate$ cargo +nightly bloat -Z build-std=std,panic_abort --release --target $RUSTC_TARGET_ARCH
    Finished release [optimized] target(s) in 0.03s
    Analyzing target/mipsel-unknown-linux-musl/release/prod-self-test

 File  .text     Size          Crate Name
 4.2%   6.1%  17.3KiB            std std::sys_common::backtrace::_print_fmt::{{closure}}
 3.1%   4.4%  12.7KiB            std std::backtrace_rs::symbolize::gimli::Context::new
 2.6%   3.7%  10.6KiB      addr2line addr2line::ResUnit<R>::find_function_or_location::{{clos...
 2.1%   3.1%   8.8KiB            std std::backtrace_rs::symbolize::gimli::elf::decompress_zlib
 2.0%   2.9%   8.4KiB            std gimli::read::unit::parse_attribute
 2.0%   2.9%   8.3KiB          gimli gimli::read::dwarf::Dwarf<R>::unit
 1.9%   2.7%   7.8KiB            std addr2line::Lines::parse
 1.1%   1.5%   4.4KiB            std gimli::read::abbrev::DebugAbbrev<R>::abbreviations
 1.0%   1.5%   4.2KiB            std gimli::read::line::parse_attribute
 1.0%   1.4%   4.1KiB            std std::backtrace_rs::symbolize::gimli::libs_dl_iterate_phd...
 1.0%   1.4%   4.0KiB rustc_demangle rustc_demangle::v0::Printer::print_const
 0.9%   1.4%   3.9KiB            std core::slice::sort::recurse
 0.9%   1.3%   3.7KiB            std addr2line::function::Function<R>::parse_children
 0.9%   1.2%   3.5KiB            std std::rt::lang_start
 0.8%   1.2%   3.4KiB            std gimli::read::unit::Attribute<R>::value
 0.8%   1.1%   3.2KiB rustc_demangle <&T as core::fmt::Display>::fmt
 0.8%   1.1%   3.1KiB rustc_demangle rustc_demangle::v0::Printer::print_path
 0.7%   1.1%   3.0KiB rustc_demangle rustc_demangle::v0::Printer::print_type
 0.7%   1.0%   2.9KiB            std gimli::read::rnglists::RngListIter<R>::next
 0.6%   0.9%   2.5KiB      gpio_cdev <&T as core::fmt::Display>::fmt
40.2%  57.9% 165.6KiB                And 751 smaller methods. Use -n N to show more.
69.4% 100.0% 285.9KiB                .text section size, the file size is 411.7KiB
buildbot@3156e9e8fc34:/crate$ 
workingjubilee commented 1 year ago

Note that this measurement, in order to be effective, requires:

  1. modifying libstd's source repo to use the PR's fresh commit for the submodule
  2. building the modified libstd
  3. building a new binary with the modified libstd
Kobzol commented 1 year ago

It's in the works (https://github.com/Kobzol/backtrace-rs/pull/1), but I need to resolve https://github.com/rust-lang/rust/issues/101691 first to avoid rebuilding the whole compiler on CI.

the8472 commented 1 year ago

Would it be possible to use -Zbuild-std and patch backtrace into the sources used by that?

jyn514 commented 1 year ago

to my knowledge, build-std does not allow patching the standard library sources. https://github.com/rust-lang/wg-cargo-std-aware/issues/7