Skip to content

Commit 52cc430

Browse files
authored
Rollup merge of rust-lang#110908 - petrochenkov:notagain4, r=compiler-errors
resolve: One more attempt to simplify `module_children` If the next step is performed and `fn module_children_local` is merged with the `module_children` query, then it causes perf regressions, regardless of whether query result feeding is [used](https://perf.rust-lang.org/compare.html?start=43a78029b4f4d92978b8fde0a677ea300b113c41&end=2eb5bcc5068b9d92f74bcb1797da664865d6981d&stat=instructions:u) or [not](https://perf.rust-lang.org/compare.html?start=2fce2290865f012391b8f3e581c3852a248031fa&end=2a33d6cd99481d1712037a79e7d66a8aefadbf72&stat=instructions:u).
2 parents 9a6ac2c + ef77dd2 commit 52cc430

File tree

10 files changed

+45
-53
lines changed

10 files changed

+45
-53
lines changed

compiler/rustc_metadata/src/rmeta/encoder.rs

+9-7
Original file line numberDiff line numberDiff line change
@@ -1364,9 +1364,9 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
13641364
record!(self.tables.params_in_repr[def_id] <- params_in_repr);
13651365

13661366
if adt_def.is_enum() {
1367-
let module_children = tcx.module_children_non_reexports(local_def_id);
1367+
let module_children = tcx.module_children_local(local_def_id);
13681368
record_array!(self.tables.module_children_non_reexports[def_id] <-
1369-
module_children.iter().map(|def_id| def_id.local_def_index));
1369+
module_children.iter().map(|child| child.res.def_id().index));
13701370
} else {
13711371
// For non-enum, there is only one variant, and its def_id is the adt's.
13721372
debug_assert_eq!(adt_def.variants().len(), 1);
@@ -1412,12 +1412,14 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
14121412
// Encode this here because we don't do it in encode_def_ids.
14131413
record!(self.tables.expn_that_defined[def_id] <- tcx.expn_that_defined(local_def_id));
14141414
} else {
1415-
let non_reexports = tcx.module_children_non_reexports(local_def_id);
1415+
let module_children = tcx.module_children_local(local_def_id);
1416+
14161417
record_array!(self.tables.module_children_non_reexports[def_id] <-
1417-
non_reexports.iter().map(|def_id| def_id.local_def_index));
1418+
module_children.iter().filter(|child| child.reexport_chain.is_empty())
1419+
.map(|child| child.res.def_id().index));
14181420

14191421
record_defaulted_array!(self.tables.module_children_reexports[def_id] <-
1420-
tcx.module_children_reexports(local_def_id));
1422+
module_children.iter().filter(|child| !child.reexport_chain.is_empty()));
14211423
}
14221424
}
14231425

@@ -1676,9 +1678,9 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
16761678
hir::ItemKind::Trait(..) => {
16771679
record!(self.tables.trait_def[def_id] <- self.tcx.trait_def(def_id));
16781680

1679-
let module_children = tcx.module_children_non_reexports(item.owner_id.def_id);
1681+
let module_children = tcx.module_children_local(item.owner_id.def_id);
16801682
record_array!(self.tables.module_children_non_reexports[def_id] <-
1681-
module_children.iter().map(|def_id| def_id.local_def_index));
1683+
module_children.iter().map(|child| child.res.def_id().index));
16821684

16831685
let associated_item_def_ids = self.tcx.associated_item_def_ids(def_id);
16841686
record_associated_item_def_ids(self, associated_item_def_ids);

compiler/rustc_metadata/src/rmeta/mod.rs

+6
Original file line numberDiff line numberDiff line change
@@ -357,10 +357,16 @@ define_tables! {
357357
associated_types_for_impl_traits_in_associated_fn: Table<DefIndex, LazyArray<DefId>>,
358358
opt_rpitit_info: Table<DefIndex, Option<LazyValue<ty::ImplTraitInTraitData>>>,
359359
unused_generic_params: Table<DefIndex, UnusedGenericParams>,
360+
// Reexported names are not associated with individual `DefId`s,
361+
// e.g. a glob import can introduce a lot of names, all with the same `DefId`.
362+
// That's why the encoded list needs to contain `ModChild` structures describing all the names
363+
// individually instead of `DefId`s.
360364
module_children_reexports: Table<DefIndex, LazyArray<ModChild>>,
361365

362366
- optional:
363367
attributes: Table<DefIndex, LazyArray<ast::Attribute>>,
368+
// For non-reexported names in a module every name is associated with a separate `DefId`,
369+
// so we can take their names, visibilities etc from other encoded tables.
364370
module_children_non_reexports: Table<DefIndex, LazyArray<DefIndex>>,
365371
associated_item_or_field_def_ids: Table<DefIndex, LazyArray<DefIndex>>,
366372
opt_def_kind: Table<DefIndex, DefKind>,

compiler/rustc_middle/src/ty/context.rs

+6-15
Original file line numberDiff line numberDiff line change
@@ -2414,26 +2414,17 @@ impl<'tcx> TyCtxt<'tcx> {
24142414
}
24152415
}
24162416

2417-
/// Named module children from all items except `use` and `extern crate` imports.
2418-
///
2419-
/// In addition to regular items this list also includes struct or variant constructors, and
2417+
/// Named module children from all kinds of items, including imports.
2418+
/// In addition to regular items this list also includes struct and variant constructors, and
24202419
/// items inside `extern {}` blocks because all of them introduce names into parent module.
2421-
/// For non-reexported children every such name is associated with a separate `DefId`.
24222420
///
24232421
/// Module here is understood in name resolution sense - it can be a `mod` item,
24242422
/// or a crate root, or an enum, or a trait.
2425-
pub fn module_children_non_reexports(self, def_id: LocalDefId) -> &'tcx [LocalDefId] {
2426-
self.resolutions(()).module_children_non_reexports.get(&def_id).map_or(&[], |v| &v[..])
2427-
}
2428-
2429-
/// Named module children from `use` and `extern crate` imports.
24302423
///
2431-
/// Reexported names are not associated with individual `DefId`s,
2432-
/// e.g. a glob import can introduce a lot of names, all with the same `DefId`.
2433-
/// That's why the list needs to contain `ModChild` structures describing all the names
2434-
/// individually instead of `DefId`s.
2435-
pub fn module_children_reexports(self, def_id: LocalDefId) -> &'tcx [ModChild] {
2436-
self.resolutions(()).module_children_reexports.get(&def_id).map_or(&[], |v| &v[..])
2424+
/// This is not a query, making it a query causes perf regressions
2425+
/// (probably due to hashing spans in `ModChild`ren).
2426+
pub fn module_children_local(self, def_id: LocalDefId) -> &'tcx [ModChild] {
2427+
self.resolutions(()).module_children.get(&def_id).map_or(&[], |v| &v[..])
24372428
}
24382429
}
24392430

compiler/rustc_middle/src/ty/mod.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -165,8 +165,7 @@ pub struct ResolverGlobalCtxt {
165165
pub effective_visibilities: EffectiveVisibilities,
166166
pub extern_crate_map: FxHashMap<LocalDefId, CrateNum>,
167167
pub maybe_unused_trait_imports: FxIndexSet<LocalDefId>,
168-
pub module_children_non_reexports: LocalDefIdMap<Vec<LocalDefId>>,
169-
pub module_children_reexports: LocalDefIdMap<Vec<ModChild>>,
168+
pub module_children: LocalDefIdMap<Vec<ModChild>>,
170169
pub glob_map: FxHashMap<LocalDefId, FxHashSet<Symbol>>,
171170
pub main_def: Option<MainDefinition>,
172171
pub trait_impls: FxIndexMap<DefId, Vec<LocalDefId>>,

compiler/rustc_privacy/src/lib.rs

+5-3
Original file line numberDiff line numberDiff line change
@@ -515,9 +515,11 @@ impl<'tcx> EmbargoVisitor<'tcx> {
515515
let vis = self.tcx.local_visibility(item_id.owner_id.def_id);
516516
self.update_macro_reachable_def(item_id.owner_id.def_id, def_kind, vis, defining_mod);
517517
}
518-
for export in self.tcx.module_children_reexports(module_def_id) {
519-
if export.vis.is_accessible_from(defining_mod, self.tcx)
520-
&& let Res::Def(def_kind, def_id) = export.res
518+
for child in self.tcx.module_children_local(module_def_id) {
519+
// FIXME: Use module children for the logic above too.
520+
if !child.reexport_chain.is_empty()
521+
&& child.vis.is_accessible_from(defining_mod, self.tcx)
522+
&& let Res::Def(def_kind, def_id) = child.res
521523
&& let Some(def_id) = def_id.as_local() {
522524
let vis = self.tcx.local_visibility(def_id);
523525
self.update_macro_reachable_def(def_id, def_kind, vis, defining_mod);

compiler/rustc_resolve/src/imports.rs

+6-13
Original file line numberDiff line numberDiff line change
@@ -1261,32 +1261,25 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
12611261
*module.globs.borrow_mut() = Vec::new();
12621262

12631263
if let Some(def_id) = module.opt_def_id() {
1264-
let mut non_reexports = Vec::new();
1265-
let mut reexports = Vec::new();
1264+
let mut children = Vec::new();
12661265

12671266
module.for_each_child(self, |this, ident, _, binding| {
12681267
let res = binding.res().expect_non_local();
1269-
if !binding.is_import() {
1270-
non_reexports.push(res.def_id().expect_local());
1271-
} else if res != def::Res::Err && !binding.is_ambiguity() {
1268+
if res != def::Res::Err && !binding.is_ambiguity() {
12721269
let mut reexport_chain = SmallVec::new();
12731270
let mut next_binding = binding;
12741271
while let NameBindingKind::Import { binding, import, .. } = next_binding.kind {
12751272
reexport_chain.push(import.simplify(this));
12761273
next_binding = binding;
12771274
}
12781275

1279-
reexports.push(ModChild { ident, res, vis: binding.vis, reexport_chain });
1276+
children.push(ModChild { ident, res, vis: binding.vis, reexport_chain });
12801277
}
12811278
});
12821279

1283-
// Should be fine because this code is only called for local modules.
1284-
let def_id = def_id.expect_local();
1285-
if !non_reexports.is_empty() {
1286-
self.module_children_non_reexports.insert(def_id, non_reexports);
1287-
}
1288-
if !reexports.is_empty() {
1289-
self.module_children_reexports.insert(def_id, reexports);
1280+
if !children.is_empty() {
1281+
// Should be fine because this code is only called for local modules.
1282+
self.module_children.insert(def_id.expect_local(), children);
12901283
}
12911284
}
12921285
}

compiler/rustc_resolve/src/lib.rs

+3-6
Original file line numberDiff line numberDiff line change
@@ -909,8 +909,7 @@ pub struct Resolver<'a, 'tcx> {
909909

910910
/// `CrateNum` resolutions of `extern crate` items.
911911
extern_crate_map: FxHashMap<LocalDefId, CrateNum>,
912-
module_children_non_reexports: LocalDefIdMap<Vec<LocalDefId>>,
913-
module_children_reexports: LocalDefIdMap<Vec<ModChild>>,
912+
module_children: LocalDefIdMap<Vec<ModChild>>,
914913
trait_map: NodeMap<Vec<TraitCandidate>>,
915914

916915
/// A map from nodes to anonymous modules.
@@ -1260,8 +1259,7 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
12601259
lifetimes_res_map: Default::default(),
12611260
extra_lifetime_params_map: Default::default(),
12621261
extern_crate_map: Default::default(),
1263-
module_children_non_reexports: Default::default(),
1264-
module_children_reexports: Default::default(),
1262+
module_children: Default::default(),
12651263
trait_map: NodeMap::default(),
12661264
underscore_disambiguator: 0,
12671265
empty_module,
@@ -1399,8 +1397,7 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
13991397
has_pub_restricted,
14001398
effective_visibilities,
14011399
extern_crate_map,
1402-
module_children_non_reexports: self.module_children_non_reexports,
1403-
module_children_reexports: self.module_children_reexports,
1400+
module_children: self.module_children,
14041401
glob_map,
14051402
maybe_unused_trait_imports,
14061403
main_def,

src/librustdoc/clean/inline.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -152,8 +152,9 @@ pub(crate) fn try_inline_glob(
152152
// reexported by the glob, e.g. because they are shadowed by something else.
153153
let reexports = cx
154154
.tcx
155-
.module_children_reexports(current_mod)
155+
.module_children_local(current_mod)
156156
.iter()
157+
.filter(|child| !child.reexport_chain.is_empty())
157158
.filter_map(|child| child.res.opt_def_id())
158159
.collect();
159160
let mut items = build_module_items(cx, did, visited, inlined_names, Some(&reexports));

src/librustdoc/clean/mod.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -2089,9 +2089,9 @@ pub(crate) fn reexport_chain<'tcx>(
20892089
import_def_id: LocalDefId,
20902090
target_def_id: LocalDefId,
20912091
) -> &'tcx [Reexport] {
2092-
for child in tcx.module_children_reexports(tcx.local_parent(import_def_id)) {
2092+
for child in tcx.module_children_local(tcx.local_parent(import_def_id)) {
20932093
if child.res.opt_def_id() == Some(target_def_id.to_def_id())
2094-
&& child.reexport_chain[0].id() == Some(import_def_id.to_def_id())
2094+
&& child.reexport_chain.first().and_then(|r| r.id()) == Some(import_def_id.to_def_id())
20952095
{
20962096
return &child.reexport_chain;
20972097
}

src/librustdoc/visit_ast.rs

+5-4
Original file line numberDiff line numberDiff line change
@@ -136,14 +136,15 @@ impl<'a, 'tcx> RustdocVisitor<'a, 'tcx> {
136136
// is declared but also a reexport of itself producing two exports of the same
137137
// macro in the same module.
138138
let mut inserted = FxHashSet::default();
139-
for export in self.cx.tcx.module_children_reexports(CRATE_DEF_ID) {
140-
if let Res::Def(DefKind::Macro(_), def_id) = export.res &&
139+
for child in self.cx.tcx.module_children_local(CRATE_DEF_ID) {
140+
if !child.reexport_chain.is_empty() &&
141+
let Res::Def(DefKind::Macro(_), def_id) = child.res &&
141142
let Some(local_def_id) = def_id.as_local() &&
142143
self.cx.tcx.has_attr(def_id, sym::macro_export) &&
143144
inserted.insert(def_id)
144145
{
145-
let item = self.cx.tcx.hir().expect_item(local_def_id);
146-
top_level_module.items.insert((local_def_id, Some(item.ident.name)), (item, None, None));
146+
let item = self.cx.tcx.hir().expect_item(local_def_id);
147+
top_level_module.items.insert((local_def_id, Some(item.ident.name)), (item, None, None));
147148
}
148149
}
149150

0 commit comments

Comments
 (0)