Skip to content

Commit 2232321

Browse files
committed
Optimize TyCtxt::adjust_ident.
It's a hot function that returns a 2-tuple, but the hottest call site (`hygienic_eq`) discards the second element. This commit renames `adjust_ident` as `adjust_ident_and_get_scope`, and then introduces a new `adjust_ident` that only computes the first element. This change also avoids the need to pass in an unused `DUMMY_HIR_ID` argument in a couple of places, which is nice.
1 parent 2ca6fac commit 2232321

File tree

6 files changed

+27
-16
lines changed

6 files changed

+27
-16
lines changed

src/librustc/ty/mod.rs

+16-8
Original file line numberDiff line numberDiff line change
@@ -3089,20 +3089,28 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
30893089
// comparison fails frequently, and we want to avoid the expensive
30903090
// `modern()` calls required for the span comparison whenever possible.
30913091
use_name.name == def_name.name &&
3092-
self.adjust_ident(use_name, def_parent_def_id, hir::DUMMY_HIR_ID).0.span.ctxt() ==
3093-
def_name.modern().span.ctxt()
3092+
self.adjust_ident(use_name, def_parent_def_id).span.ctxt() == def_name.modern().span.ctxt()
30943093
}
30953094

3096-
pub fn adjust_ident(self, mut ident: Ident, scope: DefId, block: hir::HirId) -> (Ident, DefId) {
3097-
ident = ident.modern();
3098-
let target_expansion = match scope.krate {
3095+
fn expansion_that_defined(self, scope: DefId) -> Mark {
3096+
match scope.krate {
30993097
LOCAL_CRATE => self.hir().definitions().expansion_that_defined(scope.index),
31003098
_ => Mark::root(),
3101-
};
3102-
let scope = match ident.span.adjust(target_expansion) {
3099+
}
3100+
}
3101+
3102+
pub fn adjust_ident(self, mut ident: Ident, scope: DefId) -> Ident {
3103+
ident = ident.modern();
3104+
ident.span.adjust(self.expansion_that_defined(scope));
3105+
ident
3106+
}
3107+
3108+
pub fn adjust_ident_and_get_scope(self, mut ident: Ident, scope: DefId, block: hir::HirId)
3109+
-> (Ident, DefId) {
3110+
ident = ident.modern();
3111+
let scope = match ident.span.adjust(self.expansion_that_defined(scope)) {
31033112
Some(actual_expansion) =>
31043113
self.hir().definitions().parent_module_of_macro_def(actual_expansion),
3105-
None if block == hir::DUMMY_HIR_ID => DefId::local(CRATE_DEF_INDEX), // Dummy DefId
31063114
None => self.hir().get_module_parent_by_hir_id(block),
31073115
};
31083116
(ident, scope)

src/librustc_privacy/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -845,7 +845,7 @@ impl<'a, 'tcx> NamePrivacyVisitor<'a, 'tcx> {
845845
field: &'tcx ty::FieldDef) { // definition of the field
846846
let ident = Ident::new(kw::Invalid, use_ctxt);
847847
let current_hir = self.current_item;
848-
let def_id = self.tcx.adjust_ident(ident, def.did, current_hir).1;
848+
let def_id = self.tcx.adjust_ident_and_get_scope(ident, def.did, current_hir).1;
849849
if !def.is_enum() && !field.vis.is_accessible_from(def_id, self.tcx) {
850850
struct_span_err!(self.tcx.sess, span, E0451, "field `{}` of {} `{}` is private",
851851
field.ident, def.variant_descr(), self.tcx.def_path_str(def.did))

src/librustc_typeck/astconv.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -903,7 +903,7 @@ impl<'o, 'gcx: 'tcx, 'tcx> dyn AstConv<'gcx, 'tcx> + 'o {
903903
}?;
904904

905905
let (assoc_ident, def_scope) =
906-
tcx.adjust_ident(binding.item_name, candidate.def_id(), hir_ref_id);
906+
tcx.adjust_ident_and_get_scope(binding.item_name, candidate.def_id(), hir_ref_id);
907907
let assoc_ty = tcx.associated_items(candidate.def_id()).find(|i| {
908908
i.kind == ty::AssocKind::Type && i.ident.modern() == assoc_ident
909909
}).expect("missing associated type");
@@ -1433,7 +1433,8 @@ impl<'o, 'gcx: 'tcx, 'tcx> dyn AstConv<'gcx, 'tcx> + 'o {
14331433
};
14341434

14351435
let trait_did = bound.def_id();
1436-
let (assoc_ident, def_scope) = tcx.adjust_ident(assoc_ident, trait_did, hir_ref_id);
1436+
let (assoc_ident, def_scope) =
1437+
tcx.adjust_ident_and_get_scope(assoc_ident, trait_did, hir_ref_id);
14371438
let item = tcx.associated_items(trait_did).find(|i| {
14381439
Namespace::from(i.kind) == Namespace::Type &&
14391440
i.ident.modern() == assoc_ident

src/librustc_typeck/check/_match.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1184,7 +1184,7 @@ https://doc.rust-lang.org/reference/types.html#trait-objects");
11841184
let mut inexistent_fields = vec![];
11851185
// Typecheck each field.
11861186
for &Spanned { node: ref field, span } in fields {
1187-
let ident = tcx.adjust_ident(field.ident, variant.def_id, self.body_id).0;
1187+
let ident = tcx.adjust_ident(field.ident, variant.def_id);
11881188
let field_ty = match used_fields.entry(ident) {
11891189
Occupied(occupied) => {
11901190
struct_span_err!(tcx.sess, span, E0025,

src/librustc_typeck/check/method/probe.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -510,7 +510,8 @@ impl<'a, 'gcx, 'tcx> ProbeContext<'a, 'gcx, 'tcx> {
510510
{
511511
let is_accessible = if let Some(name) = self.method_name {
512512
let item = candidate.item;
513-
let def_scope = self.tcx.adjust_ident(name, item.container.id(), self.body_id).1;
513+
let def_scope =
514+
self.tcx.adjust_ident_and_get_scope(name, item.container.id(), self.body_id).1;
514515
item.vis.is_accessible_from(def_scope, self.tcx)
515516
} else {
516517
true

src/librustc_typeck/check/mod.rs

+4-3
Original file line numberDiff line numberDiff line change
@@ -3339,7 +3339,7 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
33393339
ty::Adt(base_def, substs) if !base_def.is_enum() => {
33403340
debug!("struct named {:?}", base_t);
33413341
let (ident, def_scope) =
3342-
self.tcx.adjust_ident(field, base_def.did, self.body_id);
3342+
self.tcx.adjust_ident_and_get_scope(field, base_def.did, self.body_id);
33433343
let fields = &base_def.non_enum_variant().fields;
33443344
if let Some(index) = fields.iter().position(|f| f.ident.modern() == ident) {
33453345
let field = &fields[index];
@@ -3510,7 +3510,8 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
35103510

35113511
fn available_field_names(&self, variant: &'tcx ty::VariantDef) -> Vec<ast::Name> {
35123512
variant.fields.iter().filter(|field| {
3513-
let def_scope = self.tcx.adjust_ident(field.ident, variant.def_id, self.body_id).1;
3513+
let def_scope =
3514+
self.tcx.adjust_ident_and_get_scope(field.ident, variant.def_id, self.body_id).1;
35143515
field.vis.is_accessible_from(def_scope, self.tcx)
35153516
})
35163517
.map(|field| field.ident.name)
@@ -3628,7 +3629,7 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
36283629

36293630
// Type-check each field.
36303631
for field in ast_fields {
3631-
let ident = tcx.adjust_ident(field.ident, variant.def_id, self.body_id).0;
3632+
let ident = tcx.adjust_ident(field.ident, variant.def_id);
36323633
let field_type = if let Some((i, v_field)) = remaining_fields.remove(&ident) {
36333634
seen_fields.insert(ident, field.span);
36343635
self.write_field_index(field.hir_id, i);

0 commit comments

Comments
 (0)