Skip to content

Commit 2381a54

Browse files
committed
internal: cleanup hprof
1 parent 5dd6b93 commit 2381a54

File tree

1 file changed

+13
-7
lines changed

1 file changed

+13
-7
lines changed

crates/profile/src/hprof.rs

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
//! Simple hierarchical profiler
2-
use once_cell::sync::Lazy;
32
use std::{
43
cell::RefCell,
54
collections::{BTreeMap, HashSet},
@@ -12,6 +11,8 @@ use std::{
1211
time::{Duration, Instant},
1312
};
1413

14+
use once_cell::sync::Lazy;
15+
1516
use crate::tree::{Idx, Tree};
1617

1718
/// Filtering syntax
@@ -56,12 +57,12 @@ type Label = &'static str;
5657
/// 0ms - profile
5758
/// 0ms - profile2
5859
/// ```
60+
#[inline]
5961
pub fn span(label: Label) -> ProfileSpan {
60-
assert!(!label.is_empty());
62+
debug_assert!(!label.is_empty());
6163

62-
if PROFILING_ENABLED.load(Ordering::Relaxed)
63-
&& PROFILE_STACK.with(|stack| stack.borrow_mut().push(label))
64-
{
64+
let enabled = PROFILING_ENABLED.load(Ordering::Relaxed);
65+
if enabled && with_profile_stack(|stack| stack.push(label)) {
6566
ProfileSpan(Some(ProfilerImpl { label, detail: None }))
6667
} else {
6768
ProfileSpan(None)
@@ -85,14 +86,19 @@ impl ProfileSpan {
8586
}
8687

8788
impl Drop for ProfilerImpl {
89+
#[inline]
8890
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()));
9092
}
9193
}
9294

9395
static PROFILING_ENABLED: AtomicBool = AtomicBool::new(false);
9496
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+
}
96102

97103
#[derive(Default, Clone, Debug)]
98104
struct Filter {

0 commit comments

Comments
 (0)