]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
gh-125196: Add fast-path for int in PyUnicodeWriter_WriteStr() (#125214)
authorVictor Stinner <vstinner@python.org>
Wed, 9 Oct 2024 22:01:02 +0000 (00:01 +0200)
committerGitHub <noreply@github.com>
Wed, 9 Oct 2024 22:01:02 +0000 (00:01 +0200)
PyUnicodeWriter_WriteStr() and PyUnicodeWriter_WriteRepr() now call
directly _PyLong_FormatWriter() if the argument is an int.

Objects/unicodeobject.c

index 4ea7d5f380e9a28dcf198a50be6b4d1d5f734875..a9b332481638800d4afe00e056b8804450d03896 100644 (file)
@@ -13632,6 +13632,10 @@ _PyUnicodeWriter_WriteStr(_PyUnicodeWriter *writer, PyObject *str)
 int
 PyUnicodeWriter_WriteStr(PyUnicodeWriter *writer, PyObject *obj)
 {
+    if (Py_TYPE(obj) == &PyLong_Type) {
+        return _PyLong_FormatWriter((_PyUnicodeWriter*)writer, obj, 10, 0);
+    }
+
     PyObject *str = PyObject_Str(obj);
     if (str == NULL) {
         return -1;
@@ -13646,6 +13650,10 @@ PyUnicodeWriter_WriteStr(PyUnicodeWriter *writer, PyObject *obj)
 int
 PyUnicodeWriter_WriteRepr(PyUnicodeWriter *writer, PyObject *obj)
 {
+    if (Py_TYPE(obj) == &PyLong_Type) {
+        return _PyLong_FormatWriter((_PyUnicodeWriter*)writer, obj, 10, 0);
+    }
+
     PyObject *repr = PyObject_Repr(obj);
     if (repr == NULL) {
         return -1;