Profiling example

This commit is contained in:
Aleksey Kladov 2020-07-11 03:39:44 +02:00
parent e7ba7f47a7
commit 5e25000763
4 changed files with 13 additions and 2 deletions

View file

@ -65,6 +65,11 @@ mod coerce;
/// The entry point of type inference.
pub(crate) fn infer_query(db: &dyn HirDatabase, def: DefWithBodyId) -> Arc<InferenceResult> {
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);

View file

@ -106,6 +106,7 @@ pub(crate) fn completions(
config: &CompletionConfig,
position: FilePosition,
) -> Option<Completions> {
let _s = ra_prof::Scope::enter();
let ctx = CompletionContext::new(db, position, config)?;
let mut acc = Completions::default();

View file

@ -24,4 +24,4 @@ cpu_profiler = []
# Uncomment to enable for the whole crate graph
# default = [ "backtrace" ]
# default = [ "jemalloc" ]
# default = [ "cpu_profiler" ]
default = [ "cpu_profiler" ]

View file

@ -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: (),