bpo-41323: compiler: Reuse tuple in constant tuple folding (GH-25419) · python/cpython@8a232c7 (original) (raw)

`@@ -6751,7 +6751,7 @@ static int

`

6751

6751

`normalize_basic_block(basicblock *bb);

`

6752

6752

``

6753

6753

`static int

`

6754

``

`-

optimize_cfg(struct assembler *a, PyObject *consts);

`

``

6754

`+

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

`

6755

6755

``

6756

6756

`static int

`

6757

6757

`ensure_exits_have_lineno(struct compiler *c);

`

`@@ -6850,7 +6850,7 @@ assemble(struct compiler *c, int addNone)

`

6850

6850

`if (consts == NULL) {

`

6851

6851

` goto error;

`

6852

6852

` }

`

6853

``

`-

if (optimize_cfg(&a, consts)) {

`

``

6853

`+

if (optimize_cfg(c, &a, consts)) {

`

6854

6854

` goto error;

`

6855

6855

` }

`

6856

6856

``

`@@ -6898,7 +6898,8 @@ assemble(struct compiler *c, int addNone)

`

6898

6898

` Called with codestr pointing to the first LOAD_CONST.

`

6899

6899

`*/

`

6900

6900

`static int

`

6901

``

`-

fold_tuple_on_constants(struct instr *inst,

`

``

6901

`+

fold_tuple_on_constants(struct compiler *c,

`

``

6902

`+

struct instr *inst,

`

6902

6903

`int n, PyObject *consts)

`

6903

6904

`{

`

6904

6905

`/* Pre-conditions */

`

`@@ -6923,15 +6924,27 @@ fold_tuple_on_constants(struct instr *inst,

`

6923

6924

`Py_INCREF(constant);

`

6924

6925

`PyTuple_SET_ITEM(newconst, i, constant);

`

6925

6926

` }

`

6926

``

`-

Py_ssize_t index = PyList_GET_SIZE(consts);

`

6927

``

`-

if ((size_t)index >= (size_t)INT_MAX - 1) {

`

``

6927

`+

if (merge_const_one(c, &newconst) == 0) {

`

6928

6928

`Py_DECREF(newconst);

`

6929

``

`-

PyErr_SetString(PyExc_OverflowError, "too many constants");

`

6930

6929

`return -1;

`

6931

6930

` }

`

6932

``

`-

if (PyList_Append(consts, newconst)) {

`

6933

``

`-

Py_DECREF(newconst);

`

6934

``

`-

return -1;

`

``

6931

+

``

6932

`+

Py_ssize_t index;

`

``

6933

`+

for (index = 0; index < PyList_GET_SIZE(consts); index++) {

`

``

6934

`+

if (PyList_GET_ITEM(consts, index) == newconst) {

`

``

6935

`+

break;

`

``

6936

`+

}

`

``

6937

`+

}

`

``

6938

`+

if (index == PyList_GET_SIZE(consts)) {

`

``

6939

`+

if ((size_t)index >= (size_t)INT_MAX - 1) {

`

``

6940

`+

Py_DECREF(newconst);

`

``

6941

`+

PyErr_SetString(PyExc_OverflowError, "too many constants");

`

``

6942

`+

return -1;

`

``

6943

`+

}

`

``

6944

`+

if (PyList_Append(consts, newconst)) {

`

``

6945

`+

Py_DECREF(newconst);

`

``

6946

`+

return -1;

`

``

6947

`+

}

`

6935

6948

` }

`

6936

6949

`Py_DECREF(newconst);

`

6937

6950

`for (int i = 0; i < n; i++) {

`

`@@ -6968,7 +6981,7 @@ eliminate_jump_to_jump(basicblock *bb, int opcode) {

`

6968

6981

``

6969

6982

`/* Optimization */

`

6970

6983

`static int

`

6971

``

`-

optimize_basic_block(basicblock *bb, PyObject *consts)

`

``

6984

`+

optimize_basic_block(struct compiler *c, basicblock *bb, PyObject *consts)

`

6972

6985

`{

`

6973

6986

`assert(PyList_CheckExact(consts));

`

6974

6987

`struct instr nop;

`

`@@ -7056,7 +7069,7 @@ optimize_basic_block(basicblock *bb, PyObject *consts)

`

7056

7069

`break;

`

7057

7070

` }

`

7058

7071

`if (i >= oparg) {

`

7059

``

`-

if (fold_tuple_on_constants(inst-oparg, oparg, consts)) {

`

``

7072

`+

if (fold_tuple_on_constants(c, inst-oparg, oparg, consts)) {

`

7060

7073

` goto error;

`

7061

7074

` }

`

7062

7075

` }

`

`@@ -7390,10 +7403,10 @@ propogate_line_numbers(struct assembler *a) {

`

7390

7403

`*/

`

7391

7404

``

7392

7405

`static int

`

7393

``

`-

optimize_cfg(struct assembler *a, PyObject *consts)

`

``

7406

`+

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

`

7394

7407

`{

`

7395

7408

`for (basicblock *b = a->a_entry; b != NULL; b = b->b_next) {

`

7396

``

`-

if (optimize_basic_block(b, consts)) {

`

``

7409

`+

if (optimize_basic_block(c, b, consts)) {

`

7397

7410

`return -1;

`

7398

7411

` }

`

7399

7412

`clean_basic_block(b, -1);

`