From 5e57a1502ec05156d4455b91e452b38c0dc482b4 Mon Sep 17 00:00:00 2001 From: Kaz Kylheku Date: Tue, 27 Mar 2018 19:56:55 -0700 Subject: eval: refactor op_prof to support reuse. * eval.c (prof_call): New function, contents based on op_prof. (struct prof_ctx): New struct type. (op_prof_callback): New static function. (op_prof): Reduced to call to prof_call, passing context through to callback which performs the evaluation that is timed. * eval.h (prof_call): Declared. --- eval.c | 20 ++++++++++++++++++-- eval.h | 1 + 2 files changed, 19 insertions(+), 2 deletions(-) diff --git a/eval.c b/eval.c index 4007f51d..cbbcaeb7 100644 --- a/eval.c +++ b/eval.c @@ -2794,12 +2794,12 @@ static val op_with_dyn_rebinds(val form, val env) } } -static val op_prof(val form, val env) +val prof_call(val (*fun)(mem_t *ctx), mem_t *ctx) { clock_t start_time = clock(); alloc_bytes_t start_mlbytes = malloc_bytes; alloc_bytes_t start_gcbytes = gc_bytes; - val result = eval_progn(rest(form), env, form); + val result = fun(ctx); alloc_bytes_t delta_mlbytes = malloc_bytes - start_mlbytes; alloc_bytes_t delta_gcbytes = gc_bytes - start_gcbytes; #if SIZEOF_ALLOC_BYTES_T > SIZEOF_PTR @@ -2822,6 +2822,22 @@ static val op_prof(val form, val env) nao); } +struct prof_ctx { + val form, env; +}; + +static val op_prof_callback(mem_t *ctx) +{ + struct prof_ctx *pctx = coerce(struct prof_ctx *, ctx); + return eval_progn(rest(pctx->form), pctx->env, pctx->form); +} + +static val op_prof(val form, val env) +{ + struct prof_ctx ctx = { form, env }; + return prof_call(op_prof_callback, coerce(mem_t *, &ctx)); +} + static val op_switch(val form, val env) { val args = cdr(form); diff --git a/eval.h b/eval.h index 6261821d..fa0dc88a 100644 --- a/eval.h +++ b/eval.h @@ -78,6 +78,7 @@ val expand_quasi(val quasi_forms, val menv); val load(val target); val expand(val form, val menv); val expand_forms(val forms, val menv); +val prof_call(val (*fun)(mem_t *ctx), mem_t *ctx); val bindable(val obj); val mapcarv(val fun, struct args *lists); val mapcarl(val fun, val list_of_lists); -- cgit v1.2.3