From e6003f466e6f5b7eddd01740666230b62ab5a872 Mon Sep 17 00:00:00 2001 From: Damien George Date: Mon, 13 Feb 2017 15:44:31 +1100 Subject: [PATCH] py: De-optimise some uses of mp_getiter, so they don't use the C stack. In these cases the heap is anyway used to create a new object so no real need to use the C stack for iterating. It saves a few bytes of code size. --- py/objarray.c | 3 +-- py/objdict.c | 9 +++------ py/objlist.c | 3 +-- py/objset.c | 15 +++++---------- py/objtuple.c | 3 +-- py/runtime.c | 6 ++---- 6 files changed, 13 insertions(+), 26 deletions(-) diff --git a/py/objarray.c b/py/objarray.c index c81aebb50c..1b590f3c05 100644 --- a/py/objarray.c +++ b/py/objarray.c @@ -141,8 +141,7 @@ STATIC mp_obj_t array_construct(char typecode, mp_obj_t initializer) { mp_obj_array_t *array = array_new(typecode, len); - mp_obj_iter_buf_t iter_buf; - mp_obj_t iterable = mp_getiter(initializer, &iter_buf); + mp_obj_t iterable = mp_getiter(initializer, NULL); mp_obj_t item; size_t i = 0; while ((item = mp_iternext(iterable)) != MP_OBJ_STOP_ITERATION) { diff --git a/py/objdict.c b/py/objdict.c index c52403f715..013cc0a045 100644 --- a/py/objdict.c +++ b/py/objdict.c @@ -250,8 +250,7 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_1(dict_copy_obj, dict_copy); // this is a classmethod STATIC mp_obj_t dict_fromkeys(size_t n_args, const mp_obj_t *args) { - mp_obj_iter_buf_t iter_buf; - mp_obj_t iter = mp_getiter(args[1], &iter_buf); + mp_obj_t iter = mp_getiter(args[1], NULL); mp_obj_t value = mp_const_none; mp_obj_t next = MP_OBJ_NULL; @@ -377,12 +376,10 @@ STATIC mp_obj_t dict_update(size_t n_args, const mp_obj_t *args, mp_map_t *kwarg } } else { // update from a generic iterable of pairs - mp_obj_iter_buf_t iter_buf; - mp_obj_t iter = mp_getiter(args[1], &iter_buf); + mp_obj_t iter = mp_getiter(args[1], NULL); mp_obj_t next = MP_OBJ_NULL; while ((next = mp_iternext(iter)) != MP_OBJ_STOP_ITERATION) { - mp_obj_iter_buf_t inner_iter_buf; - mp_obj_t inneriter = mp_getiter(next, &inner_iter_buf); + mp_obj_t inneriter = mp_getiter(next, NULL); mp_obj_t key = mp_iternext(inneriter); mp_obj_t value = mp_iternext(inneriter); mp_obj_t stop = mp_iternext(inneriter); diff --git a/py/objlist.c b/py/objlist.c index 28da109912..55ee191207 100644 --- a/py/objlist.c +++ b/py/objlist.c @@ -60,8 +60,7 @@ STATIC void list_print(const mp_print_t *print, mp_obj_t o_in, mp_print_kind_t k } STATIC mp_obj_t list_extend_from_iter(mp_obj_t list, mp_obj_t iterable) { - mp_obj_iter_buf_t iter_buf; - mp_obj_t iter = mp_getiter(iterable, &iter_buf); + mp_obj_t iter = mp_getiter(iterable, NULL); mp_obj_t item; while ((item = mp_iternext(iter)) != MP_OBJ_STOP_ITERATION) { mp_obj_list_append(list, item); diff --git a/py/objset.c b/py/objset.c index 99e1e8ca57..f74bc74a07 100644 --- a/py/objset.c +++ b/py/objset.c @@ -129,8 +129,7 @@ STATIC mp_obj_t set_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_ default: { // can only be 0 or 1 arg // 1 argument, an iterable from which we make a new set mp_obj_t set = mp_obj_new_set(0, NULL); - mp_obj_iter_buf_t iter_buf; - mp_obj_t iterable = mp_getiter(args[0], &iter_buf); + mp_obj_t iterable = mp_getiter(args[0], NULL); mp_obj_t item; while ((item = mp_iternext(iterable)) != MP_OBJ_STOP_ITERATION) { mp_obj_set_store(set, item); @@ -235,8 +234,7 @@ STATIC mp_obj_t set_diff_int(size_t n_args, const mp_obj_t *args, bool update) { if (self == other) { set_clear(self); } else { - mp_obj_iter_buf_t iter_buf; - mp_obj_t iter = mp_getiter(other, &iter_buf); + mp_obj_t iter = mp_getiter(other, NULL); mp_obj_t next; while ((next = mp_iternext(iter)) != MP_OBJ_STOP_ITERATION) { set_discard(self, next); @@ -273,8 +271,7 @@ STATIC mp_obj_t set_intersect_int(mp_obj_t self_in, mp_obj_t other, bool update) mp_obj_set_t *self = MP_OBJ_TO_PTR(self_in); mp_obj_set_t *out = MP_OBJ_TO_PTR(mp_obj_new_set(0, NULL)); - mp_obj_iter_buf_t iter_buf; - mp_obj_t iter = mp_getiter(other, &iter_buf); + mp_obj_t iter = mp_getiter(other, NULL); mp_obj_t next; while ((next = mp_iternext(iter)) != MP_OBJ_STOP_ITERATION) { if (mp_set_lookup(&self->set, next, MP_MAP_LOOKUP)) { @@ -414,8 +411,7 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_2(set_remove_obj, set_remove); STATIC mp_obj_t set_symmetric_difference_update(mp_obj_t self_in, mp_obj_t other_in) { check_set(self_in); mp_obj_set_t *self = MP_OBJ_TO_PTR(self_in); - mp_obj_iter_buf_t iter_buf; - mp_obj_t iter = mp_getiter(other_in, &iter_buf); + mp_obj_t iter = mp_getiter(other_in, NULL); mp_obj_t next; while ((next = mp_iternext(iter)) != MP_OBJ_STOP_ITERATION) { mp_set_lookup(&self->set, next, MP_MAP_LOOKUP_ADD_IF_NOT_FOUND_OR_REMOVE_IF_FOUND); @@ -434,8 +430,7 @@ STATIC mp_obj_t set_symmetric_difference(mp_obj_t self_in, mp_obj_t other_in) { STATIC MP_DEFINE_CONST_FUN_OBJ_2(set_symmetric_difference_obj, set_symmetric_difference); STATIC void set_update_int(mp_obj_set_t *self, mp_obj_t other_in) { - mp_obj_iter_buf_t iter_buf; - mp_obj_t iter = mp_getiter(other_in, &iter_buf); + mp_obj_t iter = mp_getiter(other_in, NULL); mp_obj_t next; while ((next = mp_iternext(iter)) != MP_OBJ_STOP_ITERATION) { mp_set_lookup(&self->set, next, MP_MAP_LOOKUP_ADD_IF_NOT_FOUND); diff --git a/py/objtuple.c b/py/objtuple.c index ad45696cae..b28807c0d0 100644 --- a/py/objtuple.c +++ b/py/objtuple.c @@ -82,8 +82,7 @@ STATIC mp_obj_t mp_obj_tuple_make_new(const mp_obj_type_t *type_in, size_t n_arg size_t len = 0; mp_obj_t *items = m_new(mp_obj_t, alloc); - mp_obj_iter_buf_t iter_buf; - mp_obj_t iterable = mp_getiter(args[0], &iter_buf); + mp_obj_t iterable = mp_getiter(args[0], NULL); mp_obj_t item; while ((item = mp_iternext(iterable)) != MP_OBJ_STOP_ITERATION) { if (len >= alloc) { diff --git a/py/runtime.c b/py/runtime.c index b792eabe7c..db6a6f18f9 100644 --- a/py/runtime.c +++ b/py/runtime.c @@ -745,8 +745,7 @@ void mp_call_prepare_args_n_kw_var(bool have_self, size_t n_args_n_kw, const mp_ // get the keys iterable mp_obj_t dest[3]; mp_load_method(kw_dict, MP_QSTR_keys, dest); - mp_obj_iter_buf_t iter_buf; - mp_obj_t iterable = mp_getiter(mp_call_method_n_kw(0, 0, dest), &iter_buf); + mp_obj_t iterable = mp_getiter(mp_call_method_n_kw(0, 0, dest), NULL); mp_obj_t key; while ((key = mp_iternext(iterable)) != MP_OBJ_STOP_ITERATION) { @@ -877,8 +876,7 @@ void mp_unpack_ex(mp_obj_t seq_in, size_t num_in, mp_obj_t *items) { // items destination array, then the rest to a dynamically created list. Once the // iterable is exhausted, we take from this list for the right part of the items. // TODO Improve to waste less memory in the dynamically created list. - mp_obj_iter_buf_t iter_buf; - mp_obj_t iterable = mp_getiter(seq_in, &iter_buf); + mp_obj_t iterable = mp_getiter(seq_in, NULL); mp_obj_t item; for (seq_len = 0; seq_len < num_left; seq_len++) { item = mp_iternext(iterable);