Skip to content

Commit d852534

Browse files
authored
Compile with panic = "abort" (#1813)
* Compile with `panic = "abort"` This PR sets `panic = "abort"` for both debug and release builds. This cuts down the `rerun` binary size in release builds from 29.9 MB to 22.7 MB - a 25% reduction! ## Details The default panic behavior in Rust is to unwind the stack. This leads to a lot of extra code bloat, and some missed opportunities for optimization. The benefit is that one can let a thread die without crashing the whole application, and one can use `std::panic::catch_unwind` as a kind of try-catch block. We don't make use of these features at all (at least not intentionally), and so are paying a cost for something we don't need. I would also argue that a panic SHOULD lead to a hard crash unless you are building an Erlang-like robust actor system where you use defensive programming to protect against programmer errors (all panics are programmer errors - user errors should use `Result`). * Quiet clippy
1 parent 49d5de8 commit d852534

File tree

3 files changed

+12
-3
lines changed

3 files changed

+12
-3
lines changed

Cargo.toml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,14 +89,16 @@ wgpu-hal = { version = "0.15.4", default-features = false }
8989

9090

9191
[profile.dev]
92-
opt-level = 1 # Make debug builds run faster
92+
opt-level = 1 # Make debug builds run faster
93+
panic = "abort" # This leads to better optimizations and smaller binaries (and is the default in Wasm anyways).
9394

9495
# Optimize all dependencies even in debug builds (does not affect workspace packages):
9596
[profile.dev.package."*"]
9697
opt-level = 2
9798

9899
[profile.release]
99100
# debug = true # good for profilers
101+
panic = "abort" # This leads to better optimizations and smaller binaries (and is the default in Wasm anyways).
100102

101103
[profile.bench]
102104
debug = true

clippy.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ disallowed-methods = [
2727
"std::thread::spawn", # Use `std::thread::Builder` and name the thread
2828

2929
"sha1::Digest::new", # SHA1 is cryptographically broken
30+
31+
"std::panic::catch_unwind", # We compile with `panic = "abort"`
3032
]
3133

3234
# https://rust-lang.github.io/rust-clippy/master/index.html#disallowed_names

crates/rerun/src/crash_handler.rs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,6 @@ fn install_panic_hook(_build_info: BuildInfo) {
3030
format!("{file}:{}", location.line())
3131
});
3232

33-
// `panic_info.message` is unstable, so this is the recommended way of getting
34-
// the panic message out. We need both the `&str` and `String` variants.
3533
let msg = panic_info_message(panic_info);
3634

3735
if let Some(msg) = &msg {
@@ -90,10 +88,17 @@ fn install_panic_hook(_build_info: BuildInfo) {
9088
std::thread::sleep(std::time::Duration::from_secs(1)); // Give analytics time to send the event
9189
}
9290
}
91+
92+
// We compile with `panic = "abort"`, but we don't want to report the same problem twice, so just exit:
93+
#[allow(clippy::exit)]
94+
std::process::exit(102);
9395
}));
9496
}
9597

9698
fn panic_info_message(panic_info: &std::panic::PanicInfo<'_>) -> Option<String> {
99+
// `panic_info.message` is unstable, so this is the recommended way of getting
100+
// the panic message out. We need both the `&str` and `String` variants.
101+
97102
#[allow(clippy::manual_map)]
98103
if let Some(msg) = panic_info.payload().downcast_ref::<&str>() {
99104
Some((*msg).to_owned())

0 commit comments

Comments
 (0)