Skip to content

Commit 0a587b8

Browse files
committed
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.
1 parent 9a58d76 commit 0a587b8

1 file changed

Lines changed: 4 additions & 8 deletions

File tree

py/objfun.c

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -54,15 +54,11 @@ mp_obj_t fun_native_call(mp_obj_t self_in, uint n_args, uint n_kw, const mp_obj_
5454
if (self->is_kw) {
5555
// function allows keywords
5656

57-
// TODO if n_kw==0 then don't allocate any memory for map (either pass NULL or allocate it on the heap)
58-
mp_map_t *kw_args = mp_map_new(n_kw);
59-
for (int i = 0; i < 2 * n_kw; i += 2) {
60-
mp_map_lookup(kw_args, args[n_args + i], MP_MAP_LOOKUP_ADD_IF_NOT_FOUND)->value = args[n_args + i + 1];
61-
}
62-
mp_obj_t res = ((mp_fun_kw_t)self->fun)(n_args, args, kw_args);
63-
// TODO clean up kw_args
57+
// we create a map directly from the given args array
58+
mp_map_t kw_args;
59+
mp_map_init_fixed_table(&kw_args, n_kw, args + n_args);
6460

65-
return res;
61+
return ((mp_fun_kw_t)self->fun)(n_args, args, &kw_args);
6662

6763
} else if (self->n_args_min <= 3 && self->n_args_min == self->n_args_max) {
6864
// function requires a fixed number of arguments

0 commit comments

Comments
 (0)