mirror of
https://github.com/python/cpython.git
synced 2025-10-09 16:34:44 +00:00
gh-93382: Cache result of PyCode_GetCode
in codeobject (GH-93383)
Co-authored-by: Kumar Aditya <59607654+kumaraditya303@users.noreply.github.com> Co-authored-by: Dennis Sweeney <36520290+sweeneyde@users.noreply.github.com>
This commit is contained in:
parent
d8f40ead92
commit
d52ffc1d1f
5 changed files with 21 additions and 4 deletions
|
@ -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;
|
||||
memcpy(_PyCode_CODE(co), PyBytes_AS_STRING(con->code),
|
||||
|
@ -1367,12 +1368,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 = (void *)Py_NewRef(code);
|
||||
return code;
|
||||
}
|
||||
|
||||
|
@ -1531,6 +1537,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);
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue