Skip to content

Commit f22063b

Browse files
committed
Try to fix perf
1 parent 9136618 commit f22063b

File tree

3 files changed

+24
-21
lines changed

3 files changed

+24
-21
lines changed

compiler/rustc_middle/src/hir/map.rs

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -836,7 +836,24 @@ impl<'tcx> TyCtxt<'tcx> {
836836

837837
#[inline]
838838
pub fn hir_opt_ident(self, id: HirId) -> Option<Ident> {
839-
self.hir_crate(()).opt_ident(self, id)
839+
// If possible don't force lowering of delayed owner, as it can lead to cycles.
840+
if let MaybeOwner::Delayed(delayed_owner) =
841+
self.hir_maybe_owner_unprocessed(id.owner.def_id)
842+
{
843+
return Some(delayed_owner.ident);
844+
}
845+
846+
match self.hir_node(id) {
847+
Node::Pat(&Pat { kind: PatKind::Binding(_, _, ident, _), .. }) => Some(ident),
848+
// A `Ctor` doesn't have an identifier itself, but its parent
849+
// struct/variant does. Compare with `hir::Map::span`.
850+
Node::Ctor(..) => match self.parent_hir_node(id) {
851+
Node::Item(item) => Some(item.kind.ident().unwrap()),
852+
Node::Variant(variant) => Some(variant.ident),
853+
_ => unreachable!(),
854+
},
855+
node => node.ident(),
856+
}
840857
}
841858

842859
#[inline]

compiler/rustc_middle/src/hir/mod.rs

Lines changed: 2 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ use rustc_hir::lints::DelayedLint;
2121
use rustc_hir::*;
2222
use rustc_index::IndexVec;
2323
use rustc_macros::{Decodable, Encodable, HashStable};
24-
use rustc_span::{ErrorGuaranteed, ExpnId, Ident, Span};
24+
use rustc_span::{ErrorGuaranteed, ExpnId, Span};
2525

2626
use crate::query::Providers;
2727
use crate::ty::{ResolverAstLowering, TyCtxt};
@@ -76,25 +76,6 @@ impl<'hir> Crate<'hir> {
7676

7777
tcx.delayed_owner(def_id)
7878
}
79-
80-
pub fn opt_ident(&self, tcx: TyCtxt<'hir>, id: HirId) -> Option<Ident> {
81-
// If possible don't force lowering of delayed owner, as it can lead to cycles.
82-
if let MaybeOwner::Delayed(delayed_owner) = self.owners[id.owner.def_id] {
83-
return Some(delayed_owner.ident);
84-
}
85-
86-
match tcx.hir_node(id) {
87-
Node::Pat(&Pat { kind: PatKind::Binding(_, _, ident, _), .. }) => Some(ident),
88-
// A `Ctor` doesn't have an identifier itself, but its parent
89-
// struct/variant does. Compare with `hir::Map::span`.
90-
Node::Ctor(..) => match tcx.parent_hir_node(id) {
91-
Node::Item(item) => Some(item.kind.ident().unwrap()),
92-
Node::Variant(variant) => Some(variant.ident),
93-
_ => unreachable!(),
94-
},
95-
node => node.ident(),
96-
}
97-
}
9879
}
9980

10081
impl<HirCtx: HashStableContext> HashStable<HirCtx> for Crate<'_> {
@@ -510,6 +491,7 @@ pub fn provide(providers: &mut Providers) {
510491
providers.hir_crate_items = map::hir_crate_items;
511492
providers.crate_hash = map::crate_hash;
512493
providers.hir_module_items = map::hir_module_items;
494+
providers.hir_maybe_owner_unprocessed = |tcx, id| &tcx.hir_crate(()).owners[id];
513495
providers.local_def_id_to_hir_id = |tcx, def_id| match tcx.hir_crate(()).owner(tcx, def_id) {
514496
MaybeOwner::Owner(_) => HirId::make_owner(def_id),
515497
MaybeOwner::NonOwner(hir_id) => hir_id,

compiler/rustc_middle/src/queries.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,10 @@ rustc_queries! {
221221
desc { "getting child of lowered delayed AST owner `{}`", tcx.def_path_str(def_id) }
222222
}
223223

224+
query hir_maybe_owner_unprocessed(def_id: LocalDefId) -> &'tcx hir::MaybeOwner<'tcx> {
225+
desc { "getting MaybeOwner straight from `tcx.hir_crate(()).owners` for `{}`", tcx.def_path_str(def_id) }
226+
}
227+
224228
/// All items in the crate.
225229
query hir_crate_items(_: ()) -> &'tcx rustc_middle::hir::ModuleItems {
226230
arena_cache

0 commit comments

Comments
 (0)