summaryrefslogtreecommitdiff
path: root/src/lisp/ao_lisp_cons.c
diff options
context:
space:
mode:
authorKeith Packard <keithp@keithp.com>2016-11-14 21:25:38 -0800
committerKeith Packard <keithp@keithp.com>2016-11-17 22:18:39 -0800
commitc0a56550cf647a1647392557e82bcaa96934cd51 (patch)
treefe72c38d0dbc28626320bc41219f4d90a6254131 /src/lisp/ao_lisp_cons.c
parent65fcd6afa22bfefb61420e668c16632657eb8b4f (diff)
altos/lisp: Cache freed cons and stack items
Track freed cons cells and stack items from the eval process where possible so that they can be re-used without needing to collect. This dramatically reduces the number of collect calls. Signed-off-by: Keith Packard <keithp@keithp.com>
Diffstat (limited to 'src/lisp/ao_lisp_cons.c')
-rw-r--r--src/lisp/ao_lisp_cons.c32
1 files changed, 25 insertions, 7 deletions
diff --git a/src/lisp/ao_lisp_cons.c b/src/lisp/ao_lisp_cons.c
index 311d63ab..d2b60c9a 100644
--- a/src/lisp/ao_lisp_cons.c
+++ b/src/lisp/ao_lisp_cons.c
@@ -69,24 +69,42 @@ const struct ao_lisp_type ao_lisp_cons_type = {
.name = "cons",
};
+struct ao_lisp_cons *ao_lisp_cons_free_list;
+
struct ao_lisp_cons *
ao_lisp_cons_cons(ao_poly car, struct ao_lisp_cons *cdr)
{
struct ao_lisp_cons *cons;
- ao_lisp_poly_stash(0, car);
- ao_lisp_cons_stash(0, cdr);
- cons = ao_lisp_alloc(sizeof (struct ao_lisp_cons));
- car = ao_lisp_poly_fetch(0);
- cdr = ao_lisp_cons_fetch(0);
- if (!cons)
- return NULL;
+ if (ao_lisp_cons_free_list) {
+ cons = ao_lisp_cons_free_list;
+ ao_lisp_cons_free_list = ao_lisp_poly_cons(cons->cdr);
+ } else {
+ ao_lisp_poly_stash(0, car);
+ ao_lisp_cons_stash(0, cdr);
+ cons = ao_lisp_alloc(sizeof (struct ao_lisp_cons));
+ car = ao_lisp_poly_fetch(0);
+ cdr = ao_lisp_cons_fetch(0);
+ if (!cons)
+ return NULL;
+ }
cons->car = car;
cons->cdr = ao_lisp_cons_poly(cdr);
return cons;
}
void
+ao_lisp_cons_free(struct ao_lisp_cons *cons)
+{
+ while (cons) {
+ ao_poly cdr = cons->cdr;
+ cons->cdr = ao_lisp_cons_poly(ao_lisp_cons_free_list);
+ ao_lisp_cons_free_list = cons;
+ cons = ao_lisp_poly_cons(cdr);
+ }
+}
+
+void
ao_lisp_cons_print(ao_poly c)
{
struct ao_lisp_cons *cons = ao_lisp_poly_cons(c);