From 24ad1cce2c3cf2c0ce8288fc02c4c55529598990 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Sun, 2 Feb 2020 18:54:26 +0100 Subject: Avoid premature pessimization The extra allocation for message should not matter here at all, but using a static string is just as ergonomic, if not more, and there's no reason to write deliberately slow code --- crates/ra_prof/src/lib.rs | 64 +++++++++++++++++++++++------------------------ 1 file changed, 32 insertions(+), 32 deletions(-) (limited to 'crates/ra_prof') diff --git a/crates/ra_prof/src/lib.rs b/crates/ra_prof/src/lib.rs index 4a49e9f95..d38ff397e 100644 --- a/crates/ra_prof/src/lib.rs +++ b/crates/ra_prof/src/lib.rs @@ -9,7 +9,6 @@ use std::{ collections::BTreeMap, collections::HashSet, io::{stderr, Write}, - mem, sync::{ atomic::{AtomicBool, Ordering}, RwLock, @@ -50,6 +49,8 @@ pub fn set_filter(f: Filter) { *old = filter_data; } +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. @@ -77,10 +78,10 @@ pub fn set_filter(f: Filter) { /// 0ms - profile /// 0ms - profile2 /// ``` -pub fn profile(desc: &str) -> Profiler { - assert!(!desc.is_empty()); +pub fn profile(label: Label) -> Profiler { + assert!(!label.is_empty()); if !PROFILING_ENABLED.load(Ordering::Relaxed) { - return Profiler { desc: None }; + return Profiler { label: None }; } PROFILE_STACK.with(|stack| { @@ -93,35 +94,35 @@ pub fn profile(desc: &str) -> Profiler { }; } if stack.starts.len() > stack.filter_data.depth { - return Profiler { desc: None }; + return Profiler { label: None }; } let allowed = &stack.filter_data.allowed; - if stack.starts.is_empty() && !allowed.is_empty() && !allowed.contains(desc) { - return Profiler { desc: None }; + if stack.starts.is_empty() && !allowed.is_empty() && !allowed.contains(label) { + return Profiler { label: None }; } stack.starts.push(Instant::now()); - Profiler { desc: Some(desc.to_string()) } + Profiler { label: Some(label) } }) } -pub fn print_time(desc: &str) -> impl Drop + '_ { - struct Guard<'a> { - desc: &'a str, +pub fn print_time(label: Label) -> impl Drop { + struct Guard { + label: Label, start: Instant, } - impl Drop for Guard<'_> { + impl Drop for Guard { fn drop(&mut self) { - eprintln!("{}: {:?}", self.desc, self.start.elapsed()) + eprintln!("{}: {:?}", self.label, self.start.elapsed()) } } - Guard { desc, start: Instant::now() } + Guard { label, start: Instant::now() } } pub struct Profiler { - desc: Option, + label: Option