Skip to content

Commit 7d050fe

Browse files
committed
analysis-stats: expose and print some limited statistics from hir-def
1 parent 288d783 commit 7d050fe

File tree

2 files changed

+90
-3
lines changed

2 files changed

+90
-3
lines changed

crates/hir-def/src/item_tree.rs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,22 @@ impl ItemTree {
218218
Attrs::filter(db, krate, self.raw_attrs(of).clone())
219219
}
220220

221+
/// Returns a count of a few, expensive items.
222+
///
223+
/// For more detail, see [`ItemTreeDataStats`].
224+
pub fn item_tree_stats(&self) -> ItemTreeDataStats {
225+
match self.data {
226+
Some(ref data) => ItemTreeDataStats {
227+
traits: data.traits.len(),
228+
impls: data.impls.len(),
229+
mods: data.mods.len(),
230+
macro_calls: data.macro_calls.len(),
231+
macro_rules: data.macro_rules.len(),
232+
},
233+
None => ItemTreeDataStats::default(),
234+
}
235+
}
236+
221237
pub fn pretty_print(&self, db: &dyn DefDatabase, edition: Edition) -> String {
222238
pretty::print_item_tree(db, self, edition)
223239
}
@@ -328,6 +344,15 @@ struct ItemTreeData {
328344
vis: ItemVisibilities,
329345
}
330346

347+
#[derive(Default, Debug, Eq, PartialEq)]
348+
pub struct ItemTreeDataStats {
349+
pub traits: usize,
350+
pub impls: usize,
351+
pub mods: usize,
352+
pub macro_calls: usize,
353+
pub macro_rules: usize,
354+
}
355+
331356
#[derive(Default, Debug, Eq, PartialEq)]
332357
pub struct ItemTreeSourceMaps {
333358
all_concatenated: Box<[TypesSourceMap]>,

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

Lines changed: 65 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
44
use std::{
55
env, fmt,
6+
ops::AddAssign,
67
time::{SystemTime, UNIX_EPOCH},
78
};
89

@@ -129,6 +130,9 @@ impl flags::AnalysisStats {
129130
let mut dep_item_trees = 0;
130131
let mut workspace_item_trees = 0;
131132

133+
let mut workspace_item_stats = PrettyItemStats::default();
134+
let mut dep_item_stats = PrettyItemStats::default();
135+
132136
for source_root_id in source_roots {
133137
let source_root = db.source_root(source_root_id).source_root(db);
134138
for file_id in source_root.iter() {
@@ -137,16 +141,24 @@ impl flags::AnalysisStats {
137141
// measure workspace/project code
138142
if !source_root.is_library || self.with_deps {
139143
let length = db.file_text(file_id).text(db).lines().count();
140-
db.file_item_tree(EditionedFileId::current_edition(file_id).into());
144+
let item_stats = db
145+
.file_item_tree(EditionedFileId::current_edition(file_id).into())
146+
.item_tree_stats()
147+
.into();
141148

142149
workspace_loc += length;
143150
workspace_item_trees += 1;
151+
workspace_item_stats += item_stats;
144152
} else {
145153
let length = db.file_text(file_id).text(db).lines().count();
146-
db.file_item_tree(EditionedFileId::current_edition(file_id).into());
154+
let item_stats = db
155+
.file_item_tree(EditionedFileId::current_edition(file_id).into())
156+
.item_tree_stats()
157+
.into();
147158

148159
dep_loc += length;
149-
dep_item_trees += 1
160+
dep_item_trees += 1;
161+
dep_item_stats += item_stats;
150162
}
151163
}
152164
}
@@ -161,11 +173,13 @@ impl flags::AnalysisStats {
161173
UsizeWithUnderscore(dep_loc),
162174
UsizeWithUnderscore(dep_item_trees),
163175
);
176+
eprintln!(" dependency item stats: {}", dep_item_stats);
164177
eprintln!(
165178
" workspace lines of code: {}, item trees: {}",
166179
UsizeWithUnderscore(workspace_loc),
167180
UsizeWithUnderscore(workspace_item_trees),
168181
);
182+
eprintln!(" workspace stats: {}", workspace_item_stats);
169183

170184
// FIXME(salsa-transition): bring back stats for ParseQuery (file size)
171185
// and ParseMacroExpansionQuery (macro expansion "file") size whenever we implement
@@ -1258,6 +1272,7 @@ fn percentage(n: u64, total: u64) -> u64 {
12581272
(n * 100).checked_div(total).unwrap_or(100)
12591273
}
12601274

1275+
#[derive(Default, Debug, Eq, PartialEq)]
12611276
struct UsizeWithUnderscore(usize);
12621277

12631278
impl fmt::Display for UsizeWithUnderscore {
@@ -1282,6 +1297,53 @@ impl fmt::Display for UsizeWithUnderscore {
12821297
}
12831298
}
12841299

1300+
impl std::ops::AddAssign for UsizeWithUnderscore {
1301+
fn add_assign(&mut self, other: UsizeWithUnderscore) {
1302+
self.0 += other.0;
1303+
}
1304+
}
1305+
1306+
#[derive(Default, Debug, Eq, PartialEq)]
1307+
struct PrettyItemStats {
1308+
traits: UsizeWithUnderscore,
1309+
impls: UsizeWithUnderscore,
1310+
mods: UsizeWithUnderscore,
1311+
macro_calls: UsizeWithUnderscore,
1312+
macro_rules: UsizeWithUnderscore,
1313+
}
1314+
1315+
impl From<hir_def::item_tree::ItemTreeDataStats> for PrettyItemStats {
1316+
fn from(value: hir_def::item_tree::ItemTreeDataStats) -> Self {
1317+
Self {
1318+
traits: UsizeWithUnderscore(value.traits),
1319+
impls: UsizeWithUnderscore(value.impls),
1320+
mods: UsizeWithUnderscore(value.mods),
1321+
macro_calls: UsizeWithUnderscore(value.macro_calls),
1322+
macro_rules: UsizeWithUnderscore(value.macro_rules),
1323+
}
1324+
}
1325+
}
1326+
1327+
impl AddAssign for PrettyItemStats {
1328+
fn add_assign(&mut self, rhs: Self) {
1329+
self.traits += rhs.traits;
1330+
self.impls += rhs.impls;
1331+
self.mods += rhs.mods;
1332+
self.macro_calls += rhs.macro_calls;
1333+
self.macro_rules += rhs.macro_rules;
1334+
}
1335+
}
1336+
1337+
impl fmt::Display for PrettyItemStats {
1338+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1339+
write!(
1340+
f,
1341+
"traits: {}, impl: {}, mods: {}, macro calls: {}, macro rules: {}",
1342+
self.traits, self.impls, self.mods, self.macro_calls, self.macro_rules
1343+
)
1344+
}
1345+
}
1346+
12851347
// FIXME(salsa-transition): bring this back whenever we implement
12861348
// Salsa's memory usage tracking to work with tracked functions.
12871349
// fn syntax_len(node: SyntaxNode) -> usize {

0 commit comments

Comments
 (0)