From eae893a1a2da7a075a7c90b23949dce2f57b2d1b Mon Sep 17 00:00:00 2001 From: Kaz Kylheku Date: Thu, 14 Mar 2019 23:18:56 -0700 Subject: append*: save a bit of memory. * lib.c (lazy_appendv_func, lazy_appendv): Do not allocate a cons cell for maintaining the state. Use the function environment for one of the two state values, and thread the other state value through the initial car contents of each lazy cons cell. --- lib.c | 17 ++++++++--------- 1 file changed, 8 insertions(+), 9 deletions(-) (limited to 'lib.c') diff --git a/lib.c b/lib.c index 6e09308c..10df5305 100644 --- a/lib.c +++ b/lib.c @@ -1513,16 +1513,15 @@ val replace_list(val list, val items, val from, val to) } } -static val lazy_appendv_func(val env, val lcons) +static val lazy_appendv_func(val rl, val lcons) { - cons_bind (fl, rl, env); + val fl = us_car(lcons); cons_bind (fe, re, fl); us_rplaca(lcons, fe); if (re) { - us_rplaca(env, re); - us_rplacd(lcons, make_lazy_cons(us_lcons_fun(lcons))); + us_rplacd(lcons, make_lazy_cons_car(us_lcons_fun(lcons), re)); } else if (rl) { do { fl = car(rl); @@ -1531,9 +1530,9 @@ static val lazy_appendv_func(val env, val lcons) if (fl) { if (rl) { - us_rplaca(env, fl); - us_rplacd(env, rl); - us_rplacd(lcons, make_lazy_cons(us_lcons_fun(lcons))); + val fun = us_lcons_fun(lcons); + us_func_set_env(fun, rl); + us_rplacd(lcons, make_lazy_cons_car(fun, fl)); } else { us_rplacd(lcons, fl); } @@ -1562,8 +1561,8 @@ val lazy_appendv(struct args *args) nonempty, nao); { - return make_lazy_cons(func_f1(cons(nonempty, args_get_rest(args, index)), - lazy_appendv_func)); + return make_lazy_cons_car(func_f1(args_get_rest(args, index), + lazy_appendv_func), nonempty); } } -- cgit v1.2.3