From be6c36414226d8ff6ad5047870d6ec7a0b2bd505 Mon Sep 17 00:00:00 2001 From: Nicholas Nethercote Date: Thu, 9 Jun 2022 11:51:41 +1000 Subject: [PATCH] simplify_match_pair: avoid the second comparison if possible. Also, the `try_to_bits` always succeeds, so use `unwrap`. --- .../src/build/matches/simplify.rs | 21 +++++++++++-------- 1 file changed, 12 insertions(+), 9 deletions(-) diff --git a/compiler/rustc_mir_build/src/build/matches/simplify.rs b/compiler/rustc_mir_build/src/build/matches/simplify.rs index b4a0c965d6b..c6298904140 100644 --- a/compiler/rustc_mir_build/src/build/matches/simplify.rs +++ b/compiler/rustc_mir_build/src/build/matches/simplify.rs @@ -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(()); }