-
Notifications
You must be signed in to change notification settings - Fork 12.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add a fast path for std::thread::panicking
.
#72617
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -258,16 +258,25 @@ pub mod panic_count { | |||||||||||||
LOCAL_PANIC_COUNT.with(|c| c.get()) | ||||||||||||||
} | ||||||||||||||
|
||||||||||||||
#[inline] | ||||||||||||||
pub fn is_zero() -> bool { | ||||||||||||||
if GLOBAL_PANIC_COUNT.load(Ordering::Relaxed) == 0 { | ||||||||||||||
// Fast path: if `GLOBAL_PANIC_COUNT` is zero, all threads | ||||||||||||||
// (including the current one) will have `LOCAL_PANIC_COUNT` | ||||||||||||||
// equal to zero, so TLS access can be avoided. | ||||||||||||||
true | ||||||||||||||
} else { | ||||||||||||||
LOCAL_PANIC_COUNT.with(|c| c.get() == 0) | ||||||||||||||
is_zero_slow_path() | ||||||||||||||
} | ||||||||||||||
} | ||||||||||||||
|
||||||||||||||
// Slow path is in a separate function to reduce the amount of code | ||||||||||||||
// inlined from `is_zero`. | ||||||||||||||
#[inline(never)] | ||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I did that way because that is how it is done here: Lines 1262 to 1267 in 0b66a89
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fair enough, I guess it doesn't really matter in practice. |
||||||||||||||
#[cold] | ||||||||||||||
fn is_zero_slow_path() -> bool { | ||||||||||||||
LOCAL_PANIC_COUNT.with(|c| c.get() == 0) | ||||||||||||||
} | ||||||||||||||
} | ||||||||||||||
|
||||||||||||||
#[cfg(test)] | ||||||||||||||
|
@@ -350,6 +359,7 @@ pub unsafe fn r#try<R, F: FnOnce() -> R>(f: F) -> Result<R, Box<dyn Any + Send>> | |||||||||||||
} | ||||||||||||||
|
||||||||||||||
/// Determines whether the current thread is unwinding because of panic. | ||||||||||||||
#[inline] | ||||||||||||||
pub fn panicking() -> bool { | ||||||||||||||
!panic_count::is_zero() | ||||||||||||||
} | ||||||||||||||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This function should be
#[inline]
, and a separate#[cold]
function should be used for the slow path that is not inlined.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should the
std::panicking::panicking
function be also made#[inline]
? Otherwise I'm not sure there will be much benefit.The current call stack is:
std::thread::panicking
, which is#[inline]
std::panicking::panicking
, which is not#[inline]
std::panicking::panic_count::is_zero
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah yes, that should probably be inline too.