From 4602c2eeaaeaf997d0f665d6064c469af53687ca Mon Sep 17 00:00:00 2001 From: Jonas Schievink Date: Tue, 30 Jun 2020 17:00:17 +0200 Subject: analysis-stats: allow parallel type inference --- Cargo.lock | 1 + crates/rust-analyzer/Cargo.toml | 1 + crates/rust-analyzer/src/bin/args.rs | 13 ++++++++--- crates/rust-analyzer/src/bin/main.rs | 2 ++ crates/rust-analyzer/src/cli/analysis_stats.rs | 31 ++++++++++++++++++++++++-- 5 files changed, 43 insertions(+), 5 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index c10803645..eb03caa83 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1485,6 +1485,7 @@ dependencies = [ "ra_toolchain", "ra_tt", "rand", + "rayon", "rustc-hash", "serde", "serde_json", diff --git a/crates/rust-analyzer/Cargo.toml b/crates/rust-analyzer/Cargo.toml index 803755106..837b6714d 100644 --- a/crates/rust-analyzer/Cargo.toml +++ b/crates/rust-analyzer/Cargo.toml @@ -28,6 +28,7 @@ rustc-hash = "1.1.0" serde = { version = "1.0.106", features = ["derive"] } serde_json = "1.0.48" threadpool = "1.7.1" +rayon = "1.3.1" stdx = { path = "../stdx" } diff --git a/crates/rust-analyzer/src/bin/args.rs b/crates/rust-analyzer/src/bin/args.rs index cf1108e12..8a0b10117 100644 --- a/crates/rust-analyzer/src/bin/args.rs +++ b/crates/rust-analyzer/src/bin/args.rs @@ -25,6 +25,7 @@ pub(crate) enum Command { }, Stats { randomize: bool, + parallel: bool, memory_usage: bool, only: Option, with_deps: bool, @@ -157,10 +158,14 @@ USAGE: rust-analyzer analysis-stats [FLAGS] [OPTIONS] [PATH] FLAGS: + -o, --only Only analyze items matching this path -h, --help Prints help information - --memory-usage + --memory-usage Collect memory usage statistics (requires `--feature jemalloc`) + --randomize Randomize order in which crates, modules, and items are processed + --parallel Run type inference in parallel --load-output-dirs Load OUT_DIR values by running `cargo check` before analysis - --with-proc-macro Use ra-proc-macro-srv for proc-macro expanding + --with-proc-macro Use ra-proc-macro-srv for proc-macro expanding + --with-deps Also analyze all dependencies -v, --verbose -q, --quiet @@ -174,6 +179,7 @@ ARGS: } let randomize = matches.contains("--randomize"); + let parallel = matches.contains("--parallel"); let memory_usage = matches.contains("--memory-usage"); let only: Option = matches.opt_value_from_str(["-o", "--only"])?; let with_deps: bool = matches.contains("--with-deps"); @@ -189,6 +195,7 @@ ARGS: Command::Stats { randomize, + parallel, memory_usage, only, with_deps, @@ -209,7 +216,7 @@ USAGE: FLAGS: -h, --help Prints help information --load-output-dirs Load OUT_DIR values by running `cargo check` before analysis - --with-proc-macro Use ra-proc-macro-srv for proc-macro expanding + --with-proc-macro Use ra-proc-macro-srv for proc-macro expanding -v, --verbose OPTIONS: diff --git a/crates/rust-analyzer/src/bin/main.rs b/crates/rust-analyzer/src/bin/main.rs index 16882fc13..0f55c3ee2 100644 --- a/crates/rust-analyzer/src/bin/main.rs +++ b/crates/rust-analyzer/src/bin/main.rs @@ -32,6 +32,7 @@ fn main() -> Result<()> { args::Command::Highlight { rainbow } => cli::highlight(rainbow)?, args::Command::Stats { randomize, + parallel, memory_usage, only, with_deps, @@ -45,6 +46,7 @@ fn main() -> Result<()> { only.as_ref().map(String::as_ref), with_deps, randomize, + parallel, load_output_dirs, with_proc_macro, )?, diff --git a/crates/rust-analyzer/src/cli/analysis_stats.rs b/crates/rust-analyzer/src/cli/analysis_stats.rs index 9d09501cd..14982919d 100644 --- a/crates/rust-analyzer/src/cli/analysis_stats.rs +++ b/crates/rust-analyzer/src/cli/analysis_stats.rs @@ -5,6 +5,7 @@ use std::{path::Path, time::Instant}; use itertools::Itertools; use rand::{seq::SliceRandom, thread_rng}; +use rayon::prelude::*; use rustc_hash::FxHashSet; use hir::{ @@ -13,12 +14,23 @@ use hir::{ }; use hir_def::FunctionId; use hir_ty::{Ty, TypeWalk}; -use ra_db::SourceDatabaseExt; +use ra_db::{ + salsa::{self, ParallelDatabase}, + SourceDatabaseExt, +}; use ra_syntax::AstNode; use stdx::format_to; use crate::cli::{load_cargo::load_cargo, progress_report::ProgressReport, Result, Verbosity}; +/// Need to wrap Snapshot to provide `Clone` impl for `map_with` +struct Snap(DB); +impl Clone for Snap> { + fn clone(&self) -> Snap> { + Snap(self.0.snapshot()) + } +} + pub fn analysis_stats( verbosity: Verbosity, memory_usage: bool, @@ -26,6 +38,7 @@ pub fn analysis_stats( only: Option<&str>, with_deps: bool, randomize: bool, + parallel: bool, load_output_dirs: bool, with_proc_macro: bool, ) -> Result<()> { @@ -91,12 +104,26 @@ pub fn analysis_stats( funcs.shuffle(&mut thread_rng()); } - let inference_time = Instant::now(); let mut bar = match verbosity { Verbosity::Quiet | Verbosity::Spammy => ProgressReport::hidden(), _ => ProgressReport::new(funcs.len() as u64), }; + if parallel { + let inference_time = Instant::now(); + let snap = Snap(db.snapshot()); + funcs + .par_iter() + .map_with(snap, |snap, &f| { + let f_id = FunctionId::from(f); + snap.0.body(f_id.into()); + snap.0.infer(f_id.into()); + }) + .count(); + println!("Parallel Inference: {:?}, {}", inference_time.elapsed(), ra_prof::memory_usage()); + } + + let inference_time = Instant::now(); bar.tick(); let mut num_exprs = 0; let mut num_exprs_unknown = 0; -- cgit v1.2.3