From 2381a54c2f6ff1d97b9d6cb982dde5644f09a396 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Mon, 29 Mar 2021 20:54:15 +0300 Subject: internal: cleanup hprof --- crates/profile/src/hprof.rs | 20 +++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-) (limited to 'crates/profile') 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 @@ //! Simple hierarchical profiler -use once_cell::sync::Lazy; use std::{ cell::RefCell, collections::{BTreeMap, HashSet}, @@ -12,6 +11,8 @@ use std::{ time::{Duration, Instant}, }; +use once_cell::sync::Lazy; + use crate::tree::{Idx, Tree}; /// Filtering syntax @@ -56,12 +57,12 @@ type Label = &'static str; /// 0ms - profile /// 0ms - profile2 /// ``` +#[inline] pub fn span(label: Label) -> ProfileSpan { - assert!(!label.is_empty()); + debug_assert!(!label.is_empty()); - if PROFILING_ENABLED.load(Ordering::Relaxed) - && PROFILE_STACK.with(|stack| stack.borrow_mut().push(label)) - { + let enabled = PROFILING_ENABLED.load(Ordering::Relaxed); + if enabled && with_profile_stack(|stack| stack.push(label)) { ProfileSpan(Some(ProfilerImpl { label, detail: None })) } else { ProfileSpan(None) @@ -85,14 +86,19 @@ impl ProfileSpan { } impl Drop for ProfilerImpl { + #[inline] fn drop(&mut self) { - PROFILE_STACK.with(|it| it.borrow_mut().pop(self.label, self.detail.take())); + with_profile_stack(|it| it.pop(self.label, self.detail.take())); } } static PROFILING_ENABLED: AtomicBool = AtomicBool::new(false); static FILTER: Lazy> = Lazy::new(Default::default); -thread_local!(static PROFILE_STACK: RefCell = RefCell::new(ProfileStack::new())); + +fn with_profile_stack(f: impl FnOnce(&mut ProfileStack) -> T) -> T { + thread_local!(static STACK: RefCell = RefCell::new(ProfileStack::new())); + STACK.with(|it| f(&mut *it.borrow_mut())) +} #[derive(Default, Clone, Debug)] struct Filter { -- cgit v1.2.3