Skip to content

Commit 6da41b5

Browse files
jimmodpgeorge
authored andcommitted
py/obj: Merge getiter and iternext mp_obj_type_t slots.
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 <[email protected]>
1 parent 3c6127d commit 6da41b5

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

54 files changed

+234
-227
lines changed

examples/natmod/btree/btree_c.c

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,7 @@ int mp_stream_posix_fsync(void *stream) {
9595
}
9696

9797
mp_obj_full_type_t btree_type;
98+
mp_getiter_iternext_custom_t btree_getiter_iternext;
9899

99100
#include "extmod/modbtree.c"
100101

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

126+
btree_getiter_iternext.getiter = btree_getiter;
127+
btree_getiter_iternext.iternext = btree_iternext;
128+
125129
btree_type.base.type = (void*)&mp_fun_table.type_type;
130+
btree_type.flags = MP_TYPE_FLAG_ITER_IS_CUSTOM;
126131
btree_type.name = MP_QSTR_btree;
127132
MP_OBJ_TYPE_SET_SLOT(&btree_type, print, btree_print, 0);
128-
MP_OBJ_TYPE_SET_SLOT(&btree_type, getiter, btree_getiter, 1);
129-
MP_OBJ_TYPE_SET_SLOT(&btree_type, iternext, btree_iternext, 2);
130-
MP_OBJ_TYPE_SET_SLOT(&btree_type, binary_op, btree_binary_op, 3);
131-
MP_OBJ_TYPE_SET_SLOT(&btree_type, subscr, btree_subscr, 4);
133+
MP_OBJ_TYPE_SET_SLOT(&btree_type, iter, &btree_getiter_iternext, 1);
134+
MP_OBJ_TYPE_SET_SLOT(&btree_type, binary_op, btree_binary_op, 2);
135+
MP_OBJ_TYPE_SET_SLOT(&btree_type, subscr, btree_subscr, 3);
132136
btree_locals_dict_table[0] = (mp_map_elem_t){ MP_OBJ_NEW_QSTR(MP_QSTR_close), MP_OBJ_FROM_PTR(&btree_close_obj) };
133137
btree_locals_dict_table[1] = (mp_map_elem_t){ MP_OBJ_NEW_QSTR(MP_QSTR_flush), MP_OBJ_FROM_PTR(&btree_flush_obj) };
134138
btree_locals_dict_table[2] = (mp_map_elem_t){ MP_OBJ_NEW_QSTR(MP_QSTR_get), MP_OBJ_FROM_PTR(&btree_get_obj) };
@@ -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
137141
btree_locals_dict_table[5] = (mp_map_elem_t){ MP_OBJ_NEW_QSTR(MP_QSTR_keys), MP_OBJ_FROM_PTR(&btree_keys_obj) };
138142
btree_locals_dict_table[6] = (mp_map_elem_t){ MP_OBJ_NEW_QSTR(MP_QSTR_values), MP_OBJ_FROM_PTR(&btree_values_obj) };
139143
btree_locals_dict_table[7] = (mp_map_elem_t){ MP_OBJ_NEW_QSTR(MP_QSTR_items), MP_OBJ_FROM_PTR(&btree_items_obj) };
140-
MP_OBJ_TYPE_SET_SLOT(&btree_type, locals_dict, (void*)&btree_locals_dict, 5);
144+
MP_OBJ_TYPE_SET_SLOT(&btree_type, locals_dict, (void*)&btree_locals_dict, 4);
141145

142146
mp_store_global(MP_QSTR__open, MP_OBJ_FROM_PTR(&btree_open_obj));
143147
mp_store_global(MP_QSTR_INCL, MP_OBJ_NEW_SMALL_INT(FLAG_END_KEY_INCL));

extmod/modbtree.c

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -319,15 +319,19 @@ STATIC const mp_rom_map_elem_t btree_locals_dict_table[] = {
319319

320320
STATIC MP_DEFINE_CONST_DICT(btree_locals_dict, btree_locals_dict_table);
321321

322+
STATIC const mp_getiter_iternext_custom_t btree_getiter_iternext = {
323+
.getiter = btree_getiter,
324+
.iternext = btree_iternext,
325+
};
326+
322327
STATIC MP_DEFINE_CONST_OBJ_TYPE(
323328
btree_type,
324329
MP_QSTR_btree,
325-
MP_TYPE_FLAG_NONE,
330+
MP_TYPE_FLAG_ITER_IS_CUSTOM,
326331
MP_TYPE_NULL_MAKE_NEW,
327332
// Save on qstr's, reuse same as for module
328333
print, btree_print,
329-
getiter, btree_getiter,
330-
iternext, btree_iternext,
334+
iter, &btree_getiter_iternext,
331335
binary_op, btree_binary_op,
332336
subscr, btree_subscr,
333337
locals_dict, &btree_locals_dict

extmod/moduasyncio.c

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -287,14 +287,18 @@ STATIC mp_obj_t task_iternext(mp_obj_t self_in) {
287287
return mp_const_none;
288288
}
289289

290+
STATIC const mp_getiter_iternext_custom_t task_getiter_iternext = {
291+
.getiter = task_getiter,
292+
.iternext = task_iternext,
293+
};
294+
290295
STATIC MP_DEFINE_CONST_OBJ_TYPE(
291296
task_type,
292297
MP_QSTR_Task,
293-
MP_TYPE_FLAG_NONE,
298+
MP_TYPE_FLAG_ITER_IS_CUSTOM,
294299
task_make_new,
295300
attr, task_attr,
296-
getiter, task_getiter,
297-
iternext, task_iternext
301+
iter, &task_getiter_iternext
298302
);
299303

300304
/******************************************************************************/

extmod/moduselect.c

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -339,10 +339,9 @@ STATIC MP_DEFINE_CONST_DICT(poll_locals_dict, poll_locals_dict_table);
339339
STATIC MP_DEFINE_CONST_OBJ_TYPE(
340340
mp_type_poll,
341341
MP_QSTR_poll,
342-
MP_TYPE_FLAG_NONE,
342+
MP_TYPE_FLAG_ITER_IS_ITERNEXT,
343343
MP_TYPE_NULL_MAKE_NEW,
344-
getiter, mp_identity_getiter,
345-
iternext, poll_iternext,
344+
iter, poll_iternext,
346345
locals_dict, &poll_locals_dict
347346
);
348347

extmod/modussl_axtls.c

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -321,8 +321,6 @@ STATIC MP_DEFINE_CONST_OBJ_TYPE(
321321
MP_TYPE_NULL_MAKE_NEW,
322322
// Save on qstr's, reuse same as for module
323323
print, ussl_socket_print,
324-
getiter, NULL,
325-
iternext, NULL,
326324
protocol, &ussl_socket_stream_p,
327325
locals_dict, &ussl_socket_locals_dict
328326
);

extmod/modussl_mbedtls.c

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -399,8 +399,6 @@ STATIC MP_DEFINE_CONST_OBJ_TYPE(
399399
MP_TYPE_NULL_MAKE_NEW,
400400
// Save on qstr's, reuse same as for module
401401
print, socket_print,
402-
getiter, NULL,
403-
iternext, NULL,
404402
protocol, &ussl_socket_stream_p,
405403
locals_dict, &ussl_socket_locals_dict
406404
);

extmod/vfs_fat_file.c

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -179,11 +179,9 @@ STATIC const mp_stream_p_t vfs_fat_fileio_stream_p = {
179179
MP_DEFINE_CONST_OBJ_TYPE(
180180
mp_type_vfs_fat_fileio,
181181
MP_QSTR_FileIO,
182-
MP_TYPE_FLAG_NONE,
182+
MP_TYPE_FLAG_ITER_IS_STREAM,
183183
MP_TYPE_NULL_MAKE_NEW,
184184
print, file_obj_print,
185-
getiter, mp_identity_getiter,
186-
iternext, mp_stream_unbuffered_iter,
187185
protocol, &vfs_fat_fileio_stream_p,
188186
locals_dict, &vfs_fat_rawfile_locals_dict
189187
);
@@ -198,11 +196,9 @@ STATIC const mp_stream_p_t vfs_fat_textio_stream_p = {
198196
MP_DEFINE_CONST_OBJ_TYPE(
199197
mp_type_vfs_fat_textio,
200198
MP_QSTR_TextIOWrapper,
201-
MP_TYPE_FLAG_NONE,
199+
MP_TYPE_FLAG_ITER_IS_STREAM,
202200
MP_TYPE_NULL_MAKE_NEW,
203201
print, file_obj_print,
204-
getiter, mp_identity_getiter,
205-
iternext, mp_stream_unbuffered_iter,
206202
protocol, &vfs_fat_textio_stream_p,
207203
locals_dict, &vfs_fat_rawfile_locals_dict
208204
);

extmod/vfs_lfsx_file.c

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -223,11 +223,9 @@ STATIC const mp_stream_p_t MP_VFS_LFSx(fileio_stream_p) = {
223223
MP_DEFINE_CONST_OBJ_TYPE(
224224
MP_TYPE_VFS_LFSx_(_fileio),
225225
MP_QSTR_FileIO,
226-
MP_TYPE_FLAG_NONE,
226+
MP_TYPE_FLAG_ITER_IS_STREAM,
227227
MP_TYPE_NULL_MAKE_NEW,
228228
print, MP_VFS_LFSx(file_print),
229-
getiter, mp_identity_getiter,
230-
iternext, mp_stream_unbuffered_iter,
231229
protocol, &MP_VFS_LFSx(fileio_stream_p),
232230
locals_dict, &MP_VFS_LFSx(file_locals_dict)
233231
);
@@ -242,11 +240,9 @@ STATIC const mp_stream_p_t MP_VFS_LFSx(textio_stream_p) = {
242240
MP_DEFINE_CONST_OBJ_TYPE(
243241
MP_TYPE_VFS_LFSx_(_textio),
244242
MP_QSTR_TextIOWrapper,
245-
MP_TYPE_FLAG_NONE,
243+
MP_TYPE_FLAG_ITER_IS_STREAM,
246244
MP_TYPE_NULL_MAKE_NEW,
247245
print, MP_VFS_LFSx(file_print),
248-
getiter, mp_identity_getiter,
249-
iternext, mp_stream_unbuffered_iter,
250246
protocol, &MP_VFS_LFSx(textio_stream_p),
251247
locals_dict, &MP_VFS_LFSx(file_locals_dict)
252248
);

extmod/vfs_posix_file.c

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -252,11 +252,9 @@ STATIC const mp_stream_p_t vfs_posix_fileio_stream_p = {
252252
MP_DEFINE_CONST_OBJ_TYPE(
253253
mp_type_vfs_posix_fileio,
254254
MP_QSTR_FileIO,
255-
MP_TYPE_FLAG_NONE,
255+
MP_TYPE_FLAG_ITER_IS_STREAM,
256256
MP_TYPE_NULL_MAKE_NEW,
257257
print, vfs_posix_file_print,
258-
getiter, mp_identity_getiter,
259-
iternext, mp_stream_unbuffered_iter,
260258
protocol, &vfs_posix_fileio_stream_p,
261259
locals_dict, &vfs_posix_rawfile_locals_dict
262260
);
@@ -271,11 +269,9 @@ STATIC const mp_stream_p_t vfs_posix_textio_stream_p = {
271269
MP_DEFINE_CONST_OBJ_TYPE(
272270
mp_type_vfs_posix_textio,
273271
MP_QSTR_TextIOWrapper,
274-
MP_TYPE_FLAG_NONE,
272+
MP_TYPE_FLAG_ITER_IS_STREAM,
275273
MP_TYPE_NULL_MAKE_NEW,
276274
print, vfs_posix_file_print,
277-
getiter, mp_identity_getiter,
278-
iternext, mp_stream_unbuffered_iter,
279275
protocol, &vfs_posix_textio_stream_p,
280276
locals_dict, &vfs_posix_rawfile_locals_dict
281277
);

ports/cc3200/mods/modussl.c

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -64,9 +64,6 @@ STATIC MP_DEFINE_CONST_OBJ_TYPE(
6464
ssl_socket_type,
6565
MP_QSTR_ussl,
6666
MP_TYPE_FLAG_NONE,
67-
MP_TYPE_NULL_MAKE_NEW,
68-
getiter, NULL,
69-
iternext, NULL,
7067
protocol, &socket_stream_p,
7168
locals_dict, &socket_locals_dict
7269
);

0 commit comments

Comments
 (0)