Skip to content

Commit 4d2bab1

Browse files
committed
py/compile: Optimise list/dict/set comprehensions to use stack iter.
1 parent 861b001 commit 4d2bab1

1 file changed

Lines changed: 12 additions & 5 deletions

File tree

py/compile.c

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2372,7 +2372,9 @@ STATIC void compile_comprehension(compiler_t *comp, mp_parse_node_struct_t *pns,
23722372
close_over_variables_etc(comp, this_scope, 0, 0);
23732373

23742374
compile_node(comp, pns_comp_for->nodes[1]); // source of the iterator
2375-
EMIT_ARG(get_iter, false);
2375+
if (kind == SCOPE_GEN_EXPR) {
2376+
EMIT_ARG(get_iter, false);
2377+
}
23762378
EMIT_ARG(call_function, 1, 0, 0);
23772379
}
23782380

@@ -3072,10 +3074,15 @@ STATIC void compile_scope(compiler_t *comp, scope_t *scope, pass_kind_t pass) {
30723074

30733075
// There are 4 slots on the stack for the iterator, and the first one is
30743076
// NULL to indicate that the second one points to the iterator object.
3075-
EMIT(load_null);
3076-
compile_load_id(comp, qstr_arg);
3077-
EMIT(load_null);
3078-
EMIT(load_null);
3077+
if (scope->kind == SCOPE_GEN_EXPR) {
3078+
EMIT(load_null);
3079+
compile_load_id(comp, qstr_arg);
3080+
EMIT(load_null);
3081+
EMIT(load_null);
3082+
} else {
3083+
compile_load_id(comp, qstr_arg);
3084+
EMIT_ARG(get_iter, true);
3085+
}
30793086

30803087
compile_scope_comp_iter(comp, pns_comp_for, pns->nodes[0], 0);
30813088

0 commit comments

Comments
 (0)