summaryrefslogtreecommitdiffstats
path: root/Objects
diff options
context:
space:
mode:
authorDennis Sweeney <36520290+sweeneyde@users.noreply.github.com>2022-05-03 14:59:12 (GMT)
committerGitHub <noreply@github.com>2022-05-03 14:59:12 (GMT)
commitb156578bd63a94fe3d80a4c6a8dbc067feba5d06 (patch)
treeef52569184e48c4f282032e3f67b519c432c372f /Objects
parent04dc4b06a3afc79a658cacca87ee1d93a34653bd (diff)
downloadcpython-b156578bd63a94fe3d80a4c6a8dbc067feba5d06.zip
cpython-b156578bd63a94fe3d80a4c6a8dbc067feba5d06.tar.gz
cpython-b156578bd63a94fe3d80a4c6a8dbc067feba5d06.tar.bz2
gh-92031: Deoptimize Static Code at Finalization (GH-92039)
Diffstat (limited to 'Objects')
-rw-r--r--Objects/codeobject.c27
1 files changed, 17 insertions, 10 deletions
diff --git a/Objects/codeobject.c b/Objects/codeobject.c
index 9f922da..c2b29be 100644
--- a/Objects/codeobject.c
+++ b/Objects/codeobject.c
@@ -1350,23 +1350,29 @@ _PyCode_GetFreevars(PyCodeObject *co)
return get_localsplus_names(co, CO_FAST_FREE, co->co_nfreevars);
}
-PyObject *
-_PyCode_GetCode(PyCodeObject *co)
+static void
+deopt_code(_Py_CODEUNIT *instructions, Py_ssize_t len)
{
- PyObject *code = PyBytes_FromStringAndSize(NULL, _PyCode_NBYTES(co));
- if (code == NULL) {
- return NULL;
- }
- _Py_CODEUNIT *instructions = (_Py_CODEUNIT *)PyBytes_AS_STRING(code);
- for (int i = 0; i < Py_SIZE(co); i++) {
- _Py_CODEUNIT instruction = _PyCode_CODE(co)[i];
- int opcode = _PyOpcode_Deopt[_Py_OPCODE(instruction)];
+ for (int i = 0; i < len; i++) {
+ _Py_CODEUNIT instruction = instructions[i];
+ int opcode = _PyOpcode_Original[_Py_OPCODE(instruction)];
int caches = _PyOpcode_Caches[opcode];
instructions[i] = _Py_MAKECODEUNIT(opcode, _Py_OPARG(instruction));
while (caches--) {
instructions[++i] = _Py_MAKECODEUNIT(CACHE, 0);
}
}
+}
+
+PyObject *
+_PyCode_GetCode(PyCodeObject *co)
+{
+ 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));
return code;
}
@@ -2076,6 +2082,7 @@ _PyStaticCode_Dealloc(PyCodeObject *co)
if (co->co_warmup == 0) {
_Py_QuickenedCount--;
}
+ deopt_code(_PyCode_CODE(co), Py_SIZE(co));
co->co_warmup = QUICKENING_INITIAL_WARMUP_VALUE;
PyMem_Free(co->co_extra);
co->co_extra = NULL;