gh-124218: Use per-thread reference counting for globals and builtins (#125713)

Use per-thread refcounting for the reference from function objects to
the globals and builtins dictionaries.
This commit is contained in:
Sam Gross 2024-10-21 12:51:29 -04:00 committed by GitHub
parent d880c83ff7
commit 9b0bfba2a2
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
8 changed files with 115 additions and 11 deletions

View file

@ -1636,6 +1636,24 @@ _PyDict_MaybeUntrack(PyObject *op)
_PyObject_GC_UNTRACK(op);
}
void
_PyDict_EnablePerThreadRefcounting(PyObject *op)
{
assert(PyDict_Check(op));
#ifdef Py_GIL_DISABLED
Py_ssize_t id = _PyObject_AssignUniqueId(op);
if ((uint64_t)id >= (uint64_t)DICT_UNIQUE_ID_MAX) {
_PyObject_ReleaseUniqueId(id);
return;
}
PyDictObject *mp = (PyDictObject *)op;
assert((mp->_ma_watcher_tag >> DICT_UNIQUE_ID_SHIFT) == 0);
// Plus 1 so that _ma_watcher_tag=0 represents an unassigned id
mp->_ma_watcher_tag += ((uint64_t)id + 1) << DICT_UNIQUE_ID_SHIFT;
#endif
}
static inline int
is_unusable_slot(Py_ssize_t ix)
{