Skip to content

Commit 6af7291

Browse files
committed
analysis-stats: emit lines of code and item tree counts for workspace; dependencies
1 parent 3ed13b4 commit 6af7291

File tree

1 file changed

+62
-25
lines changed

1 file changed

+62
-25
lines changed

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

Lines changed: 62 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -118,27 +118,67 @@ impl flags::AnalysisStats {
118118
}
119119

120120
let mut item_tree_sw = self.stop_watch();
121-
let mut num_item_trees = 0;
122121
let source_roots = krates
123122
.iter()
124123
.cloned()
125124
.map(|krate| db.file_source_root(krate.root_file(db)).source_root_id(db))
126125
.unique();
126+
127+
let mut dep_loc = 0;
128+
let mut workspace_loc = 0;
129+
let mut deps_item_trees = 0;
130+
let mut workspace_item_trees = 0;
131+
127132
for source_root_id in source_roots {
128133
let source_root = db.source_root(source_root_id).source_root(db);
129-
if !source_root.is_library || self.with_deps {
130-
for file_id in source_root.iter() {
131-
if let Some(p) = source_root.path_for_file(&file_id) {
132-
if let Some((_, Some("rs"))) = p.name_and_extension() {
134+
for file_id in source_root.iter() {
135+
if let Some(p) = source_root.path_for_file(&file_id) {
136+
if let Some((_, Some("rs"))) = p.name_and_extension() {
137+
// measure workspace/project code
138+
if !source_root.is_library || self.with_deps {
139+
let length = db.file_text(file_id).text(db).lines().count();
133140
db.file_item_tree(EditionedFileId::current_edition(file_id).into());
134-
num_item_trees += 1;
141+
142+
workspace_loc += length;
143+
workspace_item_trees += 1;
144+
} else if self.source_stats {
145+
let length = db.file_text(file_id).text(db).lines().count();
146+
db.file_item_tree(EditionedFileId::current_edition(file_id).into());
147+
148+
dep_loc += length;
149+
deps_item_trees += 1
135150
}
136151
}
137152
}
138153
}
139154
}
140-
eprintln!(" item trees: {num_item_trees}");
155+
eprintln!(" item trees: {workspace_item_trees}");
141156
let item_tree_time = item_tree_sw.elapsed();
157+
158+
if self.source_stats {
159+
eprintln!("Source stats:");
160+
eprintln!(" dependency lines of code: {dep_loc}, item trees: {deps_item_trees}");
161+
eprintln!(
162+
" workspace lines of code: {workspace_loc}, item trees: {workspace_item_trees}"
163+
);
164+
165+
// FIXME(salsa-transition): bring back stats for ParseQuery (file size)
166+
// and ParseMacroExpansionQuery (mcaro expansion "file") size whenever we implement
167+
// Salsa's memory usage tracking works with tracked functions.
168+
169+
// let mut total_file_size = Bytes::default();
170+
// for e in ide_db::base_db::ParseQuery.in_db(db).entries::<Vec<_>>() {
171+
// total_file_size += syntax_len(db.parse(e.key).syntax_node())
172+
// }
173+
174+
// let mut total_macro_file_size = Bytes::default();
175+
// for e in hir::db::ParseMacroExpansionQuery.in_db(db).entries::<Vec<_>>() {
176+
// let val = db.parse_macro_expansion(e.key).value.0;
177+
// total_macro_file_size += syntax_len(val.syntax_node())
178+
// }
179+
// eprintln!("source files: {total_file_size}, macro files: {total_macro_file_size}");
180+
}
181+
142182
eprintln!("{:<20} {}", "Item Tree Collection:", item_tree_time);
143183
report_metric("item tree time", item_tree_time.time.as_millis() as u64, "ms");
144184

@@ -168,6 +208,11 @@ impl flags::AnalysisStats {
168208
let mut bodies = Vec::new();
169209
let mut adts = Vec::new();
170210
let mut file_ids = Vec::new();
211+
212+
let mut num_traits = 0;
213+
let mut num_macro_rules_macros = 0;
214+
let mut num_proc_macros = 0;
215+
171216
while let Some(module) = visit_queue.pop() {
172217
if visited_modules.insert(module) {
173218
file_ids.extend(module.as_source_file_id(db));
@@ -189,6 +234,14 @@ impl flags::AnalysisStats {
189234
bodies.push(DefWithBody::from(c));
190235
}
191236
ModuleDef::Static(s) => bodies.push(DefWithBody::from(s)),
237+
ModuleDef::Trait(_) => num_traits += 1,
238+
ModuleDef::Macro(m) => match m.kind(db) {
239+
hir::MacroKind::Declarative => num_macro_rules_macros += 1,
240+
hir::MacroKind::Derive
241+
| hir::MacroKind::Attr
242+
| hir::MacroKind::ProcMacro => num_proc_macros += 1,
243+
_ => (),
244+
},
192245
_ => (),
193246
};
194247
}
@@ -217,6 +270,8 @@ impl flags::AnalysisStats {
217270
.filter(|it| matches!(it, DefWithBody::Const(_) | DefWithBody::Static(_)))
218271
.count(),
219272
);
273+
eprintln!(" traits: {num_traits}, macro_rules macros: {num_macro_rules_macros}, proc_macros: {num_proc_macros}");
274+
220275
let crate_def_map_time = crate_def_map_sw.elapsed();
221276
eprintln!("{:<20} {}", "Item Collection:", crate_def_map_time);
222277
report_metric("crate def map time", crate_def_map_time.time.as_millis() as u64, "ms");
@@ -264,24 +319,6 @@ impl flags::AnalysisStats {
264319
}
265320
report_metric("total memory", total_span.memory.allocated.megabytes() as u64, "MB");
266321

267-
if self.source_stats {
268-
// FIXME(salsa-transition): bring back stats for ParseQuery (file size)
269-
// and ParseMacroExpansionQuery (mcaro expansion "file") size whenever we implement
270-
// Salsa's memory usage tracking works with tracked functions.
271-
272-
// let mut total_file_size = Bytes::default();
273-
// for e in ide_db::base_db::ParseQuery.in_db(db).entries::<Vec<_>>() {
274-
// total_file_size += syntax_len(db.parse(e.key).syntax_node())
275-
// }
276-
277-
// let mut total_macro_file_size = Bytes::default();
278-
// for e in hir::db::ParseMacroExpansionQuery.in_db(db).entries::<Vec<_>>() {
279-
// let val = db.parse_macro_expansion(e.key).value.0;
280-
// total_macro_file_size += syntax_len(val.syntax_node())
281-
// }
282-
// eprintln!("source files: {total_file_size}, macro files: {total_macro_file_size}");
283-
}
284-
285322
if verbosity.is_verbose() {
286323
print_memory_usage(host, vfs);
287324
}

0 commit comments

Comments
 (0)