fix(perf): mark error constructors cold (#378)

* refactor: mark error constructors cold

* refactor: remove `Send + Sync` impl
This commit is contained in:
Rintaro Itokawa 2024-06-27 01:33:42 +09:00 committed by GitHub
parent 73da45b65c
commit 9bbcf3c601
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 19 additions and 0 deletions

View File

@ -44,6 +44,7 @@ pub(crate) enum ChainState<'a> {
} }
impl<'a> Chain<'a> { impl<'a> Chain<'a> {
#[cold]
pub(crate) fn new(head: &'a (dyn StdError + 'static)) -> Self { pub(crate) fn new(head: &'a (dyn StdError + 'static)) -> Self {
Chain { Chain {
state: ChainState::Linked { next: Some(head) }, state: ChainState::Linked { next: Some(head) },

View File

@ -21,6 +21,7 @@ impl Report {
/// If the error type does not provide a backtrace, a backtrace will be /// If the error type does not provide a backtrace, a backtrace will be
/// created here to ensure that a backtrace exists. /// created here to ensure that a backtrace exists.
#[cfg_attr(track_caller, track_caller)] #[cfg_attr(track_caller, track_caller)]
#[cold]
pub fn new<E>(error: E) -> Self pub fn new<E>(error: E) -> Self
where where
E: Diagnostic + Send + Sync + 'static, E: Diagnostic + Send + Sync + 'static,
@ -66,6 +67,7 @@ impl Report {
/// } /// }
/// ``` /// ```
#[cfg_attr(track_caller, track_caller)] #[cfg_attr(track_caller, track_caller)]
#[cold]
pub fn msg<M>(message: M) -> Self pub fn msg<M>(message: M) -> Self
where where
M: Display + Debug + Send + Sync + 'static, M: Display + Debug + Send + Sync + 'static,
@ -86,6 +88,7 @@ impl Report {
} }
#[cfg_attr(track_caller, track_caller)] #[cfg_attr(track_caller, track_caller)]
#[cold]
pub(crate) fn from_std<E>(error: E) -> Self pub(crate) fn from_std<E>(error: E) -> Self
where where
E: Diagnostic + Send + Sync + 'static, E: Diagnostic + Send + Sync + 'static,
@ -107,6 +110,7 @@ impl Report {
} }
#[cfg_attr(track_caller, track_caller)] #[cfg_attr(track_caller, track_caller)]
#[cold]
pub(crate) fn from_adhoc<M>(message: M) -> Self pub(crate) fn from_adhoc<M>(message: M) -> Self
where where
M: Display + Debug + Send + Sync + 'static, M: Display + Debug + Send + Sync + 'static,
@ -131,6 +135,7 @@ impl Report {
} }
#[cfg_attr(track_caller, track_caller)] #[cfg_attr(track_caller, track_caller)]
#[cold]
pub(crate) fn from_msg<D, E>(msg: D, error: E) -> Self pub(crate) fn from_msg<D, E>(msg: D, error: E) -> Self
where where
D: Display + Send + Sync + 'static, D: Display + Send + Sync + 'static,
@ -155,6 +160,7 @@ impl Report {
} }
#[cfg_attr(track_caller, track_caller)] #[cfg_attr(track_caller, track_caller)]
#[cold]
pub(crate) fn from_boxed(error: Box<dyn Diagnostic + Send + Sync>) -> Self { pub(crate) fn from_boxed(error: Box<dyn Diagnostic + Send + Sync>) -> Self {
use super::wrapper::BoxedError; use super::wrapper::BoxedError;
let error = BoxedError(error); let error = BoxedError(error);
@ -180,6 +186,7 @@ impl Report {
// //
// Unsafe because the given vtable must have sensible behavior on the error // Unsafe because the given vtable must have sensible behavior on the error
// value of type E. // value of type E.
#[cold]
unsafe fn construct<E>( unsafe fn construct<E>(
error: E, error: E,
vtable: &'static ErrorVTable, vtable: &'static ErrorVTable,
@ -262,6 +269,7 @@ impl Report {
/// None /// None
/// } /// }
/// ``` /// ```
#[cold]
pub fn chain(&self) -> Chain<'_> { pub fn chain(&self) -> Chain<'_> {
unsafe { ErrorImpl::chain(self.inner.by_ref()) } unsafe { ErrorImpl::chain(self.inner.by_ref()) }
} }
@ -424,6 +432,7 @@ where
E: Diagnostic + Send + Sync + 'static, E: Diagnostic + Send + Sync + 'static,
{ {
#[cfg_attr(track_caller, track_caller)] #[cfg_attr(track_caller, track_caller)]
#[cold]
fn from(error: E) -> Self { fn from(error: E) -> Self {
Report::from_std(error) Report::from_std(error)
} }
@ -711,6 +720,7 @@ impl ErasedErrorImpl {
.deref_mut() .deref_mut()
} }
#[cold]
pub(crate) unsafe fn chain(this: Ref<'_, Self>) -> Chain<'_> { pub(crate) unsafe fn chain(this: Ref<'_, Self>) -> Chain<'_> {
Chain::new(Self::error(this)) Chain::new(Self::error(this))
} }
@ -746,6 +756,7 @@ where
} }
impl From<Report> for Box<dyn Diagnostic + Send + Sync + 'static> { impl From<Report> for Box<dyn Diagnostic + Send + Sync + 'static> {
#[cold]
fn from(error: Report) -> Self { fn from(error: Report) -> Self {
let outer = ManuallyDrop::new(error); let outer = ManuallyDrop::new(error);
unsafe { unsafe {
@ -757,6 +768,7 @@ impl From<Report> for Box<dyn Diagnostic + Send + Sync + 'static> {
} }
impl From<Report> for Box<dyn StdError + Send + Sync + 'static> { impl From<Report> for Box<dyn StdError + Send + Sync + 'static> {
#[cold]
fn from(error: Report) -> Self { fn from(error: Report) -> Self {
let outer = ManuallyDrop::new(error); let outer = ManuallyDrop::new(error);
unsafe { unsafe {
@ -768,12 +780,14 @@ impl From<Report> for Box<dyn StdError + Send + Sync + 'static> {
} }
impl From<Report> for Box<dyn Diagnostic + 'static> { impl From<Report> for Box<dyn Diagnostic + 'static> {
#[cold]
fn from(error: Report) -> Self { fn from(error: Report) -> Self {
Box::<dyn Diagnostic + Send + Sync>::from(error) Box::<dyn Diagnostic + Send + Sync>::from(error)
} }
} }
impl From<Report> for Box<dyn StdError + 'static> { impl From<Report> for Box<dyn StdError + 'static> {
#[cold]
fn from(error: Report) -> Self { fn from(error: Report) -> Self {
Box::<dyn StdError + Send + Sync>::from(error) Box::<dyn StdError + Send + Sync>::from(error)
} }

View File

@ -63,6 +63,7 @@ impl<T> AdhocKind for &T where T: ?Sized + Display + Debug + Send + Sync + 'stat
impl Adhoc { impl Adhoc {
#[cfg_attr(track_caller, track_caller)] #[cfg_attr(track_caller, track_caller)]
#[cold]
pub fn new<M>(self, message: M) -> Report pub fn new<M>(self, message: M) -> Report
where where
M: Display + Debug + Send + Sync + 'static, M: Display + Debug + Send + Sync + 'static,
@ -84,6 +85,7 @@ impl<E> TraitKind for E where E: Into<Report> {}
impl Trait { impl Trait {
#[cfg_attr(track_caller, track_caller)] #[cfg_attr(track_caller, track_caller)]
#[cold]
pub fn new<E>(self, error: E) -> Report pub fn new<E>(self, error: E) -> Report
where where
E: Into<Report>, E: Into<Report>,
@ -105,6 +107,7 @@ impl BoxedKind for Box<dyn Diagnostic + Send + Sync> {}
impl Boxed { impl Boxed {
#[cfg_attr(track_caller, track_caller)] #[cfg_attr(track_caller, track_caller)]
#[cold]
pub fn new(self, error: Box<dyn Diagnostic + Send + Sync>) -> Report { pub fn new(self, error: Box<dyn Diagnostic + Send + Sync>) -> Report {
Report::from_boxed(error) Report::from_boxed(error)
} }

View File

@ -475,6 +475,7 @@ pub mod private {
} }
#[cfg_attr(track_caller, track_caller)] #[cfg_attr(track_caller, track_caller)]
#[cold]
pub fn new_adhoc<M>(message: M) -> Report pub fn new_adhoc<M>(message: M) -> Report
where where
M: Display + Debug + Send + Sync + 'static, M: Display + Debug + Send + Sync + 'static,