Skip to content

Commit fa9bcc0

Browse files
committed
Mark error constructors cold
All `#[cold]` to all error constructors. As discussed in the [issue #165](#165), marking functions cold helps LLVM optimize code paths not leading to error. Although in our case marking only `from` was enough, I took the liberty of marking all error constructors `#[cold]`. Note, I marked cold both inner function and public functions, because LLVM does not always takes into account `#[cold]` attribute of inlined functions, see [an example in compiled explorer](https://rust.godbolt.org/z/vzxazMMnb).
1 parent b3c5e25 commit fa9bcc0

File tree

4 files changed

+18
-0
lines changed

4 files changed

+18
-0
lines changed

src/chain.rs

+1
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ pub(crate) enum ChainState<'a> {
2424
}
2525

2626
impl<'a> Chain<'a> {
27+
#[cold]
2728
pub fn new(head: &'a (dyn StdError + 'static)) -> Self {
2829
Chain {
2930
state: ChainState::Linked { next: Some(head) },

src/error.rs

+13
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ impl Error {
2525
/// created here to ensure that a backtrace exists.
2626
#[cfg(feature = "std")]
2727
#[cfg_attr(doc_cfg, doc(cfg(feature = "std")))]
28+
#[cold]
2829
pub fn new<E>(error: E) -> Self
2930
where
3031
E: StdError + Send + Sync + 'static,
@@ -70,6 +71,7 @@ impl Error {
7071
/// .await
7172
/// }
7273
/// ```
74+
#[cold]
7375
pub fn msg<M>(message: M) -> Self
7476
where
7577
M: Display + Debug + Send + Sync + 'static,
@@ -78,6 +80,7 @@ impl Error {
7880
}
7981

8082
#[cfg(feature = "std")]
83+
#[cold]
8184
pub(crate) fn from_std<E>(error: E, backtrace: Option<Backtrace>) -> Self
8285
where
8386
E: StdError + Send + Sync + 'static,
@@ -100,6 +103,7 @@ impl Error {
100103
unsafe { Error::construct(error, vtable, backtrace) }
101104
}
102105

106+
#[cold]
103107
pub(crate) fn from_adhoc<M>(message: M, backtrace: Option<Backtrace>) -> Self
104108
where
105109
M: Display + Debug + Send + Sync + 'static,
@@ -125,6 +129,7 @@ impl Error {
125129
unsafe { Error::construct(error, vtable, backtrace) }
126130
}
127131

132+
#[cold]
128133
pub(crate) fn from_display<M>(message: M, backtrace: Option<Backtrace>) -> Self
129134
where
130135
M: Display + Send + Sync + 'static,
@@ -151,6 +156,7 @@ impl Error {
151156
}
152157

153158
#[cfg(feature = "std")]
159+
#[cold]
154160
pub(crate) fn from_context<C, E>(context: C, error: E, backtrace: Option<Backtrace>) -> Self
155161
where
156162
C: Display + Send + Sync + 'static,
@@ -177,6 +183,7 @@ impl Error {
177183
}
178184

179185
#[cfg(feature = "std")]
186+
#[cold]
180187
pub(crate) fn from_boxed(
181188
error: Box<dyn StdError + Send + Sync>,
182189
backtrace: Option<Backtrace>,
@@ -207,6 +214,7 @@ impl Error {
207214
//
208215
// Unsafe because the given vtable must have sensible behavior on the error
209216
// value of type E.
217+
#[cold]
210218
unsafe fn construct<E>(
211219
error: E,
212220
vtable: &'static ErrorVTable,
@@ -284,6 +292,7 @@ impl Error {
284292
/// })
285293
/// }
286294
/// ```
295+
#[cold]
287296
pub fn context<C>(self, context: C) -> Self
288297
where
289298
C: Display + Send + Sync + 'static,
@@ -373,6 +382,7 @@ impl Error {
373382
/// ```
374383
#[cfg(feature = "std")]
375384
#[cfg_attr(doc_cfg, doc(cfg(feature = "std")))]
385+
#[cold]
376386
pub fn chain(&self) -> Chain {
377387
unsafe { ErrorImpl::chain(self.inner.by_ref()) }
378388
}
@@ -515,6 +525,7 @@ impl<E> From<E> for Error
515525
where
516526
E: StdError + Send + Sync + 'static,
517527
{
528+
#[cold]
518529
fn from(error: E) -> Self {
519530
let backtrace = backtrace_if_absent!(error);
520531
Error::from_std(error, backtrace)
@@ -879,6 +890,7 @@ impl ErrorImpl {
879890
.expect("backtrace capture failed")
880891
}
881892

893+
#[cold]
882894
pub(crate) unsafe fn chain(this: Ref<Self>) -> Chain {
883895
Chain::new(Self::error(this))
884896
}
@@ -917,6 +929,7 @@ where
917929
}
918930

919931
impl From<Error> for Box<dyn StdError + Send + Sync + 'static> {
932+
#[cold]
920933
fn from(error: Error) -> Self {
921934
let outer = ManuallyDrop::new(error);
922935
unsafe {

src/kind.rs

+3
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ pub trait AdhocKind: Sized {
6262
impl<T> AdhocKind for &T where T: ?Sized + Display + Debug + Send + Sync + 'static {}
6363

6464
impl Adhoc {
65+
#[cold]
6566
pub fn new<M>(self, message: M) -> Error
6667
where
6768
M: Display + Debug + Send + Sync + 'static,
@@ -82,6 +83,7 @@ pub trait TraitKind: Sized {
8283
impl<E> TraitKind for E where E: Into<Error> {}
8384

8485
impl Trait {
86+
#[cold]
8587
pub fn new<E>(self, error: E) -> Error
8688
where
8789
E: Into<Error>,
@@ -106,6 +108,7 @@ impl BoxedKind for Box<dyn StdError + Send + Sync> {}
106108

107109
#[cfg(feature = "std")]
108110
impl Boxed {
111+
#[cold]
109112
pub fn new(self, error: Box<dyn StdError + Send + Sync>) -> Error {
110113
let backtrace = backtrace_if_absent!(error);
111114
Error::from_boxed(error, backtrace)

src/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -626,6 +626,7 @@ pub mod private {
626626
pub use crate::kind::BoxedKind;
627627
}
628628

629+
#[cold]
629630
pub fn new_adhoc<M>(message: M) -> Error
630631
where
631632
M: Display + Debug + Send + Sync + 'static,

0 commit comments

Comments
 (0)