gh-118527: Intern filename, name, and qualname in code objects. (#118558)

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.
This commit is contained in:
Sam Gross 2024-05-03 18:16:45 -04:00 committed by GitHub
parent 0e78a545e6
commit 37c31bea72
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 5 additions and 0 deletions

View File

@ -390,6 +390,11 @@ init_code(PyCodeObject *co, struct _PyCodeConstructor *con)
co->co_filename = Py_NewRef(con->filename); co->co_filename = Py_NewRef(con->filename);
co->co_name = Py_NewRef(con->name); co->co_name = Py_NewRef(con->name);
co->co_qualname = Py_NewRef(con->qualname); 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_flags = con->flags;
co->co_firstlineno = con->firstlineno; co->co_firstlineno = con->firstlineno;