Skip to content

Commit 8c6b0ea

Browse files
committed
optimize op_sub for small int
1 parent bf87f30 commit 8c6b0ea

File tree

1 file changed

+19
-4
lines changed

1 file changed

+19
-4
lines changed

src/more_ops.rs

+19-4
Original file line numberDiff line numberDiff line change
@@ -397,12 +397,27 @@ pub fn op_subtract(a: &mut Allocator, mut input: NodePtr, max_cost: Cost) -> Res
397397
input = rest;
398398
cost += ARITH_COST_PER_ARG;
399399
check_cost(a, cost + byte_count as Cost * ARITH_COST_PER_BYTE, max_cost)?;
400-
let (v, len) = int_atom(a, arg, "-")?;
401-
byte_count += len;
402400
if is_first {
403-
total += v;
401+
let (v, len) = int_atom(a, arg, "-")?;
402+
byte_count = len;
403+
total = v;
404404
} else {
405-
total -= v;
405+
a.visit_node(arg, |node| -> Result<(), EvalErr> {
406+
match node {
407+
NodeVisitor::Buffer(buf) => {
408+
use crate::number::number_from_u8;
409+
total -= number_from_u8(buf);
410+
byte_count += buf.len();
411+
Ok(())
412+
}
413+
NodeVisitor::U32(val) => {
414+
total -= *val;
415+
byte_count += len_for_value(*val);
416+
Ok(())
417+
}
418+
NodeVisitor::Pair(_, _) => err(arg, "- requires int args"),
419+
}
420+
})?;
406421
};
407422
is_first = false;
408423
}

0 commit comments

Comments
 (0)