Skip to content

Commit 9b2e58c

Browse files
committed
Auto merge of #64673 - Mark-Simulacrum:opt-match-ck, r=<try>
Optimize match checking to avoid layout queries In code with large, single-value match statements, we were previously spending a lot of time running layout_of for the primitive types (integers, chars) -- which is essentially useless. This optimizes the code to avoid those query calls by directly obtaining the size for these types, when possible. We fallback to the (slower) previous code if that fails, so this is not a behavior change. r? @Centril who I believe knows this code enough, but if not feel free to re-assign
2 parents ed8b708 + 511352c commit 9b2e58c

File tree

1 file changed

+21
-1
lines changed

1 file changed

+21
-1
lines changed

src/librustc_mir/hair/pattern/_match.rs

+21-1
Original file line numberDiff line numberDiff line change
@@ -859,7 +859,27 @@ impl<'tcx> IntRange<'tcx> {
859859
}
860860
ConstantValue(val) if is_integral(val.ty) => {
861861
let ty = val.ty;
862-
if let Some(val) = val.try_eval_bits(tcx, param_env, ty) {
862+
let size = match ty.sty {
863+
ty::Char => Size::from_bytes(4),
864+
ty::Int(ity) => {
865+
Integer::from_attr(&tcx, SignedInt(ity)).size()
866+
}
867+
ty::Uint(uty) => {
868+
Integer::from_attr(&tcx, UnsignedInt(uty)).size()
869+
}
870+
// per is_integral above
871+
_ => unreachable!(),
872+
};
873+
// This is purely an optimization -- try_eval_bits is a pretty expensive operation,
874+
// as it invokes the layout_of query, but here we know that the type is a primitive,
875+
// so we don't need all that complexity (hashing, caching, etc.). As such, try to
876+
// skip it.
877+
//
878+
// FIXME: Is it possible for try_to_bits to fail here? There *are* other variants of
879+
// ty::Const, but it seems like they might be impossible to encounter in match
880+
// checking...
881+
if let Some(val) = val.val.try_to_bits(size)
882+
.or_else(|| val.try_eval_bits(tcx, param_env, ty)) {
863883
let bias = IntRange::signed_bias(tcx, ty);
864884
let val = val ^ bias;
865885
Some(IntRange { range: val..=val, ty })

0 commit comments

Comments
 (0)