Skip to content

Commit b769675

Browse files
authored
Merge pull request #1500 from youknowone/refactor-seq-cmp
Refactor sequence item comparison
2 parents 72b4965 + 2fac9ba commit b769675

File tree

3 files changed

+52
-100
lines changed

3 files changed

+52
-100
lines changed

vm/src/obj/objsequence.rs

+22-66
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ use crate::vm::VirtualMachine;
1010
use num_bigint::{BigInt, ToBigInt};
1111
use num_traits::{One, Signed, ToPrimitive, Zero};
1212

13-
use super::objbool;
1413
use super::objint::{PyInt, PyIntRef};
1514
use super::objlist::PyList;
1615
use super::objslice::{PySlice, PySliceRef};
@@ -285,15 +284,14 @@ pub fn seq_equal(
285284
vm: &VirtualMachine,
286285
zelf: &dyn SimpleSeq,
287286
other: &dyn SimpleSeq,
288-
) -> Result<bool, PyObjectRef> {
287+
) -> PyResult<bool> {
289288
if zelf.len() == other.len() {
290289
for (a, b) in Iterator::zip(zelf.iter(), other.iter()) {
291-
if !a.is(b) {
292-
let eq = vm._eq(a.clone(), b.clone())?;
293-
let value = objbool::boolval(vm, eq)?;
294-
if !value {
295-
return Ok(false);
296-
}
290+
if a.is(b) {
291+
continue;
292+
}
293+
if !vm.bool_eq(a.clone(), b.clone())? {
294+
return Ok(false);
297295
}
298296
}
299297
Ok(true)
@@ -302,84 +300,42 @@ pub fn seq_equal(
302300
}
303301
}
304302

305-
pub fn seq_lt(
306-
vm: &VirtualMachine,
307-
zelf: &dyn SimpleSeq,
308-
other: &dyn SimpleSeq,
309-
) -> Result<bool, PyObjectRef> {
303+
pub fn seq_lt(vm: &VirtualMachine, zelf: &dyn SimpleSeq, other: &dyn SimpleSeq) -> PyResult<bool> {
310304
for (a, b) in Iterator::zip(zelf.iter(), other.iter()) {
311-
if vm.bool_lt(a.clone(), b.clone())? {
312-
return Ok(true);
313-
} else if !vm.bool_eq(a.clone(), b.clone())? {
314-
return Ok(false);
305+
if let Some(v) = vm.bool_seq_lt(a.clone(), b.clone())? {
306+
return Ok(v);
315307
}
316308
}
317-
318-
if zelf.len() == other.len() {
319-
Ok(false)
320-
} else {
321-
Ok(zelf.len() < other.len())
322-
}
309+
Ok(zelf.len() < other.len())
323310
}
324311

325-
pub fn seq_gt(
326-
vm: &VirtualMachine,
327-
zelf: &dyn SimpleSeq,
328-
other: &dyn SimpleSeq,
329-
) -> Result<bool, PyObjectRef> {
312+
pub fn seq_gt(vm: &VirtualMachine, zelf: &dyn SimpleSeq, other: &dyn SimpleSeq) -> PyResult<bool> {
330313
for (a, b) in Iterator::zip(zelf.iter(), other.iter()) {
331-
if vm.bool_gt(a.clone(), b.clone())? {
332-
return Ok(true);
333-
} else if !vm.bool_eq(a.clone(), b.clone())? {
334-
return Ok(false);
314+
if let Some(v) = vm.bool_seq_gt(a.clone(), b.clone())? {
315+
return Ok(v);
335316
}
336317
}
337-
338-
if zelf.len() == other.len() {
339-
Ok(false)
340-
} else {
341-
Ok(zelf.len() > other.len())
342-
}
318+
Ok(zelf.len() > other.len())
343319
}
344320

345-
pub fn seq_ge(
346-
vm: &VirtualMachine,
347-
zelf: &dyn SimpleSeq,
348-
other: &dyn SimpleSeq,
349-
) -> Result<bool, PyObjectRef> {
321+
pub fn seq_ge(vm: &VirtualMachine, zelf: &dyn SimpleSeq, other: &dyn SimpleSeq) -> PyResult<bool> {
350322
for (a, b) in Iterator::zip(zelf.iter(), other.iter()) {
351-
if vm.bool_gt(a.clone(), b.clone())? {
352-
return Ok(true);
353-
} else if !vm.bool_eq(a.clone(), b.clone())? {
354-
return Ok(false);
323+
if let Some(v) = vm.bool_seq_gt(a.clone(), b.clone())? {
324+
return Ok(v);
355325
}
356326
}
357327

358-
if zelf.len() == other.len() {
359-
Ok(true)
360-
} else {
361-
Ok(zelf.len() > other.len())
362-
}
328+
Ok(zelf.len() >= other.len())
363329
}
364330

365-
pub fn seq_le(
366-
vm: &VirtualMachine,
367-
zelf: &dyn SimpleSeq,
368-
other: &dyn SimpleSeq,
369-
) -> Result<bool, PyObjectRef> {
331+
pub fn seq_le(vm: &VirtualMachine, zelf: &dyn SimpleSeq, other: &dyn SimpleSeq) -> PyResult<bool> {
370332
for (a, b) in Iterator::zip(zelf.iter(), other.iter()) {
371-
if vm.bool_lt(a.clone(), b.clone())? {
372-
return Ok(true);
373-
} else if !vm.bool_eq(a.clone(), b.clone())? {
374-
return Ok(false);
333+
if let Some(v) = vm.bool_seq_lt(a.clone(), b.clone())? {
334+
return Ok(v);
375335
}
376336
}
377337

378-
if zelf.len() == other.len() {
379-
Ok(true)
380-
} else {
381-
Ok(zelf.len() < other.len())
382-
}
338+
Ok(zelf.len() <= other.len())
383339
}
384340

385341
pub struct SeqMul<'a> {

vm/src/obj/objslice.rs

+14-28
Original file line numberDiff line numberDiff line change
@@ -129,44 +129,30 @@ impl PySlice {
129129
Ok(true)
130130
}
131131

132+
#[inline]
132133
fn inner_lte(&self, other: &PySlice, eq: bool, vm: &VirtualMachine) -> PyResult<bool> {
133-
if vm.bool_lt(self.start(vm), other.start(vm))? {
134-
return Ok(true);
135-
} else if !vm.bool_eq(self.start(vm), other.start(vm))? {
136-
return Ok(false);
134+
if let Some(v) = vm.bool_seq_lt(self.start(vm), other.start(vm))? {
135+
return Ok(v);
137136
}
138-
139-
if vm.bool_lt(self.stop(vm), other.stop(vm))? {
140-
return Ok(true);
141-
} else if !vm.bool_eq(self.stop(vm), other.stop(vm))? {
142-
return Ok(false);
137+
if let Some(v) = vm.bool_seq_lt(self.stop(vm), other.stop(vm))? {
138+
return Ok(v);
143139
}
144-
145-
if vm.bool_lt(self.step(vm), other.step(vm))? {
146-
return Ok(true);
147-
} else if !vm.bool_eq(self.step(vm), other.step(vm))? {
148-
return Ok(false);
140+
if let Some(v) = vm.bool_seq_lt(self.step(vm), other.step(vm))? {
141+
return Ok(v);
149142
}
150143
Ok(eq)
151144
}
152145

146+
#[inline]
153147
fn inner_gte(&self, other: &PySlice, eq: bool, vm: &VirtualMachine) -> PyResult<bool> {
154-
if vm.bool_gt(self.start(vm), other.start(vm))? {
155-
return Ok(true);
156-
} else if !vm.bool_eq(self.start(vm), other.start(vm))? {
157-
return Ok(false);
148+
if let Some(v) = vm.bool_seq_gt(self.start(vm), other.start(vm))? {
149+
return Ok(v);
158150
}
159-
160-
if vm.bool_gt(self.stop(vm), other.stop(vm))? {
161-
return Ok(true);
162-
} else if !vm.bool_eq(self.stop(vm), other.stop(vm))? {
163-
return Ok(false);
151+
if let Some(v) = vm.bool_seq_gt(self.stop(vm), other.stop(vm))? {
152+
return Ok(v);
164153
}
165-
166-
if vm.bool_gt(self.step(vm), other.step(vm))? {
167-
return Ok(true);
168-
} else if !vm.bool_eq(self.step(vm), other.step(vm))? {
169-
return Ok(false);
154+
if let Some(v) = vm.bool_seq_gt(self.step(vm), other.step(vm))? {
155+
return Ok(v);
170156
}
171157
Ok(eq)
172158
}

vm/src/vm.rs

+16-6
Original file line numberDiff line numberDiff line change
@@ -1272,15 +1272,25 @@ impl VirtualMachine {
12721272
Ok(value)
12731273
}
12741274

1275-
pub fn bool_lt(&self, a: PyObjectRef, b: PyObjectRef) -> PyResult<bool> {
1276-
let lt = self._lt(a.clone(), b.clone())?;
1277-
let value = objbool::boolval(self, lt)?;
1275+
pub fn bool_seq_lt(&self, a: PyObjectRef, b: PyObjectRef) -> PyResult<Option<bool>> {
1276+
let value = if objbool::boolval(self, self._lt(a.clone(), b.clone())?)? {
1277+
Some(true)
1278+
} else if !objbool::boolval(self, self._eq(a.clone(), b.clone())?)? {
1279+
Some(false)
1280+
} else {
1281+
None
1282+
};
12781283
Ok(value)
12791284
}
12801285

1281-
pub fn bool_gt(&self, a: PyObjectRef, b: PyObjectRef) -> PyResult<bool> {
1282-
let gt = self._gt(a.clone(), b.clone())?;
1283-
let value = objbool::boolval(self, gt)?;
1286+
pub fn bool_seq_gt(&self, a: PyObjectRef, b: PyObjectRef) -> PyResult<Option<bool>> {
1287+
let value = if objbool::boolval(self, self._gt(a.clone(), b.clone())?)? {
1288+
Some(true)
1289+
} else if !objbool::boolval(self, self._eq(a.clone(), b.clone())?)? {
1290+
Some(false)
1291+
} else {
1292+
None
1293+
};
12841294
Ok(value)
12851295
}
12861296
}

0 commit comments

Comments
 (0)