Skip to content

Commit 3332e1e

Browse files
committed
py/objzip: Adjust zip iter code to not use mp_obj_tuple_del.
This is the only location in the code base that uses `mp_obj_tuple_del()`, so we can reduce code size by reworking the iter code not to use that function. The zip iter implementation should now have slightly better GC behaviour: it only allocates the return tuple if needed, instead of allocating it and then freeing it when the zip iterator is exhausted. Signed-off-by: Damien George <damien@micropython.org>
1 parent 71866ed commit 3332e1e

1 file changed

Lines changed: 7 additions & 6 deletions

File tree

py/objzip.c

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -50,19 +50,20 @@ static mp_obj_t zip_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_
5050
static mp_obj_t zip_iternext(mp_obj_t self_in) {
5151
mp_check_self(mp_obj_is_type(self_in, &mp_type_zip));
5252
mp_obj_zip_t *self = MP_OBJ_TO_PTR(self_in);
53-
if (self->n_iters == 0) {
54-
return MP_OBJ_STOP_ITERATION;
55-
}
56-
mp_obj_tuple_t *tuple = MP_OBJ_TO_PTR(mp_obj_new_tuple(self->n_iters, NULL));
57-
53+
mp_obj_tuple_t *tuple = NULL;
5854
for (size_t i = 0; i < self->n_iters; i++) {
5955
mp_obj_t next = mp_iternext(self->iters[i]);
6056
if (next == MP_OBJ_STOP_ITERATION) {
61-
mp_obj_tuple_del(MP_OBJ_FROM_PTR(tuple));
6257
return MP_OBJ_STOP_ITERATION;
6358
}
59+
if (tuple == NULL) {
60+
tuple = MP_OBJ_TO_PTR(mp_obj_new_tuple(self->n_iters, NULL));
61+
}
6462
tuple->items[i] = next;
6563
}
64+
if (tuple == NULL) {
65+
return MP_OBJ_STOP_ITERATION;
66+
}
6667
return MP_OBJ_FROM_PTR(tuple);
6768
}
6869

0 commit comments

Comments
 (0)