diff options
Diffstat (limited to 'crates/profile')
-rw-r--r-- | crates/profile/src/hprof.rs | 20 |
1 files changed, 13 insertions, 7 deletions
diff --git a/crates/profile/src/hprof.rs b/crates/profile/src/hprof.rs index 29d2ed518..014e906e6 100644 --- a/crates/profile/src/hprof.rs +++ b/crates/profile/src/hprof.rs | |||
@@ -1,5 +1,4 @@ | |||
1 | //! Simple hierarchical profiler | 1 | //! Simple hierarchical profiler |
2 | use once_cell::sync::Lazy; | ||
3 | use std::{ | 2 | use std::{ |
4 | cell::RefCell, | 3 | cell::RefCell, |
5 | collections::{BTreeMap, HashSet}, | 4 | collections::{BTreeMap, HashSet}, |
@@ -12,6 +11,8 @@ use std::{ | |||
12 | time::{Duration, Instant}, | 11 | time::{Duration, Instant}, |
13 | }; | 12 | }; |
14 | 13 | ||
14 | use once_cell::sync::Lazy; | ||
15 | |||
15 | use crate::tree::{Idx, Tree}; | 16 | use crate::tree::{Idx, Tree}; |
16 | 17 | ||
17 | /// Filtering syntax | 18 | /// Filtering syntax |
@@ -56,12 +57,12 @@ type Label = &'static str; | |||
56 | /// 0ms - profile | 57 | /// 0ms - profile |
57 | /// 0ms - profile2 | 58 | /// 0ms - profile2 |
58 | /// ``` | 59 | /// ``` |
60 | #[inline] | ||
59 | pub fn span(label: Label) -> ProfileSpan { | 61 | pub fn span(label: Label) -> ProfileSpan { |
60 | assert!(!label.is_empty()); | 62 | debug_assert!(!label.is_empty()); |
61 | 63 | ||
62 | if PROFILING_ENABLED.load(Ordering::Relaxed) | 64 | let enabled = PROFILING_ENABLED.load(Ordering::Relaxed); |
63 | && PROFILE_STACK.with(|stack| stack.borrow_mut().push(label)) | 65 | if enabled && with_profile_stack(|stack| stack.push(label)) { |
64 | { | ||
65 | ProfileSpan(Some(ProfilerImpl { label, detail: None })) | 66 | ProfileSpan(Some(ProfilerImpl { label, detail: None })) |
66 | } else { | 67 | } else { |
67 | ProfileSpan(None) | 68 | ProfileSpan(None) |
@@ -85,14 +86,19 @@ impl ProfileSpan { | |||
85 | } | 86 | } |
86 | 87 | ||
87 | impl Drop for ProfilerImpl { | 88 | impl Drop for ProfilerImpl { |
89 | #[inline] | ||
88 | fn drop(&mut self) { | 90 | fn drop(&mut self) { |
89 | PROFILE_STACK.with(|it| it.borrow_mut().pop(self.label, self.detail.take())); | 91 | with_profile_stack(|it| it.pop(self.label, self.detail.take())); |
90 | } | 92 | } |
91 | } | 93 | } |
92 | 94 | ||
93 | static PROFILING_ENABLED: AtomicBool = AtomicBool::new(false); | 95 | static PROFILING_ENABLED: AtomicBool = AtomicBool::new(false); |
94 | static FILTER: Lazy<RwLock<Filter>> = Lazy::new(Default::default); | 96 | static FILTER: Lazy<RwLock<Filter>> = Lazy::new(Default::default); |
95 | thread_local!(static PROFILE_STACK: RefCell<ProfileStack> = RefCell::new(ProfileStack::new())); | 97 | |
98 | fn with_profile_stack<T>(f: impl FnOnce(&mut ProfileStack) -> T) -> T { | ||
99 | thread_local!(static STACK: RefCell<ProfileStack> = RefCell::new(ProfileStack::new())); | ||
100 | STACK.with(|it| f(&mut *it.borrow_mut())) | ||
101 | } | ||
96 | 102 | ||
97 | #[derive(Default, Clone, Debug)] | 103 | #[derive(Default, Clone, Debug)] |
98 | struct Filter { | 104 | struct Filter { |