gh-133968: Add fast path to PyUnicodeWriter_WriteStr() (GH-133969)
Don't call PyObject_Str() if the input type is str.
(cherry picked from commit
fe9f6e829a535747b1e06d9bfda033a9a47165ed)
Co-authored-by: Victor Stinner <vstinner@python.org>
int
PyUnicodeWriter_WriteStr(PyUnicodeWriter *writer, PyObject *obj)
{
- if (Py_TYPE(obj) == &PyLong_Type) {
+ PyTypeObject *type = Py_TYPE(obj);
+ if (type == &PyUnicode_Type) {
+ return _PyUnicodeWriter_WriteStr((_PyUnicodeWriter*)writer, obj);
+ }
+
+ if (type == &PyLong_Type) {
return _PyLong_FormatWriter((_PyUnicodeWriter*)writer, obj, 10, 0);
}