]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
gh-118527: Intern filename, name, and qualname in code objects. (#118558)
authorSam Gross <colesbury@gmail.com>
Fri, 3 May 2024 22:16:45 +0000 (18:16 -0400)
committerGitHub <noreply@github.com>
Fri, 3 May 2024 22:16:45 +0000 (18:16 -0400)
This interns the strings for `co_filename`, `co_name`, and `co_qualname`
on codeobjects in the free-threaded build. This partially addresses a
reference counting bottleneck when creating closures concurrently. The
closures take the name and qualified name from the code object.

Objects/codeobject.c

index 7d02b038cf52caf631a620b2661451d75dc3a13c..15d3960fc2df5128cf17675da520fda0ea14e142 100644 (file)
@@ -390,6 +390,11 @@ init_code(PyCodeObject *co, struct _PyCodeConstructor *con)
     co->co_filename = Py_NewRef(con->filename);
     co->co_name = Py_NewRef(con->name);
     co->co_qualname = Py_NewRef(con->qualname);
+#ifdef Py_GIL_DISABLED
+    PyUnicode_InternInPlace(&co->co_filename);
+    PyUnicode_InternInPlace(&co->co_name);
+    PyUnicode_InternInPlace(&co->co_qualname);
+#endif
     co->co_flags = con->flags;
 
     co->co_firstlineno = con->firstlineno;