From 5e25000763e97ef006dd05ac40198ae59e3f03c3 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Sat, 11 Jul 2020 03:39:44 +0200 Subject: [PATCH 1/2] Profiling example --- crates/ra_hir_ty/src/infer.rs | 5 +++++ crates/ra_ide/src/completion.rs | 1 + crates/ra_prof/Cargo.toml | 2 +- crates/ra_prof/src/lib.rs | 7 ++++++- 4 files changed, 13 insertions(+), 2 deletions(-) diff --git a/crates/ra_hir_ty/src/infer.rs b/crates/ra_hir_ty/src/infer.rs index 5c56c2eb09..d8888e050d 100644 --- a/crates/ra_hir_ty/src/infer.rs +++ b/crates/ra_hir_ty/src/infer.rs @@ -65,6 +65,11 @@ mod coerce; /// The entry point of type inference. pub(crate) fn infer_query(db: &dyn HirDatabase, def: DefWithBodyId) -> Arc { let _p = profile("infer_query"); + let _cpu_profieler; + if ra_prof::Scope::is_active() { + _cpu_profieler = ra_prof::cpu_profiler(); + } + let resolver = def.resolver(db.upcast()); let mut ctx = InferenceContext::new(db, def, resolver); diff --git a/crates/ra_ide/src/completion.rs b/crates/ra_ide/src/completion.rs index f3a5e95735..2a285718e2 100644 --- a/crates/ra_ide/src/completion.rs +++ b/crates/ra_ide/src/completion.rs @@ -106,6 +106,7 @@ pub(crate) fn completions( config: &CompletionConfig, position: FilePosition, ) -> Option { + let _s = ra_prof::Scope::enter(); let ctx = CompletionContext::new(db, position, config)?; let mut acc = Completions::default(); diff --git a/crates/ra_prof/Cargo.toml b/crates/ra_prof/Cargo.toml index eabfcebb07..db9b59ed64 100644 --- a/crates/ra_prof/Cargo.toml +++ b/crates/ra_prof/Cargo.toml @@ -24,4 +24,4 @@ cpu_profiler = [] # Uncomment to enable for the whole crate graph # default = [ "backtrace" ] # default = [ "jemalloc" ] -# default = [ "cpu_profiler" ] +default = [ "cpu_profiler" ] diff --git a/crates/ra_prof/src/lib.rs b/crates/ra_prof/src/lib.rs index 0fb4683755..7163a84243 100644 --- a/crates/ra_prof/src/lib.rs +++ b/crates/ra_prof/src/lib.rs @@ -66,7 +66,8 @@ impl Drop for Scope { /// 2. Build with `cpu_profiler` feature. /// 3. Tun the code, the *raw* output would be in the `./out.profile` file. /// 4. Install pprof for visualization (https://github.com/google/pprof). -/// 5. Use something like `pprof -svg target/release/rust-analyzer ./out.profile` to see the results. +/// 5. Bump sampling frequency to once per ms: `export CPUPROFILE_FREQUENCY=1000` +/// 6. Use something like `pprof -svg target/release/rust-analyzer ./out.profile` to see the results. /// /// For example, here's how I run profiling on NixOS: /// @@ -74,6 +75,10 @@ impl Drop for Scope { /// $ nix-shell -p gperftools --run \ /// 'cargo run --release -p rust-analyzer -- parse < ~/projects/rustbench/parser.rs > /dev/null' /// ``` +/// +/// See this diff for how to profile completions: +/// +/// https://github.com/rust-analyzer/rust-analyzer/pull/5306 #[derive(Debug)] pub struct CpuProfiler { _private: (), From 7f6e5de37c3c5a73865350cab1c976291be56fe2 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Sat, 11 Jul 2020 03:41:52 +0200 Subject: [PATCH 2/2] disable profiling --- crates/ra_hir_ty/src/infer.rs | 5 ----- crates/ra_ide/src/completion.rs | 1 - crates/ra_prof/Cargo.toml | 2 +- 3 files changed, 1 insertion(+), 7 deletions(-) diff --git a/crates/ra_hir_ty/src/infer.rs b/crates/ra_hir_ty/src/infer.rs index d8888e050d..5c56c2eb09 100644 --- a/crates/ra_hir_ty/src/infer.rs +++ b/crates/ra_hir_ty/src/infer.rs @@ -65,11 +65,6 @@ mod coerce; /// The entry point of type inference. pub(crate) fn infer_query(db: &dyn HirDatabase, def: DefWithBodyId) -> Arc { let _p = profile("infer_query"); - let _cpu_profieler; - if ra_prof::Scope::is_active() { - _cpu_profieler = ra_prof::cpu_profiler(); - } - let resolver = def.resolver(db.upcast()); let mut ctx = InferenceContext::new(db, def, resolver); diff --git a/crates/ra_ide/src/completion.rs b/crates/ra_ide/src/completion.rs index 2a285718e2..f3a5e95735 100644 --- a/crates/ra_ide/src/completion.rs +++ b/crates/ra_ide/src/completion.rs @@ -106,7 +106,6 @@ pub(crate) fn completions( config: &CompletionConfig, position: FilePosition, ) -> Option { - let _s = ra_prof::Scope::enter(); let ctx = CompletionContext::new(db, position, config)?; let mut acc = Completions::default(); diff --git a/crates/ra_prof/Cargo.toml b/crates/ra_prof/Cargo.toml index db9b59ed64..eabfcebb07 100644 --- a/crates/ra_prof/Cargo.toml +++ b/crates/ra_prof/Cargo.toml @@ -24,4 +24,4 @@ cpu_profiler = [] # Uncomment to enable for the whole crate graph # default = [ "backtrace" ] # default = [ "jemalloc" ] -default = [ "cpu_profiler" ] +# default = [ "cpu_profiler" ]