mirror of
https://github.com/python/cpython.git
synced 2025-07-12 13:55:34 +00:00
gh-124218: Avoid refcount contention on builtins module (GH-125847)
This replaces `_PyEval_BuiltinsFromGlobals` with `_PyDict_LoadBuiltinsFromGlobals`, which returns a new reference instead of a borrowed reference. Internally, the new function uses per-thread reference counting when possible to avoid contention on the refcount fields on the builtins module.
This commit is contained in:
parent
5003ad5c5e
commit
3c4a7fa617
6 changed files with 74 additions and 47 deletions
|
@ -639,7 +639,7 @@ PyEval_EvalCode(PyObject *co, PyObject *globals, PyObject *locals)
|
|||
if (locals == NULL) {
|
||||
locals = globals;
|
||||
}
|
||||
PyObject *builtins = _PyEval_BuiltinsFromGlobals(tstate, globals); // borrowed ref
|
||||
PyObject *builtins = _PyDict_LoadBuiltinsFromGlobals(globals);
|
||||
if (builtins == NULL) {
|
||||
return NULL;
|
||||
}
|
||||
|
@ -654,6 +654,7 @@ PyEval_EvalCode(PyObject *co, PyObject *globals, PyObject *locals)
|
|||
.fc_closure = NULL
|
||||
};
|
||||
PyFunctionObject *func = _PyFunction_FromConstructor(&desc);
|
||||
_Py_DECREF_BUILTINS(builtins);
|
||||
if (func == NULL) {
|
||||
return NULL;
|
||||
}
|
||||
|
@ -1899,7 +1900,7 @@ PyEval_EvalCodeEx(PyObject *_co, PyObject *globals, PyObject *locals,
|
|||
if (defaults == NULL) {
|
||||
return NULL;
|
||||
}
|
||||
PyObject *builtins = _PyEval_BuiltinsFromGlobals(tstate, globals); // borrowed ref
|
||||
PyObject *builtins = _PyDict_LoadBuiltinsFromGlobals(globals);
|
||||
if (builtins == NULL) {
|
||||
Py_DECREF(defaults);
|
||||
return NULL;
|
||||
|
@ -1954,6 +1955,7 @@ fail:
|
|||
Py_XDECREF(func);
|
||||
Py_XDECREF(kwnames);
|
||||
PyMem_Free(newargs);
|
||||
_Py_DECREF_BUILTINS(builtins);
|
||||
Py_DECREF(defaults);
|
||||
return res;
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue