Skip to content

Commit 4602c2e

Browse files
author
Jonas Schievink
committed
analysis-stats: allow parallel type inference
1 parent 0954d31 commit 4602c2e

File tree

5 files changed

+43
-5
lines changed

5 files changed

+43
-5
lines changed

Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/rust-analyzer/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ rustc-hash = "1.1.0"
2828
serde = { version = "1.0.106", features = ["derive"] }
2929
serde_json = "1.0.48"
3030
threadpool = "1.7.1"
31+
rayon = "1.3.1"
3132

3233
stdx = { path = "../stdx" }
3334

crates/rust-analyzer/src/bin/args.rs

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ pub(crate) enum Command {
2525
},
2626
Stats {
2727
randomize: bool,
28+
parallel: bool,
2829
memory_usage: bool,
2930
only: Option<String>,
3031
with_deps: bool,
@@ -157,10 +158,14 @@ USAGE:
157158
rust-analyzer analysis-stats [FLAGS] [OPTIONS] [PATH]
158159
159160
FLAGS:
161+
-o, --only Only analyze items matching this path
160162
-h, --help Prints help information
161-
--memory-usage
163+
--memory-usage Collect memory usage statistics (requires `--feature jemalloc`)
164+
--randomize Randomize order in which crates, modules, and items are processed
165+
--parallel Run type inference in parallel
162166
--load-output-dirs Load OUT_DIR values by running `cargo check` before analysis
163-
--with-proc-macro Use ra-proc-macro-srv for proc-macro expanding
167+
--with-proc-macro Use ra-proc-macro-srv for proc-macro expanding
168+
--with-deps Also analyze all dependencies
164169
-v, --verbose
165170
-q, --quiet
166171
@@ -174,6 +179,7 @@ ARGS:
174179
}
175180

176181
let randomize = matches.contains("--randomize");
182+
let parallel = matches.contains("--parallel");
177183
let memory_usage = matches.contains("--memory-usage");
178184
let only: Option<String> = matches.opt_value_from_str(["-o", "--only"])?;
179185
let with_deps: bool = matches.contains("--with-deps");
@@ -189,6 +195,7 @@ ARGS:
189195

190196
Command::Stats {
191197
randomize,
198+
parallel,
192199
memory_usage,
193200
only,
194201
with_deps,
@@ -209,7 +216,7 @@ USAGE:
209216
FLAGS:
210217
-h, --help Prints help information
211218
--load-output-dirs Load OUT_DIR values by running `cargo check` before analysis
212-
--with-proc-macro Use ra-proc-macro-srv for proc-macro expanding
219+
--with-proc-macro Use ra-proc-macro-srv for proc-macro expanding
213220
-v, --verbose
214221
215222
OPTIONS:

crates/rust-analyzer/src/bin/main.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ fn main() -> Result<()> {
3232
args::Command::Highlight { rainbow } => cli::highlight(rainbow)?,
3333
args::Command::Stats {
3434
randomize,
35+
parallel,
3536
memory_usage,
3637
only,
3738
with_deps,
@@ -45,6 +46,7 @@ fn main() -> Result<()> {
4546
only.as_ref().map(String::as_ref),
4647
with_deps,
4748
randomize,
49+
parallel,
4850
load_output_dirs,
4951
with_proc_macro,
5052
)?,

crates/rust-analyzer/src/cli/analysis_stats.rs

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ use std::{path::Path, time::Instant};
55

66
use itertools::Itertools;
77
use rand::{seq::SliceRandom, thread_rng};
8+
use rayon::prelude::*;
89
use rustc_hash::FxHashSet;
910

1011
use hir::{
@@ -13,19 +14,31 @@ use hir::{
1314
};
1415
use hir_def::FunctionId;
1516
use hir_ty::{Ty, TypeWalk};
16-
use ra_db::SourceDatabaseExt;
17+
use ra_db::{
18+
salsa::{self, ParallelDatabase},
19+
SourceDatabaseExt,
20+
};
1721
use ra_syntax::AstNode;
1822
use stdx::format_to;
1923

2024
use crate::cli::{load_cargo::load_cargo, progress_report::ProgressReport, Result, Verbosity};
2125

26+
/// Need to wrap Snapshot to provide `Clone` impl for `map_with`
27+
struct Snap<DB>(DB);
28+
impl<DB: ParallelDatabase> Clone for Snap<salsa::Snapshot<DB>> {
29+
fn clone(&self) -> Snap<salsa::Snapshot<DB>> {
30+
Snap(self.0.snapshot())
31+
}
32+
}
33+
2234
pub fn analysis_stats(
2335
verbosity: Verbosity,
2436
memory_usage: bool,
2537
path: &Path,
2638
only: Option<&str>,
2739
with_deps: bool,
2840
randomize: bool,
41+
parallel: bool,
2942
load_output_dirs: bool,
3043
with_proc_macro: bool,
3144
) -> Result<()> {
@@ -91,12 +104,26 @@ pub fn analysis_stats(
91104
funcs.shuffle(&mut thread_rng());
92105
}
93106

94-
let inference_time = Instant::now();
95107
let mut bar = match verbosity {
96108
Verbosity::Quiet | Verbosity::Spammy => ProgressReport::hidden(),
97109
_ => ProgressReport::new(funcs.len() as u64),
98110
};
99111

112+
if parallel {
113+
let inference_time = Instant::now();
114+
let snap = Snap(db.snapshot());
115+
funcs
116+
.par_iter()
117+
.map_with(snap, |snap, &f| {
118+
let f_id = FunctionId::from(f);
119+
snap.0.body(f_id.into());
120+
snap.0.infer(f_id.into());
121+
})
122+
.count();
123+
println!("Parallel Inference: {:?}, {}", inference_time.elapsed(), ra_prof::memory_usage());
124+
}
125+
126+
let inference_time = Instant::now();
100127
bar.tick();
101128
let mut num_exprs = 0;
102129
let mut num_exprs_unknown = 0;

0 commit comments

Comments
 (0)