Rollup merge of #92992 - kornelski:backtraceopt, r=Mark-Simulacrum

Help optimize out backtraces when disabled

The comment in `rust_backtrace_env` says:

>    // If the `backtrace` feature of this crate isn't enabled quickly return
>   // `None` so this can be constant propagated all over the place to turn
>  // optimize away callers.

but this optimization has regressed, because the only caller of this function had an alternative path that unconditionally (and pointlessly) asked for a full backtrace, so the disabled state couldn't propagate.

I've added a getter for the full format that respects the feature flag, so that the caller will now be able to really optimize out the disabled backtrace path. I've also made `rust_backtrace_env` trivially inlineable when backtraces are disabled.
This commit is contained in:
Matthias Krüger 2022-01-20 17:10:40 +01:00 committed by GitHub
commit 1cb57e2d2b
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 19 additions and 8 deletions

View file

@ -263,7 +263,7 @@ fn default_hook(info: &PanicInfo<'_>) {
// If this is a double panic, make sure that we print a backtrace
// for this panic. Otherwise only print it if logging is enabled.
let backtrace_env = if panic_count::get_count() >= 2 {
RustBacktrace::Print(crate::backtrace_rs::PrintFmt::Full)
backtrace::rust_backtrace_print_full()
} else {
backtrace::rust_backtrace_env()
};

View file

@ -150,16 +150,18 @@ pub enum RustBacktrace {
RuntimeDisabled,
}
// For now logging is turned off by default, and this function checks to see
// whether the magical environment variable is present to see if it's turned on.
pub fn rust_backtrace_env() -> RustBacktrace {
// If the `backtrace` feature of this crate isn't enabled quickly return
// `None` so this can be constant propagated all over the place to turn
// `Disabled` so this can be constant propagated all over the place to
// optimize away callers.
if !cfg!(feature = "backtrace") {
return RustBacktrace::Disabled;
#[cfg(not(feature = "backtrace"))]
pub fn rust_backtrace_env() -> RustBacktrace {
RustBacktrace::Disabled
}
// For now logging is turned off by default, and this function checks to see
// whether the magical environment variable is present to see if it's turned on.
#[cfg(feature = "backtrace")]
pub fn rust_backtrace_env() -> RustBacktrace {
// Setting environment variables for Fuchsia components isn't a standard
// or easily supported workflow. For now, always display backtraces.
if cfg!(target_os = "fuchsia") {
@ -189,6 +191,15 @@ pub fn rust_backtrace_env() -> RustBacktrace {
format
}
/// Setting for printing the full backtrace, unless backtraces are completely disabled
pub(crate) fn rust_backtrace_print_full() -> RustBacktrace {
if cfg!(feature = "backtrace") {
RustBacktrace::Print(PrintFmt::Full)
} else {
RustBacktrace::Disabled
}
}
/// Prints the filename of the backtrace frame.
///
/// See also `output`.