Skip to content

Commit 041121a

Browse files
committed
Optimize relate_substs by extracting match
There was no need to keep doing the match inside the iterator.
1 parent 4e1927d commit 041121a

File tree

1 file changed

+12
-12
lines changed

1 file changed

+12
-12
lines changed

compiler/rustc_middle/src/ty/relate.rs

+12-12
Original file line numberDiff line numberDiff line change
@@ -142,11 +142,12 @@ pub fn relate_substs<'tcx, R: TypeRelation<'tcx>>(
142142
b_subst: SubstsRef<'tcx>,
143143
) -> RelateResult<'tcx, SubstsRef<'tcx>> {
144144
let tcx = relation.tcx();
145-
let mut cached_ty = None;
146145

147-
let params = iter::zip(a_subst, b_subst).enumerate().map(|(i, (a, b))| {
148-
let (variance, variance_info) = match variances {
149-
Some((ty_def_id, variances)) => {
146+
let zipped = iter::zip(a_subst, b_subst);
147+
match variances {
148+
Some((ty_def_id, variances)) => {
149+
let mut cached_ty = None;
150+
tcx.mk_substs(zipped.enumerate().map(|(i, (a, b))| {
150151
let variance = variances[i];
151152
let variance_info = if variance == ty::Invariant {
152153
let ty = *cached_ty
@@ -155,14 +156,13 @@ pub fn relate_substs<'tcx, R: TypeRelation<'tcx>>(
155156
} else {
156157
ty::VarianceDiagInfo::default()
157158
};
158-
(variance, variance_info)
159-
}
160-
None => (ty::Invariant, ty::VarianceDiagInfo::default()),
161-
};
162-
relation.relate_with_variance(variance, variance_info, a, b)
163-
});
164-
165-
tcx.mk_substs(params)
159+
relation.relate_with_variance(variance, variance_info, a, b)
160+
}))
161+
}
162+
None => tcx.mk_substs(zipped.map(|(a, b)| {
163+
relation.relate_with_variance(ty::Invariant, ty::VarianceDiagInfo::default(), a, b)
164+
})),
165+
}
166166
}
167167

168168
impl<'tcx> Relate<'tcx> for ty::FnSig<'tcx> {

0 commit comments

Comments
 (0)