Skip to content

Commit

Permalink
py/obj: Merge getiter and iternext mp_obj_type_t slots.
Browse files Browse the repository at this point in the history
The goal here is to remove a slot (making way to turn make_new into a slot)
as well as reduce code size by the ~40 references to mp_identity_getiter
and mp_stream_unbuffered_iter.

This introduces two new type flags:
- MP_TYPE_FLAG_ITER_IS_ITERNEXT: This means that the "iter" slot in the
  type is "iternext", and should use the identity getiter.
- MP_TYPE_FLAG_ITER_IS_CUSTOM: This means that the "iter" slot is a pointer
  to a mp_getiter_iternext_custom_t instance, which then defines both
  getiter and iternext.

And a third flag that is the OR of both, MP_TYPE_FLAG_ITER_IS_STREAM: This
means that the type should use the identity getiter, and
mp_stream_unbuffered_iter as iternext.

Finally, MP_TYPE_FLAG_ITER_IS_GETITER is defined as a no-op flag to give
the default case where "iter" is "getiter".

Signed-off-by: Jim Mussared <jim.mussared@gmail.com>
  • Loading branch information
jimmo authored and dpgeorge committed Sep 19, 2022
1 parent 3c6127d commit 6da41b5
Show file tree
Hide file tree
Showing 54 changed files with 234 additions and 227 deletions.
14 changes: 9 additions & 5 deletions examples/natmod/btree/btree_c.c
Expand Up @@ -95,6 +95,7 @@ int mp_stream_posix_fsync(void *stream) {
}

mp_obj_full_type_t btree_type;
mp_getiter_iternext_custom_t btree_getiter_iternext;

#include "extmod/modbtree.c"

Expand Down Expand Up @@ -122,13 +123,16 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(btree_open_obj, 5, 5, btree_open);
mp_obj_t mpy_init(mp_obj_fun_bc_t *self, size_t n_args, size_t n_kw, mp_obj_t *args) {
MP_DYNRUNTIME_INIT_ENTRY

btree_getiter_iternext.getiter = btree_getiter;
btree_getiter_iternext.iternext = btree_iternext;

btree_type.base.type = (void*)&mp_fun_table.type_type;
btree_type.flags = MP_TYPE_FLAG_ITER_IS_CUSTOM;
btree_type.name = MP_QSTR_btree;
MP_OBJ_TYPE_SET_SLOT(&btree_type, print, btree_print, 0);
MP_OBJ_TYPE_SET_SLOT(&btree_type, getiter, btree_getiter, 1);
MP_OBJ_TYPE_SET_SLOT(&btree_type, iternext, btree_iternext, 2);
MP_OBJ_TYPE_SET_SLOT(&btree_type, binary_op, btree_binary_op, 3);
MP_OBJ_TYPE_SET_SLOT(&btree_type, subscr, btree_subscr, 4);
MP_OBJ_TYPE_SET_SLOT(&btree_type, iter, &btree_getiter_iternext, 1);
MP_OBJ_TYPE_SET_SLOT(&btree_type, binary_op, btree_binary_op, 2);
MP_OBJ_TYPE_SET_SLOT(&btree_type, subscr, btree_subscr, 3);
btree_locals_dict_table[0] = (mp_map_elem_t){ MP_OBJ_NEW_QSTR(MP_QSTR_close), MP_OBJ_FROM_PTR(&btree_close_obj) };
btree_locals_dict_table[1] = (mp_map_elem_t){ MP_OBJ_NEW_QSTR(MP_QSTR_flush), MP_OBJ_FROM_PTR(&btree_flush_obj) };
btree_locals_dict_table[2] = (mp_map_elem_t){ MP_OBJ_NEW_QSTR(MP_QSTR_get), MP_OBJ_FROM_PTR(&btree_get_obj) };
Expand All @@ -137,7 +141,7 @@ mp_obj_t mpy_init(mp_obj_fun_bc_t *self, size_t n_args, size_t n_kw, mp_obj_t *a
btree_locals_dict_table[5] = (mp_map_elem_t){ MP_OBJ_NEW_QSTR(MP_QSTR_keys), MP_OBJ_FROM_PTR(&btree_keys_obj) };
btree_locals_dict_table[6] = (mp_map_elem_t){ MP_OBJ_NEW_QSTR(MP_QSTR_values), MP_OBJ_FROM_PTR(&btree_values_obj) };
btree_locals_dict_table[7] = (mp_map_elem_t){ MP_OBJ_NEW_QSTR(MP_QSTR_items), MP_OBJ_FROM_PTR(&btree_items_obj) };
MP_OBJ_TYPE_SET_SLOT(&btree_type, locals_dict, (void*)&btree_locals_dict, 5);
MP_OBJ_TYPE_SET_SLOT(&btree_type, locals_dict, (void*)&btree_locals_dict, 4);

mp_store_global(MP_QSTR__open, MP_OBJ_FROM_PTR(&btree_open_obj));
mp_store_global(MP_QSTR_INCL, MP_OBJ_NEW_SMALL_INT(FLAG_END_KEY_INCL));
Expand Down
10 changes: 7 additions & 3 deletions extmod/modbtree.c
Expand Up @@ -319,15 +319,19 @@ STATIC const mp_rom_map_elem_t btree_locals_dict_table[] = {

STATIC MP_DEFINE_CONST_DICT(btree_locals_dict, btree_locals_dict_table);

STATIC const mp_getiter_iternext_custom_t btree_getiter_iternext = {
.getiter = btree_getiter,
.iternext = btree_iternext,
};

STATIC MP_DEFINE_CONST_OBJ_TYPE(
btree_type,
MP_QSTR_btree,
MP_TYPE_FLAG_NONE,
MP_TYPE_FLAG_ITER_IS_CUSTOM,
MP_TYPE_NULL_MAKE_NEW,
// Save on qstr's, reuse same as for module
print, btree_print,
getiter, btree_getiter,
iternext, btree_iternext,
iter, &btree_getiter_iternext,
binary_op, btree_binary_op,
subscr, btree_subscr,
locals_dict, &btree_locals_dict
Expand Down
10 changes: 7 additions & 3 deletions extmod/moduasyncio.c
Expand Up @@ -287,14 +287,18 @@ STATIC mp_obj_t task_iternext(mp_obj_t self_in) {
return mp_const_none;
}

STATIC const mp_getiter_iternext_custom_t task_getiter_iternext = {
.getiter = task_getiter,
.iternext = task_iternext,
};

STATIC MP_DEFINE_CONST_OBJ_TYPE(
task_type,
MP_QSTR_Task,
MP_TYPE_FLAG_NONE,
MP_TYPE_FLAG_ITER_IS_CUSTOM,
task_make_new,
attr, task_attr,
getiter, task_getiter,
iternext, task_iternext
iter, &task_getiter_iternext
);

/******************************************************************************/
Expand Down
5 changes: 2 additions & 3 deletions extmod/moduselect.c
Expand Up @@ -339,10 +339,9 @@ STATIC MP_DEFINE_CONST_DICT(poll_locals_dict, poll_locals_dict_table);
STATIC MP_DEFINE_CONST_OBJ_TYPE(
mp_type_poll,
MP_QSTR_poll,
MP_TYPE_FLAG_NONE,
MP_TYPE_FLAG_ITER_IS_ITERNEXT,
MP_TYPE_NULL_MAKE_NEW,
getiter, mp_identity_getiter,
iternext, poll_iternext,
iter, poll_iternext,
locals_dict, &poll_locals_dict
);

Expand Down
2 changes: 0 additions & 2 deletions extmod/modussl_axtls.c
Expand Up @@ -321,8 +321,6 @@ STATIC MP_DEFINE_CONST_OBJ_TYPE(
MP_TYPE_NULL_MAKE_NEW,
// Save on qstr's, reuse same as for module
print, ussl_socket_print,
getiter, NULL,
iternext, NULL,
protocol, &ussl_socket_stream_p,
locals_dict, &ussl_socket_locals_dict
);
Expand Down
2 changes: 0 additions & 2 deletions extmod/modussl_mbedtls.c
Expand Up @@ -399,8 +399,6 @@ STATIC MP_DEFINE_CONST_OBJ_TYPE(
MP_TYPE_NULL_MAKE_NEW,
// Save on qstr's, reuse same as for module
print, socket_print,
getiter, NULL,
iternext, NULL,
protocol, &ussl_socket_stream_p,
locals_dict, &ussl_socket_locals_dict
);
Expand Down
8 changes: 2 additions & 6 deletions extmod/vfs_fat_file.c
Expand Up @@ -179,11 +179,9 @@ STATIC const mp_stream_p_t vfs_fat_fileio_stream_p = {
MP_DEFINE_CONST_OBJ_TYPE(
mp_type_vfs_fat_fileio,
MP_QSTR_FileIO,
MP_TYPE_FLAG_NONE,
MP_TYPE_FLAG_ITER_IS_STREAM,
MP_TYPE_NULL_MAKE_NEW,
print, file_obj_print,
getiter, mp_identity_getiter,
iternext, mp_stream_unbuffered_iter,
protocol, &vfs_fat_fileio_stream_p,
locals_dict, &vfs_fat_rawfile_locals_dict
);
Expand All @@ -198,11 +196,9 @@ STATIC const mp_stream_p_t vfs_fat_textio_stream_p = {
MP_DEFINE_CONST_OBJ_TYPE(
mp_type_vfs_fat_textio,
MP_QSTR_TextIOWrapper,
MP_TYPE_FLAG_NONE,
MP_TYPE_FLAG_ITER_IS_STREAM,
MP_TYPE_NULL_MAKE_NEW,
print, file_obj_print,
getiter, mp_identity_getiter,
iternext, mp_stream_unbuffered_iter,
protocol, &vfs_fat_textio_stream_p,
locals_dict, &vfs_fat_rawfile_locals_dict
);
Expand Down
8 changes: 2 additions & 6 deletions extmod/vfs_lfsx_file.c
Expand Up @@ -223,11 +223,9 @@ STATIC const mp_stream_p_t MP_VFS_LFSx(fileio_stream_p) = {
MP_DEFINE_CONST_OBJ_TYPE(
MP_TYPE_VFS_LFSx_(_fileio),
MP_QSTR_FileIO,
MP_TYPE_FLAG_NONE,
MP_TYPE_FLAG_ITER_IS_STREAM,
MP_TYPE_NULL_MAKE_NEW,
print, MP_VFS_LFSx(file_print),
getiter, mp_identity_getiter,
iternext, mp_stream_unbuffered_iter,
protocol, &MP_VFS_LFSx(fileio_stream_p),
locals_dict, &MP_VFS_LFSx(file_locals_dict)
);
Expand All @@ -242,11 +240,9 @@ STATIC const mp_stream_p_t MP_VFS_LFSx(textio_stream_p) = {
MP_DEFINE_CONST_OBJ_TYPE(
MP_TYPE_VFS_LFSx_(_textio),
MP_QSTR_TextIOWrapper,
MP_TYPE_FLAG_NONE,
MP_TYPE_FLAG_ITER_IS_STREAM,
MP_TYPE_NULL_MAKE_NEW,
print, MP_VFS_LFSx(file_print),
getiter, mp_identity_getiter,
iternext, mp_stream_unbuffered_iter,
protocol, &MP_VFS_LFSx(textio_stream_p),
locals_dict, &MP_VFS_LFSx(file_locals_dict)
);
8 changes: 2 additions & 6 deletions extmod/vfs_posix_file.c
Expand Up @@ -252,11 +252,9 @@ STATIC const mp_stream_p_t vfs_posix_fileio_stream_p = {
MP_DEFINE_CONST_OBJ_TYPE(
mp_type_vfs_posix_fileio,
MP_QSTR_FileIO,
MP_TYPE_FLAG_NONE,
MP_TYPE_FLAG_ITER_IS_STREAM,
MP_TYPE_NULL_MAKE_NEW,
print, vfs_posix_file_print,
getiter, mp_identity_getiter,
iternext, mp_stream_unbuffered_iter,
protocol, &vfs_posix_fileio_stream_p,
locals_dict, &vfs_posix_rawfile_locals_dict
);
Expand All @@ -271,11 +269,9 @@ STATIC const mp_stream_p_t vfs_posix_textio_stream_p = {
MP_DEFINE_CONST_OBJ_TYPE(
mp_type_vfs_posix_textio,
MP_QSTR_TextIOWrapper,
MP_TYPE_FLAG_NONE,
MP_TYPE_FLAG_ITER_IS_STREAM,
MP_TYPE_NULL_MAKE_NEW,
print, vfs_posix_file_print,
getiter, mp_identity_getiter,
iternext, mp_stream_unbuffered_iter,
protocol, &vfs_posix_textio_stream_p,
locals_dict, &vfs_posix_rawfile_locals_dict
);
Expand Down
3 changes: 0 additions & 3 deletions ports/cc3200/mods/modussl.c
Expand Up @@ -64,9 +64,6 @@ STATIC MP_DEFINE_CONST_OBJ_TYPE(
ssl_socket_type,
MP_QSTR_ussl,
MP_TYPE_FLAG_NONE,
MP_TYPE_NULL_MAKE_NEW,
getiter, NULL,
iternext, NULL,
protocol, &socket_stream_p,
locals_dict, &socket_locals_dict
);
Expand Down
4 changes: 1 addition & 3 deletions ports/cc3200/mods/pybuart.c
Expand Up @@ -688,11 +688,9 @@ STATIC const mp_irq_methods_t uart_irq_methods = {
MP_DEFINE_CONST_OBJ_TYPE(
pyb_uart_type,
MP_QSTR_UART,
MP_TYPE_FLAG_NONE,
MP_TYPE_FLAG_ITER_IS_STREAM,
pyb_uart_make_new,
print, pyb_uart_print,
getiter, mp_identity_getiter,
iternext, mp_stream_unbuffered_iter,
protocol, &uart_stream_p,
locals_dict, &pyb_uart_locals_dict
);
Expand Down
4 changes: 1 addition & 3 deletions ports/esp32/machine_i2s.c
Expand Up @@ -832,11 +832,9 @@ STATIC const mp_stream_p_t i2s_stream_p = {
MP_DEFINE_CONST_OBJ_TYPE(
machine_i2s_type,
MP_QSTR_I2S,
MP_TYPE_FLAG_NONE,
MP_TYPE_FLAG_ITER_IS_STREAM,
machine_i2s_make_new,
print, machine_i2s_print,
getiter, mp_identity_getiter,
iternext, mp_stream_unbuffered_iter,
protocol, &i2s_stream_p,
locals_dict, &machine_i2s_locals_dict
);
Expand Down
4 changes: 1 addition & 3 deletions ports/esp32/machine_uart.c
Expand Up @@ -533,11 +533,9 @@ STATIC const mp_stream_p_t uart_stream_p = {
MP_DEFINE_CONST_OBJ_TYPE(
machine_uart_type,
MP_QSTR_UART,
MP_TYPE_FLAG_NONE,
MP_TYPE_FLAG_ITER_IS_STREAM,
machine_uart_make_new,
print, machine_uart_print,
getiter, mp_identity_getiter,
iternext, mp_stream_unbuffered_iter,
protocol, &uart_stream_p,
locals_dict, &machine_uart_locals_dict
);
4 changes: 1 addition & 3 deletions ports/esp8266/machine_uart.c
Expand Up @@ -346,11 +346,9 @@ STATIC const mp_stream_p_t uart_stream_p = {
MP_DEFINE_CONST_OBJ_TYPE(
pyb_uart_type,
MP_QSTR_UART,
MP_TYPE_FLAG_NONE,
MP_TYPE_FLAG_ITER_IS_STREAM,
pyb_uart_make_new,
print, pyb_uart_print,
getiter, mp_identity_getiter,
iternext, mp_stream_unbuffered_iter,
protocol, &uart_stream_p,
locals_dict, &pyb_uart_locals_dict
);
Expand Down
4 changes: 1 addition & 3 deletions ports/mimxrt/machine_i2s.c
Expand Up @@ -1216,11 +1216,9 @@ STATIC const mp_stream_p_t i2s_stream_p = {
MP_DEFINE_CONST_OBJ_TYPE(
machine_i2s_type,
MP_QSTR_I2S,
MP_TYPE_FLAG_NONE,
MP_TYPE_FLAG_ITER_IS_STREAM,
machine_i2s_make_new,
print, machine_i2s_print,
getiter, mp_identity_getiter,
iternext, mp_stream_unbuffered_iter,
protocol, &i2s_stream_p,
locals_dict, &machine_i2s_locals_dict
);
Expand Down
4 changes: 1 addition & 3 deletions ports/mimxrt/machine_uart.c
Expand Up @@ -472,11 +472,9 @@ STATIC const mp_stream_p_t uart_stream_p = {
MP_DEFINE_CONST_OBJ_TYPE(
machine_uart_type,
MP_QSTR_UART,
MP_TYPE_FLAG_NONE,
MP_TYPE_FLAG_ITER_IS_STREAM,
machine_uart_make_new,
print, machine_uart_print,
getiter, mp_identity_getiter,
iternext, mp_stream_unbuffered_iter,
protocol, &uart_stream_p,
locals_dict, &machine_uart_locals_dict
);
5 changes: 2 additions & 3 deletions ports/nrf/boards/microbit/modules/iters.c
Expand Up @@ -46,10 +46,9 @@ static mp_obj_t microbit_repeat_iter_next(mp_obj_t iter_in) {
MP_DEFINE_CONST_OBJ_TYPE(
microbit_repeat_iterator_type,
MP_QSTR_iterator,
MP_TYPE_FLAG_NONE,
MP_TYPE_FLAG_ITER_IS_ITERNEXT,
MP_TYPE_NULL_MAKE_NEW,
getiter, mp_identity_getiter,
iternext, microbit_repeat_iter_next
iter, microbit_repeat_iter_next
);

mp_obj_t microbit_repeat_iterator(mp_obj_t iterable) {
Expand Down
20 changes: 8 additions & 12 deletions ports/nrf/boards/microbit/modules/microbitimage.c
Expand Up @@ -824,18 +824,16 @@ STATIC mp_obj_t microbit_scrolling_string_iter_next(mp_obj_t o_in) {
MP_DEFINE_CONST_OBJ_TYPE(
microbit_scrolling_string_type,
MP_QSTR_ScrollingString,
MP_TYPE_FLAG_NONE,
MP_TYPE_NULL_MAKE_NEW,
getiter, get_microbit_scrolling_string_iter
MP_TYPE_FLAG_ITER_IS_GETITER,
iter, get_microbit_scrolling_string_iter
);

MP_DEFINE_CONST_OBJ_TYPE(
microbit_scrolling_string_iterator_type,
MP_QSTR_iterator,
MP_TYPE_FLAG_NONE,
MP_TYPE_FLAG_ITER_IS_ITERNEXT,
MP_TYPE_NULL_MAKE_NEW,
getiter, mp_identity_getiter,
iternext, microbit_scrolling_string_iter_next
iter, microbit_scrolling_string_iter_next
);

/** Facade types to present a string as a sequence of images.
Expand Down Expand Up @@ -877,11 +875,10 @@ static mp_obj_t microbit_facade_iterator(mp_obj_t iterable_in, mp_obj_iter_buf_t
MP_DEFINE_CONST_OBJ_TYPE(
string_image_facade_type,
MP_QSTR_Facade,
MP_TYPE_FLAG_NONE,
MP_TYPE_NULL_MAKE_NEW,
MP_TYPE_FLAG_ITER_IS_GETITER,
unary_op, facade_unary_op,
subscr, string_image_facade_subscr,
getiter, microbit_facade_iterator
iter, microbit_facade_iterator
);


Expand Down Expand Up @@ -914,10 +911,9 @@ static mp_obj_t microbit_facade_iter_next(mp_obj_t iter_in) {
MP_DEFINE_CONST_OBJ_TYPE(
microbit_facade_iterator_type,
MP_QSTR_iterator,
MP_TYPE_FLAG_NONE,
MP_TYPE_FLAG_ITER_IS_ITERNEXT,
MP_TYPE_NULL_MAKE_NEW,
getiter, mp_identity_getiter,
iternext, microbit_facade_iter_next
iter, microbit_facade_iter_next
);

mp_obj_t microbit_facade_iterator(mp_obj_t iterable_in, mp_obj_iter_buf_t *iter_buf) {
Expand Down
4 changes: 1 addition & 3 deletions ports/nrf/modules/machine/uart.c
Expand Up @@ -373,11 +373,9 @@ STATIC const mp_stream_p_t uart_stream_p = {
MP_DEFINE_CONST_OBJ_TYPE(
machine_hard_uart_type,
MP_QSTR_UART,
MP_TYPE_FLAG_NONE,
MP_TYPE_FLAG_ITER_IS_STREAM,
machine_hard_uart_make_new,
print, machine_hard_uart_print,
getiter, mp_identity_getiter,
iternext, mp_stream_unbuffered_iter,
protocol, &uart_stream_p,
locals_dict, &machine_hard_uart_locals_dict
);
Expand Down
4 changes: 1 addition & 3 deletions ports/renesas-ra/machine_uart.c
Expand Up @@ -574,12 +574,10 @@ STATIC const mp_stream_p_t uart_stream_p = {
MP_DEFINE_CONST_OBJ_TYPE(
machine_uart_type,
MP_QSTR_UART,
MP_TYPE_FLAG_NONE,
MP_TYPE_FLAG_ITER_IS_STREAM,
machine_uart_make_new,
locals_dict, &machine_uart_locals_dict,
print, machine_uart_print,
getiter, mp_identity_getiter,
iternext, mp_stream_unbuffered_iter,
protocol, &uart_stream_p
);

Expand Down
4 changes: 1 addition & 3 deletions ports/rp2/machine_i2s.c
Expand Up @@ -1140,11 +1140,9 @@ STATIC const mp_stream_p_t i2s_stream_p = {
MP_DEFINE_CONST_OBJ_TYPE(
machine_i2s_type,
MP_QSTR_I2S,
MP_TYPE_FLAG_NONE,
MP_TYPE_FLAG_ITER_IS_STREAM,
machine_i2s_make_new,
print, machine_i2s_print,
getiter, mp_identity_getiter,
iternext, mp_stream_unbuffered_iter,
protocol, &i2s_stream_p,
locals_dict, &machine_i2s_locals_dict
);
Expand Down
4 changes: 1 addition & 3 deletions ports/rp2/machine_uart.c
Expand Up @@ -582,11 +582,9 @@ STATIC const mp_stream_p_t uart_stream_p = {
MP_DEFINE_CONST_OBJ_TYPE(
machine_uart_type,
MP_QSTR_UART,
MP_TYPE_FLAG_NONE,
MP_TYPE_FLAG_ITER_IS_STREAM,
machine_uart_make_new,
print, machine_uart_print,
getiter, mp_identity_getiter,
iternext, mp_stream_unbuffered_iter,
protocol, &uart_stream_p,
locals_dict, &machine_uart_locals_dict
);
Expand Down

0 comments on commit 6da41b5

Please sign in to comment.