Skip to content

Commit a82cc1f

Browse files
committed
Add a stat for local DefId density
1 parent fb1fe32 commit a82cc1f

File tree

1 file changed

+35
-2
lines changed

1 file changed

+35
-2
lines changed

src/librustc/ty/query/stats.rs

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,27 @@ use crate::ty::query::config::QueryAccessors;
22
use crate::ty::query::plumbing::QueryState;
33
use crate::ty::query::queries;
44
use crate::ty::TyCtxt;
5+
use rustc_hir::def_id::{DefId, LOCAL_CRATE};
56

67
use std::any::type_name;
78
use std::mem;
89

10+
trait KeyStats {
11+
fn key_stats(&self, stats: &mut QueryStats);
12+
}
13+
14+
impl<T> KeyStats for T {
15+
default fn key_stats(&self, _: &mut QueryStats) {}
16+
}
17+
18+
impl KeyStats for DefId {
19+
fn key_stats(&self, stats: &mut QueryStats) {
20+
if self.krate == LOCAL_CRATE {
21+
stats.local_def_id_keys = Some(stats.local_def_id_keys.unwrap_or(0) + 1);
22+
}
23+
}
24+
}
25+
926
#[derive(Clone)]
1027
struct QueryStats {
1128
name: &'static str,
@@ -15,13 +32,14 @@ struct QueryStats {
1532
value_size: usize,
1633
value_type: &'static str,
1734
entry_count: usize,
35+
local_def_id_keys: Option<usize>,
1836
}
1937

2038
fn stats<'tcx, Q: QueryAccessors<'tcx>>(
2139
name: &'static str,
2240
map: &QueryState<'tcx, Q>,
2341
) -> QueryStats {
24-
QueryStats {
42+
let mut stats = QueryStats {
2543
name,
2644
#[cfg(debug_assertions)]
2745
cache_hits: map.cache_hits,
@@ -32,7 +50,14 @@ fn stats<'tcx, Q: QueryAccessors<'tcx>>(
3250
value_size: mem::size_of::<Q::Value>(),
3351
value_type: type_name::<Q::Value>(),
3452
entry_count: map.iter_results(|results| results.count()),
35-
}
53+
local_def_id_keys: None,
54+
};
55+
map.iter_results(|results| {
56+
for (key, _, _) in results {
57+
key.key_stats(&mut stats)
58+
}
59+
});
60+
stats
3661
}
3762

3863
pub fn print_stats(tcx: TyCtxt<'_>) {
@@ -78,6 +103,14 @@ pub fn print_stats(tcx: TyCtxt<'_>) {
78103
for q in query_value_count.iter().rev() {
79104
println!(" {} - {}", q.name, q.entry_count);
80105
}
106+
107+
let mut def_id_density: Vec<_> =
108+
queries.iter().filter(|q| q.local_def_id_keys.is_some()).collect();
109+
def_id_density.sort_by_key(|q| q.local_def_id_keys.unwrap());
110+
println!("\nLocal DefId density:");
111+
for q in def_id_density.iter().rev() {
112+
println!(" {} - {}", q.name, q.local_def_id_keys.unwrap());
113+
}
81114
}
82115

83116
macro_rules! print_stats {

0 commit comments

Comments
 (0)