Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

gh-92031: Deoptimize Static Code at Finalization #92039

Merged
merged 8 commits into from
May 3, 2022
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions Lib/test/test_embed.py
Original file line number Diff line number Diff line change
Expand Up @@ -343,6 +343,14 @@ def test_finalize_structseq(self):
out, err = self.run_embedded_interpreter("test_repeated_init_exec", code)
self.assertEqual(out, 'Tests passed\n' * INIT_LOOPS)

def test_quickened_static_code_gets_unquickened_at_Py_FINALIZE(self):
sweeneyde marked this conversation as resolved.
Show resolved Hide resolved
# https://github.com/python/cpython/issues/92031
code = "import unittest; print('Tests passed')"
run = self.run_embedded_interpreter
for i in range(50):
out, err = run("test_repeated_init_exec", code)
self.assertEqual(out, 'Tests passed\n' * INIT_LOOPS)

def test_ucnhash_capi_reset(self):
# bpo-47182: unicodeobject.c:ucnhash_capi was not reset on shutdown.
code = "print('\\N{digit nine}')"
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Deoptimize statically-allocated code objects during ``Py_FINALIZE()`` so that future ``_PyCode_Quicken`` calls always start with unquickened code.
25 changes: 16 additions & 9 deletions Objects/codeobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -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];
for (int i = 0; i < len; i++) {
_Py_CODEUNIT instruction = instructions[i];
int opcode = _PyOpcode_Deopt[_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;
}

Expand Down Expand Up @@ -2071,6 +2077,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;
Expand Down