Skip to content

bpo-42217: compiler: merge same co_code and co_linetable objects #23056

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Feb 10, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
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
11 changes: 11 additions & 0 deletions Lib/test/test_compile.py
Original file line number Diff line number Diff line change
Expand Up @@ -632,6 +632,17 @@ def check_same_constant(const):
self.check_constant(f1, frozenset({0}))
self.assertTrue(f1(0))

# Merging equal co_linetable and co_code is not a strict requirement
# for the Python semantics, it's a more an implementation detail.
@support.cpython_only
def test_merge_code_attrs(self):
# See https://bugs.python.org/issue42217
f1 = lambda x: x.y.z
f2 = lambda a: a.b.c

self.assertIs(f1.__code__.co_linetable, f2.__code__.co_linetable)
self.assertIs(f1.__code__.co_code, f2.__code__.co_code)

# This is a regression test for a CPython specific peephole optimizer
# implementation bug present in a few releases. It's assertion verifies
# that peephole optimization was actually done though that isn't an
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Make the compiler merges same co_code and co_linetable objects in a module like already did for co_consts.
44 changes: 27 additions & 17 deletions Python/compile.c
Original file line number Diff line number Diff line change
Expand Up @@ -5806,14 +5806,12 @@ compute_code_flags(struct compiler *c)
return flags;
}

// Merge *tuple* with constant cache.
// Merge *obj* with constant cache.
// Unlike merge_consts_recursive(), this function doesn't work recursively.
static int
merge_const_tuple(struct compiler *c, PyObject **tuple)
merge_const_one(struct compiler *c, PyObject **obj)
{
assert(PyTuple_CheckExact(*tuple));

PyObject *key = _PyCode_ConstantKey(*tuple);
PyObject *key = _PyCode_ConstantKey(*obj);
if (key == NULL) {
return 0;
}
Expand All @@ -5824,14 +5822,18 @@ merge_const_tuple(struct compiler *c, PyObject **tuple)
if (t == NULL) {
return 0;
}
if (t == key) { // tuple is new constant.
if (t == key) { // obj is new constant.
return 1;
}

PyObject *u = PyTuple_GET_ITEM(t, 1);
Py_INCREF(u);
Py_DECREF(*tuple);
*tuple = u;
if (PyTuple_CheckExact(t)) {
// t is still borrowed reference
t = PyTuple_GET_ITEM(t, 1);
}

Py_INCREF(t);
Py_DECREF(*obj);
*obj = t;
return 1;
}

Expand Down Expand Up @@ -5861,10 +5863,10 @@ makecode(struct compiler *c, struct assembler *a, PyObject *consts)
if (!freevars)
goto error;

if (!merge_const_tuple(c, &names) ||
!merge_const_tuple(c, &varnames) ||
!merge_const_tuple(c, &cellvars) ||
!merge_const_tuple(c, &freevars))
if (!merge_const_one(c, &names) ||
!merge_const_one(c, &varnames) ||
!merge_const_one(c, &cellvars) ||
!merge_const_one(c, &freevars))
{
goto error;
}
Expand All @@ -5881,7 +5883,7 @@ makecode(struct compiler *c, struct assembler *a, PyObject *consts)
if (consts == NULL) {
goto error;
}
if (!merge_const_tuple(c, &consts)) {
if (!merge_const_one(c, &consts)) {
Py_DECREF(consts);
goto error;
}
Expand Down Expand Up @@ -6028,10 +6030,18 @@ assemble(struct compiler *c, int addNone)
goto error;
}

if (_PyBytes_Resize(&a.a_lnotab, a.a_lnotab_off) < 0)
if (_PyBytes_Resize(&a.a_lnotab, a.a_lnotab_off) < 0) {
goto error;
if (_PyBytes_Resize(&a.a_bytecode, a.a_offset * sizeof(_Py_CODEUNIT)) < 0)
}
if (!merge_const_one(c, &a.a_lnotab)) {
goto error;
}
if (_PyBytes_Resize(&a.a_bytecode, a.a_offset * sizeof(_Py_CODEUNIT)) < 0) {
goto error;
}
if (!merge_const_one(c, &a.a_bytecode)) {
goto error;
}

co = makecode(c, &a, consts);
error:
Expand Down
Loading