Skip to content

Commit c3ceb00

Browse files
committed
Auto merge of #124190 - RalfJung:pat-compare-with-fast-path, r=Nadrieril
PatRangeBoundary::compare_with: als add a fast-path for signed integers Not sure if we have a benchmark that hits this... but it seems odd to only do this for unsigned integers.
2 parents 584f183 + 727fe81 commit c3ceb00

File tree

1 file changed

+8
-5
lines changed

1 file changed

+8
-5
lines changed

compiler/rustc_middle/src/thir.rs

+8-5
Original file line numberDiff line numberDiff line change
@@ -1006,15 +1006,18 @@ impl<'tcx> PatRangeBoundary<'tcx> {
10061006

10071007
// This code is hot when compiling matches with many ranges. So we
10081008
// special-case extraction of evaluated scalars for speed, for types where
1009-
// unsigned int comparisons are appropriate. E.g. `unicode-normalization` has
1009+
// we can do scalar comparisons. E.g. `unicode-normalization` has
10101010
// many ranges such as '\u{037A}'..='\u{037F}', and chars can be compared
10111011
// in this way.
1012-
(Finite(a), Finite(b)) if matches!(ty.kind(), ty::Uint(_) | ty::Char) => {
1012+
(Finite(a), Finite(b)) if matches!(ty.kind(), ty::Int(_) | ty::Uint(_) | ty::Char) => {
10131013
if let (Some(a), Some(b)) = (a.try_to_scalar_int(), b.try_to_scalar_int()) {
10141014
let sz = ty.primitive_size(tcx);
1015-
let a = a.assert_uint(sz);
1016-
let b = b.assert_uint(sz);
1017-
return Some(a.cmp(&b));
1015+
let cmp = match ty.kind() {
1016+
ty::Uint(_) | ty::Char => a.assert_uint(sz).cmp(&b.assert_uint(sz)),
1017+
ty::Int(_) => a.assert_int(sz).cmp(&b.assert_int(sz)),
1018+
_ => unreachable!(),
1019+
};
1020+
return Some(cmp);
10181021
}
10191022
}
10201023
_ => {}

0 commit comments

Comments
 (0)