From 24d18d92f69c1ce3d4fc3ff3be67d4f9bf65d857 Mon Sep 17 00:00:00 2001 From: veetaha Date: Sun, 26 Apr 2020 00:10:44 +0300 Subject: Simplify profiler impl (bubble up Option and shorten code --- crates/ra_prof/src/hprof.rs | 45 +++++++++++++++++++++------------------------ 1 file changed, 21 insertions(+), 24 deletions(-) (limited to 'crates') diff --git a/crates/ra_prof/src/hprof.rs b/crates/ra_prof/src/hprof.rs index 2b8a90363..a3f5321fb 100644 --- a/crates/ra_prof/src/hprof.rs +++ b/crates/ra_prof/src/hprof.rs @@ -30,8 +30,9 @@ pub fn init_from(spec: &str) { pub type Label = &'static str; /// This function starts a profiling scope in the current execution stack with a given description. -/// It returns a Profile structure and measure elapsed time between this method invocation and Profile structure drop. -/// It supports nested profiling scopes in case when this function invoked multiple times at the execution stack. In this case the profiling information will be nested at the output. +/// It returns a `Profile` struct that measures elapsed time between this method invocation and `Profile` struct drop. +/// It supports nested profiling scopes in case when this function is invoked multiple times at the execution stack. +/// In this case the profiling information will be nested at the output. /// Profiling information is being printed in the stderr. /// /// # Example @@ -58,36 +59,35 @@ pub type Label = &'static str; /// ``` pub fn profile(label: Label) -> Profiler { assert!(!label.is_empty()); - let enabled = PROFILING_ENABLED.load(Ordering::Relaxed) - && PROFILE_STACK.with(|stack| stack.borrow_mut().push(label)); - let label = if enabled { Some(label) } else { None }; - Profiler { label, detail: None } + + if PROFILING_ENABLED.load(Ordering::Relaxed) + && PROFILE_STACK.with(|stack| stack.borrow_mut().push(label)) + { + Profiler(Some(ProfilerImpl { label, detail: None })) + } else { + Profiler(None) + } } -pub struct Profiler { - label: Option