Skip to content

Commit 40f4c1b

Browse files
committed
Auto merge of #139584 - oli-obk:horrible-experiment-1, r=<try>
Avoid a reverse map that is only used in diagnostics paths r? `@ghost` 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 #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 6813f95 + ae8c33c commit 40f4c1b

File tree

8 files changed

+48
-37
lines changed

8 files changed

+48
-37
lines changed

Cargo.lock

-1
Original file line numberDiff line numberDiff line change
@@ -4408,7 +4408,6 @@ dependencies = [
44084408
"rustc_feature",
44094409
"rustc_fluent_macro",
44104410
"rustc_hir",
4411-
"rustc_index",
44124411
"rustc_macros",
44134412
"rustc_metadata",
44144413
"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/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

+2-2
Original file line numberDiff line numberDiff line change
@@ -5007,8 +5007,8 @@ 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+
let node_id = self.r.def_id_to_node_id(local_did);
5011+
!self.r.proc_macros.contains(&node_id)
50125012
}
50135013

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

compiler/rustc_resolve/src/lib.rs

+9-6
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;
@@ -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>,
@@ -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,15 @@ impl<'tcx> Resolver<'_, 'tcx> {
13851382
pub fn tcx(&self) -> TyCtxt<'tcx> {
13861383
self.tcx
13871384
}
1385+
1386+
fn def_id_to_node_id(&self, def_id: LocalDefId) -> NodeId {
1387+
self.node_id_to_def_id
1388+
.items()
1389+
.filter(|(_, v)| v.key() == def_id)
1390+
.map(|(k, _)| *k)
1391+
.get_only()
1392+
.unwrap()
1393+
}
13881394
}
13891395

13901396
impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
@@ -1417,8 +1423,6 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
14171423
&mut Default::default(),
14181424
);
14191425

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);
14221426
let mut node_id_to_def_id = NodeMap::default();
14231427
let crate_feed = tcx.create_local_crate_def_id(crate_span);
14241428

@@ -1553,7 +1557,6 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
15531557
lint_buffer: LintBuffer::default(),
15541558
next_node_id: CRATE_NODE_ID,
15551559
node_id_to_def_id,
1556-
def_id_to_node_id,
15571560
placeholder_field_indices: Default::default(),
15581561
invocation_parents,
15591562
legacy_const_generic_args: Default::default(),

compiler/rustc_resolve/src/macros.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -345,7 +345,7 @@ impl<'ra, 'tcx> ResolverExpand for Resolver<'ra, 'tcx> {
345345
// We already lint the entire macro as unused
346346
continue;
347347
}
348-
let node_id = self.def_id_to_node_id[def_id];
348+
let node_id = self.def_id_to_node_id(def_id);
349349
self.lint_buffer.buffer_lint(
350350
UNUSED_MACRO_RULES,
351351
node_id,
@@ -932,7 +932,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
932932
.invocation_parents
933933
.get(&parent_scope.expansion)
934934
.map_or(ast::CRATE_NODE_ID, |parent| {
935-
self.def_id_to_node_id[parent.parent_def]
935+
self.def_id_to_node_id(parent.parent_def)
936936
});
937937
self.lint_buffer.buffer_lint(
938938
LEGACY_DERIVE_HELPERS,

0 commit comments

Comments
 (0)