From 42dba9da6ea8fe44bcc16e7f487505d2ccc5a30f Mon Sep 17 00:00:00 2001 From: Stjepan Glavina Date: Mon, 3 Dec 2018 16:42:39 +0000 Subject: [PATCH 1/6] Optimize thread::panicking() --- src/libstd/panicking.rs | 19 +++++++++---------- 1 file changed, 9 insertions(+), 10 deletions(-) diff --git a/src/libstd/panicking.rs b/src/libstd/panicking.rs index 4930d35660814..ffe3cc3f78092 100644 --- a/src/libstd/panicking.rs +++ b/src/libstd/panicking.rs @@ -22,7 +22,7 @@ use core::panic::BoxMeUp; use io::prelude::*; use any::Any; -use cell::RefCell; +use cell::{Cell, RefCell}; use core::panic::{PanicInfo, Location}; use fmt; use intrinsics; @@ -229,19 +229,17 @@ fn default_hook(info: &PanicInfo) { } } +#[thread_local] +static PANIC_COUNT: Cell = Cell::new(0); #[cfg(not(test))] #[doc(hidden)] +#[inline] #[unstable(feature = "update_panic_count", issue = "0")] pub fn update_panic_count(amt: isize) -> usize { - use cell::Cell; - thread_local! { static PANIC_COUNT: Cell = Cell::new(0) } - - PANIC_COUNT.with(|c| { - let next = (c.get() as isize + amt) as usize; - c.set(next); - return next - }) + let next = (PANIC_COUNT.get() as isize + amt) as usize; + PANIC_COUNT.set(next); + next } #[cfg(test)] @@ -313,8 +311,9 @@ pub unsafe fn try R>(f: F) -> Result> { } /// Determines whether the current thread is unwinding because of panic. +#[inline] pub fn panicking() -> bool { - update_panic_count(0) != 0 + PANIC_COUNT.get() != 0 } /// Entry point of panic from the libcore crate. From 7dce3dc6e36acd274cb8407d4b200b0eb7495fd8 Mon Sep 17 00:00:00 2001 From: Stjepan Glavina Date: Mon, 3 Dec 2018 18:20:30 +0000 Subject: [PATCH 2/6] Remove #[inline] attributes --- src/libstd/panicking.rs | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/libstd/panicking.rs b/src/libstd/panicking.rs index ffe3cc3f78092..b534083e05fcd 100644 --- a/src/libstd/panicking.rs +++ b/src/libstd/panicking.rs @@ -232,9 +232,10 @@ fn default_hook(info: &PanicInfo) { #[thread_local] static PANIC_COUNT: Cell = Cell::new(0); +// This function must not be `#[inline]` because it touches a `#[thread_local]` variable. +// See: https://github.com/rust-lang/rust/pull/56469#issuecomment-443810577 #[cfg(not(test))] #[doc(hidden)] -#[inline] #[unstable(feature = "update_panic_count", issue = "0")] pub fn update_panic_count(amt: isize) -> usize { let next = (PANIC_COUNT.get() as isize + amt) as usize; @@ -311,7 +312,8 @@ pub unsafe fn try R>(f: F) -> Result> { } /// Determines whether the current thread is unwinding because of panic. -#[inline] +// This function must not be `#[inline]` because it touches a `#[thread_local]` variable. +// See: https://github.com/rust-lang/rust/pull/56469#issuecomment-443810577 pub fn panicking() -> bool { PANIC_COUNT.get() != 0 } From c092af904fae56784a1a22420e4e0231320dd3f6 Mon Sep 17 00:00:00 2001 From: Stjepan Glavina Date: Tue, 4 Dec 2018 15:36:55 +0000 Subject: [PATCH 3/6] Use thread_local! on platforms that don't support #[thread_local] --- src/libstd/panicking.rs | 45 ++++++++++++++++++++++++++++++++--------- 1 file changed, 35 insertions(+), 10 deletions(-) diff --git a/src/libstd/panicking.rs b/src/libstd/panicking.rs index b534083e05fcd..e8572608573b1 100644 --- a/src/libstd/panicking.rs +++ b/src/libstd/panicking.rs @@ -229,18 +229,45 @@ fn default_hook(info: &PanicInfo) { } } -#[thread_local] -static PANIC_COUNT: Cell = Cell::new(0); +fn with_panic_count(f: impl FnOnce(&Cell) -> R) -> R { + // Use `static mut` on wasm32 if there are no atomics. + #[cfg(all(target_arch = "wasm32", not(target_feature = "atomics")))] + { + static mut PANIC_COUNT: Cell = Cell::new(0); + unsafe { f(&PANIC_COUNT) } + } + + // Use fast `#[thread_local]` on platforms that support it. + #[cfg(all( + target_thread_local, + not(all(target_arch = "wasm32", not(target_feature = "atomics"))), + ))] + { + #[thread_local] + static PANIC_COUNT: Cell = Cell::new(0); + f(&PANIC_COUNT) + } + + // Fall back to regular `thread_local!` in all other cases. + #[cfg(all( + not(target_thread_local), + not(all(target_arch = "wasm32", not(target_feature = "atomics"))), + ))] + { + thread_local! { static PANIC_COUNT: Cell = Cell::new(0) } + PANIC_COUNT.with(f) + } +} -// This function must not be `#[inline]` because it touches a `#[thread_local]` variable. -// See: https://github.com/rust-lang/rust/pull/56469#issuecomment-443810577 #[cfg(not(test))] #[doc(hidden)] #[unstable(feature = "update_panic_count", issue = "0")] pub fn update_panic_count(amt: isize) -> usize { - let next = (PANIC_COUNT.get() as isize + amt) as usize; - PANIC_COUNT.set(next); - next + with_panic_count(|c| { + let next = (c.get() as isize + amt) as usize; + c.set(next); + next + }) } #[cfg(test)] @@ -312,10 +339,8 @@ pub unsafe fn try R>(f: F) -> Result> { } /// Determines whether the current thread is unwinding because of panic. -// This function must not be `#[inline]` because it touches a `#[thread_local]` variable. -// See: https://github.com/rust-lang/rust/pull/56469#issuecomment-443810577 pub fn panicking() -> bool { - PANIC_COUNT.get() != 0 + with_panic_count(|c| c.get() != 0) } /// Entry point of panic from the libcore crate. From b48a110b05a52ddbcfc28919c461d5977f3a99a9 Mon Sep 17 00:00:00 2001 From: Stjepan Glavina Date: Tue, 4 Dec 2018 17:46:01 +0000 Subject: [PATCH 4/6] Revert changes in rust_begin_panic --- src/libstd/panicking.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/libstd/panicking.rs b/src/libstd/panicking.rs index e8572608573b1..173d08e0be052 100644 --- a/src/libstd/panicking.rs +++ b/src/libstd/panicking.rs @@ -340,7 +340,7 @@ pub unsafe fn try R>(f: F) -> Result> { /// Determines whether the current thread is unwinding because of panic. pub fn panicking() -> bool { - with_panic_count(|c| c.get() != 0) + update_panic_count(0) != 0 } /// Entry point of panic from the libcore crate. From bc0de72a1bd4dc54d7263f5af2fe1d91d08b5e4d Mon Sep 17 00:00:00 2001 From: Stjepan Glavina Date: Tue, 4 Dec 2018 19:09:53 +0000 Subject: [PATCH 5/6] Disable with_panic_count in test mode --- src/libstd/panicking.rs | 60 ++++++++++++++++++++--------------------- 1 file changed, 30 insertions(+), 30 deletions(-) diff --git a/src/libstd/panicking.rs b/src/libstd/panicking.rs index 173d08e0be052..057bb80848d80 100644 --- a/src/libstd/panicking.rs +++ b/src/libstd/panicking.rs @@ -229,40 +229,40 @@ fn default_hook(info: &PanicInfo) { } } -fn with_panic_count(f: impl FnOnce(&Cell) -> R) -> R { - // Use `static mut` on wasm32 if there are no atomics. - #[cfg(all(target_arch = "wasm32", not(target_feature = "atomics")))] - { - static mut PANIC_COUNT: Cell = Cell::new(0); - unsafe { f(&PANIC_COUNT) } - } - - // Use fast `#[thread_local]` on platforms that support it. - #[cfg(all( - target_thread_local, - not(all(target_arch = "wasm32", not(target_feature = "atomics"))), - ))] - { - #[thread_local] - static PANIC_COUNT: Cell = Cell::new(0); - f(&PANIC_COUNT) - } - - // Fall back to regular `thread_local!` in all other cases. - #[cfg(all( - not(target_thread_local), - not(all(target_arch = "wasm32", not(target_feature = "atomics"))), - ))] - { - thread_local! { static PANIC_COUNT: Cell = Cell::new(0) } - PANIC_COUNT.with(f) - } -} - #[cfg(not(test))] #[doc(hidden)] #[unstable(feature = "update_panic_count", issue = "0")] pub fn update_panic_count(amt: isize) -> usize { + fn with_panic_count(f: impl FnOnce(&Cell) -> R) -> R { + // Use `static mut` on wasm32 if there are no atomics. + #[cfg(all(target_arch = "wasm32", not(target_feature = "atomics")))] + { + static mut PANIC_COUNT: Cell = Cell::new(0); + unsafe { f(&PANIC_COUNT) } + } + + // Use fast `#[thread_local]` on platforms that support it. + #[cfg(all( + target_thread_local, + not(all(target_arch = "wasm32", not(target_feature = "atomics"))), + ))] + { + #[thread_local] + static PANIC_COUNT: Cell = Cell::new(0); + f(&PANIC_COUNT) + } + + // Fall back to regular `thread_local!` in all other cases. + #[cfg(all( + not(target_thread_local), + not(all(target_arch = "wasm32", not(target_feature = "atomics"))), + ))] + { + thread_local! { static PANIC_COUNT: Cell = Cell::new(0) } + PANIC_COUNT.with(f) + } + } + with_panic_count(|c| { let next = (c.get() as isize + amt) as usize; c.set(next); From 81c9f8af922033aa4286e8af12ab6a8d4de30dc8 Mon Sep 17 00:00:00 2001 From: Stjepan Glavina Date: Tue, 4 Dec 2018 20:27:55 +0000 Subject: [PATCH 6/6] Fix unused warning --- src/libstd/panicking.rs | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/libstd/panicking.rs b/src/libstd/panicking.rs index 057bb80848d80..6e14e416a3456 100644 --- a/src/libstd/panicking.rs +++ b/src/libstd/panicking.rs @@ -22,7 +22,7 @@ use core::panic::BoxMeUp; use io::prelude::*; use any::Any; -use cell::{Cell, RefCell}; +use cell::RefCell; use core::panic::{PanicInfo, Location}; use fmt; use intrinsics; @@ -233,6 +233,8 @@ fn default_hook(info: &PanicInfo) { #[doc(hidden)] #[unstable(feature = "update_panic_count", issue = "0")] pub fn update_panic_count(amt: isize) -> usize { + use cell::Cell; + fn with_panic_count(f: impl FnOnce(&Cell) -> R) -> R { // Use `static mut` on wasm32 if there are no atomics. #[cfg(all(target_arch = "wasm32", not(target_feature = "atomics")))]