Skip to content

Commit 771a1d8

Browse files
committed
Make std::panicking::panic_count::is_zero inline and move the slow path into a separate cold function.
1 parent f03cf99 commit 771a1d8

File tree

1 file changed

+11
-1
lines changed

1 file changed

+11
-1
lines changed

src/libstd/panicking.rs

+11-1
Original file line numberDiff line numberDiff line change
@@ -258,16 +258,25 @@ pub mod panic_count {
258258
LOCAL_PANIC_COUNT.with(|c| c.get())
259259
}
260260

261+
#[inline]
261262
pub fn is_zero() -> bool {
262263
if GLOBAL_PANIC_COUNT.load(Ordering::Relaxed) == 0 {
263264
// Fast path: if `GLOBAL_PANIC_COUNT` is zero, all threads
264265
// (including the current one) will have `LOCAL_PANIC_COUNT`
265266
// equal to zero, so TLS access can be avoided.
266267
true
267268
} else {
268-
LOCAL_PANIC_COUNT.with(|c| c.get() == 0)
269+
is_zero_slow_path()
269270
}
270271
}
272+
273+
// Slow path is in a separate function to reduce the amount of code
274+
// inlined from `is_zero`.
275+
#[inline(never)]
276+
#[cold]
277+
fn is_zero_slow_path() -> bool {
278+
LOCAL_PANIC_COUNT.with(|c| c.get() == 0)
279+
}
271280
}
272281

273282
#[cfg(test)]
@@ -350,6 +359,7 @@ pub unsafe fn r#try<R, F: FnOnce() -> R>(f: F) -> Result<R, Box<dyn Any + Send>>
350359
}
351360

352361
/// Determines whether the current thread is unwinding because of panic.
362+
#[inline]
353363
pub fn panicking() -> bool {
354364
!panic_count::is_zero()
355365
}

0 commit comments

Comments
 (0)