]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
gh-133968: Add fast path to PyUnicodeWriter_WriteStr() (#133969)
authorVictor Stinner <vstinner@python.org>
Tue, 13 May 2025 13:31:41 +0000 (15:31 +0200)
committerGitHub <noreply@github.com>
Tue, 13 May 2025 13:31:41 +0000 (15:31 +0200)
Don't call PyObject_Str() if the input type is str.

Objects/unicodeobject.c

index cd26494ad8f1d6680f88662d6827216dd8f17cf3..aa94fb91e65fc3a7a101ee4654bbbceb0ef4e3b8 100644 (file)
@@ -13929,7 +13929,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);
     }