Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Replace a sequence of LOAD_CONSTs with a single LOAD_CONST and UNPACK…
…_SEQUENCE.
  • Loading branch information
serhiy-storchaka committed Apr 21, 2018
commit ca508b9db375e96a370ff01efcd30cb101c15513
23 changes: 23 additions & 0 deletions Python/compile.c
Original file line number Diff line number Diff line change
Expand Up @@ -5506,6 +5506,29 @@ fold_tuples_on_constants(struct compiler *c, basicblock *b)
continue;
}
}
if (nconsts > 2) {
/* Replace LOAD_CONST c1, LOAD_CONST c2, ... LOAD_CONST cn
with LOAD_CONST (c1, c2, ... cn), UNPACK_SEQUENCE n.
*/
n = nconsts;
newconst = PyTuple_New(n);
if (!newconst)
return 0;
for (j = 0; j < n; j++) {
arg = b->b_instr[i - 1 - j].i_oparg;
item = PyList_GET_ITEM(c->u->u_consts_list, arg);
Py_INCREF(item);
PyTuple_SET_ITEM(newconst, j, item);
}
arg = compiler_add_const(c, newconst);
Py_DECREF(newconst);
if (arg < 0)
return 0;
set_instr(b, i - n, LOAD_CONST, arg);
set_instr(b, i - n + 1, UNPACK_SEQUENCE, n);
remove_instr_range(b, i - n + 2, i);
i -= n - 1;
}
nconsts = 0;
}
return 1;
Expand Down
Loading