Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Avoid unnecessary allocations when comparing code objects.
26 changes: 13 additions & 13 deletions Objects/codeobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -1398,21 +1398,21 @@ code_richcompare(PyObject *self, PyObject *other, int op)
if (!eq) goto unequal;
eq = co->co_firstlineno == cp->co_firstlineno;
if (!eq) goto unequal;
PyObject *co_code = _PyCode_GetCode(co);
if (co_code == NULL) {
return NULL;
}
PyObject *cp_code = _PyCode_GetCode(cp);
if (cp_code == NULL) {
Py_DECREF(co_code);
return NULL;
}
eq = PyObject_RichCompareBool(co_code, cp_code, Py_EQ);
Py_DECREF(co_code);
Py_DECREF(cp_code);
if (eq <= 0) {
eq = Py_SIZE(co) == Py_SIZE(cp);
if (!eq) {
goto unequal;
}
for (int i = 0; i < Py_SIZE(co); i++) {
_Py_CODEUNIT co_instr = _PyCode_CODE(co)[i];
_Py_CODEUNIT cp_instr = _PyCode_CODE(cp)[i];
_Py_SET_OPCODE(co_instr, _PyOpcode_Deopt[_Py_OPCODE(co_instr)]);
_Py_SET_OPCODE(cp_instr, _PyOpcode_Deopt[_Py_OPCODE(cp_instr)]);
eq = co_instr == cp_instr;
if (!eq) {
goto unequal;
}
i += _PyOpcode_Caches[_Py_OPCODE(co_instr)];
}

/* compare constants */
consts1 = _PyCode_ConstantKey(co->co_consts);
Expand Down