Skip to content

Commit 40b73e9

Browse files
authored
Unrolled build for rust-lang#139584
Rollup merge of rust-lang#139584 - oli-obk:horrible-experiment-1, r=petrochenkov Avoid a reverse map that is only used in diagnostics paths r? `@petrochenkov` iterating a map until a value matches and returning the key is bad obviously, but it happens very rarely and only on diagnostics paths. It would also be a lot cheaper with rust-lang#138995. Which is actually why I'm trying this out, that PR adds a new entry in `create_def`, which makes `create_def` show up in cachegrind. So I'm trying out if removing adding an entry in `create_def` is a perf improvement
2 parents ed3a4aa + 98d51fb commit 40b73e9

File tree

9 files changed

+59
-49
lines changed

9 files changed

+59
-49
lines changed

Cargo.lock

-1
Original file line numberDiff line numberDiff line change
@@ -4397,7 +4397,6 @@ dependencies = [
43974397
"rustc_feature",
43984398
"rustc_fluent_macro",
43994399
"rustc_hir",
4400-
"rustc_index",
44014400
"rustc_macros",
44024401
"rustc_metadata",
44034402
"rustc_middle",

compiler/rustc_data_structures/src/unord.rs

+10
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,16 @@ impl<T, I: Iterator<Item = T>> UnordItems<T, I> {
109109
pub fn collect<C: From<UnordItems<T, I>>>(self) -> C {
110110
self.into()
111111
}
112+
113+
/// If the iterator has only one element, returns it, otherwise returns `None`.
114+
#[track_caller]
115+
pub fn get_only(mut self) -> Option<T> {
116+
let item = self.0.next();
117+
if self.0.next().is_some() {
118+
return None;
119+
}
120+
item
121+
}
112122
}
113123

114124
impl<T> UnordItems<T, std::iter::Empty<T>> {

compiler/rustc_resolve/Cargo.toml

-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ rustc_expand = { path = "../rustc_expand" }
1818
rustc_feature = { path = "../rustc_feature" }
1919
rustc_fluent_macro = { path = "../rustc_fluent_macro" }
2020
rustc_hir = { path = "../rustc_hir" }
21-
rustc_index = { path = "../rustc_index" }
2221
rustc_macros = { path = "../rustc_macros" }
2322
rustc_metadata = { path = "../rustc_metadata" }
2423
rustc_middle = { path = "../rustc_middle" }

compiler/rustc_resolve/src/build_reduced_graph.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1207,7 +1207,7 @@ impl<'a, 'ra, 'tcx> BuildReducedGraphVisitor<'a, 'ra, 'tcx> {
12071207
for (rule_i, rule_span) in &self.r.macro_map[&def_id.to_def_id()].rule_spans {
12081208
self.r
12091209
.unused_macro_rules
1210-
.entry(def_id)
1210+
.entry(node_id)
12111211
.or_default()
12121212
.insert(*rule_i, (ident, *rule_span));
12131213
}

compiler/rustc_resolve/src/diagnostics.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -170,10 +170,10 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
170170

171171
fn report_with_use_injections(&mut self, krate: &Crate) {
172172
for UseError { mut err, candidates, def_id, instead, suggestion, path, is_call } in
173-
self.use_injections.drain(..)
173+
std::mem::take(&mut self.use_injections)
174174
{
175175
let (span, found_use) = if let Some(def_id) = def_id.as_local() {
176-
UsePlacementFinder::check(krate, self.def_id_to_node_id[def_id])
176+
UsePlacementFinder::check(krate, self.def_id_to_node_id(def_id))
177177
} else {
178178
(None, FoundUse::No)
179179
};
@@ -1435,7 +1435,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
14351435
let import_suggestions =
14361436
self.lookup_import_candidates(ident, Namespace::MacroNS, parent_scope, is_expected);
14371437
let (span, found_use) = match parent_scope.module.nearest_parent_mod().as_local() {
1438-
Some(def_id) => UsePlacementFinder::check(krate, self.def_id_to_node_id[def_id]),
1438+
Some(def_id) => UsePlacementFinder::check(krate, self.def_id_to_node_id(def_id)),
14391439
None => (None, FoundUse::No),
14401440
};
14411441
show_candidates(

compiler/rustc_resolve/src/imports.rs

+22-22
Original file line numberDiff line numberDiff line change
@@ -639,38 +639,38 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
639639
}
640640

641641
if let Some(glob_binding) = resolution.shadowed_glob {
642-
let binding_id = match binding.kind {
643-
NameBindingKind::Res(res) => {
644-
Some(self.def_id_to_node_id[res.def_id().expect_local()])
645-
}
646-
NameBindingKind::Module(module) => {
647-
Some(self.def_id_to_node_id[module.def_id().expect_local()])
648-
}
649-
NameBindingKind::Import { import, .. } => import.id(),
650-
};
651-
652642
if binding.res() != Res::Err
653643
&& glob_binding.res() != Res::Err
654644
&& let NameBindingKind::Import { import: glob_import, .. } =
655645
glob_binding.kind
656-
&& let Some(binding_id) = binding_id
657646
&& let Some(glob_import_id) = glob_import.id()
658647
&& let glob_import_def_id = self.local_def_id(glob_import_id)
659648
&& self.effective_visibilities.is_exported(glob_import_def_id)
660649
&& glob_binding.vis.is_public()
661650
&& !binding.vis.is_public()
662651
{
663-
self.lint_buffer.buffer_lint(
664-
HIDDEN_GLOB_REEXPORTS,
665-
binding_id,
666-
binding.span,
667-
BuiltinLintDiag::HiddenGlobReexports {
668-
name: key.ident.name.to_string(),
669-
namespace: key.ns.descr().to_owned(),
670-
glob_reexport_span: glob_binding.span,
671-
private_item_span: binding.span,
672-
},
673-
);
652+
let binding_id = match binding.kind {
653+
NameBindingKind::Res(res) => {
654+
Some(self.def_id_to_node_id(res.def_id().expect_local()))
655+
}
656+
NameBindingKind::Module(module) => {
657+
Some(self.def_id_to_node_id(module.def_id().expect_local()))
658+
}
659+
NameBindingKind::Import { import, .. } => import.id(),
660+
};
661+
if let Some(binding_id) = binding_id {
662+
self.lint_buffer.buffer_lint(
663+
HIDDEN_GLOB_REEXPORTS,
664+
binding_id,
665+
binding.span,
666+
BuiltinLintDiag::HiddenGlobReexports {
667+
name: key.ident.name.to_string(),
668+
namespace: key.ns.descr().to_owned(),
669+
glob_reexport_span: glob_binding.span,
670+
private_item_span: binding.span,
671+
},
672+
);
673+
}
674674
}
675675
}
676676
}

compiler/rustc_resolve/src/late.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -5007,8 +5007,7 @@ impl<'a, 'ast, 'ra: 'ast, 'tcx> LateResolutionVisitor<'a, 'ast, 'ra, 'tcx> {
50075007
return false;
50085008
}
50095009
let Some(local_did) = did.as_local() else { return true };
5010-
let Some(node_id) = self.r.def_id_to_node_id.get(local_did) else { return true };
5011-
!self.r.proc_macros.contains(node_id)
5010+
!self.r.proc_macros.contains(&local_did)
50125011
}
50135012

50145013
fn resolve_doc_links(&mut self, attrs: &[Attribute], maybe_exported: MaybeExported<'_>) {

compiler/rustc_resolve/src/lib.rs

+16-9
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,6 @@ use rustc_hir::def::{
5656
};
5757
use rustc_hir::def_id::{CRATE_DEF_ID, CrateNum, DefId, LOCAL_CRATE, LocalDefId, LocalDefIdMap};
5858
use rustc_hir::{PrimTy, TraitCandidate};
59-
use rustc_index::IndexVec;
6059
use rustc_metadata::creader::{CStore, CrateLoader};
6160
use rustc_middle::metadata::ModChild;
6261
use rustc_middle::middle::privacy::EffectiveVisibilities;
@@ -1141,7 +1140,7 @@ pub struct Resolver<'ra, 'tcx> {
11411140
ast_transform_scopes: FxHashMap<LocalExpnId, Module<'ra>>,
11421141
unused_macros: FxIndexMap<LocalDefId, (NodeId, Ident)>,
11431142
/// A map from the macro to all its potentially unused arms.
1144-
unused_macro_rules: FxIndexMap<LocalDefId, UnordMap<usize, (Ident, Span)>>,
1143+
unused_macro_rules: FxIndexMap<NodeId, UnordMap<usize, (Ident, Span)>>,
11451144
proc_macro_stubs: FxHashSet<LocalDefId>,
11461145
/// Traces collected during macro resolution and validated when it's complete.
11471146
single_segment_macro_resolutions:
@@ -1184,7 +1183,6 @@ pub struct Resolver<'ra, 'tcx> {
11841183
next_node_id: NodeId,
11851184

11861185
node_id_to_def_id: NodeMap<Feed<'tcx, LocalDefId>>,
1187-
def_id_to_node_id: IndexVec<LocalDefId, ast::NodeId>,
11881186

11891187
/// Indices of unnamed struct or variant fields with unresolved attributes.
11901188
placeholder_field_indices: FxHashMap<NodeId, usize>,
@@ -1202,7 +1200,7 @@ pub struct Resolver<'ra, 'tcx> {
12021200
trait_impls: FxIndexMap<DefId, Vec<LocalDefId>>,
12031201
/// A list of proc macro LocalDefIds, written out in the order in which
12041202
/// they are declared in the static array generated by proc_macro_harness.
1205-
proc_macros: Vec<NodeId>,
1203+
proc_macros: Vec<LocalDefId>,
12061204
confused_type_with_std_module: FxIndexMap<Span, Span>,
12071205
/// Whether lifetime elision was successful.
12081206
lifetime_elision_allowed: FxHashSet<NodeId>,
@@ -1369,7 +1367,6 @@ impl<'tcx> Resolver<'_, 'tcx> {
13691367
debug!("create_def: def_id_to_node_id[{:?}] <-> {:?}", def_id, node_id);
13701368
self.node_id_to_def_id.insert(node_id, feed.downgrade());
13711369
}
1372-
assert_eq!(self.def_id_to_node_id.push(node_id), def_id);
13731370

13741371
feed
13751372
}
@@ -1385,6 +1382,19 @@ impl<'tcx> Resolver<'_, 'tcx> {
13851382
pub fn tcx(&self) -> TyCtxt<'tcx> {
13861383
self.tcx
13871384
}
1385+
1386+
/// This function is very slow, as it iterates over the entire
1387+
/// [Resolver::node_id_to_def_id] map just to find the [NodeId]
1388+
/// that corresponds to the given [LocalDefId]. Only use this in
1389+
/// diagnostics code paths.
1390+
fn def_id_to_node_id(&self, def_id: LocalDefId) -> NodeId {
1391+
self.node_id_to_def_id
1392+
.items()
1393+
.filter(|(_, v)| v.key() == def_id)
1394+
.map(|(k, _)| *k)
1395+
.get_only()
1396+
.unwrap()
1397+
}
13881398
}
13891399

13901400
impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
@@ -1417,8 +1427,6 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
14171427
&mut Default::default(),
14181428
);
14191429

1420-
let mut def_id_to_node_id = IndexVec::default();
1421-
assert_eq!(def_id_to_node_id.push(CRATE_NODE_ID), CRATE_DEF_ID);
14221430
let mut node_id_to_def_id = NodeMap::default();
14231431
let crate_feed = tcx.create_local_crate_def_id(crate_span);
14241432

@@ -1553,7 +1561,6 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
15531561
lint_buffer: LintBuffer::default(),
15541562
next_node_id: CRATE_NODE_ID,
15551563
node_id_to_def_id,
1556-
def_id_to_node_id,
15571564
placeholder_field_indices: Default::default(),
15581565
invocation_parents,
15591566
legacy_const_generic_args: Default::default(),
@@ -1633,7 +1640,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
16331640
}
16341641

16351642
pub fn into_outputs(self) -> ResolverOutputs {
1636-
let proc_macros = self.proc_macros.iter().map(|id| self.local_def_id(*id)).collect();
1643+
let proc_macros = self.proc_macros;
16371644
let expn_that_defined = self.expn_that_defined;
16381645
let extern_crate_map = self.extern_crate_map;
16391646
let maybe_unused_trait_imports = self.maybe_unused_trait_imports;

compiler/rustc_resolve/src/macros.rs

+6-10
Original file line numberDiff line numberDiff line change
@@ -323,8 +323,7 @@ impl<'ra, 'tcx> ResolverExpand for Resolver<'ra, 'tcx> {
323323
}
324324

325325
fn record_macro_rule_usage(&mut self, id: NodeId, rule_i: usize) {
326-
let did = self.local_def_id(id);
327-
if let Some(rules) = self.unused_macro_rules.get_mut(&did) {
326+
if let Some(rules) = self.unused_macro_rules.get_mut(&id) {
328327
rules.remove(&rule_i);
329328
}
330329
}
@@ -337,15 +336,12 @@ impl<'ra, 'tcx> ResolverExpand for Resolver<'ra, 'tcx> {
337336
ident.span,
338337
BuiltinLintDiag::UnusedMacroDefinition(ident.name),
339338
);
339+
// Do not report unused individual rules if the entire macro is unused
340+
self.unused_macro_rules.swap_remove(&node_id);
340341
}
341342

342-
for (&def_id, unused_arms) in self.unused_macro_rules.iter() {
343+
for (&node_id, unused_arms) in self.unused_macro_rules.iter() {
343344
for (&arm_i, &(ident, rule_span)) in unused_arms.to_sorted_stable_ord() {
344-
if self.unused_macros.contains_key(&def_id) {
345-
// We already lint the entire macro as unused
346-
continue;
347-
}
348-
let node_id = self.def_id_to_node_id[def_id];
349345
self.lint_buffer.buffer_lint(
350346
UNUSED_MACRO_RULES,
351347
node_id,
@@ -466,7 +462,7 @@ impl<'ra, 'tcx> ResolverExpand for Resolver<'ra, 'tcx> {
466462
}
467463

468464
fn declare_proc_macro(&mut self, id: NodeId) {
469-
self.proc_macros.push(id)
465+
self.proc_macros.push(self.local_def_id(id))
470466
}
471467

472468
fn append_stripped_cfg_item(&mut self, parent_node: NodeId, ident: Ident, cfg: ast::MetaItem) {
@@ -932,7 +928,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
932928
.invocation_parents
933929
.get(&parent_scope.expansion)
934930
.map_or(ast::CRATE_NODE_ID, |parent| {
935-
self.def_id_to_node_id[parent.parent_def]
931+
self.def_id_to_node_id(parent.parent_def)
936932
});
937933
self.lint_buffer.buffer_lint(
938934
LEGACY_DERIVE_HELPERS,

0 commit comments

Comments
 (0)