8000 gh-124218: Use per-thread reference counting for globals and builtins · colesbury/cpython@c318ca8 · GitHub
[go: up one dir, main page]

Skip to content

Commit c318ca8

Browse files
committed
pythongh-124218: Use per-thread reference counting for globals and builtins
Use per-thread refcounting for the reference from function objects to the globals and builtins dictionaries.
1 parent d8c8648 commit c318ca8

File tree

5 files changed

+91
-8
lines changed

5 files changed

+91
-8
lines changed

Include/internal/pycore_dict.h

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,8 @@ static inline PyDictUnicodeEntry* DK_UNICODE_ENTRIES(PyDictKeysObject *dk) {
229229
#define DICT_VERSION_INCREMENT (1 << (DICT_MAX_WATCHERS + DICT_WATCHED_MUTATION_BITS))
230230
#define DICT_WATCHER_MASK ((1 << DICT_MAX_WATCHERS) - 1)
231231
#define DICT_WATCHER_AND_MODIFICATION_MASK ((1 << (DICT_MAX_WATCHERS + DICT_WATCHED_MUTATION_BITS)) - 1)
232+
#define DICT_UNIQUE_ID_SHIFT (DICT_MAX_WATCHERS + DICT_WATCHED_MUTATION_BITS)
233+
#define DICT_UNIQUE_ID_MAX (UINT64_MAX >> DICT_UNIQUE_ID_SHIFT)
232234

233235

234236
PyAPI_FUNC(void)
@@ -307,8 +309,38 @@ _PyInlineValuesSize(PyTypeObject *tp)
307309
int
308310
_PyDict_DetachFromObject(PyDictObject *dict, PyObject *obj);
309311

312+
// Enables per-thread ref counting on this dict in the free threading build
313+
extern void _PyDict_EnablePerThreadRefcounting(PyObject *op);
314+
310315
PyDictObject *_PyObject_MaterializeManagedDict_LockHeld(PyObject *);
311316

317+
#ifndef Py_GIL_DISABLED
318+
# define _Py_INCREF_DICT Py_INCREF
319+
# define _Py_DECREF_DICT Py_DECREF
320+
#else
321+
static inline Py_ssize_t
322+
_PyDict_UniqueId(PyDictObject *mp)
323+
{
324+
return (Py_ssize_t)(mp->_ma_watcher_tag >> DICT_UNIQUE_ID_SHIFT) - 1;
325+
}
326+
327+
static inline void
328+
_Py_INCREF_DICT(PyObject *op)
329+
{
330+
assert(PyDict_Check(op));
331+
Py_ssize_t id = _PyDict_UniqueId((PyDictObject *)op);
332+
_Py_THREAD_INCREF_OBJECT(op, id);
333+
}
334+
335+
static inline void
336+
_Py_DECREF_DICT(PyObject *op)
337+
{
338+
assert(PyDict_Check(op));
339+
Py_ssize_t id = _PyDict_UniqueId((PyDictObject *)op);
340+
_Py_THREAD_DECREF_OBJECT(op, id);
341+
}
342+
#endif
343+
312344
#ifdef __cplusplus
313345
}
314346
#endif

Objects/dictobject.c

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1636,6 +1636,19 @@ _PyDict_MaybeUntrack(PyObject *op)
16361636
_PyObject_GC_UNTRACK(op);
16371637
}
16381638

1639+
void
1640+
_PyDict_EnablePerThreadRefcounting(PyObject *op)
1641+
{
1642+
assert(PyDict_Check(op));
1643+
#ifdef Py_GIL_DISABLED
1644+
Py_ssize_t id = _PyObject_AssignUniqueId(op);
1645+
1646+
PyDictObject *mp = (PyDictObject *)op;
1647+
assert((mp->_ma_watcher_tag >> DICT_UNIQUE_ID_SHIFT) == 0);
1648+
mp->_ma_watcher_tag += (id + 1) << DICT_UNIQUE_ID_SHIFT;
1649+
#endif
1650+
}
1651+
16391652
static inline int
16401653
is_unusable_slot(Py_ssize_t ix)
16411654
{

Objects/funcobject.c

Lines changed: 32 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
#include "Python.h"
55
#include "pycore_ceval.h" // _PyEval_BuiltinsFromGlobals()
6+
#include "pycore_dict.h" // _Py_INCREF_DICT()
67
#include "pycore_long.h" // _PyLong_GetOne()
78
#include "pycore_modsupport.h" // _PyArg_NoKeywords()
89
#include "pycore_object.h" // _PyObject_GC_UNTRACK()
@@ -112,8 +113,15 @@ _PyFunction_FromConstructor(PyFrameConstructor *constr)
112113
Py_XDECREF(module);
113114
return NULL;
114115
}
115-
op->func_globals = Py_NewRef(constr->fc_globals);
116-
op->func_builtins = Py_NewRef(constr->fc_builtins);
116+
_Py_INCREF_DICT(constr->fc_globals);
117+
op->func_globals = constr->fc_globals;
118+
if (PyDict_Check(constr->fc_builtins)) {
119+
_Py_INCREF_DICT(constr->fc_builtins);
120+
}
121+
else {
122+
Py_INCREF(constr->fc_builtins);
123+
}
124+
op->func_builtins = constr->fc_builtins;
117125
op->func_name = Py_NewRef(constr->fc_name);
118126
op->func_qualname = Py_NewRef(constr->fc_qualname);
119127
_Py_INCREF_CODE((PyCodeObject *)constr->fc_code);
@@ -143,7 +151,7 @@ PyFunction_NewWithQualName(PyObject *code, PyObject *globals, PyObject *qualname
143151
{
144152
assert(globals != NULL);
145153
assert(PyDict_Check(globals));
146-
Py_INCREF(globals);
154+
_Py_INCREF_DICT(globals);
147155

148156
PyThreadState *tstate = _PyThreadState_GET();
149157

@@ -184,7 +192,12 @@ PyFunction_NewWithQualName(PyObject *code, PyObject *globals, PyObject *qualname
184192
if (builtins == NULL) {
185193
goto error;
186194
}
187-
Py_INCREF(builtins);
195+
if (PyDict_Check(builtins)) {
196+
_Py_INCREF_DICT(builtins);
197+
}
198+
else {
199+
Py_INCREF(builtins);
200+
}
188201

189202
PyFunctionObject *op = PyObject_GC_New(PyFunctionObject, &PyFunction_Type);
190203
if (op == NULL) {
@@ -1057,8 +1070,21 @@ func_clear(PyObject *self)
10571070
{
10581071
PyFunctionObject *op = _PyFunction_CAST(self);
10591072
func_clear_version(_PyInterpreterState_GET(), op);
1060-
Py_CLEAR(op->func_globals);
1061-
Py_CLEAR(op->func_builtins);
1073+
PyObject *globals = op->func_globals;
1074+
op->func_globals = NULL;
1075+
if (globals != NULL) {
1076+
_Py_DECREF_DICT(globals);
1077+
}
1078+
PyObject *builtins = op->func_builtins;
1079+
op->func_builtins = NULL;
1080+
if (builtins != NULL) {
1081+
if (PyDict_Check(builtins)) {
1082+
_Py_DECREF_DICT(builtins);
1083+
}
1084+
else {
1085+
Py_DECREF(builtins);
1086+
}
1087+
}
10621088
Py_CLEAR(op->func_module);
10631089
Py_CLEAR(op->func_defaults);
10641090
Py_CLEAR(op->func_kwdefaults);

Objects/moduleobject.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
#include "Python.h"
55
#include "pycore_call.h" // _PyObject_CallNoArgs()
6+
#include "pycore_dict.h" // _PyDict_EnablePerThreadRefcounting()
67
#include "pycore_fileutils.h" // _Py_wgetcwd
78
#include "pycore_interp.h" // PyInterpreterState.importlib
89
#include "pycore_long.h" // _PyLong_GetOne()
@@ -105,7 +106,7 @@ new_module_notrack(PyTypeObject *mt)
105106
static void
106107
track_module(PyModuleObject *m)
107108
{
108-
_PyObject_SetDeferredRefcount(m->md_dict);
109+
_PyDict_EnablePerThreadRefcounting(m->md_dict);
109110
PyObject_GC_Track(m->md_dict);
110111

111112
_PyObject_SetDeferredRefcount((PyObject *)m);

Python/uniqueid.c

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#include "Python.h"
22

3+
#include "pycore_dict.h" // DICT_UNIQUE_ID_MAX
34
#include "pycore_lock.h" // PyMutex_LockFlags()
45
#include "pycore_pystate.h" // _PyThreadState_GET()
56
#include "pycore_object.h" // _Py_IncRefTotal
@@ -90,10 +91,15 @@ _PyObject_AssignUniqueId(PyObject *obj)
9091
}
9192

9293
_Py_unique_id_entry *entry = pool->freelist;
94+
Py_ssize_t unique_id = (entry - pool->table);
95+
if ((uint64_t)unique_id >= DICT_UNIQUE_ID_MAX) {
96+
UNLOCK_POOL(pool);
97+
return -1;
98+
}
99+
93100
pool->freelist = entry->next;
94101
entry->obj = obj;
95102
_PyObject_SetDeferredRefcount(obj);
96-
Py_ssize_t unique_id = (entry - pool->table);
97103
UNLOCK_POOL(pool);
98104
return unique_id;
99105
}
@@ -128,6 +134,11 @@ clear_unique_id(PyObject *obj)
128134
id = co->_co_unique_id;
129135
co->_co_unique_id = -1;
130136
}
137+
else if (PyDict_Check(obj)) {
138+
PyDictObject *mp = (PyDictObject *)obj;
139+
id = _PyDict_UniqueId(mp);
140+
mp->_ma_watcher_tag &= ~(UINT64_MAX << DICT_UNIQUE_ID_SHIFT);
141+
}
131142
return id;
132143
}
133144

0 commit comments

Comments
 (0)
0