Skip to content

Commit 8bf197c

Browse files
committed
Optimize eval_ord
1 parent 6095557 commit 8bf197c

1 file changed

Lines changed: 14 additions & 11 deletions

File tree

vm/src/slots.rs

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -243,12 +243,14 @@ pub trait Comparable: PyValue {
243243

244244
#[derive(Debug, Copy, Clone, Eq, PartialEq)]
245245
pub enum PyComparisonOp {
246-
Lt,
247-
Le,
248-
Eq,
249-
Ne,
250-
Ge,
251-
Gt,
246+
// be intentional with bits so that we can do eval_ord with just a bitwise and
247+
// bits: | Equal | Greater | Less |
248+
Lt = 0b001,
249+
Gt = 0b010,
250+
Ne = 0b011,
251+
Eq = 0b100,
252+
Le = 0b101,
253+
Ge = 0b110,
252254
}
253255

254256
use PyComparisonOp::*;
@@ -265,11 +267,12 @@ impl PyComparisonOp {
265267
}
266268

267269
pub fn eval_ord(self, ord: Ordering) -> bool {
268-
match ord {
269-
Ordering::Less => matches!(self, Lt | Le | Ne),
270-
Ordering::Equal => matches!(self, Le | Eq | Ge),
271-
Ordering::Greater => matches!(self, Ne | Ge | Gt),
272-
}
270+
let bit = match ord {
271+
Ordering::Less => Lt,
272+
Ordering::Equal => Eq,
273+
Ordering::Greater => Gt,
274+
};
275+
self as u8 & bit as u8 != 0
273276
}
274277

275278
pub fn swapped(self) -> Self {

0 commit comments

Comments
 (0)