From 0a587b85fb19e57644ac420c642d0d75d2f9346f Mon Sep 17 00:00:00 2001 From: Damien George Date: Sat, 8 Feb 2014 18:53:41 +0000 Subject: [PATCH] py: Pass keyword args to native functions by using the stack. Passing keyword arguments to a native function now no longer requires heap memory. The kw_args map is created on the stack using the args array as the table. --- py/objfun.c | 12 ++++-------- 1 file changed, 4 insertions(+), 8 deletions(-) diff --git a/py/objfun.c b/py/objfun.c index fbc0cab118..9ce517f806 100644 --- a/py/objfun.c +++ b/py/objfun.c @@ -54,15 +54,11 @@ mp_obj_t fun_native_call(mp_obj_t self_in, uint n_args, uint n_kw, const mp_obj_ if (self->is_kw) { // function allows keywords - // TODO if n_kw==0 then don't allocate any memory for map (either pass NULL or allocate it on the heap) - mp_map_t *kw_args = mp_map_new(n_kw); - for (int i = 0; i < 2 * n_kw; i += 2) { - mp_map_lookup(kw_args, args[n_args + i], MP_MAP_LOOKUP_ADD_IF_NOT_FOUND)->value = args[n_args + i + 1]; - } - mp_obj_t res = ((mp_fun_kw_t)self->fun)(n_args, args, kw_args); - // TODO clean up kw_args + // we create a map directly from the given args array + mp_map_t kw_args; + mp_map_init_fixed_table(&kw_args, n_kw, args + n_args); - return res; + return ((mp_fun_kw_t)self->fun)(n_args, args, &kw_args); } else if (self->n_args_min <= 3 && self->n_args_min == self->n_args_max) { // function requires a fixed number of arguments