Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

Commit bc26e81

Browse files
committed
Auto merge of rust-lang#15070 - Veykril:analysis-stat-stuff, r=Veykril
internal: Report metric timings for file item trees and crate def map creation
2 parents 2d64a0a + a824b73 commit bc26e81

File tree

7 files changed

+67
-16
lines changed

7 files changed

+67
-16
lines changed

crates/hir-def/src/nameres.rs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ mod tests;
6060
use std::{cmp::Ord, ops::Deref};
6161

6262
use base_db::{CrateId, Edition, FileId, ProcMacroKind};
63-
use hir_expand::{name::Name, InFile, MacroCallId, MacroDefId};
63+
use hir_expand::{name::Name, HirFileId, InFile, MacroCallId, MacroDefId};
6464
use itertools::Itertools;
6565
use la_arena::Arena;
6666
use profile::Count;
@@ -626,6 +626,17 @@ impl ModuleData {
626626
self.origin.definition_source(db)
627627
}
628628

629+
/// Same as [`definition_source`] but only returns the file id to prevent parsing the ASt.
630+
pub fn definition_source_file_id(&self) -> HirFileId {
631+
match self.origin {
632+
ModuleOrigin::File { definition, .. } | ModuleOrigin::CrateRoot { definition } => {
633+
definition.into()
634+
}
635+
ModuleOrigin::Inline { definition, .. } => definition.file_id,
636+
ModuleOrigin::BlockExpr { block } => block.file_id,
637+
}
638+
}
639+
629640
/// Returns a node which declares this module, either a `mod foo;` or a `mod foo {}`.
630641
/// `None` for the crate root or block.
631642
pub fn declaration_source(&self, db: &dyn DefDatabase) -> Option<InFile<ast::Module>> {

crates/hir/src/has_source.rs

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
//! Provides set of implementation for hir's objects that allows get back location in file.
22
3+
use base_db::FileId;
34
use either::Either;
45
use hir_def::{
56
nameres::{ModuleOrigin, ModuleSource},
67
src::{HasChildSource, HasSource as _},
78
Lookup, MacroId, VariantId,
89
};
9-
use hir_expand::InFile;
10+
use hir_expand::{HirFileId, InFile};
1011
use syntax::ast;
1112

1213
use crate::{
@@ -32,6 +33,11 @@ impl Module {
3233
def_map[self.id.local_id].definition_source(db.upcast())
3334
}
3435

36+
pub fn definition_source_file_id(self, db: &dyn HirDatabase) -> HirFileId {
37+
let def_map = self.id.def_map(db.upcast());
38+
def_map[self.id.local_id].definition_source_file_id()
39+
}
40+
3541
pub fn is_mod_rs(self, db: &dyn HirDatabase) -> bool {
3642
let def_map = self.id.def_map(db.upcast());
3743
match def_map[self.id.local_id].origin {
@@ -40,6 +46,16 @@ impl Module {
4046
}
4147
}
4248

49+
pub fn as_source_file_id(self, db: &dyn HirDatabase) -> Option<FileId> {
50+
let def_map = self.id.def_map(db.upcast());
51+
match def_map[self.id.local_id].origin {
52+
ModuleOrigin::File { definition, .. } | ModuleOrigin::CrateRoot { definition, .. } => {
53+
Some(definition)
54+
}
55+
_ => None,
56+
}
57+
}
58+
4359
pub fn is_inline(self, db: &dyn HirDatabase) -> bool {
4460
let def_map = self.id.def_map(db.upcast());
4561
def_map[self.id.local_id].origin.is_inline()

crates/ide-completion/src/completions/mod_.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ pub(crate) fn complete_mod(
4242
}
4343

4444
let module_definition_file =
45-
current_module.definition_source(ctx.db).file_id.original_file(ctx.db);
45+
current_module.definition_source_file_id(ctx.db).original_file(ctx.db);
4646
let source_root = ctx.db.source_root(ctx.db.file_source_root(module_definition_file));
4747
let directory_to_look_for_submodules = directory_to_look_for_submodules(
4848
current_module,

crates/ide-db/src/search.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -149,10 +149,8 @@ impl SearchScope {
149149

150150
let mut to_visit: Vec<_> = module.children(db).collect();
151151
while let Some(module) = to_visit.pop() {
152-
if let InFile { file_id, value: ModuleSource::SourceFile(_) } =
153-
module.definition_source(db)
154-
{
155-
entries.insert(file_id.original_file(db), None);
152+
if let Some(file_id) = module.as_source_file_id(db) {
153+
entries.insert(file_id, None);
156154
}
157155
to_visit.extend(module.children(db));
158156
}

crates/ide/src/static_index.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,7 @@ impl StaticIndex<'_> {
187187
pub fn compute(analysis: &Analysis) -> StaticIndex<'_> {
188188
let db = &*analysis.db;
189189
let work = all_modules(db).into_iter().filter(|module| {
190-
let file_id = module.definition_source(db).file_id.original_file(db);
190+
let file_id = module.definition_source_file_id(db).original_file(db);
191191
let source_root = db.file_source_root(file_id);
192192
let source_root = db.source_root(source_root);
193193
!source_root.is_library
@@ -201,7 +201,7 @@ impl StaticIndex<'_> {
201201
};
202202
let mut visited_files = FxHashSet::default();
203203
for module in work {
204-
let file_id = module.definition_source(db).file_id.original_file(db);
204+
let file_id = module.definition_source_file_id(db).original_file(db);
205205
if visited_files.contains(&file_id) {
206206
continue;
207207
}

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

Lines changed: 31 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -95,17 +95,41 @@ impl flags::AnalysisStats {
9595
eprintln!(")");
9696

9797
let mut analysis_sw = self.stop_watch();
98-
let mut num_crates = 0;
99-
let mut visited_modules = FxHashSet::default();
100-
let mut visit_queue = Vec::new();
10198

10299
let mut krates = Crate::all(db);
103100
if self.randomize {
104101
shuffle(&mut rng, &mut krates);
105102
}
103+
104+
let mut item_tree_sw = self.stop_watch();
105+
let mut num_item_trees = 0;
106+
let source_roots =
107+
krates.iter().cloned().map(|krate| db.file_source_root(krate.root_file(db))).unique();
108+
for source_root_id in source_roots {
109+
let source_root = db.source_root(source_root_id);
110+
if !source_root.is_library || self.with_deps {
111+
for file_id in source_root.iter() {
112+
if let Some(p) = source_root.path_for_file(&file_id) {
113+
if let Some((_, Some("rs"))) = p.name_and_extension() {
114+
db.file_item_tree(file_id.into());
115+
num_item_trees += 1;
116+
}
117+
}
118+
}
119+
}
120+
}
121+
eprintln!(" item trees: {num_item_trees}");
122+
let item_tree_time = item_tree_sw.elapsed();
123+
eprintln!("{:<20} {}", "Item Tree Collection:", item_tree_time);
124+
report_metric("item tree time", item_tree_time.time.as_millis() as u64, "ms");
125+
126+
let mut crate_def_map_sw = self.stop_watch();
127+
let mut num_crates = 0;
128+
let mut visited_modules = FxHashSet::default();
129+
let mut visit_queue = Vec::new();
106130
for krate in krates {
107131
let module = krate.root_module(db);
108-
let file_id = module.definition_source(db).file_id;
132+
let file_id = module.definition_source_file_id(db);
109133
let file_id = file_id.original_file(db);
110134
let source_root = db.file_source_root(file_id);
111135
let source_root = db.source_root(source_root);
@@ -171,7 +195,9 @@ impl flags::AnalysisStats {
171195
adts.len(),
172196
consts.len(),
173197
);
174-
eprintln!("{:<20} {}", "Item Collection:", analysis_sw.elapsed());
198+
let crate_def_map_time = crate_def_map_sw.elapsed();
199+
eprintln!("{:<20} {}", "Item Collection:", crate_def_map_time);
200+
report_metric("crate def map time", crate_def_map_time.time.as_millis() as u64, "ms");
175201

176202
if self.randomize {
177203
shuffle(&mut rng, &mut bodies);

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,14 +37,14 @@ impl flags::Diagnostics {
3737
let mut visited_files = FxHashSet::default();
3838

3939
let work = all_modules(db).into_iter().filter(|module| {
40-
let file_id = module.definition_source(db).file_id.original_file(db);
40+
let file_id = module.definition_source_file_id(db).original_file(db);
4141
let source_root = db.file_source_root(file_id);
4242
let source_root = db.source_root(source_root);
4343
!source_root.is_library
4444
});
4545

4646
for module in work {
47-
let file_id = module.definition_source(db).file_id.original_file(db);
47+
let file_id = module.definition_source_file_id(db).original_file(db);
4848
if !visited_files.contains(&file_id) {
4949
let crate_name =
5050
module.krate().display_name(db).as_deref().unwrap_or("unknown").to_string();

0 commit comments

Comments
 (0)