Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

Commit c4cd044

Browse files
committed
sort_candidates: avoid the second comparison if possible.
This is a performance win for `unicode-normalization`. The commit also removes the closure, which isn't necessary. And reformulates the comparison into a form I find easier to read.
1 parent 7e4ec35 commit c4cd044

File tree

1 file changed

+19
-25
lines changed
  • compiler/rustc_mir_build/src/build/matches

1 file changed

+19
-25
lines changed

compiler/rustc_mir_build/src/build/matches/test.rs

Lines changed: 19 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -632,39 +632,33 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
632632
}
633633

634634
(&TestKind::Range(test), &PatKind::Range(pat)) => {
635+
use std::cmp::Ordering::*;
636+
635637
if test == pat {
636638
self.candidate_without_match_pair(match_pair_index, candidate);
637639
return Some(0);
638640
}
639641

640-
let no_overlap = (|| {
641-
use rustc_hir::RangeEnd::*;
642-
use std::cmp::Ordering::*;
643-
644-
let tcx = self.tcx;
645-
646-
let test_ty = test.lo.ty();
647-
let lo = compare_const_vals(tcx, test.lo, pat.hi, self.param_env, test_ty)?;
648-
let hi = compare_const_vals(tcx, test.hi, pat.lo, self.param_env, test_ty)?;
649-
650-
match (test.end, pat.end, lo, hi) {
651-
// pat < test
652-
(_, _, Greater, _) |
653-
(_, Excluded, Equal, _) |
654-
// pat > test
655-
(_, _, _, Less) |
656-
(Excluded, _, _, Equal) => Some(true),
657-
_ => Some(false),
658-
}
659-
})();
660-
661-
if let Some(true) = no_overlap {
662-
// Testing range does not overlap with pattern range,
663-
// so the pattern can be matched only if this test fails.
642+
let tcx = self.tcx;
643+
let test_ty = test.lo.ty();
644+
645+
// For performance, it's important to only do the second
646+
// `compare_const_vals` if necessary.
647+
let no_overlap = if matches!(
648+
(compare_const_vals(tcx, test.hi, pat.lo, self.param_env, test_ty)?, test.end),
649+
(Less, _) | (Equal, RangeEnd::Excluded) // test < pat
650+
) || matches!(
651+
(compare_const_vals(tcx, test.lo, pat.hi, self.param_env, test_ty)?, pat.end),
652+
(Greater, _) | (Equal, RangeEnd::Excluded) // test > pat
653+
) {
664654
Some(1)
665655
} else {
666656
None
667-
}
657+
};
658+
659+
// If the testing range does not overlap with pattern range,
660+
// the pattern can be matched only if this test fails.
661+
no_overlap
668662
}
669663

670664
(&TestKind::Range(range), &PatKind::Constant { value }) => {

0 commit comments

Comments
 (0)