Skip to content

Commit 81778a5

Browse files
committed
optimize op_mul for small int
1 parent 8c6b0ea commit 81778a5

File tree

1 file changed

+24
-7
lines changed

1 file changed

+24
-7
lines changed

src/more_ops.rs

+24-7
Original file line numberDiff line numberDiff line change
@@ -440,15 +440,32 @@ pub fn op_multiply(a: &mut Allocator, mut input: NodePtr, max_cost: Cost) -> Res
440440
continue;
441441
}
442442

443-
let (v0, l1) = int_atom(a, arg, "*")?;
444-
445-
total *= v0;
446-
cost += MUL_COST_PER_OP;
443+
a.visit_node(arg, |node| -> Result<(), EvalErr> {
444+
match node {
445+
NodeVisitor::Buffer(buf) => {
446+
use crate::number::number_from_u8;
447+
total *= number_from_u8(buf);
448+
let l1 = buf.len();
447449

448-
cost += (l0 + l1) as Cost * MUL_LINEAR_COST_PER_BYTE;
449-
cost += (l0 * l1) as Cost / MUL_SQUARE_COST_PER_BYTE_DIVIDER;
450+
cost += MUL_COST_PER_OP;
451+
cost += (l0 + l1) as Cost * MUL_LINEAR_COST_PER_BYTE;
452+
cost += (l0 * l1) as Cost / MUL_SQUARE_COST_PER_BYTE_DIVIDER;
453+
l0 = limbs_for_int(&total);
454+
Ok(())
455+
}
456+
NodeVisitor::U32(val) => {
457+
total *= *val;
458+
let l1 = len_for_value(*val);
450459

451-
l0 = limbs_for_int(&total);
460+
cost += MUL_COST_PER_OP;
461+
cost += (l0 + l1) as Cost * MUL_LINEAR_COST_PER_BYTE;
462+
cost += (l0 * l1) as Cost / MUL_SQUARE_COST_PER_BYTE_DIVIDER;
463+
l0 = limbs_for_int(&total);
464+
Ok(())
465+
}
466+
NodeVisitor::Pair(_, _) => err(arg, "* requires int args"),
467+
}
468+
})?;
452469
}
453470
let total = a.new_number(total)?;
454471
Ok(malloc_cost(a, cost, total))

0 commit comments

Comments
 (0)