Skip to content

Commit da5d8f2

Browse files
committed
analysis-stats: expose and print some limited statistics from hir-def
1 parent 1eb7646 commit da5d8f2

File tree

2 files changed

+90
-3
lines changed

2 files changed

+90
-3
lines changed

crates/hir-def/src/item_tree.rs

+25
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

+65-3
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

@@ -125,6 +126,9 @@ impl flags::AnalysisStats {
125126
let mut dep_item_trees = 0;
126127
let mut workspace_item_trees = 0;
127128

129+
let mut workspace_item_stats = PrettyItemStats::default();
130+
let mut dep_item_stats = PrettyItemStats::default();
131+
128132
for source_root_id in source_roots {
129133
let source_root = db.source_root(source_root_id).source_root(db);
130134
for file_id in source_root.iter() {
@@ -133,16 +137,24 @@ impl flags::AnalysisStats {
133137
// measure workspace/project code
134138
if !source_root.is_library || self.with_deps {
135139
let length = db.file_text(file_id).text(db).lines().count();
136-
db.file_item_tree(EditionedFileId::current_edition(file_id).into());
140+
let item_stats = db
141+
.file_item_tree(EditionedFileId::current_edition(file_id).into())
142+
.item_tree_stats()
143+
.into();
137144

138145
workspace_loc += length;
139146
workspace_item_trees += 1;
147+
workspace_item_stats += item_stats;
140148
} else {
141149
let length = db.file_text(file_id).text(db).lines().count();
142-
db.file_item_tree(EditionedFileId::current_edition(file_id).into());
150+
let item_stats = db
151+
.file_item_tree(EditionedFileId::current_edition(file_id).into())
152+
.item_tree_stats()
153+
.into();
143154

144155
dep_loc += length;
145-
dep_item_trees += 1
156+
dep_item_trees += 1;
157+
dep_item_stats += item_stats;
146158
}
147159
}
148160
}
@@ -157,11 +169,13 @@ impl flags::AnalysisStats {
157169
UsizeWithUnderscore(dep_loc),
158170
UsizeWithUnderscore(dep_item_trees),
159171
);
172+
eprintln!(" dependency item stats: {}", dep_item_stats);
160173
eprintln!(
161174
" workspace lines of code: {}, item trees: {}",
162175
UsizeWithUnderscore(workspace_loc),
163176
UsizeWithUnderscore(workspace_item_trees),
164177
);
178+
eprintln!(" workspace stats: {}", workspace_item_stats);
165179

166180
// FIXME(salsa-transition): bring back stats for ParseQuery (file size)
167181
// and ParseMacroExpansionQuery (macro expansion "file") size whenever we implement
@@ -1251,6 +1265,7 @@ fn percentage(n: u64, total: u64) -> u64 {
12511265
(n * 100).checked_div(total).unwrap_or(100)
12521266
}
12531267

1268+
#[derive(Default, Debug, Eq, PartialEq)]
12541269
struct UsizeWithUnderscore(usize);
12551270

12561271
impl fmt::Display for UsizeWithUnderscore {
@@ -1275,6 +1290,53 @@ impl fmt::Display for UsizeWithUnderscore {
12751290
}
12761291
}
12771292

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

0 commit comments

Comments
 (0)