From 8bf197cd81d80d5543ce59a16a4d39ac0d451ff7 Mon Sep 17 00:00:00 2001 From: Noah <33094578+coolreader18@users.noreply.github.com> Date: Thu, 17 Sep 2020 17:28:40 -0500 Subject: [PATCH] Optimize eval_ord --- vm/src/slots.rs | 25 ++++++++++++++----------- 1 file changed, 14 insertions(+), 11 deletions(-) diff --git a/vm/src/slots.rs b/vm/src/slots.rs index b0b217e25..0d337d4e0 100644 --- a/vm/src/slots.rs +++ b/vm/src/slots.rs @@ -243,12 +243,14 @@ pub trait Comparable: PyValue { #[derive(Debug, Copy, Clone, Eq, PartialEq)] pub enum PyComparisonOp { - Lt, - Le, - Eq, - Ne, - Ge, - Gt, + // be intentional with bits so that we can do eval_ord with just a bitwise and + // bits: | Equal | Greater | Less | + Lt = 0b001, + Gt = 0b010, + Ne = 0b011, + Eq = 0b100, + Le = 0b101, + Ge = 0b110, } use PyComparisonOp::*; @@ -265,11 +267,12 @@ impl PyComparisonOp { } pub fn eval_ord(self, ord: Ordering) -> bool { - match ord { - Ordering::Less => matches!(self, Lt | Le | Ne), - Ordering::Equal => matches!(self, Le | Eq | Ge), - Ordering::Greater => matches!(self, Ne | Ge | Gt), - } + let bit = match ord { + Ordering::Less => Lt, + Ordering::Equal => Eq, + Ordering::Greater => Gt, + }; + self as u8 & bit as u8 != 0 } pub fn swapped(self) -> Self {