about summary refs log tree commit diff
diff options
context:
space:
mode:
authorNicholas Nethercote <n.nethercote@gmail.com>2022-06-08 14:49:32 +1000
committerNicholas Nethercote <n.nethercote@gmail.com>2022-06-16 10:52:19 +1000
commit7e4ec35d0cc56ac3016e98909aad8688516a43f8 (patch)
tree9688a4c16c781b5fc9c6f3f90b1f366b1d1b4216
parentca983054e19afd74d63c3ed37997f3bf30fe85d0 (diff)
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.
-rw-r--r--compiler/rustc_mir_build/src/build/matches/test.rs19
1 files changed, 11 insertions, 8 deletions
diff --git a/compiler/rustc_mir_build/src/build/matches/test.rs b/compiler/rustc_mir_build/src/build/matches/test.rs
index 3774a395035..62ee73993b8 100644
--- a/compiler/rustc_mir_build/src/build/matches/test.rs
+++ b/compiler/rustc_mir_build/src/build/matches/test.rs
@@ -769,14 +769,17 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
         use std::cmp::Ordering::*;
 
         let tcx = self.tcx;
-
-        let a = compare_const_vals(tcx, range.lo, value, self.param_env, range.lo.ty())?;
-        let b = compare_const_vals(tcx, value, range.hi, self.param_env, range.lo.ty())?;
-
-        match (b, range.end) {
-            (Less, _) | (Equal, RangeEnd::Included) if a != Greater => Some(true),
-            _ => Some(false),
-        }
+        let param_env = self.param_env;
+        let ty = range.lo.ty();
+        // For performance, it's important to only do the second
+        // `compare_const_vals` if necessary.
+        Some(
+            matches!(compare_const_vals(tcx, range.lo, value, param_env, ty)?, Less | Equal)
+                && matches!(
+                    (compare_const_vals(tcx, value, range.hi, param_env, ty)?, range.end),
+                    (Less, _) | (Equal, RangeEnd::Included)
+                ),
+        )
     }
 
     fn values_not_contained_in_range(