Skip to content

Commit d793389

Browse files
committed
py: restrict further when for-range optimisation is done.
1 parent 86c7fc7 commit d793389

File tree

1 file changed

+4
-2
lines changed

1 file changed

+4
-2
lines changed

py/compile.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ typedef struct _compiler_t {
4848
qstr qstr_native;
4949
qstr qstr_viper;
5050
qstr qstr_asm_thumb;
51+
qstr qstr_range;
5152

5253
bool is_repl;
5354
pass_kind_t pass;
@@ -1409,9 +1410,9 @@ void compile_for_stmt(compiler_t *comp, py_parse_node_struct_t *pns) {
14091410
// this bit optimises: for <x> in range(...), turning it into an explicitly incremented variable
14101411
// this is actually slower, but uses no heap memory
14111412
// for viper it will be much, much faster
1412-
if (/*comp->scope_cur->emit_options == EMIT_OPT_VIPER &&*/ PY_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[1], PN_power)) {
1413+
if (/*comp->scope_cur->emit_options == EMIT_OPT_VIPER &&*/ PY_PARSE_NODE_IS_ID(pns->nodes[0]) && PY_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[1], PN_power)) {
14131414
py_parse_node_struct_t *pns_it = (py_parse_node_struct_t*)pns->nodes[1];
1414-
if (PY_PARSE_NODE_IS_ID(pns_it->nodes[0]) && PY_PARSE_NODE_IS_STRUCT_KIND(pns_it->nodes[1], PN_trailer_paren) && PY_PARSE_NODE_IS_NULL(pns_it->nodes[2])) {
1415+
if (PY_PARSE_NODE_IS_ID(pns_it->nodes[0]) && PY_PARSE_NODE_LEAF_ARG(pns_it->nodes[0]) == comp->qstr_range && PY_PARSE_NODE_IS_STRUCT_KIND(pns_it->nodes[1], PN_trailer_paren) && PY_PARSE_NODE_IS_NULL(pns_it->nodes[2])) {
14151416
py_parse_node_t pn_range_args = ((py_parse_node_struct_t*)pns_it->nodes[1])->nodes[0];
14161417
py_parse_node_t *args;
14171418
int n_args = list_get(&pn_range_args, PN_arglist, &args);
@@ -2853,6 +2854,7 @@ bool py_compile(py_parse_node_t pn, bool is_repl) {
28532854
comp->qstr_native = qstr_from_str_static("native");
28542855
comp->qstr_viper = qstr_from_str_static("viper");
28552856
comp->qstr_asm_thumb = qstr_from_str_static("asm_thumb");
2857+
comp->qstr_range = qstr_from_str_static("range");
28562858

28572859
comp->is_repl = is_repl;
28582860
comp->had_error = false;

0 commit comments

Comments
 (0)