simplify_match_pair: avoid the second comparison if possible.

Also, the `try_to_bits` always succeeds, so use `unwrap`.
This commit is contained in:
Nicholas Nethercote 2022-06-09 11:51:41 +10:00
parent c4cd04480b
commit be6c364142

View File

@ -227,15 +227,18 @@ fn simplify_match_pair<'pat>(
_ => (None, 0),
};
if let Some((min, max, sz)) = range {
if let (Some(lo), Some(hi)) = (lo.try_to_bits(sz), hi.try_to_bits(sz)) {
// We want to compare ranges numerically, but the order of the bitwise
// representation of signed integers does not match their numeric order.
// Thus, to correct the ordering, we need to shift the range of signed
// integers to correct the comparison. This is achieved by XORing with a
// bias (see pattern/_match.rs for another pertinent example of this
// pattern).
let (lo, hi) = (lo ^ bias, hi ^ bias);
if lo <= min && (hi > max || hi == max && end == RangeEnd::Included) {
// We want to compare ranges numerically, but the order of the bitwise
// representation of signed integers does not match their numeric order. Thus,
// to correct the ordering, we need to shift the range of signed integers to
// correct the comparison. This is achieved by XORing with a bias (see
// pattern/_match.rs for another pertinent example of this pattern).
//
// Also, for performance, it's important to only do the second `try_to_bits` if
// necessary.
let lo = lo.try_to_bits(sz).unwrap() ^ bias;
if lo <= min {
let hi = hi.try_to_bits(sz).unwrap() ^ bias;
if hi > max || hi == max && end == RangeEnd::Included {
// Irrefutable pattern match.
return Ok(());
}