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.
This commit is contained in:
Damien George 2016-01-03 16:27:55 +00:00
parent 17f324b836
commit 8212d97317
6 changed files with 20 additions and 57 deletions

View File

@ -186,6 +186,7 @@ STATIC mp_obj_t dict_subscr(mp_obj_t self_in, mp_obj_t index, mp_obj_t value) {
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 mp_obj_t dict_it_iternext(mp_obj_t self_in) {
}
}
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);

View File

@ -498,6 +498,7 @@ void mp_obj_list_store(mp_obj_t self_in, mp_obj_t index, mp_obj_t value) {
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 mp_obj_t list_it_iternext(mp_obj_t self_in) {
}
}
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);

View File

@ -42,6 +42,7 @@ typedef struct _mp_obj_set_t {
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 @@ STATIC mp_obj_t set_make_new(mp_obj_t type_in, mp_uint_t n_args, mp_uint_t n_kw,
}
}
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_it_iternext(mp_obj_t self_in) {
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);

View File

@ -2061,6 +2061,7 @@ const byte *mp_obj_str_get_data_no_check(mp_obj_t self_in, size_t *len) {
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 mp_obj_t str_it_iternext(mp_obj_t self_in) {
}
}
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 mp_obj_t bytes_it_iternext(mp_obj_t self_in) {
}
}
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);

View File

@ -267,6 +267,7 @@ const mp_obj_type_t mp_type_str = {
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 mp_obj_t str_it_iternext(mp_obj_t self_in) {
}
}
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);

View File

@ -270,6 +270,7 @@ void mp_obj_tuple_del(mp_obj_t self_in) {
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 mp_obj_t tuple_it_iternext(mp_obj_t self_in) {
}
}
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);