Skip to content

Commit 0b31698

Browse files
committed
Fix cache hit stats
1 parent a82cc1f commit 0b31698

File tree

2 files changed

+14
-4
lines changed

2 files changed

+14
-4
lines changed

src/librustc/ty/query/plumbing.rs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ use std::hash::{Hash, Hasher};
2424
use std::mem;
2525
use std::num::NonZeroU32;
2626
use std::ptr;
27+
#[cfg(debug_assertions)]
28+
use std::sync::atomic::{AtomicUsize, Ordering};
2729

2830
pub(crate) struct QueryStateShard<'tcx, D: QueryAccessors<'tcx> + ?Sized> {
2931
pub(super) cache: <<D as QueryAccessors<'tcx>>::Cache as QueryCache<D::Key, D::Value>>::Sharded,
@@ -51,7 +53,7 @@ pub(crate) struct QueryState<'tcx, D: QueryAccessors<'tcx> + ?Sized> {
5153
pub(super) cache: D::Cache,
5254
pub(super) shards: Sharded<QueryStateShard<'tcx, D>>,
5355
#[cfg(debug_assertions)]
54-
pub(super) cache_hits: usize,
56+
pub(super) cache_hits: AtomicUsize,
5557
}
5658

5759
impl<'tcx, Q: QueryAccessors<'tcx>> QueryState<'tcx, Q> {
@@ -100,7 +102,7 @@ impl<'tcx, M: QueryAccessors<'tcx>> Default for QueryState<'tcx, M> {
100102
cache: M::Cache::default(),
101103
shards: Default::default(),
102104
#[cfg(debug_assertions)]
103-
cache_hits: 0,
105+
cache_hits: AtomicUsize::new(0),
104106
}
105107
}
106108
}
@@ -439,6 +441,10 @@ impl<'tcx> TyCtxt<'tcx> {
439441
if unlikely!(self.prof.enabled()) {
440442
self.prof.query_cache_hit(index.into());
441443
}
444+
#[cfg(debug_assertions)]
445+
{
446+
state.cache_hits.fetch_add(1, Ordering::Relaxed);
447+
}
442448
on_hit(value, index)
443449
},
444450
on_miss,

src/librustc/ty/query/stats.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ use rustc_hir::def_id::{DefId, LOCAL_CRATE};
66

77
use std::any::type_name;
88
use std::mem;
9+
#[cfg(debug_assertions)]
10+
use std::sync::atomic::Ordering;
911

1012
trait KeyStats {
1113
fn key_stats(&self, stats: &mut QueryStats);
@@ -42,7 +44,7 @@ fn stats<'tcx, Q: QueryAccessors<'tcx>>(
4244
let mut stats = QueryStats {
4345
name,
4446
#[cfg(debug_assertions)]
45-
cache_hits: map.cache_hits,
47+
cache_hits: map.cache_hits.load(Ordering::Relaxed),
4648
#[cfg(not(debug_assertions))]
4749
cache_hits: 0,
4850
key_size: mem::size_of::<Q::Key>(),
@@ -108,8 +110,10 @@ pub fn print_stats(tcx: TyCtxt<'_>) {
108110
queries.iter().filter(|q| q.local_def_id_keys.is_some()).collect();
109111
def_id_density.sort_by_key(|q| q.local_def_id_keys.unwrap());
110112
println!("\nLocal DefId density:");
113+
let total = tcx.hir().definitions().def_index_count() as f64;
111114
for q in def_id_density.iter().rev() {
112-
println!(" {} - {}", q.name, q.local_def_id_keys.unwrap());
115+
let local = q.local_def_id_keys.unwrap();
116+
println!(" {} - {} = ({}%)", q.name, local, (local as f64 * 100.0) / total);
113117
}
114118
}
115119

0 commit comments

Comments
 (0)