py: Use polymorphic iterator type where possible to reduce code size.
Only types whose iterator instances still fit in 4 machine words have
been changed to use the polymorphic iterator.
Reduces Thumb2 arch code size by 264 bytes.
diff --git a/py/objdict.c b/py/objdict.c
index 43e1bfe..f321750 100644
--- a/py/objdict.c
+++ b/py/objdict.c
@@ -186,6 +186,7 @@
typedef struct _mp_obj_dict_it_t {
mp_obj_base_t base;
+ mp_fun_1_t iternext;
mp_obj_t dict;
mp_uint_t cur;
} mp_obj_dict_it_t;
@@ -201,16 +202,10 @@
}
}
-STATIC const mp_obj_type_t mp_type_dict_it = {
- { &mp_type_type },
- .name = MP_QSTR_iterator,
- .getiter = mp_identity,
- .iternext = dict_it_iternext,
-};
-
STATIC mp_obj_t dict_getiter(mp_obj_t self_in) {
mp_obj_dict_it_t *o = m_new_obj(mp_obj_dict_it_t);
- o->base.type = &mp_type_dict_it;
+ o->base.type = &mp_type_polymorph_iter;
+ o->iternext = dict_it_iternext;
o->dict = self_in;
o->cur = 0;
return MP_OBJ_FROM_PTR(o);
diff --git a/py/objlist.c b/py/objlist.c
index a4bec9e..d0641f2 100644
--- a/py/objlist.c
+++ b/py/objlist.c
@@ -498,6 +498,7 @@
typedef struct _mp_obj_list_it_t {
mp_obj_base_t base;
+ mp_fun_1_t iternext;
mp_obj_t list;
mp_uint_t cur;
} mp_obj_list_it_t;
@@ -514,16 +515,10 @@
}
}
-STATIC const mp_obj_type_t mp_type_list_it = {
- { &mp_type_type },
- .name = MP_QSTR_iterator,
- .getiter = mp_identity,
- .iternext = list_it_iternext,
-};
-
mp_obj_t mp_obj_new_list_iterator(mp_obj_t list, mp_uint_t cur) {
mp_obj_list_it_t *o = m_new_obj(mp_obj_list_it_t);
- o->base.type = &mp_type_list_it;
+ o->base.type = &mp_type_polymorph_iter;
+ o->iternext = list_it_iternext;
o->list = list;
o->cur = cur;
return MP_OBJ_FROM_PTR(o);
diff --git a/py/objset.c b/py/objset.c
index c09616e..fbcf0cd 100644
--- a/py/objset.c
+++ b/py/objset.c
@@ -42,6 +42,7 @@
typedef struct _mp_obj_set_it_t {
mp_obj_base_t base;
+ mp_fun_1_t iternext;
mp_obj_set_t *set;
mp_uint_t cur;
} mp_obj_set_it_t;
@@ -146,15 +147,7 @@
}
}
-const mp_obj_type_t mp_type_set_it = {
- { &mp_type_type },
- .name = MP_QSTR_iterator,
- .getiter = mp_identity,
- .iternext = set_it_iternext,
-};
-
STATIC mp_obj_t set_it_iternext(mp_obj_t self_in) {
- assert(MP_OBJ_IS_TYPE(self_in, &mp_type_set_it));
mp_obj_set_it_t *self = MP_OBJ_TO_PTR(self_in);
mp_uint_t max = self->set->set.alloc;
mp_set_t *set = &self->set->set;
@@ -171,7 +164,8 @@
STATIC mp_obj_t set_getiter(mp_obj_t set_in) {
mp_obj_set_it_t *o = m_new_obj(mp_obj_set_it_t);
- o->base.type = &mp_type_set_it;
+ o->base.type = &mp_type_polymorph_iter;
+ o->iternext = set_it_iternext;
o->set = (mp_obj_set_t *)MP_OBJ_TO_PTR(set_in);
o->cur = 0;
return MP_OBJ_FROM_PTR(o);
diff --git a/py/objstr.c b/py/objstr.c
index 7315f40..8839acb 100644
--- a/py/objstr.c
+++ b/py/objstr.c
@@ -2061,6 +2061,7 @@
typedef struct _mp_obj_str8_it_t {
mp_obj_base_t base;
+ mp_fun_1_t iternext;
mp_obj_t str;
mp_uint_t cur;
} mp_obj_str8_it_t;
@@ -2078,16 +2079,10 @@
}
}
-STATIC const mp_obj_type_t mp_type_str_it = {
- { &mp_type_type },
- .name = MP_QSTR_iterator,
- .getiter = mp_identity,
- .iternext = str_it_iternext,
-};
-
STATIC mp_obj_t mp_obj_new_str_iterator(mp_obj_t str) {
mp_obj_str8_it_t *o = m_new_obj(mp_obj_str8_it_t);
- o->base.type = &mp_type_str_it;
+ o->base.type = &mp_type_polymorph_iter;
+ o->iternext = str_it_iternext;
o->str = str;
o->cur = 0;
return o;
@@ -2106,16 +2101,10 @@
}
}
-STATIC const mp_obj_type_t mp_type_bytes_it = {
- { &mp_type_type },
- .name = MP_QSTR_iterator,
- .getiter = mp_identity,
- .iternext = bytes_it_iternext,
-};
-
mp_obj_t mp_obj_new_bytes_iterator(mp_obj_t str) {
mp_obj_str8_it_t *o = m_new_obj(mp_obj_str8_it_t);
- o->base.type = &mp_type_bytes_it;
+ o->base.type = &mp_type_polymorph_iter;
+ o->iternext = bytes_it_iternext;
o->str = str;
o->cur = 0;
return MP_OBJ_FROM_PTR(o);
diff --git a/py/objstrunicode.c b/py/objstrunicode.c
index fb3d520..8099e20 100644
--- a/py/objstrunicode.c
+++ b/py/objstrunicode.c
@@ -267,6 +267,7 @@
typedef struct _mp_obj_str_it_t {
mp_obj_base_t base;
+ mp_fun_1_t iternext;
mp_obj_t str;
mp_uint_t cur;
} mp_obj_str_it_t;
@@ -285,16 +286,10 @@
}
}
-STATIC const mp_obj_type_t mp_type_str_it = {
- { &mp_type_type },
- .name = MP_QSTR_iterator,
- .getiter = mp_identity,
- .iternext = str_it_iternext,
-};
-
STATIC mp_obj_t mp_obj_new_str_iterator(mp_obj_t str) {
mp_obj_str_it_t *o = m_new_obj(mp_obj_str_it_t);
- o->base.type = &mp_type_str_it;
+ o->base.type = &mp_type_polymorph_iter;
+ o->iternext = str_it_iternext;
o->str = str;
o->cur = 0;
return MP_OBJ_FROM_PTR(o);
diff --git a/py/objtuple.c b/py/objtuple.c
index 71cb458..de2bfc3 100644
--- a/py/objtuple.c
+++ b/py/objtuple.c
@@ -270,6 +270,7 @@
typedef struct _mp_obj_tuple_it_t {
mp_obj_base_t base;
+ mp_fun_1_t iternext;
mp_obj_tuple_t *tuple;
mp_uint_t cur;
} mp_obj_tuple_it_t;
@@ -285,16 +286,10 @@
}
}
-STATIC const mp_obj_type_t mp_type_tuple_it = {
- { &mp_type_type },
- .name = MP_QSTR_iterator,
- .getiter = mp_identity,
- .iternext = tuple_it_iternext,
-};
-
STATIC mp_obj_t mp_obj_new_tuple_iterator(mp_obj_tuple_t *tuple, mp_uint_t cur) {
mp_obj_tuple_it_t *o = m_new_obj(mp_obj_tuple_it_t);
- o->base.type = &mp_type_tuple_it;
+ o->base.type = &mp_type_polymorph_iter;
+ o->iternext = tuple_it_iternext;
o->tuple = tuple;
o->cur = cur;
return MP_OBJ_FROM_PTR(o);