From ef12675137716636077e08a6b5008de638c6d06f Mon Sep 17 00:00:00 2001 From: Kaz Kylheku Date: Thu, 14 Jan 2016 06:11:27 -0800 Subject: Optimization in n-ary numeric functions. * lib.c (nary_op): Avoid the overhead of reduce_left in the two-argument case and just call the binary function. --- lib.c | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) (limited to 'lib.c') diff --git a/lib.c b/lib.c index 2000349c..02bccc1f 100644 --- a/lib.c +++ b/lib.c @@ -2619,18 +2619,25 @@ val numberp(val num) val nary_op(val (*cfunc)(val, val), struct args *args, val emptyval) { - val fi, re; + val fi, se, re; cnum index = 0; - if (!args_more(args, 0)) + if (!args_more(args, index)) return emptyval; - else if (!args_two_more(args, 0)) - return args_atz(args, 0); fi = args_get(args, &index); + + if (!args_more(args, index)) + return fi; + + se = args_get(args, &index); + + if (!args_more(args, index)) + return cfunc(fi, se); + re = args_get_rest(args, index); - return reduce_left(func_n2(cfunc), re, fi, nil); + return reduce_left(func_n2(cfunc), re, cfunc(fi, se), nil); } val plusv(struct args *nlist) -- cgit v1.2.3