Skip to content

Commit 22d2c51

Browse files
committed
Auto merge of #143247 - cjgillot:metadata-no-red, r=<try>
Avoid depending on forever-red DepNode when encoding metadata. Split from #114669 for perf r? `@petrochenkov`
2 parents ad3b725 + 1584b0e commit 22d2c51

File tree

4 files changed

+30
-29
lines changed

4 files changed

+30
-29
lines changed

compiler/rustc_metadata/src/rmeta/encoder.rs

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1600,6 +1600,10 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
16001600
let table = tcx.associated_types_for_impl_traits_in_associated_fn(def_id);
16011601
record_defaulted_array!(self.tables.associated_types_for_impl_traits_in_associated_fn[def_id] <- table);
16021602
}
1603+
if let DefKind::Mod = tcx.def_kind(def_id) {
1604+
record!(self.tables.doc_link_resolutions[def_id] <- tcx.doc_link_resolutions(def_id));
1605+
record_array!(self.tables.doc_link_traits_in_scope[def_id] <- tcx.doc_link_traits_in_scope(def_id));
1606+
}
16031607
}
16041608

16051609
for (def_id, impls) in &tcx.crate_inherent_impls(()).0.inherent_impls {
@@ -1608,14 +1612,6 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
16081612
def_id.index
16091613
}));
16101614
}
1611-
1612-
for (def_id, res_map) in &tcx.resolutions(()).doc_link_resolutions {
1613-
record!(self.tables.doc_link_resolutions[def_id.to_def_id()] <- res_map);
1614-
}
1615-
1616-
for (def_id, traits) in &tcx.resolutions(()).doc_link_traits_in_scope {
1617-
record_array!(self.tables.doc_link_traits_in_scope[def_id.to_def_id()] <- traits);
1618-
}
16191615
}
16201616

16211617
#[instrument(level = "trace", skip(self))]

compiler/rustc_middle/src/query/mod.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,16 @@ rustc_queries! {
204204
desc { "getting the resolver for lowering" }
205205
}
206206

207+
/// Named module children from all kinds of items, including imports.
208+
/// In addition to regular items this list also includes struct and variant constructors, and
209+
/// items inside `extern {}` blocks because all of them introduce names into parent module.
210+
///
211+
/// Module here is understood in name resolution sense - it can be a `mod` item,
212+
/// or a crate root, or an enum, or a trait.
213+
query module_children_local(key: LocalDefId) -> &'tcx [ModChild] {
214+
desc { |tcx| "module exports for `{}`", tcx.def_path_str(key) }
215+
}
216+
207217
/// Return the span for a definition.
208218
///
209219
/// Contrary to `def_span` below, this query returns the full absolute span of the definition.

compiler/rustc_middle/src/ty/context.rs

Lines changed: 6 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,6 @@ use crate::arena::Arena;
6363
use crate::dep_graph::{DepGraph, DepKindStruct};
6464
use crate::infer::canonical::{CanonicalParamEnvCache, CanonicalVarKind, CanonicalVarKinds};
6565
use crate::lint::lint_level;
66-
use crate::metadata::ModChild;
6766
use crate::middle::codegen_fn_attrs::{CodegenFnAttrs, TargetFeature};
6867
use crate::middle::{resolve_bound_vars, stability};
6968
use crate::mir::interpret::{self, Allocation, ConstAllocation};
@@ -2081,9 +2080,8 @@ impl<'tcx> TyCtxt<'tcx> {
20812080
}
20822081

20832082
pub fn iter_local_def_id(self) -> impl Iterator<Item = LocalDefId> {
2084-
// Create a dependency to the red node to be sure we re-execute this when the amount of
2085-
// definitions change.
2086-
self.dep_graph.read_index(DepNodeIndex::FOREVER_RED_NODE);
2083+
// Depend on the `analysis` query to ensure compilation if finished.
2084+
self.ensure_ok().analysis(());
20872085

20882086
let definitions = &self.untracked.definitions;
20892087
gen {
@@ -2103,9 +2101,8 @@ impl<'tcx> TyCtxt<'tcx> {
21032101
}
21042102

21052103
pub fn def_path_table(self) -> &'tcx rustc_hir::definitions::DefPathTable {
2106-
// Create a dependency to the crate to be sure we re-execute this when the amount of
2107-
// definitions change.
2108-
self.dep_graph.read_index(DepNodeIndex::FOREVER_RED_NODE);
2104+
// Depend on the `analysis` query to ensure compilation if finished.
2105+
self.ensure_ok().analysis(());
21092106

21102107
// Freeze definitions once we start iterating on them, to prevent adding new ones
21112108
// while iterating. If some query needs to add definitions, it should be `ensure`d above.
@@ -3372,19 +3369,6 @@ impl<'tcx> TyCtxt<'tcx> {
33723369
self.opt_rpitit_info(def_id).is_some()
33733370
}
33743371

3375-
/// Named module children from all kinds of items, including imports.
3376-
/// In addition to regular items this list also includes struct and variant constructors, and
3377-
/// items inside `extern {}` blocks because all of them introduce names into parent module.
3378-
///
3379-
/// Module here is understood in name resolution sense - it can be a `mod` item,
3380-
/// or a crate root, or an enum, or a trait.
3381-
///
3382-
/// This is not a query, making it a query causes perf regressions
3383-
/// (probably due to hashing spans in `ModChild`ren).
3384-
pub fn module_children_local(self, def_id: LocalDefId) -> &'tcx [ModChild] {
3385-
self.resolutions(()).module_children.get(&def_id).map_or(&[], |v| &v[..])
3386-
}
3387-
33883372
pub fn resolver_for_lowering(self) -> &'tcx Steal<(ty::ResolverAstLowering, Arc<ast::Crate>)> {
33893373
self.resolver_for_lowering_raw(()).0
33903374
}
@@ -3436,6 +3420,8 @@ pub struct DeducedParamAttrs {
34363420
}
34373421

34383422
pub fn provide(providers: &mut Providers) {
3423+
providers.module_children_local =
3424+
|tcx, def_id| tcx.resolutions(()).module_children.get(&def_id).map_or(&[], |v| &v[..]);
34393425
providers.maybe_unused_trait_imports =
34403426
|tcx, ()| &tcx.resolutions(()).maybe_unused_trait_imports;
34413427
providers.names_imported_by_glob_use = |tcx, id| {

compiler/rustc_resolve/src/late.rs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1559,12 +1559,17 @@ impl<'a, 'ast, 'ra, 'tcx> LateResolutionVisitor<'a, 'ast, 'ra, 'tcx> {
15591559
}
15601560

15611561
fn with_mod_rib<T>(&mut self, id: NodeId, f: impl FnOnce(&mut Self) -> T) -> T {
1562-
let module = self.r.expect_module(self.r.local_def_id(id).to_def_id());
1562+
let def_id = self.r.local_def_id(id);
1563+
let module = self.r.expect_module(def_id.to_def_id());
15631564
// Move down in the graph.
15641565
let orig_module = replace(&mut self.parent_scope.module, module);
15651566
self.with_rib(ValueNS, RibKind::Module(module), |this| {
15661567
this.with_rib(TypeNS, RibKind::Module(module), |this| {
15671568
let ret = f(this);
1569+
// Make sure there is an entry in these maps, so metadata encoder does not need to
1570+
// understand in which case they are populated.
1571+
this.r.doc_link_resolutions.entry(def_id).or_default();
1572+
this.r.doc_link_traits_in_scope.entry(def_id).or_default();
15681573
this.parent_scope.module = orig_module;
15691574
ret
15701575
})
@@ -5247,6 +5252,10 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
52475252
BuiltinLintDiag::UnusedLabel,
52485253
);
52495254
}
5255+
// Make sure there is an entry in these maps, so metadata encoder does not need to
5256+
// understand in which case they are populated.
5257+
self.doc_link_resolutions.entry(CRATE_DEF_ID).or_default();
5258+
self.doc_link_traits_in_scope.entry(CRATE_DEF_ID).or_default();
52505259
}
52515260
}
52525261

0 commit comments

Comments
 (0)