about summary refs log tree commit diff
diff options
context:
space:
mode:
authorNicholas Nethercote <nnethercote@mozilla.com>2019-05-28 06:19:23 +1000
committerNicholas Nethercote <nnethercote@mozilla.com>2019-05-29 09:32:56 +1000
commit2ca6facaaf6dac086acf188b728edce8dd6aa8e3 (patch)
tree47444c761fffebd26a2f71a47df48729ea037805
parentcaea42f6c8ba8f5cc5ed04557ec5d072b107e7b4 (diff)
downloadrust-2ca6facaaf6dac086acf188b728edce8dd6aa8e3.tar.gz
rust-2ca6facaaf6dac086acf188b728edce8dd6aa8e3.zip
Optimize `TyCtxt::hygienic_eq`.
It's measurably faster to avoid the context comparison when possible.

The commit also adds `hygienic_eq` in one appropriate place.
-rw-r--r--src/librustc/ty/mod.rs9
1 files changed, 7 insertions, 2 deletions
diff --git a/src/librustc/ty/mod.rs b/src/librustc/ty/mod.rs
index f1d1abfa0fb..96bb05cedb6 100644
--- a/src/librustc/ty/mod.rs
+++ b/src/librustc/ty/mod.rs
@@ -2886,7 +2886,7 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
 
     pub fn find_field_index(self, ident: Ident, variant: &VariantDef) -> Option<usize> {
         variant.fields.iter().position(|field| {
-            self.adjust_ident(ident, variant.def_id, hir::DUMMY_HIR_ID).0 == field.ident.modern()
+            self.hygienic_eq(ident, field.ident, variant.def_id)
         })
     }
 
@@ -3085,7 +3085,12 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
     /// its supposed definition name (`def_name`). The method also needs `DefId` of the supposed
     /// definition's parent/scope to perform comparison.
     pub fn hygienic_eq(self, use_name: Ident, def_name: Ident, def_parent_def_id: DefId) -> bool {
-        self.adjust_ident(use_name, def_parent_def_id, hir::DUMMY_HIR_ID).0 == def_name.modern()
+        // We could use `Ident::eq` here, but we deliberately don't. The name
+        // 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()
     }
 
     pub fn adjust_ident(self, mut ident: Ident, scope: DefId, block: hir::HirId) -> (Ident, DefId) {