Skip to content

Commit 94817e3

Browse files
committed
Pre-compute hir_id_to_def_id mapping
1 parent 6a0f1af commit 94817e3

File tree

1 file changed

+10
-7
lines changed

1 file changed

+10
-7
lines changed

src/librustc_hir/definitions.rs

+10-7
Original file line numberDiff line numberDiff line change
@@ -87,8 +87,8 @@ pub struct Definitions {
8787
def_id_to_node_id: IndexVec<LocalDefId, ast::NodeId>,
8888

8989
pub(super) node_id_to_hir_id: IndexVec<ast::NodeId, Option<hir::HirId>>,
90-
/// The reverse mapping of `node_id_to_hir_id`.
91-
pub(super) hir_id_to_node_id: FxHashMap<hir::HirId, ast::NodeId>,
90+
/// The pre-computed mapping of `hir_id_to_node_id` -> `node_id_to_def_id`.
91+
pub(super) hir_id_to_def_id: FxHashMap<hir::HirId, LocalDefId>,
9292

9393
/// If `ExpnId` is an ID of some macro expansion,
9494
/// then `DefId` is the normal module (`mod`) in which the expanded macro was defined.
@@ -351,8 +351,7 @@ impl Definitions {
351351

352352
#[inline]
353353
pub fn opt_hir_id_to_local_def_id(&self, hir_id: hir::HirId) -> Option<LocalDefId> {
354-
let node_id = self.hir_id_to_node_id[&hir_id];
355-
self.opt_local_def_id(node_id)
354+
self.hir_id_to_def_id.get(&hir_id).copied()
356355
}
357356

358357
/// Retrieves the span of the given `DefId` if `DefId` is in the local crate.
@@ -467,11 +466,15 @@ impl Definitions {
467466
);
468467
self.node_id_to_hir_id = mapping;
469468

470-
// Build the reverse mapping of `node_id_to_hir_id`.
471-
self.hir_id_to_node_id = self
469+
// Build the pre-computed mapping of `hir_id_to_node_id` -> `node_id_to_def_id`.
470+
self.hir_id_to_def_id = self
472471
.node_id_to_hir_id
473472
.iter_enumerated()
474-
.filter_map(|(node_id, &hir_id)| hir_id.map(|hir_id| (hir_id, node_id)))
473+
.filter_map(|(node_id, &hir_id)| {
474+
hir_id.and_then(|hir_id| {
475+
self.node_id_to_def_id.get(&node_id).map(|&def_id| (hir_id, def_id))
476+
})
477+
})
475478
.collect();
476479
}
477480

0 commit comments

Comments
 (0)