summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorKaz Kylheku <kaz@kylheku.com>2016-01-14 06:11:27 -0800
committerKaz Kylheku <kaz@kylheku.com>2016-01-14 06:11:27 -0800
commitef12675137716636077e08a6b5008de638c6d06f (patch)
tree73d7e8b6495ed54d87f4ec7f1553697da9d3f23c
parente54cd7d4d63d866bf17576d3cac72d6ae51cbcfa (diff)
downloadtxr-ef12675137716636077e08a6b5008de638c6d06f.tar.gz
txr-ef12675137716636077e08a6b5008de638c6d06f.tar.bz2
txr-ef12675137716636077e08a6b5008de638c6d06f.zip
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.
-rw-r--r--lib.c17
1 files changed, 12 insertions, 5 deletions
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)