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.
This commit is contained in:
Sam Gross 2024-05-06 20:12:39 -04:00 committed by GitHub
parent 8d8275b0cf
commit 723d4d2fe8
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
14 changed files with 375 additions and 18 deletions

View file

@ -8,6 +8,8 @@ extern "C" {
# error "this header requires Py_BUILD_CORE define"
#endif
#include "pycore_lock.h" // PyMutex
// We hide some of the newer PyCodeObject fields behind macros.
// This helps with backporting certain changes to 3.12.
@ -16,6 +18,14 @@ extern "C" {
#define _PyCode_HAS_INSTRUMENTATION(CODE) \
(CODE->_co_instrumentation_version > 0)
struct _py_code_state {
PyMutex mutex;
// Interned constants from code objects. Used by the free-threaded build.
struct _Py_hashtable_t *constants;
};
extern PyStatus _PyCode_Init(PyInterpreterState *interp);
extern void _PyCode_Fini(PyInterpreterState *interp);
#define CODE_MAX_WATCHERS 8

View file

@ -245,6 +245,7 @@ struct _is {
struct _Py_long_state long_state;
struct _dtoa_state dtoa;
struct _py_func_state func_state;
struct _py_code_state code_state;
struct _Py_dict_state dict_state;
struct _Py_exc_state exc_state;

View file

@ -30,6 +30,9 @@ PyAPI_DATA(PyObject *) _PySet_Dummy;
PyAPI_FUNC(int) _PySet_Contains(PySetObject *so, PyObject *key);
// Clears the set without acquiring locks. Used by _PyCode_Fini.
extern void _PySet_ClearInternal(PySetObject *so);
#ifdef __cplusplus
}
#endif