diff options
| author | 许杰友 Jieyou Xu (Joe) <39484203+jieyouxu@users.noreply.github.com> | 2025-01-20 12:38:32 +0800 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2025-01-20 12:38:32 +0800 |
| commit | e5b85035fefb37b5f314ca0f07a5560de6ed0a0c (patch) | |
| tree | 01a7977480bd92485c0c8a9d8295216c461e41aa | |
| parent | 3f2f695d68334ca4ecc8aec0aa38b5391051c987 (diff) | |
| parent | 3a0554a44578afb2f69babdae39f8880d23560ae (diff) | |
| download | rust-e5b85035fefb37b5f314ca0f07a5560de6ed0a0c.tar.gz rust-e5b85035fefb37b5f314ca0f07a5560de6ed0a0c.zip | |
Rollup merge of #135446 - klensy:panic_immediate_abort_ext, r=Mark-Simulacrum
further improve panic_immediate_abort by removing rtprintpanic! messages
Reduces binary size using `panic_immediate_abort` by removing strings used by `rtprintpanic!`.
for `main.rs`
```rust
fn main() {
println!("Hello, world!");
}
```
with `Cargo.toml`
```toml
[package]
name = "tst"
version = "0.1.0"
edition = "2024"
[dependencies]
[profile.release]
lto = true
codegen-units = 1
panic = "abort"
```
and build with `RUSTFLAGS="-Zlocation-detail=none -Zfmt-debug=none" cargo +stage-1 b -r -Z build-std=std,panic_abort -Z build-std-features=optimize_for_size,panic_immediate_abort` for `x86_64-unknown-linux-gnu`
This reduces size:
| before | after | type |
| - | - | - |
| 25256 | 21880 | unstripped |
| 18072 | 15288 | stripped |
| -rw-r--r-- | library/std/src/rt.rs | 5 |
1 files changed, 5 insertions, 0 deletions
diff --git a/library/std/src/rt.rs b/library/std/src/rt.rs index 384369b0012..3a22a16cb16 100644 --- a/library/std/src/rt.rs +++ b/library/std/src/rt.rs @@ -32,9 +32,14 @@ use crate::{mem, panic, sys}; // - nothing (so this macro is a no-op) macro_rules! rtprintpanic { ($($t:tt)*) => { + #[cfg(not(feature = "panic_immediate_abort"))] if let Some(mut out) = crate::sys::stdio::panic_output() { let _ = crate::io::Write::write_fmt(&mut out, format_args!($($t)*)); } + #[cfg(feature = "panic_immediate_abort")] + { + let _ = format_args!($($t)*); + } } } |
