Skip to content

Commit

Permalink
py/runtime: Do not overallocate when len is known.
Browse files Browse the repository at this point in the history
This fixes overallocating an extra mp_obj_t when the length of *args and
**args is known. Previously we were allocating 1 mp_obj_t for each
n_args and n_kw plus the length of each *arg and **arg (if they are
known). Since n_args includes *args and n_kw includes **args, this was
allocating an extra mp_obj_t in addition to the length of these args
when unpacked.

To fix this, we just subtract 1 from the length to account for the 1
already implicitly allocated by n_args and n_kw.

Signed-off-by: David Lechner <david@pybricks.com>
  • Loading branch information
dlech authored and dpgeorge committed Mar 31, 2022
1 parent 783b1a8 commit 3679a47
Showing 1 changed file with 6 additions and 4 deletions.
10 changes: 6 additions & 4 deletions py/runtime.c
Expand Up @@ -715,27 +715,29 @@ void mp_call_prepare_args_n_kw_var(bool have_self, size_t n_args_n_kw, const mp_
uint args2_len = 0;

// Try to get a hint for unpacked * args length
uint list_len = 0;
int list_len = 0;

if (star_args != 0) {
for (uint i = 0; i < n_args; i++) {
if (star_args & (1 << i)) {
mp_obj_t len = mp_obj_len_maybe(args[i]);
if (len != MP_OBJ_NULL) {
list_len += mp_obj_get_int(len);
// -1 accounts for 1 of n_args occupied by this arg
list_len += mp_obj_get_int(len) - 1;
}
}
}
}

// Try to get a hint for the size of the kw_dict
uint kw_dict_len = 0;
int kw_dict_len = 0;

for (uint i = 0; i < n_kw; i++) {
mp_obj_t key = args[n_args + i * 2];
mp_obj_t value = args[n_args + i * 2 + 1];
if (key == MP_OBJ_NULL && value != MP_OBJ_NULL && mp_obj_is_type(value, &mp_type_dict)) {
kw_dict_len += mp_obj_dict_len(value);
// -1 accounts for 1 of n_kw occupied by this arg
kw_dict_len += mp_obj_dict_len(value) - 1;
}
}

Expand Down

0 comments on commit 3679a47

Please sign in to comment.