summaryrefslogtreecommitdiffstats
path: root/Python
diff options
context:
space:
mode:
authorSam Gross <colesbury@gmail.com>2024-05-07 00:12:39 (GMT)
committerGitHub <noreply@github.com>2024-05-07 00:12:39 (GMT)
commit723d4d2fe8e77b398f0ccffcfa541149caaac6a1 (patch)
tree8d213981ac3bc060c5046acdc256015d2c821476 /Python
parent8d8275b0cf43f0e20c72a9641cbddc5044cdae04 (diff)
downloadcpython-723d4d2fe8e77b398f0ccffcfa541149caaac6a1.zip
cpython-723d4d2fe8e77b398f0ccffcfa541149caaac6a1.tar.gz
cpython-723d4d2fe8e77b398f0ccffcfa541149caaac6a1.tar.bz2
gh-118527: Intern code consts in free-threaded build (#118667)
We already intern and immortalize most string constants. In the free-threaded build, other constants can be a source of reference count contention because they are shared by all threads running the same code objects.
Diffstat (limited to 'Python')
-rw-r--r--Python/bltinmodule.c13
-rw-r--r--Python/pylifecycle.c7
-rw-r--r--Python/pystate.c1
3 files changed, 21 insertions, 0 deletions
diff --git a/Python/bltinmodule.c b/Python/bltinmodule.c
index e7b60eb..d192d5b 100644
--- a/Python/bltinmodule.c
+++ b/Python/bltinmodule.c
@@ -866,8 +866,21 @@ builtin_compile_impl(PyObject *module, PyObject *source, PyObject *filename,
if (str == NULL)
goto error;
+#ifdef Py_GIL_DISABLED
+ // gh-118527: Disable immortalization of code constants for explicit
+ // compile() calls to get consistent frozen outputs between the default
+ // and free-threaded builds.
+ PyInterpreterState *interp = _PyInterpreterState_GET();
+ int old_value = interp->gc.immortalize.enable_on_thread_created;
+ interp->gc.immortalize.enable_on_thread_created = 0;
+#endif
+
result = Py_CompileStringObject(str, filename, start[compile_mode], &cf, optimize);
+#ifdef Py_GIL_DISABLED
+ interp->gc.immortalize.enable_on_thread_created = old_value;
+#endif
+
Py_XDECREF(source_copy);
goto finally;
diff --git a/Python/pylifecycle.c b/Python/pylifecycle.c
index f24b048..67bbbd0 100644
--- a/Python/pylifecycle.c
+++ b/Python/pylifecycle.c
@@ -854,6 +854,11 @@ pycore_interp_init(PyThreadState *tstate)
return status;
}
+ status = _PyCode_Init(interp);
+ if (_PyStatus_EXCEPTION(status)) {
+ return status;
+ }
+
status = _PyDtoa_Init(interp);
if (_PyStatus_EXCEPTION(status)) {
return status;
@@ -1827,6 +1832,8 @@ finalize_interp_types(PyInterpreterState *interp)
_PyTypes_Fini(interp);
+ _PyCode_Fini(interp);
+
// Call _PyUnicode_ClearInterned() before _PyDict_Fini() since it uses
// a dict internally.
_PyUnicode_ClearInterned(interp);
diff --git a/Python/pystate.c b/Python/pystate.c
index f442d87..7c75263 100644
--- a/Python/pystate.c
+++ b/Python/pystate.c
@@ -404,6 +404,7 @@ _Py_COMP_DIAG_POP
&(runtime)->audit_hooks.mutex, \
&(runtime)->allocators.mutex, \
&(runtime)->_main_interpreter.types.mutex, \
+ &(runtime)->_main_interpreter.code_state.mutex, \
}
static void