1
Fork 0

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.
This commit is contained in:
Nicholas Nethercote 2022-06-08 14:49:32 +10:00
parent ca983054e1
commit 7e4ec35d0c

View file

@ -769,14 +769,17 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
use std::cmp::Ordering::*; use std::cmp::Ordering::*;
let tcx = self.tcx; let tcx = self.tcx;
let param_env = self.param_env;
let a = compare_const_vals(tcx, range.lo, value, self.param_env, range.lo.ty())?; let ty = range.lo.ty();
let b = compare_const_vals(tcx, value, range.hi, self.param_env, range.lo.ty())?; // For performance, it's important to only do the second
// `compare_const_vals` if necessary.
match (b, range.end) { Some(
(Less, _) | (Equal, RangeEnd::Included) if a != Greater => Some(true), matches!(compare_const_vals(tcx, range.lo, value, param_env, ty)?, Less | Equal)
_ => Some(false), && matches!(
} (compare_const_vals(tcx, value, range.hi, param_env, ty)?, range.end),
(Less, _) | (Equal, RangeEnd::Included)
),
)
} }
fn values_not_contained_in_range( fn values_not_contained_in_range(