Skip to content
Merged
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
Next Next commit
bpo-41323: compiler: Reuse tuple in tuple folding.
  • Loading branch information
methane committed Apr 15, 2021
commit cb1e446863829b4c508556fe9ed56156a15b35d5
39 changes: 26 additions & 13 deletions Python/compile.c
Original file line number Diff line number Diff line change
Expand Up @@ -6657,7 +6657,7 @@ static int
normalize_basic_block(basicblock *bb);

static int
optimize_cfg(struct assembler *a, PyObject *consts);
optimize_cfg(struct compiler *c, struct assembler *a, PyObject *consts);

static int
ensure_exits_have_lineno(struct compiler *c);
Expand Down Expand Up @@ -6756,7 +6756,7 @@ assemble(struct compiler *c, int addNone)
if (consts == NULL) {
goto error;
}
if (optimize_cfg(&a, consts)) {
if (optimize_cfg(c, &a, consts)) {
goto error;
}

Expand Down Expand Up @@ -6804,7 +6804,8 @@ assemble(struct compiler *c, int addNone)
Called with codestr pointing to the first LOAD_CONST.
*/
static int
fold_tuple_on_constants(struct instr *inst,
fold_tuple_on_constants(struct compiler *c,
struct instr *inst,
int n, PyObject *consts)
{
/* Pre-conditions */
Expand All @@ -6829,15 +6830,27 @@ fold_tuple_on_constants(struct instr *inst,
Py_INCREF(constant);
PyTuple_SET_ITEM(newconst, i, constant);
}
Py_ssize_t index = PyList_GET_SIZE(consts);
if ((size_t)index >= (size_t)INT_MAX - 1) {
if (merge_const_one(c, &newconst) == 0) {
Py_DECREF(newconst);
PyErr_SetString(PyExc_OverflowError, "too many constants");
return -1;
}
if (PyList_Append(consts, newconst)) {
Py_DECREF(newconst);
return -1;

Py_ssize_t index;
for (index = 0; index < PyList_GET_SIZE(consts); index++) {
if (PyList_GET_ITEM(consts, index) == newconst) {
break;
}
}
if (index == PyList_GET_SIZE(consts)) {
if ((size_t)index >= (size_t)INT_MAX - 1) {
Py_DECREF(newconst);
PyErr_SetString(PyExc_OverflowError, "too many constants");
return -1;
}
if (PyList_Append(consts, newconst)) {
Py_DECREF(newconst);
return -1;
}
}
Py_DECREF(newconst);
for (int i = 0; i < n; i++) {
Expand Down Expand Up @@ -6874,7 +6887,7 @@ eliminate_jump_to_jump(basicblock *bb, int opcode) {

/* Optimization */
static int
optimize_basic_block(basicblock *bb, PyObject *consts)
optimize_basic_block(struct compiler *c, basicblock *bb, PyObject *consts)
{
assert(PyList_CheckExact(consts));
struct instr nop;
Expand Down Expand Up @@ -6962,7 +6975,7 @@ optimize_basic_block(basicblock *bb, PyObject *consts)
break;
}
if (i >= oparg) {
if (fold_tuple_on_constants(inst-oparg, oparg, consts)) {
if (fold_tuple_on_constants(c, inst-oparg, oparg, consts)) {
goto error;
}
}
Expand Down Expand Up @@ -7296,10 +7309,10 @@ propogate_line_numbers(struct assembler *a) {
*/

static int
optimize_cfg(struct assembler *a, PyObject *consts)
optimize_cfg(struct compiler *c, struct assembler *a, PyObject *consts)
{
for (basicblock *b = a->a_entry; b != NULL; b = b->b_next) {
if (optimize_basic_block(b, consts)) {
if (optimize_basic_block(c, b, consts)) {
return -1;
}
clean_basic_block(b, -1);
Expand Down