diff options
author | Ken Jin <28750310+Fidget-Spinner@users.noreply.github.com> | 2022-06-23 16:24:49 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-06-23 16:24:49 (GMT) |
commit | 852b4d4bcd12b0b6839a015a262ce976b134f6f3 (patch) | |
tree | c5dfb746d5f2c71e2989551a4bfebd129f036915 /Objects/codeobject.c | |
parent | 96a76141d52755ffda54fefc3c2c8da49e895e80 (diff) | |
download | cpython-852b4d4bcd12b0b6839a015a262ce976b134f6f3.zip cpython-852b4d4bcd12b0b6839a015a262ce976b134f6f3.tar.gz cpython-852b4d4bcd12b0b6839a015a262ce976b134f6f3.tar.bz2 |
[3.11] gh-93382: Cache result of `PyCode_GetCode` in codeobject (GH-93383) (#93493)
Co-authored-by: Kumar Aditya <59607654+kumaraditya303@users.noreply.github.com>
Co-authored-by: Dennis Sweeney <36520290+sweeneyde@users.noreply.github.com>
Diffstat (limited to 'Objects/codeobject.c')
-rw-r--r-- | Objects/codeobject.c | 8 |
1 files changed, 8 insertions, 0 deletions
diff --git a/Objects/codeobject.c b/Objects/codeobject.c index 0e91456..8b9ca89 100644 --- a/Objects/codeobject.c +++ b/Objects/codeobject.c @@ -334,6 +334,7 @@ init_code(PyCodeObject *co, struct _PyCodeConstructor *con) /* not set */ co->co_weakreflist = NULL; co->co_extra = NULL; + co->_co_code = NULL; co->co_warmup = QUICKENING_INITIAL_WARMUP_VALUE; co->_co_linearray_entry_size = 0; @@ -1421,12 +1422,17 @@ deopt_code(_Py_CODEUNIT *instructions, Py_ssize_t len) PyObject * _PyCode_GetCode(PyCodeObject *co) { + if (co->_co_code != NULL) { + return Py_NewRef(co->_co_code); + } PyObject *code = PyBytes_FromStringAndSize((const char *)_PyCode_CODE(co), _PyCode_NBYTES(co)); if (code == NULL) { return NULL; } deopt_code((_Py_CODEUNIT *)PyBytes_AS_STRING(code), Py_SIZE(co)); + assert(co->_co_code == NULL); + co->_co_code = Py_NewRef(code); return code; } @@ -1585,6 +1591,7 @@ code_dealloc(PyCodeObject *co) Py_XDECREF(co->co_qualname); Py_XDECREF(co->co_linetable); Py_XDECREF(co->co_exceptiontable); + Py_XDECREF(co->_co_code); if (co->co_weakreflist != NULL) { PyObject_ClearWeakRefs((PyObject*)co); } @@ -2142,6 +2149,7 @@ _PyStaticCode_Dealloc(PyCodeObject *co) deopt_code(_PyCode_CODE(co), Py_SIZE(co)); co->co_warmup = QUICKENING_INITIAL_WARMUP_VALUE; PyMem_Free(co->co_extra); + Py_CLEAR(co->_co_code); co->co_extra = NULL; if (co->co_weakreflist != NULL) { PyObject_ClearWeakRefs((PyObject *)co); |