Skip to content
Myr

Eliminate backtrace_if_absent in Error::context

Before adoption: this commit came in with the history the project adopted, so no landing records it.

This avoids stepping through the list of causes when adding context to the anyhow::Error type which is known to already hold a backtrace. Reduces the release-mode time of the following loop from 4 seconds to 4 milliseconds. let mut err = anyhow!("..."); for _ in 0..25000 { err = err.context("..."); }

authored by David Tolnay · committed by David Tolnay · parent 8885e93 · Browse files · 1 file+17 added−2 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

1 file+17 added−2 removed

  • other:+17 added−2 removed

1 file+17 added−2 removed

modified:src/error.rs+17 added-2 removed
165 unmodified lines
166 pub fn context<C>(self, context: C) -> Self167 where168 C: Display + Send + Sync + 'static,169 {170 Error::new(ContextError {170 let error = ContextError {171 context,172 error: self,173 })173 };174175 let vtable = &ErrorVTable {176 object_drop: object_drop::<ContextError<C, Error>>,177 object_drop_front: object_drop_front::<ContextError<C, Error>>,178 object_ref: object_ref::<ContextError<C, Error>>,179 object_mut: object_mut::<ContextError<C, Error>>,180 object_boxed: object_boxed::<ContextError<C, Error>>,181 object_is: object_is::<ContextError<C, Error>>,182 };183184 // As the cause is anyhow::Error, we already have a backtrace for it.185 let backtrace = None;186187 // Safety: passing vtable that operates on the right type.188 unsafe { Error::construct(error, vtable, backtrace) }189 }190191 /// Get the backtrace for this Error.192 ///
436 unmodified lines