mirror of
https://github.com/python/cpython.git
synced 2025-08-04 08:59:19 +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
|
@ -1,8 +1,9 @@
|
|||
/* Frame object implementation */
|
||||
|
||||
#include "Python.h"
|
||||
#include "pycore_ceval.h" // _PyEval_BuiltinsFromGlobals()
|
||||
#include "pycore_ceval.h" // _PyEval_SetOpcodeTrace()
|
||||
#include "pycore_code.h" // CO_FAST_LOCAL, etc.
|
||||
#include "pycore_dict.h" // _PyDict_LoadBuiltinsFromGlobals()
|
||||
#include "pycore_function.h" // _PyFunction_FromConstructor()
|
||||
#include "pycore_moduleobject.h" // _PyModule_GetDict()
|
||||
#include "pycore_modsupport.h" // _PyArg_CheckPositional()
|
||||
|
@ -1899,7 +1900,7 @@ PyFrameObject*
|
|||
PyFrame_New(PyThreadState *tstate, PyCodeObject *code,
|
||||
PyObject *globals, PyObject *locals)
|
||||
{
|
||||
PyObject *builtins = _PyEval_BuiltinsFromGlobals(tstate, globals); // borrowed ref
|
||||
PyObject *builtins = _PyDict_LoadBuiltinsFromGlobals(globals);
|
||||
if (builtins == NULL) {
|
||||
return NULL;
|
||||
}
|
||||
|
@ -1914,6 +1915,7 @@ PyFrame_New(PyThreadState *tstate, PyCodeObject *code,
|
|||
.fc_closure = NULL
|
||||
};
|
||||
PyFunctionObject *func = _PyFunction_FromConstructor(&desc);
|
||||
_Py_DECREF_BUILTINS(builtins);
|
||||
if (func == NULL) {
|
||||
return NULL;
|
||||
}
|
||||
|
@ -2204,21 +2206,3 @@ PyFrame_GetGenerator(PyFrameObject *frame)
|
|||
PyGenObject *gen = _PyGen_GetGeneratorFromFrame(frame->f_frame);
|
||||
return Py_NewRef(gen);
|
||||
}
|
||||
|
||||
PyObject*
|
||||
_PyEval_BuiltinsFromGlobals(PyThreadState *tstate, PyObject *globals)
|
||||
{
|
||||
PyObject *builtins = PyDict_GetItemWithError(globals, &_Py_ID(__builtins__));
|
||||
if (builtins) {
|
||||
if (PyModule_Check(builtins)) {
|
||||
builtins = _PyModule_GetDict(builtins);
|
||||
assert(builtins != NULL);
|
||||
}
|
||||
return builtins;
|
||||
}
|
||||
if (PyErr_Occurred()) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
return _PyEval_GetBuiltins(tstate);
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue