Skip to content

Commit f36f177

Browse files
authored
Unrolled build for #156173
Rollup merge of #156173 - oli-obk:fewer-global-lookups, r=petrochenkov Fewer global node_id_to_def_id lookups Several of these are unnecessary if we track the `LocalDefId` together with the `NodeId`. We can't remove the `NodeId` entirely, as it is needed for lints, but it's a useful refactoring for splitting node_id_to_def_id into a per-owner table in the future r? @petrochenkov
2 parents 0e5924a + a082567 commit f36f177

6 files changed

Lines changed: 65 additions & 48 deletions

File tree

compiler/rustc_resolve/src/build_reduced_graph.rs

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -721,13 +721,15 @@ impl<'a, 'ra, 'tcx> DefCollector<'a, 'ra, 'tcx> {
721721
decls: Default::default(),
722722
nested,
723723
id,
724+
def_id: feed.def_id(),
724725
};
725726

726727
self.add_import(module_path, kind, use_tree.span(), item, root_span, item.id, vis);
727728
}
728729
ast::UseTreeKind::Glob(_) => {
729730
if !ast::attr::contains_name(&item.attrs, sym::prelude_import) {
730-
let kind = ImportKind::Glob { max_vis: CmCell::new(None), id };
731+
let kind =
732+
ImportKind::Glob { max_vis: CmCell::new(None), id, def_id: feed.def_id() };
731733
self.add_import(prefix, kind, use_tree.span(), item, root_span, item.id, vis);
732734
} else {
733735
// Resolve the prelude import early.
@@ -1019,7 +1021,12 @@ impl<'a, 'ra, 'tcx> DefCollector<'a, 'ra, 'tcx> {
10191021
})
10201022
.unwrap_or((true, None, self.r.dummy_decl));
10211023
let import = self.r.arenas.alloc_import(ImportData {
1022-
kind: ImportKind::ExternCrate { source: orig_name, target: orig_ident, id: item.id },
1024+
kind: ImportKind::ExternCrate {
1025+
source: orig_name,
1026+
target: orig_ident,
1027+
id: item.id,
1028+
def_id: local_def_id,
1029+
},
10231030
root_id: item.id,
10241031
parent_scope,
10251032
imported_module: CmCell::new(module),
@@ -1271,7 +1278,7 @@ impl<'a, 'ra, 'tcx> DefCollector<'a, 'ra, 'tcx> {
12711278
if !ident.as_str().starts_with('_') {
12721279
self.r.unused_macros.insert(def_id, (node_id, ident));
12731280
let nrules = self.r.local_macro_map[&def_id].nrules;
1274-
self.r.unused_macro_rules.insert(node_id, DenseBitSet::new_filled(nrules));
1281+
self.r.unused_macro_rules.insert(node_id, (def_id, DenseBitSet::new_filled(nrules)));
12751282
}
12761283
}
12771284

compiler/rustc_resolve/src/check_unused.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -431,8 +431,7 @@ impl Resolver<'_, '_> {
431431
}
432432
}
433433
}
434-
ImportKind::ExternCrate { id, .. } => {
435-
let def_id = self.local_def_id(id);
434+
ImportKind::ExternCrate { id, def_id, .. } => {
436435
if self.extern_crate_map.get(&def_id).is_none_or(|&cnum| {
437436
!tcx.is_compiler_builtins(cnum)
438437
&& !tcx.is_panic_runtime(cnum)

compiler/rustc_resolve/src/effective_visibilities.rs

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -45,10 +45,7 @@ impl Resolver<'_, '_> {
4545
fn private_vis_import(&self, decl: Decl<'_>) -> Visibility {
4646
let DeclKind::Import { import, .. } = decl.kind else { unreachable!() };
4747
Visibility::Restricted(
48-
import
49-
.id()
50-
.map(|id| self.nearest_normal_mod(self.local_def_id(id)))
51-
.unwrap_or(CRATE_DEF_ID),
48+
import.def_id().map(|id| self.nearest_normal_mod(id)).unwrap_or(CRATE_DEF_ID),
5249
)
5350
}
5451

@@ -96,8 +93,8 @@ impl<'a, 'ra, 'tcx> EffectiveVisibilitiesVisitor<'a, 'ra, 'tcx> {
9693
// is the maximum value among visibilities of declarations corresponding to that def id.
9794
for (decl, eff_vis) in visitor.import_effective_visibilities.iter() {
9895
let DeclKind::Import { import, .. } = decl.kind else { unreachable!() };
99-
if let Some(node_id) = import.id() {
100-
r.effective_visibilities.update_eff_vis(r.local_def_id(node_id), eff_vis, r.tcx)
96+
if let Some(def_id) = import.def_id() {
97+
r.effective_visibilities.update_eff_vis(def_id, eff_vis, r.tcx)
10198
}
10299
if decl.ambiguity.get().is_some() && eff_vis.is_public_at_level(Level::Reexported) {
103100
exported_ambiguities.insert(*decl);

compiler/rustc_resolve/src/imports.rs

Lines changed: 43 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ use rustc_hir::Attribute;
1616
use rustc_hir::attrs::AttributeKind;
1717
use rustc_hir::attrs::diagnostic::{CustomDiagnostic, Directive, FormatArgs};
1818
use rustc_hir::def::{self, DefKind, PartialRes};
19-
use rustc_hir::def_id::{DefId, LocalDefIdMap};
19+
use rustc_hir::def_id::{DefId, LocalDefId, LocalDefIdMap};
2020
use rustc_middle::metadata::{AmbigModChild, ModChild, Reexport};
2121
use rustc_middle::span_bug;
2222
use rustc_middle::ty::{TyCtxt, Visibility};
@@ -90,17 +90,20 @@ pub(crate) enum ImportKind<'ra> {
9090
/// If this is the import for `foo::bar::a`, we would have the ID of the `UseTree`
9191
/// for `a` in this field.
9292
id: NodeId,
93+
def_id: LocalDefId,
9394
},
9495
Glob {
9596
// The visibility of the greatest re-export.
9697
// n.b. `max_vis` is only used in `finalize_import` to check for re-export errors.
9798
max_vis: CmCell<Option<Visibility>>,
9899
id: NodeId,
100+
def_id: LocalDefId,
99101
},
100102
ExternCrate {
101103
source: Option<Symbol>,
102104
target: Ident,
103105
id: NodeId,
106+
def_id: LocalDefId,
104107
},
105108
MacroUse {
106109
/// A field has been added indicating whether it should be reported as a lint,
@@ -116,7 +119,7 @@ impl<'ra> std::fmt::Debug for ImportKind<'ra> {
116119
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
117120
use ImportKind::*;
118121
match self {
119-
Single { source, target, decls, nested, id, .. } => f
122+
Single { source, target, decls, nested, id, def_id } => f
120123
.debug_struct("Single")
121124
.field("source", source)
122125
.field("target", target)
@@ -127,15 +130,20 @@ impl<'ra> std::fmt::Debug for ImportKind<'ra> {
127130
)
128131
.field("nested", nested)
129132
.field("id", id)
133+
.field("def_id", def_id)
130134
.finish(),
131-
Glob { max_vis, id } => {
132-
f.debug_struct("Glob").field("max_vis", max_vis).field("id", id).finish()
133-
}
134-
ExternCrate { source, target, id } => f
135+
Glob { max_vis, id, def_id } => f
136+
.debug_struct("Glob")
137+
.field("max_vis", max_vis)
138+
.field("id", id)
139+
.field("def_id", def_id)
140+
.finish(),
141+
ExternCrate { source, target, id, def_id } => f
135142
.debug_struct("ExternCrate")
136143
.field("source", source)
137144
.field("target", target)
138145
.field("id", id)
146+
.field("def_id", def_id)
139147
.finish(),
140148
MacroUse { warn_private } => {
141149
f.debug_struct("MacroUse").field("warn_private", warn_private).finish()
@@ -260,12 +268,20 @@ impl<'ra> ImportData<'ra> {
260268
}
261269
}
262270

263-
pub(crate) fn simplify(&self, r: &Resolver<'_, '_>) -> Reexport {
264-
let to_def_id = |id| r.local_def_id(id).to_def_id();
271+
pub(crate) fn def_id(&self) -> Option<LocalDefId> {
272+
match self.kind {
273+
ImportKind::Single { def_id, .. }
274+
| ImportKind::Glob { def_id, .. }
275+
| ImportKind::ExternCrate { def_id, .. } => Some(def_id),
276+
ImportKind::MacroUse { .. } | ImportKind::MacroExport => None,
277+
}
278+
}
279+
280+
pub(crate) fn simplify(&self) -> Reexport {
265281
match self.kind {
266-
ImportKind::Single { id, .. } => Reexport::Single(to_def_id(id)),
267-
ImportKind::Glob { id, .. } => Reexport::Glob(to_def_id(id)),
268-
ImportKind::ExternCrate { id, .. } => Reexport::ExternCrate(to_def_id(id)),
282+
ImportKind::Single { def_id, .. } => Reexport::Single(def_id.to_def_id()),
283+
ImportKind::Glob { def_id, .. } => Reexport::Glob(def_id.to_def_id()),
284+
ImportKind::ExternCrate { def_id, .. } => Reexport::ExternCrate(def_id.to_def_id()),
269285
ImportKind::MacroUse { .. } => Reexport::MacroUse,
270286
ImportKind::MacroExport => Reexport::MacroExport,
271287
}
@@ -340,13 +356,16 @@ struct UnresolvedImportError {
340356

341357
// Reexports of the form `pub use foo as bar;` where `foo` is `extern crate foo;`
342358
// are permitted for backward-compatibility under a deprecation lint.
343-
fn pub_use_of_private_extern_crate_hack(import: ImportSummary, decl: Decl<'_>) -> Option<NodeId> {
359+
fn pub_use_of_private_extern_crate_hack(
360+
import: ImportSummary,
361+
decl: Decl<'_>,
362+
) -> Option<LocalDefId> {
344363
match (import.is_single, decl.kind) {
345364
(true, DeclKind::Import { import: decl_import, .. })
346-
if let ImportKind::ExternCrate { id, .. } = decl_import.kind
365+
if let ImportKind::ExternCrate { def_id, .. } = decl_import.kind
347366
&& import.vis.is_public() =>
348367
{
349-
Some(id)
368+
Some(def_id)
350369
}
351370
_ => None,
352371
}
@@ -845,8 +864,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
845864
if binding.res() != Res::Err
846865
&& glob_decl.res() != Res::Err
847866
&& let DeclKind::Import { import: glob_import, .. } = glob_decl.kind
848-
&& let Some(glob_import_id) = glob_import.id()
849-
&& let glob_import_def_id = self.local_def_id(glob_import_id)
867+
&& let Some(glob_import_def_id) = glob_import.def_id()
850868
&& self.effective_visibilities.is_exported(glob_import_def_id)
851869
&& glob_decl.vis().is_public()
852870
&& !binding.vis().is_public()
@@ -875,7 +893,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
875893

876894
if let DeclKind::Import { import, .. } = binding.kind
877895
&& let Some(binding_id) = import.id()
878-
&& let import_def_id = self.local_def_id(binding_id)
896+
&& let import_def_id = import.def_id().unwrap()
879897
&& self.effective_visibilities.is_exported(import_def_id)
880898
&& let Res::Def(reexported_kind, reexported_def_id) = binding.res()
881899
&& !matches!(reexported_kind, DefKind::Ctor(..))
@@ -1267,7 +1285,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
12671285

12681286
let (ident, target, bindings, import_id) = match import.kind {
12691287
ImportKind::Single { source, target, ref decls, id, .. } => (source, target, decls, id),
1270-
ImportKind::Glob { ref max_vis, id } => {
1288+
ImportKind::Glob { ref max_vis, id, def_id } => {
12711289
if import.module_path.len() <= 1 {
12721290
// HACK(eddyb) `lint_if_path_starts_with_module` needs at least
12731291
// 2 segments, so the `resolve_path` above won't trigger it.
@@ -1294,7 +1312,6 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
12941312
if let Some(max_vis) = max_vis.get()
12951313
&& import.vis.greater_than(max_vis, self.tcx)
12961314
{
1297-
let def_id = self.local_def_id(id);
12981315
self.lint_buffer.buffer_lint(
12991316
UNUSED_IMPORTS,
13001317
id,
@@ -1614,7 +1631,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
16141631
if let Some(extern_crate_id) = pub_use_of_private_extern_crate_hack(import.summary(), decl)
16151632
{
16161633
let ImportKind::Single { id, .. } = import.kind else { unreachable!() };
1617-
let sugg = self.tcx.source_span(self.local_def_id(extern_crate_id)).shrink_to_lo();
1634+
let sugg = self.tcx.source_span(extern_crate_id).shrink_to_lo();
16181635
let diagnostic = crate::errors::PrivateExternCrateReexport { ident, sugg };
16191636
return Some(BufferedEarlyLint {
16201637
lint_id: LintId::of(PUB_USE_OF_PRIVATE_EXTERN_CRATE),
@@ -1656,7 +1673,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
16561673

16571674
pub(crate) fn check_for_redundant_imports(&mut self, import: Import<'ra>) -> bool {
16581675
// This function is only called for single imports.
1659-
let ImportKind::Single { source, target, ref decls, id, .. } = import.kind else {
1676+
let ImportKind::Single { source, target, ref decls, id, def_id, .. } = import.kind else {
16601677
unreachable!()
16611678
};
16621679

@@ -1675,7 +1692,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
16751692
// Skip if the import is public or was used through non scope-based resolution,
16761693
// e.g. through a module-relative path.
16771694
if self.import_use_map.get(&import) == Some(&Used::Other)
1678-
|| self.effective_visibilities.is_exported(self.local_def_id(id))
1695+
|| self.effective_visibilities.is_exported(def_id)
16791696
{
16801697
return false;
16811698
}
@@ -1829,23 +1846,23 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
18291846
let mut children = Vec::new();
18301847
let mut ambig_children = Vec::new();
18311848

1832-
module.to_module().for_each_child(self, |this, ident, orig_ident_span, _, binding| {
1849+
module.to_module().for_each_child(self, |_this, ident, orig_ident_span, _, binding| {
18331850
let res = binding.res().expect_non_local();
18341851
if res != def::Res::Err {
18351852
let ident = ident.orig(orig_ident_span);
18361853
let child =
18371854
|reexport_chain| ModChild { ident, res, vis: binding.vis(), reexport_chain };
18381855
if let Some((ambig_binding1, ambig_binding2)) = binding.descent_to_ambiguity() {
1839-
let main = child(ambig_binding1.reexport_chain(this));
1856+
let main = child(ambig_binding1.reexport_chain());
18401857
let second = ModChild {
18411858
ident,
18421859
res: ambig_binding2.res().expect_non_local(),
18431860
vis: ambig_binding2.vis(),
1844-
reexport_chain: ambig_binding2.reexport_chain(this),
1861+
reexport_chain: ambig_binding2.reexport_chain(),
18451862
};
18461863
ambig_children.push(AmbigModChild { main, second })
18471864
} else {
1848-
children.push(child(binding.reexport_chain(this)));
1865+
children.push(child(binding.reexport_chain()));
18491866
}
18501867
}
18511868
});

compiler/rustc_resolve/src/lib.rs

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1171,11 +1171,11 @@ impl<'ra> DeclData<'ra> {
11711171
self.res().macro_kinds()
11721172
}
11731173

1174-
fn reexport_chain(self: Decl<'ra>, r: &Resolver<'_, '_>) -> SmallVec<[Reexport; 2]> {
1174+
fn reexport_chain(self: Decl<'ra>) -> SmallVec<[Reexport; 2]> {
11751175
let mut reexport_chain = SmallVec::new();
11761176
let mut next_binding = self;
11771177
while let DeclKind::Import { source_decl, import, .. } = next_binding.kind {
1178-
reexport_chain.push(import.simplify(r));
1178+
reexport_chain.push(import.simplify());
11791179
next_binding = source_decl;
11801180
}
11811181
reexport_chain
@@ -1405,8 +1405,8 @@ pub struct Resolver<'ra, 'tcx> {
14051405
local_macro_def_scopes: FxHashMap<LocalDefId, LocalModule<'ra>> = default::fx_hash_map(),
14061406
ast_transform_scopes: FxHashMap<LocalExpnId, LocalModule<'ra>> = default::fx_hash_map(),
14071407
unused_macros: FxIndexMap<LocalDefId, (NodeId, Ident)>,
1408-
/// A map from the macro to all its potentially unused arms.
1409-
unused_macro_rules: FxIndexMap<NodeId, DenseBitSet<usize>>,
1408+
/// A map from the macro to all its potentially unused arms and the `LocalDefId` of the macro itself.
1409+
unused_macro_rules: FxIndexMap<NodeId, (LocalDefId, DenseBitSet<usize>)>,
14101410
proc_macro_stubs: FxHashSet<LocalDefId> = default::fx_hash_set(),
14111411
/// Traces collected during macro resolution and validated when it's complete.
14121412
single_segment_macro_resolutions:
@@ -2154,8 +2154,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
21542154
) -> &'tcx [LocalDefId] {
21552155
let mut import_ids: SmallVec<[LocalDefId; 1]> = smallvec![];
21562156
while let DeclKind::Import { import, source_decl, .. } = kind {
2157-
if let Some(node_id) = import.id() {
2158-
let def_id = self.local_def_id(node_id);
2157+
if let Some(def_id) = import.def_id() {
21592158
self.maybe_unused_trait_imports.insert(def_id);
21602159
import_ids.push(def_id);
21612160
}
@@ -2291,8 +2290,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
22912290

22922291
#[inline]
22932292
fn add_to_glob_map(&mut self, import: Import<'_>, name: Symbol) {
2294-
if let ImportKind::Glob { id, .. } = import.kind {
2295-
let def_id = self.local_def_id(id);
2293+
if let ImportKind::Glob { def_id, .. } = import.kind {
22962294
self.glob_map.entry(def_id).or_default().insert(name);
22972295
}
22982296
}

compiler/rustc_resolve/src/macros.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -339,7 +339,7 @@ impl<'ra, 'tcx> ResolverExpand for Resolver<'ra, 'tcx> {
339339
}
340340

341341
fn record_macro_rule_usage(&mut self, id: NodeId, rule_i: usize) {
342-
if let Some(rules) = self.unused_macro_rules.get_mut(&id) {
342+
if let Some((_, rules)) = self.unused_macro_rules.get_mut(&id) {
343343
rules.remove(rule_i);
344344
}
345345
}
@@ -356,11 +356,10 @@ impl<'ra, 'tcx> ResolverExpand for Resolver<'ra, 'tcx> {
356356
self.unused_macro_rules.swap_remove(&node_id);
357357
}
358358

359-
for (&node_id, unused_arms) in self.unused_macro_rules.iter() {
359+
for (&node_id, (def_id, unused_arms)) in self.unused_macro_rules.iter() {
360360
if unused_arms.is_empty() {
361361
continue;
362362
}
363-
let def_id = self.local_def_id(node_id);
364363
let m = &self.local_macro_map[&def_id];
365364
let SyntaxExtensionKind::MacroRules(ref m) = m.ext.kind else {
366365
continue;

0 commit comments

Comments
 (0)