Commits

Victor Stinner committed faa88c5

More fast path for float

Comments (0)

Files changed (3)

Objects/floatobject.c

                                       NULL);
     if (!buf)
         return PyErr_NoMemory();
-    result = PyUnicode_FromString(buf);
+    result = PyUnicode_FromKindAndData(PyUnicode_1BYTE_KIND,
+                                       buf, strlen(buf));
     PyMem_Free(buf);
     return result;
 }

Objects/unicodeobject.c

 
 /* Returns a new reference to a PyUnicode object, or NULL on failure. */
 
-static PyObject *
-formatfloat(PyObject *v, int flags, int prec, int type)
+static int
+formatfloat(PyObject *v, int flags, int prec, int type,
+            PyObject **p_output, _PyUnicodeWriter *writer)
 {
     char *p;
-    PyObject *result;
     double x;
+    Py_ssize_t len;
 
     x = PyFloat_AsDouble(v);
     if (x == -1.0 && PyErr_Occurred())
-        return NULL;
+        return -1;
 
     if (prec < 0)
         prec = 6;
     p = PyOS_double_to_string(x, type, prec,
                               (flags & F_ALT) ? Py_DTSF_ALT : 0, NULL);
     if (p == NULL)
-        return NULL;
-    result = unicode_fromascii((unsigned char*)p, strlen(p));
+        return -1;
+    len = strlen(p);
+    if (writer) {
+        if (_PyUnicodeWriter_Prepare(writer, len, 127) == -1)
+            return -1;
+        memcpy(writer->data + writer->pos * writer->kind,
+               p,
+               len);
+        writer->pos += len;
+    }
+    else
+        *p_output = unicode_fromascii((unsigned char*)p, strlen(p));
     PyMem_Free(p);
-    return result;
+    return 0;
 }
 
 /* formatlong() emulates the format codes d, u, o, x and X, and
             case 'F':
             case 'g':
             case 'G':
+                if (width == -1 && prec == -1
+                    && !(flags & (F_SIGN | F_BLANK)))
+                {
+                    /* Fast path */
+                    if (formatfloat(v, flags, prec, c, NULL, &writer) == -1)
+                        goto onError;
+                    goto nextarg;
+                }
+
                 sign = 1;
                 if (flags & F_ZERO)
                     fill = '0';
-                temp = formatfloat(v, flags, prec, c);
+                if (formatfloat(v, flags, prec, c, &temp, NULL) == -1)
+                    temp = NULL;
                 break;
 
             case 'c':

Python/formatter_unicode.c

         if (format->sign != '+' && format->sign != ' '
             && format->width == -1
             && format->type != 'X' && format->type != 'n'
+            && !format->thousands_separators
             && PyLong_CheckExact(value))
         {
             /* Fast path */
 
     if (format->sign != '+' && format->sign != ' '
         && format->width == -1
-        && format->type != 'n')
+        && format->type != 'n'
+        && !format->thousands_separators)
     {
         /* Fast path */
         result = _PyUnicodeWriter_WriteStr(writer, unicode_tmp);