Skip to content

Commit 667d75e

Browse files
committed
Add a cache for maybe_lint_level_root_bounded.
It's a nice speed win.
1 parent f234dc3 commit 667d75e

File tree

2 files changed

+47
-10
lines changed

2 files changed

+47
-10
lines changed

compiler/rustc_mir_build/src/build/mod.rs

+10
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ use rustc_hir as hir;
1010
use rustc_hir::def::DefKind;
1111
use rustc_hir::def_id::{DefId, LocalDefId};
1212
use rustc_hir::{GeneratorKind, Node};
13+
use rustc_index::bit_set::GrowableBitSet;
1314
use rustc_index::{Idx, IndexSlice, IndexVec};
1415
use rustc_infer::infer::{InferCtxt, TyCtxtInferExt};
1516
use rustc_middle::hir::place::PlaceBase as HirPlaceBase;
@@ -215,6 +216,14 @@ struct Builder<'a, 'tcx> {
215216
unit_temp: Option<Place<'tcx>>,
216217

217218
var_debug_info: Vec<VarDebugInfo<'tcx>>,
219+
220+
// A cache for `maybe_lint_level_roots_bounded`. That function is called
221+
// repeatedly, and each time it effectively traces a path through a tree
222+
// structure from a node towards the root, doing an attribute check on each
223+
// node along the way. This cache records which nodes trace all the way to
224+
// the root (most of them do) and saves us from retracing many sub-paths
225+
// many times, and rechecking many nodes.
226+
lint_level_roots_cache: GrowableBitSet<hir::ItemLocalId>,
218227
}
219228

220229
type CaptureMap<'tcx> = SortedIndexMultiMap<usize, hir::HirId, Capture<'tcx>>;
@@ -725,6 +734,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
725734
var_indices: Default::default(),
726735
unit_temp: None,
727736
var_debug_info: vec![],
737+
lint_level_roots_cache: GrowableBitSet::new_empty(),
728738
};
729739

730740
assert_eq!(builder.cfg.start_new_block(), START_BLOCK);

compiler/rustc_mir_build/src/build/scope.rs

+37-10
Original file line numberDiff line numberDiff line change
@@ -769,12 +769,16 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
769769
// the MIR->HIR mapping.
770770
(current_id, parent_id)
771771
} else {
772-
// Use `maybe_lint_level_root_bounded` with `self.hir_id` as a bound
773-
// to avoid adding Hir dependencies on our parents.
774-
// We estimate the true lint roots here to avoid creating a lot of source scopes.
772+
// Use `maybe_lint_level_root_bounded` to avoid adding Hir dependencies on our
773+
// parents. We estimate the true lint roots here to avoid creating a lot of source
774+
// scopes.
775775
(
776-
self.maybe_lint_level_root_bounded(current_id, self.hir_id),
777-
self.maybe_lint_level_root_bounded(parent_id, self.hir_id),
776+
self.maybe_lint_level_root_bounded(current_id),
777+
if parent_id == self.hir_id {
778+
parent_id // this is very common
779+
} else {
780+
self.maybe_lint_level_root_bounded(parent_id)
781+
},
778782
)
779783
};
780784

@@ -784,16 +788,24 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
784788
}
785789
}
786790

787-
/// Walks upwards from `id` to find a node which might change lint levels with attributes.
788-
/// It stops at `bound` and just returns it if reached.
789-
fn maybe_lint_level_root_bounded(&self, mut id: HirId, bound: HirId) -> HirId {
791+
/// Walks upwards from `orig_id` to find a node which might change lint levels with attributes.
792+
/// It stops at `self.hir_id` and just returns it if reached.
793+
fn maybe_lint_level_root_bounded(&mut self, orig_id: HirId) -> HirId {
794+
// This assertion lets us just store `ItemLocalId` in the cache, rather
795+
// than the full `HirId`.
796+
assert_eq!(orig_id.owner, self.hir_id.owner);
797+
798+
let mut id = orig_id;
790799
let hir = self.tcx.hir();
791800
loop {
792-
if id == bound {
793-
return bound;
801+
if id == self.hir_id {
802+
// This is a moderately common case, mostly hit for previously unseen nodes.
803+
break;
794804
}
795805

796806
if hir.attrs(id).iter().any(|attr| Level::from_attr(attr).is_some()) {
807+
// This is a rare case. It's for a node path that doesn't reach the root due to an
808+
// intervening lint level attribute. This result doesn't get cached.
797809
return id;
798810
}
799811

@@ -802,7 +814,22 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
802814
bug!("lint traversal reached the root of the crate");
803815
}
804816
id = next;
817+
818+
// This lookup is just an optimization; it can be removed without affecting
819+
// functionality. It might seem strange to see this at the end of this loop, but the
820+
// `orig_id` passed in to this function is almost always previously unseen, for which a
821+
// lookup will be a miss. So we only do lookups for nodes up the parent chain, where
822+
// cache lookups have a very high hit rate.
823+
if self.lint_level_roots_cache.contains(id.local_id) {
824+
break;
825+
}
805826
}
827+
828+
// `orig_id` traced to `self_id`; record this fact. If `orig_id` is a leaf node it will
829+
// rarely (never?) subsequently be searched for, but it's hard to know if that is the case.
830+
// The performance wins from the cache all come from caching non-leaf nodes.
831+
self.lint_level_roots_cache.insert(orig_id.local_id);
832+
self.hir_id
806833
}
807834

808835
/// Creates a new source scope, nested in the current one.

0 commit comments

Comments
 (0)