Skip to content

Commit

Permalink
Optimize TyCtxt::adjust_ident.
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
nnethercote committed May 29, 2019
1 parent 2ca6fac commit 2232321
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 16 deletions.
24 changes: 16 additions & 8 deletions src/librustc/ty/mod.rs
Expand Up @@ -3089,20 +3089,28 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
// comparison fails frequently, and we want to avoid the expensive
// `modern()` calls required for the span comparison whenever possible.
use_name.name == def_name.name &&
self.adjust_ident(use_name, def_parent_def_id, hir::DUMMY_HIR_ID).0.span.ctxt() ==
def_name.modern().span.ctxt()
self.adjust_ident(use_name, def_parent_def_id).span.ctxt() == def_name.modern().span.ctxt()
}

pub fn adjust_ident(self, mut ident: Ident, scope: DefId, block: hir::HirId) -> (Ident, DefId) {
ident = ident.modern();
let target_expansion = match scope.krate {
fn expansion_that_defined(self, scope: DefId) -> Mark {
match scope.krate {
LOCAL_CRATE => self.hir().definitions().expansion_that_defined(scope.index),
_ => Mark::root(),
};
let scope = match ident.span.adjust(target_expansion) {
}
}

pub fn adjust_ident(self, mut ident: Ident, scope: DefId) -> Ident {
ident = ident.modern();
ident.span.adjust(self.expansion_that_defined(scope));
ident
}

pub fn adjust_ident_and_get_scope(self, mut ident: Ident, scope: DefId, block: hir::HirId)
-> (Ident, DefId) {
ident = ident.modern();
let scope = match ident.span.adjust(self.expansion_that_defined(scope)) {
Some(actual_expansion) =>
self.hir().definitions().parent_module_of_macro_def(actual_expansion),
None if block == hir::DUMMY_HIR_ID => DefId::local(CRATE_DEF_INDEX), // Dummy DefId
None => self.hir().get_module_parent_by_hir_id(block),
};
(ident, scope)
Expand Down
2 changes: 1 addition & 1 deletion src/librustc_privacy/lib.rs
Expand Up @@ -845,7 +845,7 @@ impl<'a, 'tcx> NamePrivacyVisitor<'a, 'tcx> {
field: &'tcx ty::FieldDef) { // definition of the field
let ident = Ident::new(kw::Invalid, use_ctxt);
let current_hir = self.current_item;
let def_id = self.tcx.adjust_ident(ident, def.did, current_hir).1;
let def_id = self.tcx.adjust_ident_and_get_scope(ident, def.did, current_hir).1;
if !def.is_enum() && !field.vis.is_accessible_from(def_id, self.tcx) {
struct_span_err!(self.tcx.sess, span, E0451, "field `{}` of {} `{}` is private",
field.ident, def.variant_descr(), self.tcx.def_path_str(def.did))
Expand Down
5 changes: 3 additions & 2 deletions src/librustc_typeck/astconv.rs
Expand Up @@ -903,7 +903,7 @@ impl<'o, 'gcx: 'tcx, 'tcx> dyn AstConv<'gcx, 'tcx> + 'o {
}?;

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

let trait_did = bound.def_id();
let (assoc_ident, def_scope) = tcx.adjust_ident(assoc_ident, trait_did, hir_ref_id);
let (assoc_ident, def_scope) =
tcx.adjust_ident_and_get_scope(assoc_ident, trait_did, hir_ref_id);
let item = tcx.associated_items(trait_did).find(|i| {
Namespace::from(i.kind) == Namespace::Type &&
i.ident.modern() == assoc_ident
Expand Down
2 changes: 1 addition & 1 deletion src/librustc_typeck/check/_match.rs
Expand Up @@ -1184,7 +1184,7 @@ https://doc.rust-lang.org/reference/types.html#trait-objects");
let mut inexistent_fields = vec![];
// Typecheck each field.
for &Spanned { node: ref field, span } in fields {
let ident = tcx.adjust_ident(field.ident, variant.def_id, self.body_id).0;
let ident = tcx.adjust_ident(field.ident, variant.def_id);
let field_ty = match used_fields.entry(ident) {
Occupied(occupied) => {
struct_span_err!(tcx.sess, span, E0025,
Expand Down
3 changes: 2 additions & 1 deletion src/librustc_typeck/check/method/probe.rs
Expand Up @@ -510,7 +510,8 @@ impl<'a, 'gcx, 'tcx> ProbeContext<'a, 'gcx, 'tcx> {
{
let is_accessible = if let Some(name) = self.method_name {
let item = candidate.item;
let def_scope = self.tcx.adjust_ident(name, item.container.id(), self.body_id).1;
let def_scope =
self.tcx.adjust_ident_and_get_scope(name, item.container.id(), self.body_id).1;
item.vis.is_accessible_from(def_scope, self.tcx)
} else {
true
Expand Down
7 changes: 4 additions & 3 deletions src/librustc_typeck/check/mod.rs
Expand Up @@ -3339,7 +3339,7 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
ty::Adt(base_def, substs) if !base_def.is_enum() => {
debug!("struct named {:?}", base_t);
let (ident, def_scope) =
self.tcx.adjust_ident(field, base_def.did, self.body_id);
self.tcx.adjust_ident_and_get_scope(field, base_def.did, self.body_id);
let fields = &base_def.non_enum_variant().fields;
if let Some(index) = fields.iter().position(|f| f.ident.modern() == ident) {
let field = &fields[index];
Expand Down Expand Up @@ -3510,7 +3510,8 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {

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

// Type-check each field.
for field in ast_fields {
let ident = tcx.adjust_ident(field.ident, variant.def_id, self.body_id).0;
let ident = tcx.adjust_ident(field.ident, variant.def_id);
let field_type = if let Some((i, v_field)) = remaining_fields.remove(&ident) {
seen_fields.insert(ident, field.span);
self.write_field_index(field.hir_id, i);
Expand Down

0 comments on commit 2232321

Please sign in to comment.