Skip to content

[perf experiment] resolve: Do not cache nearest parent mod in ModuleData #88917

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 6 additions & 25 deletions compiler/rustc_resolve/src/build_reduced_graph.rs
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,6 @@ impl<'a> Resolver<'a> {
let module = self.arenas.alloc_module(ModuleData::new(
parent,
kind,
def_id,
self.cstore().module_expansion_untracked(def_id, &self.session),
self.cstore().get_span_untracked(def_id, &self.session),
));
Expand Down Expand Up @@ -274,7 +273,7 @@ impl<'a, 'b> BuildReducedGraphVisitor<'a, 'b> {
self.r.visibilities[&def_id.expect_local()]
}
// Otherwise, the visibility is restricted to the nearest parent `mod` item.
_ => ty::Visibility::Restricted(self.parent_scope.module.nearest_parent_mod),
_ => ty::Visibility::Restricted(self.parent_scope.module.nearest_parent_mod()),
})
}
ast::VisibilityKind::Restricted { ref path, id, .. } => {
Expand Down Expand Up @@ -773,13 +772,7 @@ impl<'a, 'b> BuildReducedGraphVisitor<'a, 'b> {
no_implicit_prelude: parent.no_implicit_prelude || {
self.r.session.contains_name(&item.attrs, sym::no_implicit_prelude)
},
..ModuleData::new(
Some(parent),
module_kind,
def_id,
expansion.to_expn_id(),
item.span,
)
..ModuleData::new(Some(parent), module_kind, expansion.to_expn_id(), item.span)
});
self.r.define(parent, ident, TypeNS, (module, vis, sp, expansion));
self.r.module_map.insert(local_def_id, module);
Expand Down Expand Up @@ -814,13 +807,8 @@ impl<'a, 'b> BuildReducedGraphVisitor<'a, 'b> {

ItemKind::Enum(_, _) => {
let module_kind = ModuleKind::Def(DefKind::Enum, def_id, ident.name);
let module = self.r.new_module(
parent,
module_kind,
parent.nearest_parent_mod,
expansion.to_expn_id(),
item.span,
);
let module =
self.r.new_module(parent, module_kind, expansion.to_expn_id(), item.span);
self.r.define(parent, ident, TypeNS, (module, vis, sp, expansion));
self.parent_scope.module = module;
}
Expand Down Expand Up @@ -889,13 +877,8 @@ impl<'a, 'b> BuildReducedGraphVisitor<'a, 'b> {
ItemKind::Trait(..) => {
// Add all the items within to a new module.
let module_kind = ModuleKind::Def(DefKind::Trait, def_id, ident.name);
let module = self.r.new_module(
parent,
module_kind,
parent.nearest_parent_mod,
expansion.to_expn_id(),
item.span,
);
let module =
self.r.new_module(parent, module_kind, expansion.to_expn_id(), item.span);
self.r.define(parent, ident, TypeNS, (module, vis, sp, expansion));
self.parent_scope.module = module;
}
Expand Down Expand Up @@ -935,7 +918,6 @@ impl<'a, 'b> BuildReducedGraphVisitor<'a, 'b> {
let module = self.r.new_module(
parent,
ModuleKind::Block(block.id),
parent.nearest_parent_mod,
expansion.to_expn_id(),
block.span,
);
Expand All @@ -956,7 +938,6 @@ impl<'a, 'b> BuildReducedGraphVisitor<'a, 'b> {
let module = self.r.new_module(
parent,
ModuleKind::Def(kind, def_id, ident.name),
def_id,
expansion.to_expn_id(),
span,
);
Expand Down
4 changes: 2 additions & 2 deletions compiler/rustc_resolve/src/late.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1872,7 +1872,7 @@ impl<'a: 'ast, 'b, 'ast> LateResolutionVisitor<'a, 'b, 'ast> {
if this.should_report_errs() {
let (err, candidates) = this.smart_resolve_report_errors(path, span, source, res);

let def_id = this.parent_scope.module.nearest_parent_mod;
let def_id = this.parent_scope.module.nearest_parent_mod();
let instead = res.is_some();
let suggestion =
if res.is_none() { this.report_missing_type_error(path) } else { None };
Expand Down Expand Up @@ -1940,7 +1940,7 @@ impl<'a: 'ast, 'b, 'ast> LateResolutionVisitor<'a, 'b, 'ast> {

drop(parent_err);

let def_id = this.parent_scope.module.nearest_parent_mod;
let def_id = this.parent_scope.module.nearest_parent_mod();

if this.should_report_errs() {
this.r.use_injections.push(UseError {
Expand Down
50 changes: 22 additions & 28 deletions compiler/rustc_resolve/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -505,10 +505,6 @@ pub struct ModuleData<'a> {
/// What kind of module this is, because this may not be a `mod`.
kind: ModuleKind,

/// The [`DefId`] of the nearest `mod` item ancestor (which may be this module).
/// This may be the crate root.
nearest_parent_mod: DefId,

/// Mapping between names and their (possibly in-progress) resolutions in this module.
/// Resolutions in modules from other crates are not populated until accessed.
lazy_resolutions: Resolutions<'a>,
Expand Down Expand Up @@ -536,19 +532,16 @@ pub struct ModuleData<'a> {
type Module<'a> = &'a ModuleData<'a>;

impl<'a> ModuleData<'a> {
fn new(
parent: Option<Module<'a>>,
kind: ModuleKind,
nearest_parent_mod: DefId,
expansion: ExpnId,
span: Span,
) -> Self {
fn new(parent: Option<Module<'a>>, kind: ModuleKind, expansion: ExpnId, span: Span) -> Self {
let is_foreign = match kind {
ModuleKind::Def(_, def_id, _) => !def_id.is_local(),
ModuleKind::Block(_) => false,
};
ModuleData {
parent,
kind,
nearest_parent_mod,
lazy_resolutions: Default::default(),
populate_on_access: Cell::new(!nearest_parent_mod.is_local()),
populate_on_access: Cell::new(is_foreign),
unexpanded_invocations: Default::default(),
no_implicit_prelude: false,
glob_importers: RefCell::new(Vec::new()),
Expand All @@ -559,6 +552,13 @@ impl<'a> ModuleData<'a> {
}
}

fn nearest_parent_mod(&self) -> DefId {
match self.kind {
ModuleKind::Def(DefKind::Mod, def_id, _) => def_id,
_ => self.parent.expect("non-root module without parent").nearest_parent_mod(),
}
}

fn for_each_child<R, F>(&'a self, resolver: &mut R, mut f: F)
where
R: AsMut<Resolver<'a>>,
Expand Down Expand Up @@ -1260,18 +1260,12 @@ impl<'a> Resolver<'a> {
let root_module_kind = ModuleKind::Def(DefKind::Mod, root_def_id, kw::Empty);
let graph_root = arenas.alloc_module(ModuleData {
no_implicit_prelude: session.contains_name(&krate.attrs, sym::no_implicit_prelude),
..ModuleData::new(None, root_module_kind, root_def_id, ExpnId::root(), krate.span)
..ModuleData::new(None, root_module_kind, ExpnId::root(), krate.span)
});
let empty_module_kind = ModuleKind::Def(DefKind::Mod, root_def_id, kw::Empty);
let empty_module = arenas.alloc_module(ModuleData {
no_implicit_prelude: true,
..ModuleData::new(
Some(graph_root),
empty_module_kind,
root_def_id,
ExpnId::root(),
DUMMY_SP,
)
..ModuleData::new(Some(graph_root), empty_module_kind, ExpnId::root(), DUMMY_SP)
});
let mut module_map = FxHashMap::default();
module_map.insert(root_local_def_id, graph_root);
Expand Down Expand Up @@ -1636,11 +1630,10 @@ impl<'a> Resolver<'a> {
&self,
parent: Module<'a>,
kind: ModuleKind,
nearest_parent_mod: DefId,
expn_id: ExpnId,
span: Span,
) -> Module<'a> {
let module = ModuleData::new(Some(parent), kind, nearest_parent_mod, expn_id, span);
let module = ModuleData::new(Some(parent), kind, expn_id, span);
self.arenas.alloc_module(module)
}

Expand Down Expand Up @@ -2167,7 +2160,8 @@ impl<'a> Resolver<'a> {
return self.graph_root;
}
};
let module = self.get_module(DefId { index: CRATE_DEF_INDEX, ..module.nearest_parent_mod });
let module =
self.get_module(DefId { index: CRATE_DEF_INDEX, ..module.nearest_parent_mod() });
debug!(
"resolve_crate_root({:?}): got module {:?} ({:?}) (ident.span = {:?})",
ident,
Expand All @@ -2179,10 +2173,10 @@ impl<'a> Resolver<'a> {
}

fn resolve_self(&mut self, ctxt: &mut SyntaxContext, module: Module<'a>) -> Module<'a> {
let mut module = self.get_module(module.nearest_parent_mod);
let mut module = self.get_module(module.nearest_parent_mod());
while module.span.ctxt().normalize_to_macros_2_0() != *ctxt {
let parent = module.parent.unwrap_or_else(|| self.macro_def_scope(ctxt.remove_mark()));
module = self.get_module(parent.nearest_parent_mod);
module = self.get_module(parent.nearest_parent_mod());
}
module
}
Expand Down Expand Up @@ -2896,7 +2890,7 @@ impl<'a> Resolver<'a> {
}

fn is_accessible_from(&self, vis: ty::Visibility, module: Module<'a>) -> bool {
vis.is_accessible_from(module.nearest_parent_mod, self)
vis.is_accessible_from(module.nearest_parent_mod(), self)
}

fn set_binding_parent_module(&mut self, binding: &'a NameBinding<'a>, module: Module<'a>) {
Expand All @@ -2920,7 +2914,7 @@ impl<'a> Resolver<'a> {
self.binding_parent_modules.get(&PtrKey(modularized)),
) {
(Some(macro_rules), Some(modularized)) => {
macro_rules.nearest_parent_mod == modularized.nearest_parent_mod
macro_rules.nearest_parent_mod() == modularized.nearest_parent_mod()
&& modularized.is_ancestor_of(macro_rules)
}
_ => false,
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_resolve/src/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -305,7 +305,7 @@ impl<'a> ResolverExpand for Resolver<'a> {
fast_print_path(path),
res.opt_def_id(),
res.opt_def_id().map(|macro_def_id| {
self.macro_def_scope_from_def_id(macro_def_id).nearest_parent_mod
self.macro_def_scope_from_def_id(macro_def_id).nearest_parent_mod()
}),
),
self.create_stable_hashing_context(),
Expand Down