Skip to content

Commit

Permalink
Compile with panic = "abort" (#1813)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
emilk authored Apr 11, 2023
1 parent 49d5de8 commit d852534
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 3 deletions.
4 changes: 3 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -89,14 +89,16 @@ wgpu-hal = { version = "0.15.4", default-features = false }


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

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

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

[profile.bench]
debug = true
Expand Down
2 changes: 2 additions & 0 deletions clippy.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ disallowed-methods = [
"std::thread::spawn", # Use `std::thread::Builder` and name the thread

"sha1::Digest::new", # SHA1 is cryptographically broken

"std::panic::catch_unwind", # We compile with `panic = "abort"`
]

# https://rust-lang.github.io/rust-clippy/master/index.html#disallowed_names
Expand Down
9 changes: 7 additions & 2 deletions crates/rerun/src/crash_handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,6 @@ fn install_panic_hook(_build_info: BuildInfo) {
format!("{file}:{}", location.line())
});

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

if let Some(msg) = &msg {
Expand Down Expand Up @@ -90,10 +88,17 @@ fn install_panic_hook(_build_info: BuildInfo) {
std::thread::sleep(std::time::Duration::from_secs(1)); // Give analytics time to send the event
}
}

// We compile with `panic = "abort"`, but we don't want to report the same problem twice, so just exit:
#[allow(clippy::exit)]
std::process::exit(102);
}));
}

fn panic_info_message(panic_info: &std::panic::PanicInfo<'_>) -> Option<String> {
// `panic_info.message` is unstable, so this is the recommended way of getting
// the panic message out. We need both the `&str` and `String` variants.

#[allow(clippy::manual_map)]
if let Some(msg) = panic_info.payload().downcast_ref::<&str>() {
Some((*msg).to_owned())
Expand Down

1 comment on commit d852534

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Rust Benchmark

Benchmark suite Current: d852534 Previous: 49d5de8 Ratio
datastore/num_rows=1000/num_instances=1000/packed=false/insert/default 9986008 ns/iter (± 786558) 12934040 ns/iter (± 803274) 0.77
datastore/num_rows=1000/num_instances=1000/packed=false/latest_at/default 1810 ns/iter (± 26) 1841 ns/iter (± 21) 0.98
datastore/num_rows=1000/num_instances=1000/packed=false/latest_at_missing/primary/default 277 ns/iter (± 4) 279 ns/iter (± 3) 0.99
datastore/num_rows=1000/num_instances=1000/packed=false/latest_at_missing/secondaries/default 428 ns/iter (± 5) 433 ns/iter (± 0) 0.99
datastore/num_rows=1000/num_instances=1000/packed=false/range/default 10083824 ns/iter (± 431121) 13266686 ns/iter (± 838433) 0.76
mono_points_arrow/generate_message_bundles 44662814 ns/iter (± 1366035) 50134150 ns/iter (± 739228) 0.89
mono_points_arrow/generate_messages 165151412 ns/iter (± 1623002) 170366330 ns/iter (± 1590613) 0.97
mono_points_arrow/encode_log_msg 190444310 ns/iter (± 1824307) 199583984 ns/iter (± 2168213) 0.95
mono_points_arrow/encode_total 406511948 ns/iter (± 2826957) 416287189 ns/iter (± 3355401) 0.98
mono_points_arrow/decode_log_msg 249669700 ns/iter (± 1955503) 250341879 ns/iter (± 1302913) 1.00
mono_points_arrow/decode_message_bundles 79645762 ns/iter (± 1588996) 85395356 ns/iter (± 712530) 0.93
mono_points_arrow/decode_total 331555134 ns/iter (± 2810134) 346548270 ns/iter (± 2555490) 0.96
mono_points_arrow_batched/generate_message_bundles 35949883 ns/iter (± 1764135) 46297044 ns/iter (± 1416505) 0.78
mono_points_arrow_batched/generate_messages 6823526 ns/iter (± 415150) 11722159 ns/iter (± 1194335) 0.58
mono_points_arrow_batched/encode_log_msg 1437597 ns/iter (± 16752) 1519551 ns/iter (± 3670) 0.95
mono_points_arrow_batched/encode_total 48628004 ns/iter (± 1902045) 55204254 ns/iter (± 2142567) 0.88
mono_points_arrow_batched/decode_log_msg 847900 ns/iter (± 7261) 866862 ns/iter (± 3233) 0.98
mono_points_arrow_batched/decode_message_bundles 11275359 ns/iter (± 517095) 16264720 ns/iter (± 1010179) 0.69
mono_points_arrow_batched/decode_total 12409735 ns/iter (± 539880) 17383368 ns/iter (± 882764) 0.71
batch_points_arrow/generate_message_bundles 323589 ns/iter (± 4333) 333580 ns/iter (± 625) 0.97
batch_points_arrow/generate_messages 6148 ns/iter (± 93) 6360 ns/iter (± 9) 0.97
batch_points_arrow/encode_log_msg 379603 ns/iter (± 3945) 401656 ns/iter (± 2386) 0.95
batch_points_arrow/encode_total 721784 ns/iter (± 7601) 769938 ns/iter (± 4064) 0.94
batch_points_arrow/decode_log_msg 332936 ns/iter (± 2915) 342696 ns/iter (± 2212) 0.97
batch_points_arrow/decode_message_bundles 2239 ns/iter (± 34) 2317 ns/iter (± 8) 0.97
batch_points_arrow/decode_total 341734 ns/iter (± 3012) 348727 ns/iter (± 1542) 0.98
arrow_mono_points/insert 7311032580 ns/iter (± 28599593) 7491778673 ns/iter (± 39495597) 0.98
arrow_mono_points/query 1751863 ns/iter (± 29209) 1820462 ns/iter (± 12696) 0.96
arrow_batch_points/insert 3212950 ns/iter (± 20038) 3280991 ns/iter (± 37898) 0.98
arrow_batch_points/query 16379 ns/iter (± 50) 16472 ns/iter (± 558) 0.99
arrow_batch_vecs/insert 44838 ns/iter (± 269) 45846 ns/iter (± 145) 0.98
arrow_batch_vecs/query 389195 ns/iter (± 1221) 389685 ns/iter (± 410) 1.00
tuid/Tuid::random 34 ns/iter (± 0) 34 ns/iter (± 0) 1

This comment was automatically generated by workflow using github-action-benchmark.

Please sign in to comment.