Skip to content

Commit 6d616a4

Browse files
committed
Auto merge of rust-lang#15204 - Veykril:analysis-stats-ide, r=Veykril
internal: Add analysis-stats flag to trigger some IDE features Closes rust-lang/rust-analyzer#15131 Running this on r-a showed an 86mb memory increase, but that was without running it on the deps, will try that later when I don't need to use my pc.
2 parents 82ef699 + e520278 commit 6d616a4

File tree

5 files changed

+95
-3
lines changed

5 files changed

+95
-3
lines changed

crates/ide-diagnostics/src/lib.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,8 @@ impl Default for ExprFillDefaultMode {
155155

156156
#[derive(Debug, Clone)]
157157
pub struct DiagnosticsConfig {
158+
/// Whether native diagnostics are enabled.
159+
pub enabled: bool,
158160
pub proc_macros_enabled: bool,
159161
pub proc_attr_macros_enabled: bool,
160162
pub disable_experimental: bool,
@@ -171,6 +173,7 @@ impl DiagnosticsConfig {
171173
use ide_db::imports::insert_use::ImportGranularity;
172174

173175
Self {
176+
enabled: true,
174177
proc_macros_enabled: Default::default(),
175178
proc_attr_macros_enabled: Default::default(),
176179
disable_experimental: Default::default(),

crates/ide/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -642,7 +642,7 @@ impl Analysis {
642642
};
643643

644644
self.with_db(|db| {
645-
let diagnostic_assists = if include_fixes {
645+
let diagnostic_assists = if diagnostics_config.enabled && include_fixes {
646646
ide_diagnostics::diagnostics(db, diagnostics_config, &resolve, frange.file_id)
647647
.into_iter()
648648
.flat_map(|it| it.fixes.unwrap_or_default())

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

Lines changed: 85 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ use hir_def::{
1515
hir::{ExprId, PatId},
1616
};
1717
use hir_ty::{Interner, Substitution, TyExt, TypeFlags};
18-
use ide::{LineCol, RootDatabase};
18+
use ide::{Analysis, AnnotationConfig, DiagnosticsConfig, InlayHintsConfig, LineCol, RootDatabase};
1919
use ide_db::{
2020
base_db::{
2121
salsa::{self, debug::DebugQueryTable, ParallelDatabase},
@@ -30,7 +30,7 @@ use project_model::{CargoConfig, ProjectManifest, ProjectWorkspace, RustLibSourc
3030
use rayon::prelude::*;
3131
use rustc_hash::FxHashSet;
3232
use syntax::{AstNode, SyntaxNode};
33-
use vfs::{AbsPathBuf, Vfs, VfsPath};
33+
use vfs::{AbsPathBuf, FileId, Vfs, VfsPath};
3434

3535
use crate::cli::{
3636
flags::{self, OutputFormat},
@@ -149,8 +149,10 @@ impl flags::AnalysisStats {
149149
let mut bodies = Vec::new();
150150
let mut adts = Vec::new();
151151
let mut consts = Vec::new();
152+
let mut file_ids = Vec::new();
152153
while let Some(module) = visit_queue.pop() {
153154
if visited_modules.insert(module) {
155+
file_ids.extend(module.as_source_file_id(db));
154156
visit_queue.extend(module.children(db));
155157

156158
for decl in module.declarations(db) {
@@ -224,6 +226,10 @@ impl flags::AnalysisStats {
224226
self.run_const_eval(db, &consts, verbosity);
225227
}
226228

229+
if self.run_all_ide_things {
230+
self.run_ide_things(host.analysis(), file_ids);
231+
}
232+
227233
let total_span = analysis_sw.elapsed();
228234
eprintln!("{:<20} {total_span}", "Total:");
229235
report_metric("total time", total_span.time.as_millis() as u64, "ms");
@@ -729,6 +735,83 @@ impl flags::AnalysisStats {
729735
report_metric("body lowering time", body_lowering_time.time.as_millis() as u64, "ms");
730736
}
731737

738+
fn run_ide_things(&self, analysis: Analysis, mut file_ids: Vec<FileId>) {
739+
file_ids.sort();
740+
file_ids.dedup();
741+
let mut sw = self.stop_watch();
742+
743+
for &file_id in &file_ids {
744+
_ = analysis.diagnostics(
745+
&DiagnosticsConfig {
746+
enabled: true,
747+
proc_macros_enabled: true,
748+
proc_attr_macros_enabled: true,
749+
disable_experimental: false,
750+
disabled: Default::default(),
751+
expr_fill_default: Default::default(),
752+
insert_use: ide_db::imports::insert_use::InsertUseConfig {
753+
granularity: ide_db::imports::insert_use::ImportGranularity::Crate,
754+
enforce_granularity: true,
755+
prefix_kind: hir::PrefixKind::ByCrate,
756+
group: true,
757+
skip_glob_imports: true,
758+
},
759+
prefer_no_std: Default::default(),
760+
},
761+
ide::AssistResolveStrategy::All,
762+
file_id,
763+
);
764+
}
765+
for &file_id in &file_ids {
766+
_ = analysis.inlay_hints(
767+
&InlayHintsConfig {
768+
render_colons: false,
769+
type_hints: true,
770+
discriminant_hints: ide::DiscriminantHints::Always,
771+
parameter_hints: true,
772+
chaining_hints: true,
773+
adjustment_hints: ide::AdjustmentHints::Always,
774+
adjustment_hints_mode: ide::AdjustmentHintsMode::Postfix,
775+
adjustment_hints_hide_outside_unsafe: false,
776+
closure_return_type_hints: ide::ClosureReturnTypeHints::Always,
777+
closure_capture_hints: true,
778+
binding_mode_hints: true,
779+
lifetime_elision_hints: ide::LifetimeElisionHints::Always,
780+
param_names_for_lifetime_elision_hints: true,
781+
hide_named_constructor_hints: false,
782+
hide_closure_initialization_hints: false,
783+
closure_style: hir::ClosureStyle::ImplFn,
784+
max_length: Some(25),
785+
closing_brace_hints_min_lines: Some(20),
786+
},
787+
file_id,
788+
None,
789+
);
790+
}
791+
for &file_id in &file_ids {
792+
analysis
793+
.annotations(
794+
&AnnotationConfig {
795+
binary_target: true,
796+
annotate_runnables: true,
797+
annotate_impls: true,
798+
annotate_references: false,
799+
annotate_method_references: false,
800+
annotate_enum_variant_references: false,
801+
location: ide::AnnotationLocation::AboveName,
802+
},
803+
file_id,
804+
)
805+
.unwrap()
806+
.into_iter()
807+
.for_each(|annotation| {
808+
_ = analysis.resolve_annotation(annotation);
809+
});
810+
}
811+
let ide_time = sw.elapsed();
812+
eprintln!("{:<20} {} ({} files)", "IDE:", ide_time, file_ids.len());
813+
}
814+
732815
fn stop_watch(&self) -> StopWatch {
733816
StopWatch::start().memory(self.memory_usage)
734817
}

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,10 @@ xflags::xflags! {
8888
optional --skip-data-layout
8989
/// Skip const evaluation
9090
optional --skip-const-eval
91+
/// Runs several IDE features after analysis, including semantics highlighting, diagnostics
92+
/// and annotations. This is useful for benchmarking the memory usage on a project that has
93+
/// been worked on for a bit in a longer running session.
94+
optional --run-all-ide-things
9195
}
9296

9397
/// Run unit tests of the project using mir interpreter
@@ -199,6 +203,7 @@ pub struct AnalysisStats {
199203
pub skip_mir_stats: bool,
200204
pub skip_data_layout: bool,
201205
pub skip_const_eval: bool,
206+
pub run_all_ide_things: bool,
202207
}
203208

204209
#[derive(Debug)]

crates/rust-analyzer/src/config.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1079,6 +1079,7 @@ impl Config {
10791079

10801080
pub fn diagnostics(&self) -> DiagnosticsConfig {
10811081
DiagnosticsConfig {
1082+
enabled: self.data.diagnostics_enable,
10821083
proc_attr_macros_enabled: self.expand_proc_attr_macros(),
10831084
proc_macros_enabled: self.data.procMacro_enable,
10841085
disable_experimental: !self.data.diagnostics_experimental_enable,

0 commit comments

Comments
 (0)