Skip to content

Commit de64b4e

Browse files
committed
[WIP] One more attempt to optimize module_children
1 parent 8b8110e commit de64b4e

File tree

21 files changed

+195
-148
lines changed

21 files changed

+195
-148
lines changed

compiler/rustc_hir_typeck/src/method/suggest.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2266,13 +2266,13 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
22662266
let (candidates, globs): (Vec<_>, Vec<_>) = candidates.into_iter().partition(|trait_did| {
22672267
if let Some(parent_did) = parent_map.get(trait_did) {
22682268
// If the item is re-exported as `_`, we should suggest a glob-import instead.
2269-
if *parent_did != self.tcx.parent(*trait_did)
2270-
&& self
2271-
.tcx
2269+
let tcx = self.tcx;
2270+
if *parent_did != tcx.parent(*trait_did)
2271+
&& tcx
22722272
.module_children(*parent_did)
22732273
.iter()
2274-
.filter(|child| child.res.opt_def_id() == Some(*trait_did))
2275-
.all(|child| child.ident.name == kw::Underscore)
2274+
.filter(|child| child.opt_def_id() == Some(*trait_did))
2275+
.all(|child| child.name(tcx) == kw::Underscore)
22762276
{
22772277
return false;
22782278
}

compiler/rustc_metadata/src/rmeta/decoder.rs

Lines changed: 14 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ use rustc_hir::def_id::{CrateNum, DefId, DefIndex, CRATE_DEF_INDEX, LOCAL_CRATE}
1717
use rustc_hir::definitions::{DefKey, DefPath, DefPathData, DefPathHash};
1818
use rustc_hir::diagnostic_items::DiagnosticItems;
1919
use rustc_index::{Idx, IndexVec};
20-
use rustc_middle::metadata::ModChild;
20+
use rustc_middle::metadata::{ModChild, ModChildData};
2121
use rustc_middle::middle::exported_symbols::{ExportedSymbol, SymbolExportInfo};
2222
use rustc_middle::mir::interpret::{AllocDecodingSession, AllocDecodingState};
2323
use rustc_middle::ty::codec::TyDecoder;
@@ -904,15 +904,17 @@ impl<'a, 'tcx> CrateMetadataRef<'a> {
904904
let variants = if let ty::AdtKind::Enum = adt_kind {
905905
self.root
906906
.tables
907-
.module_children_non_reexports
907+
.module_children
908908
.get(self, item_id)
909909
.expect("variants are not encoded for an enum")
910910
.decode(self)
911-
.filter_map(|index| {
912-
let kind = self.def_kind(index);
911+
.filter_map(|child| {
912+
let ModChild::Def(def_id) = child else { bug!() };
913+
assert_eq!(def_id.krate, self.cnum);
914+
let kind = self.def_kind(def_id.index);
913915
match kind {
914916
DefKind::Ctor(..) => None,
915-
_ => Some(self.get_variant(&kind, index, did)),
917+
_ => Some(self.get_variant(&kind, def_id.index, did)),
916918
}
917919
})
918920
.collect()
@@ -988,12 +990,12 @@ impl<'a, 'tcx> CrateMetadataRef<'a> {
988990
DiagnosticItems { id_to_name, name_to_id }
989991
}
990992

991-
fn get_mod_child(self, id: DefIndex, sess: &Session) -> ModChild {
993+
fn get_mod_child_data(self, id: DefIndex, sess: &Session) -> ModChildData {
992994
let ident = self.item_ident(id, sess);
993995
let res = Res::Def(self.def_kind(id), self.local_def_id(id));
994996
let vis = self.get_visibility(id);
995997

996-
ModChild { ident, res, vis, reexport_chain: Default::default() }
998+
ModChildData { ident, res, vis, reexport_chain: Default::default() }
997999
}
9981000

9991001
/// Iterates over all named children of the given module,
@@ -1011,21 +1013,15 @@ impl<'a, 'tcx> CrateMetadataRef<'a> {
10111013
// the view of this crate as a proc macro crate.
10121014
if id == CRATE_DEF_INDEX {
10131015
for child_index in data.macros.decode(self) {
1014-
yield self.get_mod_child(child_index, sess);
1016+
yield ModChild::Def(self.local_def_id(child_index));
10151017
}
10161018
}
10171019
} else {
10181020
// Iterate over all children.
1019-
let non_reexports = self.root.tables.module_children_non_reexports.get(self, id);
1020-
for child_index in non_reexports.unwrap().decode(self) {
1021-
yield self.get_mod_child(child_index, sess);
1022-
}
1023-
1024-
let reexports = self.root.tables.module_children_reexports.get(self, id);
1025-
if !reexports.is_default() {
1026-
for reexport in reexports.decode((self, sess)) {
1027-
yield reexport;
1028-
}
1021+
for child in
1022+
self.root.tables.module_children.get(self, id).unwrap().decode((self, sess))
1023+
{
1024+
yield child;
10291025
}
10301026
}
10311027
})

compiler/rustc_metadata/src/rmeta/decoder/cstore_impl.rs

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ use rustc_hir::def::{CtorKind, DefKind, Res};
1010
use rustc_hir::def_id::{CrateNum, DefId, DefIdMap, LOCAL_CRATE};
1111
use rustc_hir::definitions::{DefKey, DefPath, DefPathHash};
1212
use rustc_middle::arena::ArenaAllocatable;
13-
use rustc_middle::metadata::ModChild;
13+
use rustc_middle::metadata::{ModChild, ModChildData};
1414
use rustc_middle::middle::exported_symbols::ExportedSymbol;
1515
use rustc_middle::middle::stability::DeprecationEntry;
1616
use rustc_middle::query::LocalCrate;
@@ -441,12 +441,12 @@ pub(in crate::rmeta) fn provide(providers: &mut Providers) {
441441
}
442442

443443
let mut add_child = |bfs_queue: &mut VecDeque<_>, child: &ModChild, parent: DefId| {
444-
if !child.vis.is_public() {
444+
if !child.vis(tcx).is_public() {
445445
return;
446446
}
447447

448-
if let Some(def_id) = child.res.opt_def_id() {
449-
if child.ident.name == kw::Underscore {
448+
if let Some(def_id) = child.opt_def_id() {
449+
if child.name(tcx) == kw::Underscore {
450450
fallback_map.push((def_id, parent));
451451
return;
452452
}
@@ -467,7 +467,7 @@ pub(in crate::rmeta) fn provide(providers: &mut Providers) {
467467
Entry::Vacant(entry) => {
468468
entry.insert(parent);
469469
if matches!(
470-
child.res,
470+
child.res(tcx),
471471
Res::Def(DefKind::Mod | DefKind::Enum | DefKind::Trait, _)
472472
) {
473473
bfs_queue.push_back(def_id);
@@ -478,7 +478,7 @@ pub(in crate::rmeta) fn provide(providers: &mut Providers) {
478478
};
479479

480480
while let Some(def) = bfs_queue.pop_front() {
481-
for child in tcx.module_children(def).iter() {
481+
for child in tcx.module_children(def) {
482482
add_child(bfs_queue, child, def);
483483
}
484484
}
@@ -512,6 +512,10 @@ pub(in crate::rmeta) fn provide(providers: &mut Providers) {
512512
}
513513

514514
impl CStore {
515+
pub fn mod_child_data_untracked(&self, def_id: DefId, sess: &Session) -> ModChildData {
516+
self.get_crate_data(def_id.krate).get_mod_child_data(def_id.index, sess)
517+
}
518+
515519
pub fn ctor_untracked(&self, def: DefId) -> Option<(CtorKind, DefId)> {
516520
self.get_crate_data(def.krate).get_ctor(def.index)
517521
}

compiler/rustc_metadata/src/rmeta/encoder.rs

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1364,9 +1364,8 @@ 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);
1368-
record_array!(self.tables.module_children_non_reexports[def_id] <-
1369-
module_children.iter().map(|def_id| def_id.local_def_index));
1367+
record_array!(self.tables.module_children[def_id] <-
1368+
tcx.module_children_local(local_def_id));
13701369
} else {
13711370
// For non-enum, there is only one variant, and its def_id is the adt's.
13721371
debug_assert_eq!(adt_def.variants().len(), 1);
@@ -1412,12 +1411,8 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
14121411
// Encode this here because we don't do it in encode_def_ids.
14131412
record!(self.tables.expn_that_defined[def_id] <- tcx.expn_that_defined(local_def_id));
14141413
} else {
1415-
let non_reexports = tcx.module_children_non_reexports(local_def_id);
1416-
record_array!(self.tables.module_children_non_reexports[def_id] <-
1417-
non_reexports.iter().map(|def_id| def_id.local_def_index));
1418-
1419-
record_defaulted_array!(self.tables.module_children_reexports[def_id] <-
1420-
tcx.module_children_reexports(local_def_id));
1414+
record_array!(self.tables.module_children[def_id] <-
1415+
tcx.module_children_local(local_def_id));
14211416
}
14221417
}
14231418

@@ -1676,9 +1671,8 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
16761671
hir::ItemKind::Trait(..) => {
16771672
record!(self.tables.trait_def[def_id] <- self.tcx.trait_def(def_id));
16781673

1679-
let module_children = tcx.module_children_non_reexports(item.owner_id.def_id);
1680-
record_array!(self.tables.module_children_non_reexports[def_id] <-
1681-
module_children.iter().map(|def_id| def_id.local_def_index));
1674+
record_array!(self.tables.module_children[def_id] <-
1675+
tcx.module_children_local(item.owner_id.def_id));
16821676

16831677
let associated_item_def_ids = self.tcx.associated_item_def_ids(def_id);
16841678
record_associated_item_def_ids(self, associated_item_def_ids);

compiler/rustc_metadata/src/rmeta/mod.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -357,11 +357,10 @@ 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-
module_children_reexports: Table<DefIndex, LazyArray<ModChild>>,
361360

362361
- optional:
363362
attributes: Table<DefIndex, LazyArray<ast::Attribute>>,
364-
module_children_non_reexports: Table<DefIndex, LazyArray<DefIndex>>,
363+
module_children: Table<DefIndex, LazyArray<ModChild>>,
365364
associated_item_or_field_def_ids: Table<DefIndex, LazyArray<DefIndex>>,
366365
opt_def_kind: Table<DefIndex, DefKind>,
367366
visibility: Table<DefIndex, LazyValue<ty::Visibility<DefIndex>>>,

compiler/rustc_middle/src/metadata.rs

Lines changed: 55 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
use crate::ty;
22

3+
use crate::ty::TyCtxt;
34
use rustc_hir::def::Res;
45
use rustc_macros::HashStable;
56
use rustc_span::def_id::DefId;
6-
use rustc_span::symbol::Ident;
7+
use rustc_span::symbol::{Ident, Symbol};
78
use smallvec::SmallVec;
89

910
/// A simplified version of `ImportKind` from resolve.
@@ -32,7 +33,7 @@ impl Reexport {
3233
/// Module child can be either a proper item or a reexport (including private imports).
3334
/// In case of reexport all the fields describe the reexport item itself, not what it refers to.
3435
#[derive(Debug, TyEncodable, TyDecodable, HashStable)]
35-
pub struct ModChild {
36+
pub struct ModChildData {
3637
/// Name of the item.
3738
pub ident: Ident,
3839
/// Resolution result corresponding to the item.
@@ -44,3 +45,55 @@ pub struct ModChild {
4445
/// Empty if the module child is a proper item.
4546
pub reexport_chain: SmallVec<[Reexport; 2]>,
4647
}
48+
49+
#[derive(Debug, TyEncodable, TyDecodable, HashStable)]
50+
pub enum ModChild {
51+
Def(DefId),
52+
Reexport(ModChildData),
53+
}
54+
55+
impl ModChild {
56+
pub fn res(&self, tcx: TyCtxt<'_>) -> Res<!> {
57+
match self {
58+
ModChild::Def(def_id) => Res::Def(tcx.def_kind(*def_id), *def_id),
59+
ModChild::Reexport(child) => child.res,
60+
}
61+
}
62+
63+
pub fn opt_def_id(&self) -> Option<DefId> {
64+
match self {
65+
ModChild::Def(def_id) => Some(*def_id),
66+
ModChild::Reexport(child) => child.res.opt_def_id(),
67+
}
68+
}
69+
70+
pub fn vis(&self, tcx: TyCtxt<'_>) -> ty::Visibility<DefId> {
71+
match self {
72+
ModChild::Def(def_id) => tcx.visibility(*def_id),
73+
ModChild::Reexport(child) => child.vis,
74+
}
75+
}
76+
77+
pub fn name(&self, tcx: TyCtxt<'_>) -> Symbol {
78+
match self {
79+
ModChild::Def(def_id) => tcx.item_name(*def_id),
80+
ModChild::Reexport(child) => child.ident.name,
81+
}
82+
}
83+
84+
// FIXME: `opt_item_ident` is buggy and doesn't decode hygiene correctly,
85+
// figure out what happens.
86+
pub fn ident(&self, tcx: TyCtxt<'_>) -> Ident {
87+
match self {
88+
ModChild::Def(def_id) => tcx.opt_item_ident(*def_id).unwrap(),
89+
ModChild::Reexport(child) => child.ident,
90+
}
91+
}
92+
93+
pub fn reexport_chain(&self) -> &[Reexport] {
94+
match self {
95+
ModChild::Def(_) => &[],
96+
ModChild::Reexport(child) => &child.reexport_chain,
97+
}
98+
}
99+
}

compiler/rustc_middle/src/ty/context.rs

Lines changed: 2 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -2437,26 +2437,8 @@ impl<'tcx> TyCtxt<'tcx> {
24372437
}
24382438
}
24392439

2440-
/// Named module children from all items except `use` and `extern crate` imports.
2441-
///
2442-
/// In addition to regular items this list also includes struct or variant constructors, and
2443-
/// items inside `extern {}` blocks because all of them introduce names into parent module.
2444-
/// For non-reexported children every such name is associated with a separate `DefId`.
2445-
///
2446-
/// Module here is understood in name resolution sense - it can be a `mod` item,
2447-
/// or a crate root, or an enum, or a trait.
2448-
pub fn module_children_non_reexports(self, def_id: LocalDefId) -> &'tcx [LocalDefId] {
2449-
self.resolutions(()).module_children_non_reexports.get(&def_id).map_or(&[], |v| &v[..])
2450-
}
2451-
2452-
/// Named module children from `use` and `extern crate` imports.
2453-
///
2454-
/// Reexported names are not associated with individual `DefId`s,
2455-
/// e.g. a glob import can introduce a lot of names, all with the same `DefId`.
2456-
/// That's why the list needs to contain `ModChild` structures describing all the names
2457-
/// individually instead of `DefId`s.
2458-
pub fn module_children_reexports(self, def_id: LocalDefId) -> &'tcx [ModChild] {
2459-
self.resolutions(()).module_children_reexports.get(&def_id).map_or(&[], |v| &v[..])
2440+
pub fn module_children_local(self, def_id: LocalDefId) -> &'tcx [ModChild] {
2441+
self.resolutions(()).module_children.get(&def_id).map_or(&[], |v| &v[..])
24602442
}
24612443
}
24622444

compiler/rustc_middle/src/ty/mod.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -166,8 +166,7 @@ pub struct ResolverGlobalCtxt {
166166
pub effective_visibilities: EffectiveVisibilities,
167167
pub extern_crate_map: FxHashMap<LocalDefId, CrateNum>,
168168
pub maybe_unused_trait_imports: FxIndexSet<LocalDefId>,
169-
pub module_children_non_reexports: LocalDefIdMap<Vec<LocalDefId>>,
170-
pub module_children_reexports: LocalDefIdMap<Vec<ModChild>>,
169+
pub module_children: LocalDefIdMap<Vec<ModChild>>,
171170
pub glob_map: FxHashMap<LocalDefId, FxHashSet<Symbol>>,
172171
pub main_def: Option<MainDefinition>,
173172
pub trait_impls: FxIndexMap<DefId, Vec<LocalDefId>>,

0 commit comments

Comments
 (0)