Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Improve multi-threaded scaling of :func:`format`, f-strings and
:meth:`str.format` with a format specification on the :term:`free-threaded
build` by avoiding the creation of a temporary bound method and reference
count contention on the shared ``__format__`` method.
38 changes: 24 additions & 14 deletions Objects/abstract.c
Original file line number Diff line number Diff line change
Expand Up @@ -853,7 +853,6 @@ _PyBuffer_ReleaseInInterpreterAndRawFree(PyInterpreterState *interp,
PyObject *
PyObject_Format(PyObject *obj, PyObject *format_spec)
{
PyObject *meth;
PyObject *empty = NULL;
PyObject *result = NULL;

Expand All @@ -880,21 +879,32 @@ PyObject_Format(PyObject *obj, PyObject *format_spec)
format_spec = empty;
}

/* Find the (unbound!) __format__ method */
meth = _PyObject_LookupSpecial(obj, &_Py_ID(__format__));
if (meth == NULL) {
PyThreadState *tstate = _PyThreadState_GET();
if (!_PyErr_Occurred(tstate)) {
_PyErr_Format(tstate, PyExc_TypeError,
"Type %.100s doesn't define __format__",
Py_TYPE(obj)->tp_name);
/* Find the __format__ method and call it, without creating
a temporary bound method where possible. */
PyThreadState *tstate = _PyThreadState_GET();
_PyCStackRef cref;
_PyThreadState_PushCStackRef(tstate, &cref);
_PyStackRef self = PyStackRef_FromPyObjectBorrow(obj);
int found = _PyObject_LookupSpecialMethod(&_Py_ID(__format__),
&cref.ref, &self);
if (found > 0) {
PyObject *meth = PyStackRef_AsPyObjectBorrow(cref.ref);
PyObject *args[2] = {PyStackRef_AsPyObjectBorrow(self), format_spec};
if (args[0] != NULL) {
/* Unbound method: prepend self. */
result = PyObject_Vectorcall(meth, args, 2, NULL);
}
else {
result = PyObject_Vectorcall(meth, args + 1, 1, NULL);
}
goto done;
}

/* And call it. */
result = PyObject_CallOneArg(meth, format_spec);
Py_DECREF(meth);
else if (found == 0) {
_PyErr_Format(tstate, PyExc_TypeError,
"Type %.100s doesn't define __format__",
Py_TYPE(obj)->tp_name);
}
PyStackRef_XCLOSE(self);
_PyThreadState_PopCStackRef(tstate, &cref);

if (result && !PyUnicode_Check(result)) {
PyErr_Format(PyExc_TypeError,
Expand Down
Loading