Skip to content

Commit a6c5ab5

Browse files
committed
Auto merge of rust-lang#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 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 f06e5c1 + e4bd751 commit a6c5ab5

File tree

8 files changed

+25
-17
lines changed

8 files changed

+25
-17
lines changed

Cargo.lock

-1
Original file line numberDiff line numberDiff line change
@@ -4399,7 +4399,6 @@ dependencies = [
43994399
"rustc_feature",
44004400
"rustc_fluent_macro",
44014401
"rustc_hir",
4402-
"rustc_index",
44034402
"rustc_macros",
44044403
"rustc_metadata",
44054404
"rustc_middle",

compiler/rustc_data_structures/src/unord.rs

+7
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,13 @@ 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+
#[track_caller]
114+
pub fn take_sole_item(mut self) -> Option<T> {
115+
let item = self.0.next();
116+
assert!(self.0.next().is_none(), "more than one item found");
117+
item
118+
}
112119
}
113120

114121
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

+2-2
Original file line numberDiff line numberDiff line change
@@ -641,10 +641,10 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
641641
if let Some(glob_binding) = resolution.shadowed_glob {
642642
let binding_id = match binding.kind {
643643
NameBindingKind::Res(res) => {
644-
Some(self.def_id_to_node_id[res.def_id().expect_local()])
644+
Some(self.def_id_to_node_id(res.def_id().expect_local()))
645645
}
646646
NameBindingKind::Module(module) => {
647-
Some(self.def_id_to_node_id[module.def_id().expect_local()])
647+
Some(self.def_id_to_node_id(module.def_id().expect_local()))
648648
}
649649
NameBindingKind::Import { import, .. } => import.id(),
650650
};

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+
.take_sole_item()
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)