Skip to content

Commit 673012f

Browse files
committed
Some performance shenanigans
1 parent 3528a65 commit 673012f

File tree

1 file changed

+40
-29
lines changed

1 file changed

+40
-29
lines changed

Diff for: compiler/rustc_hir_typeck/src/coercion.rs

+40-29
Original file line numberDiff line numberDiff line change
@@ -163,12 +163,18 @@ impl<'f, 'tcx> Coerce<'f, 'tcx> {
163163
&self,
164164
a: Ty<'tcx>,
165165
b: Ty<'tcx>,
166-
mut adjustments: Vec<Adjustment<'tcx>>,
166+
adjustments: impl IntoIterator<Item = Adjustment<'tcx>>,
167167
final_adjustment: Adjust,
168168
) -> CoerceResult<'tcx> {
169169
self.unify_raw(a, b).and_then(|InferOk { value: ty, obligations }| {
170-
adjustments.push(Adjustment { target: ty, kind: final_adjustment });
171-
success(adjustments, ty, obligations)
170+
success(
171+
adjustments
172+
.into_iter()
173+
.chain(std::iter::once(Adjustment { target: ty, kind: final_adjustment }))
174+
.collect(),
175+
ty,
176+
obligations,
177+
)
172178
})
173179
}
174180

@@ -579,15 +585,18 @@ impl<'f, 'tcx> Coerce<'f, 'tcx> {
579585
// We only have the latter, so we use an inference variable
580586
// for the former and let type inference do the rest.
581587
let coerce_target = self.next_ty_var(self.cause.span);
582-
let mut coercion = self.unify_and(
583-
coerce_target,
584-
target,
585-
match reborrow {
586-
None => vec![],
587-
Some((ref deref, ref autoref)) => vec![deref.clone(), autoref.clone()],
588-
},
589-
Adjust::Pointer(PointerCoercion::Unsize),
590-
)?;
588+
589+
let mut coercion = match reborrow {
590+
None => {
591+
self.unify_and(coerce_target, target, [], Adjust::Pointer(PointerCoercion::Unsize))?
592+
}
593+
Some((ref deref, ref autoref)) => self.unify_and(
594+
coerce_target,
595+
target,
596+
[deref.clone(), autoref.clone()],
597+
Adjust::Pointer(PointerCoercion::Unsize),
598+
)?,
599+
};
591600

592601
let mut selcx = traits::SelectionContext::new(self);
593602

@@ -810,7 +819,7 @@ impl<'f, 'tcx> Coerce<'f, 'tcx> {
810819

811820
// To complete the reborrow, we need to make sure we can unify the inner types, and if so we
812821
// add the adjustments.
813-
self.unify_and(a, b, vec![], Adjust::ReborrowPin(mut_b))
822+
self.unify_and(a, b, [], Adjust::ReborrowPin(mut_b))
814823
}
815824

816825
fn coerce_from_safe_fn(
@@ -827,22 +836,24 @@ impl<'f, 'tcx> Coerce<'f, 'tcx> {
827836
&& hdr_b.safety.is_unsafe()
828837
{
829838
let unsafe_a = self.tcx.safe_to_unsafe_fn_ty(fn_ty_a);
830-
let adjustments = match adjustment {
831-
Some(kind) => {
832-
vec![Adjustment { kind, target: Ty::new_fn_ptr(self.tcx, fn_ty_a) }]
833-
}
834-
None => vec![],
835-
};
836-
self.unify_and(
837-
unsafe_a,
838-
b,
839-
adjustments,
840-
Adjust::Pointer(PointerCoercion::UnsafeFnPointer),
841-
)
839+
match adjustment {
840+
Some(kind) => self.unify_and(
841+
unsafe_a,
842+
b,
843+
[Adjustment { kind, target: Ty::new_fn_ptr(self.tcx, fn_ty_a) }],
844+
Adjust::Pointer(PointerCoercion::UnsafeFnPointer),
845+
),
846+
None => self.unify_and(
847+
unsafe_a,
848+
b,
849+
[],
850+
Adjust::Pointer(PointerCoercion::UnsafeFnPointer),
851+
),
852+
}
842853
} else {
843854
let a = Ty::new_fn_ptr(self.tcx, fn_ty_a);
844855
match adjustment {
845-
Some(adjust) => self.unify_and(a, b, vec![], adjust),
856+
Some(adjust) => self.unify_and(a, b, [], adjust),
846857
None => self.unify(a, b),
847858
}
848859
};
@@ -971,7 +982,7 @@ impl<'f, 'tcx> Coerce<'f, 'tcx> {
971982
self.unify_and(
972983
pointer_ty,
973984
b,
974-
vec![],
985+
[],
975986
Adjust::Pointer(PointerCoercion::ClosureFnPointer(safety)),
976987
)
977988
}
@@ -1003,11 +1014,11 @@ impl<'f, 'tcx> Coerce<'f, 'tcx> {
10031014
self.unify_and(
10041015
a_raw,
10051016
b,
1006-
vec![Adjustment { kind: Adjust::Deref(None), target: mt_a.ty }],
1017+
[Adjustment { kind: Adjust::Deref(None), target: mt_a.ty }],
10071018
Adjust::Borrow(AutoBorrow::RawPtr(mutbl_b)),
10081019
)
10091020
} else if mt_a.mutbl != mutbl_b {
1010-
self.unify_and(a_raw, b, vec![], Adjust::Pointer(PointerCoercion::MutToConstPointer))
1021+
self.unify_and(a_raw, b, [], Adjust::Pointer(PointerCoercion::MutToConstPointer))
10111022
} else {
10121023
self.unify(a_raw, b)
10131024
}

0 commit comments

Comments
 (0)