Skip to content

Commit

Permalink
fix(perf): mark error constructors cold (#378)
Browse files Browse the repository at this point in the history
* refactor: mark error constructors cold

* refactor: remove `Send + Sync` impl
  • Loading branch information
re-taro authored Jun 26, 2024
1 parent 73da45b commit 9bbcf3c
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/chain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ pub(crate) enum ChainState<'a> {
}

impl<'a> Chain<'a> {
#[cold]
pub(crate) fn new(head: &'a (dyn StdError + 'static)) -> Self {
Chain {
state: ChainState::Linked { next: Some(head) },
Expand Down
14 changes: 14 additions & 0 deletions src/eyreish/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ impl Report {
/// If the error type does not provide a backtrace, a backtrace will be
/// created here to ensure that a backtrace exists.
#[cfg_attr(track_caller, track_caller)]
#[cold]
pub fn new<E>(error: E) -> Self
where
E: Diagnostic + Send + Sync + 'static,
Expand Down Expand Up @@ -66,6 +67,7 @@ impl Report {
/// }
/// ```
#[cfg_attr(track_caller, track_caller)]
#[cold]
pub fn msg<M>(message: M) -> Self
where
M: Display + Debug + Send + Sync + 'static,
Expand All @@ -86,6 +88,7 @@ impl Report {
}

#[cfg_attr(track_caller, track_caller)]
#[cold]
pub(crate) fn from_std<E>(error: E) -> Self
where
E: Diagnostic + Send + Sync + 'static,
Expand All @@ -107,6 +110,7 @@ impl Report {
}

#[cfg_attr(track_caller, track_caller)]
#[cold]
pub(crate) fn from_adhoc<M>(message: M) -> Self
where
M: Display + Debug + Send + Sync + 'static,
Expand All @@ -131,6 +135,7 @@ impl Report {
}

#[cfg_attr(track_caller, track_caller)]
#[cold]
pub(crate) fn from_msg<D, E>(msg: D, error: E) -> Self
where
D: Display + Send + Sync + 'static,
Expand All @@ -155,6 +160,7 @@ impl Report {
}

#[cfg_attr(track_caller, track_caller)]
#[cold]
pub(crate) fn from_boxed(error: Box<dyn Diagnostic + Send + Sync>) -> Self {
use super::wrapper::BoxedError;
let error = BoxedError(error);
Expand All @@ -180,6 +186,7 @@ impl Report {
//
// Unsafe because the given vtable must have sensible behavior on the error
// value of type E.
#[cold]
unsafe fn construct<E>(
error: E,
vtable: &'static ErrorVTable,
Expand Down Expand Up @@ -262,6 +269,7 @@ impl Report {
/// None
/// }
/// ```
#[cold]
pub fn chain(&self) -> Chain<'_> {
unsafe { ErrorImpl::chain(self.inner.by_ref()) }
}
Expand Down Expand Up @@ -424,6 +432,7 @@ where
E: Diagnostic + Send + Sync + 'static,
{
#[cfg_attr(track_caller, track_caller)]
#[cold]
fn from(error: E) -> Self {
Report::from_std(error)
}
Expand Down Expand Up @@ -711,6 +720,7 @@ impl ErasedErrorImpl {
.deref_mut()
}

#[cold]
pub(crate) unsafe fn chain(this: Ref<'_, Self>) -> Chain<'_> {
Chain::new(Self::error(this))
}
Expand Down Expand Up @@ -746,6 +756,7 @@ where
}

impl From<Report> for Box<dyn Diagnostic + Send + Sync + 'static> {
#[cold]
fn from(error: Report) -> Self {
let outer = ManuallyDrop::new(error);
unsafe {
Expand All @@ -757,6 +768,7 @@ impl From<Report> for Box<dyn Diagnostic + Send + Sync + 'static> {
}

impl From<Report> for Box<dyn StdError + Send + Sync + 'static> {
#[cold]
fn from(error: Report) -> Self {
let outer = ManuallyDrop::new(error);
unsafe {
Expand All @@ -768,12 +780,14 @@ impl From<Report> for Box<dyn StdError + Send + Sync + 'static> {
}

impl From<Report> for Box<dyn Diagnostic + 'static> {
#[cold]
fn from(error: Report) -> Self {
Box::<dyn Diagnostic + Send + Sync>::from(error)
}
}

impl From<Report> for Box<dyn StdError + 'static> {
#[cold]
fn from(error: Report) -> Self {
Box::<dyn StdError + Send + Sync>::from(error)
}
Expand Down
3 changes: 3 additions & 0 deletions src/eyreish/kind.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ impl<T> AdhocKind for &T where T: ?Sized + Display + Debug + Send + Sync + 'stat

impl Adhoc {
#[cfg_attr(track_caller, track_caller)]
#[cold]
pub fn new<M>(self, message: M) -> Report
where
M: Display + Debug + Send + Sync + 'static,
Expand All @@ -84,6 +85,7 @@ impl<E> TraitKind for E where E: Into<Report> {}

impl Trait {
#[cfg_attr(track_caller, track_caller)]
#[cold]
pub fn new<E>(self, error: E) -> Report
where
E: Into<Report>,
Expand All @@ -105,6 +107,7 @@ impl BoxedKind for Box<dyn Diagnostic + Send + Sync> {}

impl Boxed {
#[cfg_attr(track_caller, track_caller)]
#[cold]
pub fn new(self, error: Box<dyn Diagnostic + Send + Sync>) -> Report {
Report::from_boxed(error)
}
Expand Down
1 change: 1 addition & 0 deletions src/eyreish/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -475,6 +475,7 @@ pub mod private {
}

#[cfg_attr(track_caller, track_caller)]
#[cold]
pub fn new_adhoc<M>(message: M) -> Report
where
M: Display + Debug + Send + Sync + 'static,
Expand Down

0 comments on commit 9bbcf3c

Please sign in to comment.