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

Commit 7e4ec35

Browse files
committed
const_range_contains: avoid the second comparison if possible.
This is a performance win for `unicode-normalization`. Also, I find the new formulation easier to read.
1 parent ca98305 commit 7e4ec35

File tree

1 file changed

+11
-8
lines changed
  • compiler/rustc_mir_build/src/build/matches

1 file changed

+11
-8
lines changed

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

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -769,14 +769,17 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
769769
use std::cmp::Ordering::*;
770770

771771
let tcx = self.tcx;
772-
773-
let a = compare_const_vals(tcx, range.lo, value, self.param_env, range.lo.ty())?;
774-
let b = compare_const_vals(tcx, value, range.hi, self.param_env, range.lo.ty())?;
775-
776-
match (b, range.end) {
777-
(Less, _) | (Equal, RangeEnd::Included) if a != Greater => Some(true),
778-
_ => Some(false),
779-
}
772+
let param_env = self.param_env;
773+
let ty = range.lo.ty();
774+
// For performance, it's important to only do the second
775+
// `compare_const_vals` if necessary.
776+
Some(
777+
matches!(compare_const_vals(tcx, range.lo, value, param_env, ty)?, Less | Equal)
778+
&& matches!(
779+
(compare_const_vals(tcx, value, range.hi, param_env, ty)?, range.end),
780+
(Less, _) | (Equal, RangeEnd::Included)
781+
),
782+
)
780783
}
781784

782785
fn values_not_contained_in_range(

0 commit comments

Comments
 (0)