ref: c2b864c7185a983b3ec16f7e38332a6518521e30
parent: ce2bd9c0913fdc7127a52bcb91ffb9d9d03c6c5c
author: Sigrid Solveig Haflínudóttir <sigrid@ftrv.se>
date: Mon Feb 24 19:45:55 EST 2025
reduce branching and FL(...) reads a bit
--- a/src/flisp.c
+++ b/src/flisp.c
@@ -554,8 +554,8 @@
static value_t
_applyn(int n)
{
- value_t f = FL(sp)[-n-1];
value_t *saveSP = FL(sp);
+ value_t f = saveSP[-n-1];
value_t v;
if(iscbuiltin(f))
v = ((cvalue_t*)ptr(f))->cbuiltin(saveSP-n, n);
@@ -676,8 +676,10 @@
fl_hotfn
static value_t
-list(value_t *args, int nargs, int star)
+list(value_t *args, int nargs, bool star)
{
+ if(fl_unlikely(nargs == 0))
+ return FL_nil;
value_t v = cons_reserve(nargs);
cons_t *c = ptr(v);
for(int i = 0; i < nargs; i++){
@@ -697,10 +699,10 @@
{
if(!iscons(L))
return FL_nil;
+ value_t *plcons = FL(sp);
+ value_t *pL = plcons+1;
PUSH(FL_nil);
PUSH(L);
- value_t *plcons = FL(sp)-2;
- value_t *pL = FL(sp)-1;
value_t c;
c = mk_cons(); PUSH(c); // save first cons
car_(c) = car_(*pL);
@@ -725,8 +727,8 @@
{
value_t *saveSP = FL(sp);
value_t v = FL_nil;
- value_t thunk = FL(sp)[-2];
- FL(sp)[-2] = FL(sp)[-1];
+ value_t thunk = saveSP[-2];
+ FL(sp)[-2] = saveSP[-1];
FL(sp)[-1] = thunk;
FL_TRY{
@@ -1059,7 +1061,7 @@
return args[0];
if(nargs == 0)
argcount(nargs, 1);
- return list(args, nargs, 1);
+ return list(args, nargs, true);
}
BUILTIN("stacktrace", stacktrace)
--- a/src/vm.inc
+++ b/src/vm.inc
@@ -287,7 +287,6 @@
if(FL(curheap) > FL(lim)){
FL(sp) = sp;
fl_gc(0);
- sp = FL(sp);
}
cons_t *c = (cons_t*)FL(curheap);
FL(curheap) += sizeof(cons_t);
@@ -337,7 +336,7 @@
}
fixnum_t s = (fixnum_t)nargs - (fixnum_t)i;
if(s > 0){
- value_t v = list(bp+i, s, 0);
+ value_t v = list(bp+i, s, false);
bp[i] = v;
if(s > 1){
bp[i+1] = bp[nargs+0];
@@ -645,12 +644,8 @@
n = *ip++;
LABEL(apply_list):;
value_t v;
- if(n > 0){
- v = list(sp-n, n, 0);
- sp -= n;
- }else{
- v = FL_nil;
- }
+ v = list(sp-n, n, false);
+ sp -= n;
*sp++ = v;
NEXT_OP;
}
@@ -807,10 +802,8 @@
LABEL(apply_vector):;
FL(sp) = sp;
value_t v = alloc_vector(n, 0);
- if(n){
- memcpy(&vector_elt(v, 0), sp-n, n*sizeof(value_t));
- sp -= n;
- }
+ memcpy(&vector_elt(v, 0), sp-n, n*sizeof(value_t));
+ sp -= n;
*sp++ = v;
NEXT_OP;
}