Skip to content

Commit 51b56a9

Browse files
Chore: Eliminate unnecessary full-vector clone in extract_iterable.
1 parent 067a9c6 commit 51b56a9

File tree

1 file changed

+12
-11
lines changed

1 file changed

+12
-11
lines changed

compiler/src/modules/vm/builtins.rs

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -85,11 +85,9 @@ impl<'a> VM<'a> {
8585
let o = self.pop()?;
8686
if o.is_heap() {
8787
if let HeapObj::BigInt(b) = self.heap.get(o) {
88-
let pushed = {
89-
let b = b.clone();
90-
self.bigint_to_val(b)?
91-
};
92-
self.push(pushed);
88+
let b = b.clone();
89+
let v = self.bigint_to_val(b)?;
90+
self.push(v);
9391
return Ok(());
9492
}
9593
}
@@ -203,20 +201,22 @@ impl<'a> VM<'a> {
203201
*/
204202

205203
pub fn call_min(&mut self, op: u16) -> Result<(), VmErr> {
206-
let args = self.pop_n(op as usize)?;
204+
let args: Vec<Val> = self.pop_n(op as usize)?;
207205
let items = self.unwrap_single_iterable(args)?;
208206
if items.is_empty() { return Err(VmErr::Type("min() arg is empty sequence".into())); }
209-
let mut m = items[0];
210-
for x in &items[1..] { if self.lt_vals(*x, m)? { m = *x; } }
207+
let m = items[1..].iter().try_fold(items[0], |m, &x| {
208+
self.lt_vals(x, m).map(|lt| if lt { x } else { m })
209+
})?;
211210
self.push(m); Ok(())
212211
}
213212

214213
pub fn call_max(&mut self, op: u16) -> Result<(), VmErr> {
215214
let args = self.pop_n(op as usize)?;
216215
let items = self.unwrap_single_iterable(args)?;
217216
if items.is_empty() { return Err(VmErr::Type("max() arg is empty sequence".into())); }
218-
let mut m = items[0];
219-
for x in &items[1..] { if self.lt_vals(m, *x)? { m = *x; } }
217+
let m = items[1..].iter().try_fold(items[0], |m, &x| {
218+
self.lt_vals(m, x).map(|lt| if lt { x } else { m })
219+
})?;
220220
self.push(m); Ok(())
221221
}
222222

@@ -363,14 +363,15 @@ impl<'a> VM<'a> {
363363

364364
/*
365365
Iterable Unwrap
366-
If single-arg is list/tuple, returns its items; otherwise returns args as-is.
366+
If single-arg is list/tuple/set, returns its items; otherwise returns args as-is.
367367
*/
368368

369369
fn unwrap_single_iterable(&self, args: Vec<Val>) -> Result<Vec<Val>, VmErr> {
370370
if args.len() == 1 && args[0].is_heap() {
371371
match self.heap.get(args[0]) {
372372
HeapObj::List(v) => return Ok(v.borrow().clone()),
373373
HeapObj::Tuple(v) => return Ok(v.clone()),
374+
HeapObj::Set(v) => return Ok(v.borrow().clone()),
374375
_ => {}
375376
}
376377
}

0 commit comments

Comments
 (0)