From 861317084a6b392d2706027121f178d221ef5bf9 Mon Sep 17 00:00:00 2001 From: Victor Stinner Date: Tue, 13 May 2025 14:50:32 +0200 Subject: [PATCH] gh-133968: Add fast path to PyUnicodeWriter_WriteStr() Don't call PyObject_Str() if the input type is str. --- Objects/unicodeobject.c | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/Objects/unicodeobject.c b/Objects/unicodeobject.c index f3f0c9646a652e..af2d7938399015 100644 --- a/Objects/unicodeobject.c +++ b/Objects/unicodeobject.c @@ -13919,7 +13919,12 @@ _PyUnicodeWriter_WriteStr(_PyUnicodeWriter *writer, PyObject *str) 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); }