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_ide/src/change.rs | 2 ++ crates/ra_lsp_server/src/main_loop.rs | 1 - crates/ra_prof/src/lib.rs | 64 +++++++++++++++++------------------ 3 files changed, 34 insertions(+), 33 deletions(-) diff --git a/crates/ra_ide/src/change.rs b/crates/ra_ide/src/change.rs index 45a58690b..18dad2ea3 100644 --- a/crates/ra_ide/src/change.rs +++ b/crates/ra_ide/src/change.rs @@ -145,6 +145,8 @@ impl LibraryData { root_id: SourceRootId, files: Vec<(FileId, RelativePathBuf, Arc)>, ) -> LibraryData { + let _p = profile("LibraryData::prepare"); + #[cfg(not(feature = "wasm"))] let iter = files.par_iter(); #[cfg(feature = "wasm")] diff --git a/crates/ra_lsp_server/src/main_loop.rs b/crates/ra_lsp_server/src/main_loop.rs index d850ded37..508fe08c0 100644 --- a/crates/ra_lsp_server/src/main_loop.rs +++ b/crates/ra_lsp_server/src/main_loop.rs @@ -403,7 +403,6 @@ fn loop_turn( let sender = libdata_sender.clone(); pool.execute(move || { log::info!("indexing {:?} ... ", root); - let _p = profile(&format!("indexed {:?}", root)); let data = LibraryData::prepare(root, files); sender.send(data).unwrap(); }); 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