summaryrefslogtreecommitdiffstats
path: root/Objects
diff options
context:
space:
mode:
authorKen Jin <kenjin4096@gmail.com>2022-06-03 16:41:18 (GMT)
committerGitHub <noreply@github.com>2022-06-03 16:41:18 (GMT)
commitd52ffc1d1f14576605195e4ed3a4d5d63497c3e3 (patch)
tree12016895476caabdc350c7a4f92c130842cfa8c5 /Objects
parentd8f40ead92b5a973cff3a30482a7659d3b46b1ba (diff)
downloadcpython-d52ffc1d1f14576605195e4ed3a4d5d63497c3e3.zip
cpython-d52ffc1d1f14576605195e4ed3a4d5d63497c3e3.tar.gz
cpython-d52ffc1d1f14576605195e4ed3a4d5d63497c3e3.tar.bz2
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>
Diffstat (limited to 'Objects')
-rw-r--r--Objects/codeobject.c7
-rw-r--r--Objects/frameobject.c8
2 files changed, 15 insertions, 0 deletions
diff --git a/Objects/codeobject.c b/Objects/codeobject.c
index 68b0b1e..dc6dec1 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;
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);
}
diff --git a/Objects/frameobject.c b/Objects/frameobject.c
index a448ba3..545c1b6 100644
--- a/Objects/frameobject.c
+++ b/Objects/frameobject.c
@@ -458,22 +458,30 @@ _PyFrame_GetState(PyFrameObject *frame)
static void
add_load_fast_null_checks(PyCodeObject *co)
{
+ int changed = 0;
_Py_CODEUNIT *instructions = _PyCode_CODE(co);
for (Py_ssize_t i = 0; i < Py_SIZE(co); i++) {
switch (_Py_OPCODE(instructions[i])) {
case LOAD_FAST:
case LOAD_FAST__LOAD_FAST:
case LOAD_FAST__LOAD_CONST:
+ changed = 1;
_Py_SET_OPCODE(instructions[i], LOAD_FAST_CHECK);
break;
case LOAD_CONST__LOAD_FAST:
+ changed = 1;
_Py_SET_OPCODE(instructions[i], LOAD_CONST);
break;
case STORE_FAST__LOAD_FAST:
+ changed = 1;
_Py_SET_OPCODE(instructions[i], STORE_FAST);
break;
}
}
+ if (changed) {
+ // invalidate cached co_code object
+ Py_CLEAR(co->_co_code);
+ }
}
/* Setter for f_lineno - you can set f_lineno from within a trace function in