From fb4b9e9b632e88b1328a76c5636a338e73c9c746 Mon Sep 17 00:00:00 2001 From: Kaz Kylheku Date: Tue, 19 Feb 2019 07:00:30 -0800 Subject: structs: optimize struct creating functions. We reduce consing in the structure instantiating API. * struct.c (make_struct_impl): New static function, formed from make_struct. This takes two "struct args *" arguments, for both the slot/pair property list and the BOA list. (make_struct): Now a wrapper for make_struct_impl. (struct_from_plist): Call make_struct_impl instead of make_struct, avoiding the args_get_list operation that potentially conses args on the stack into a list. Diagnosis is improved because make_struct_impl takes a self argument. (struct_from_args): Call make_struct_impl instead of make_struct. --- struct.c | 26 +++++++++++++++++++------- 1 file changed, 19 insertions(+), 7 deletions(-) (limited to 'struct.c') diff --git a/struct.c b/struct.c index a2399a4d..6de9044f 100644 --- a/struct.c +++ b/struct.c @@ -514,9 +514,9 @@ static void call_postinitfun_chain(struct struct_type *st, val strct) } } -val make_struct(val type, val plist, struct args *args) +static val make_struct_impl(val self, val type, + struct args *plist, struct args *args) { - val self = lit("make-struct"); struct struct_type *st = stype_handle(&type, self); cnum nslots = st->nslots, sl; size_t size = offsetof(struct struct_inst, slot) + sizeof (val) * nslots; @@ -546,8 +546,14 @@ val make_struct(val type, val plist, struct args *args) call_initfun_chain(st, sinst); - for (; plist; plist = cddr(plist)) - slotset(sinst, car(plist), cadr(plist)); + { + cnum index = 0; + while (args_more(plist, index)) { + val slot = args_get(plist, &index); + val value = args_get(plist, &index); + slotset(sinst, slot, value); + } + } if (args_more(args, 0)) { args_decl(args_copy, max(args->fill + 1, ARGS_MIN)); @@ -570,16 +576,22 @@ val make_struct(val type, val plist, struct args *args) return sinst; } +val make_struct(val type, val plist, struct args *boa) +{ + args_decl_list(pargs, ARGS_MIN, plist); + return make_struct_impl(lit("make-struct"), type, pargs, boa); +} + val struct_from_plist(val type, struct args *plist) { - val list = args_get_list(plist); args_decl(boa, 0); - return make_struct(type, list, boa); + return make_struct_impl(lit("struct-from-plist"), type, plist, boa); } val struct_from_args(val type, struct args *boa) { - return make_struct(type, nil, boa); + args_decl(pargs, 0); + return make_struct_impl(lit("struct-from-args"), type, pargs, boa); } static void lazy_struct_init(val sinst, struct struct_inst *si) -- cgit v1.2.3