Skip to content

Commit 2ca6fac

Browse files
committed
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.
1 parent caea42f commit 2ca6fac

File tree

1 file changed

+7
-2
lines changed

1 file changed

+7
-2
lines changed

src/librustc/ty/mod.rs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2886,7 +2886,7 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
28862886

28872887
pub fn find_field_index(self, ident: Ident, variant: &VariantDef) -> Option<usize> {
28882888
variant.fields.iter().position(|field| {
2889-
self.adjust_ident(ident, variant.def_id, hir::DUMMY_HIR_ID).0 == field.ident.modern()
2889+
self.hygienic_eq(ident, field.ident, variant.def_id)
28902890
})
28912891
}
28922892

@@ -3085,7 +3085,12 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
30853085
/// its supposed definition name (`def_name`). The method also needs `DefId` of the supposed
30863086
/// definition's parent/scope to perform comparison.
30873087
pub fn hygienic_eq(self, use_name: Ident, def_name: Ident, def_parent_def_id: DefId) -> bool {
3088-
self.adjust_ident(use_name, def_parent_def_id, hir::DUMMY_HIR_ID).0 == def_name.modern()
3088+
// We could use `Ident::eq` here, but we deliberately don't. The name
3089+
// comparison fails frequently, and we want to avoid the expensive
3090+
// `modern()` calls required for the span comparison whenever possible.
3091+
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()
30893094
}
30903095

30913096
pub fn adjust_ident(self, mut ident: Ident, scope: DefId, block: hir::HirId) -> (Ident, DefId) {

0 commit comments

Comments
 (0)