summaryrefslogtreecommitdiff
path: root/py/objarray.c
diff options
context:
space:
mode:
authorDamien George <damien.p.george@gmail.com>2016-01-09 23:14:54 +0000
committerDamien George <damien.p.george@gmail.com>2017-02-16 18:38:06 +1100
commitae8d86758631e62466a55d179897d2111c3cb1c1 (patch)
tree1852733b57cd4334727203e11d5af76243615636 /py/objarray.c
parent101886f5291fdbef07ba70d386c5e3e644b71cfb (diff)
py: Add iter_buf to getiter type method.
Allows to iterate over the following without allocating on the heap: - tuple - list - string, bytes - bytearray, array - dict (not dict.keys, dict.values, dict.items) - set, frozenset Allows to call the following without heap memory: - all, any, min, max, sum TODO: still need to allocate stack memory in bytecode for iter_buf.
Diffstat (limited to 'py/objarray.c')
-rw-r--r--py/objarray.c14
1 files changed, 9 insertions, 5 deletions
diff --git a/py/objarray.c b/py/objarray.c
index f23791857..c81aebb50 100644
--- a/py/objarray.c
+++ b/py/objarray.c
@@ -59,7 +59,7 @@
#define TYPECODE_MASK (~(size_t)0)
#endif
-STATIC mp_obj_t array_iterator_new(mp_obj_t array_in);
+STATIC mp_obj_t array_iterator_new(mp_obj_t array_in, mp_obj_iter_buf_t *iter_buf);
STATIC mp_obj_t array_append(mp_obj_t self_in, mp_obj_t arg);
STATIC mp_obj_t array_extend(mp_obj_t self_in, mp_obj_t arg_in);
STATIC mp_int_t array_get_buffer(mp_obj_t o_in, mp_buffer_info_t *bufinfo, mp_uint_t flags);
@@ -141,7 +141,8 @@ STATIC mp_obj_t array_construct(char typecode, mp_obj_t initializer) {
mp_obj_array_t *array = array_new(typecode, len);
- mp_obj_t iterable = mp_getiter(initializer);
+ mp_obj_iter_buf_t iter_buf;
+ mp_obj_t iterable = mp_getiter(initializer, &iter_buf);
mp_obj_t item;
size_t i = 0;
while ((item = mp_iternext(iterable)) != MP_OBJ_STOP_ITERATION) {
@@ -608,15 +609,18 @@ STATIC mp_obj_t array_it_iternext(mp_obj_t self_in) {
STATIC const mp_obj_type_t array_it_type = {
{ &mp_type_type },
.name = MP_QSTR_iterator,
- .getiter = mp_identity,
+ .getiter = mp_identity_getiter,
.iternext = array_it_iternext,
};
-STATIC mp_obj_t array_iterator_new(mp_obj_t array_in) {
+STATIC mp_obj_t array_iterator_new(mp_obj_t array_in, mp_obj_iter_buf_t *iter_buf) {
+ assert(sizeof(mp_obj_array_t) <= sizeof(mp_obj_iter_buf_t));
mp_obj_array_t *array = MP_OBJ_TO_PTR(array_in);
- mp_obj_array_it_t *o = m_new0(mp_obj_array_it_t, 1);
+ mp_obj_array_it_t *o = (mp_obj_array_it_t*)iter_buf;
o->base.type = &array_it_type;
o->array = array;
+ o->offset = 0;
+ o->cur = 0;
#if MICROPY_PY_BUILTINS_MEMORYVIEW
if (array->base.type == &mp_type_memoryview) {
o->offset = array->free;