diff options
author | Brandt Bucher <brandtbucher@microsoft.com> | 2022-04-01 10:42:46 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-04-01 10:42:46 (GMT) |
commit | bd2e47c8830d1b2869f2b4345945a5e0c3b4e3fb (patch) | |
tree | d8177a35651397bca64e3bd20a9b76451ef5d782 /Objects | |
parent | a0ea7a116ce52a178c02d42b684089758bd7f355 (diff) | |
download | cpython-bd2e47c8830d1b2869f2b4345945a5e0c3b4e3fb.zip cpython-bd2e47c8830d1b2869f2b4345945a5e0c3b4e3fb.tar.gz cpython-bd2e47c8830d1b2869f2b4345945a5e0c3b4e3fb.tar.bz2 |
bpo-46841: Avoid unnecessary allocations in code object comparisons (GH-32222)
Diffstat (limited to 'Objects')
-rw-r--r-- | Objects/codeobject.c | 26 |
1 files changed, 13 insertions, 13 deletions
diff --git a/Objects/codeobject.c b/Objects/codeobject.c index 224493e..987cdef 100644 --- a/Objects/codeobject.c +++ b/Objects/codeobject.c @@ -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); |