Commits

Victor Stinner  committed b77851f

More fast path

  • Participants
  • Parent commits d789054

Comments (0)

Files changed (3)

File Objects/stringlib/unicode_format.h

     else if (PyFloat_CheckExact(fieldobj))
         formatter = _PyFloat_FormatAdvanced;
 
-    /* XXX: for 2.6, convert format_spec to the appropriate type
-       (unicode, str) */
-
     if (formatter) {
         /* we know exactly which formatter will be called when __format__ is
            looked up, so call it directly, instead. */

File Objects/unicodeobject.c

             case 's':
             case 'r':
             case 'a':
-                if (width == -1 && prec == -1) {
+                if (PyLong_CheckExact(v) && width == -1 && prec == -1) {
                     /* Fast path */
-                    if (PyUnicode_CheckExact(v) && c == 's') {
-                        if (_PyUnicodeWriter_WriteStr(&writer, v) == -1)
-                            goto onError;
-                        goto nextarg;
-                    }
-                    else if (PyLong_CheckExact(v)) {
-                        if (_PyLong_FormatWriter(v, 10, flags & F_ALT, &writer) == -1)
-                            goto onError;
-                        goto nextarg;
-                    }
+                    if (_PyLong_FormatWriter(v, 10, flags & F_ALT, &writer) == -1)
+                        goto onError;
+                    goto nextarg;
                 }
 
                 if (PyUnicode_CheckExact(v) && c == 's') {
                     {
                     case 'd':
                     case 'i':
+                    case 'u':
                         if (_PyLong_FormatWriter(v, 10, flags & F_ALT, &writer) == -1)
                             goto onError;
                         goto nextarg;
                 Py_UCS4 ch = formatchar(v);
                 if (ch == (Py_UCS4) -1)
                     goto onError;
+                if (width == -1 && prec == -1) {
+                    /* Fast path */
+                    if (_PyUnicodeWriter_Prepare(&writer, 1, ch) == -1)
+                        goto onError;
+                    PyUnicode_WRITE(writer.kind, writer.data, writer.pos, ch);
+                    writer.pos += 1;
+                    goto nextarg;
+                }
                 temp = PyUnicode_FromOrdinal(ch);
                 break;
             }
             if (temp == NULL)
                 goto onError;
             assert (PyUnicode_Check(temp));
+
+            if (width == -1 && prec == -1) {
+                /* Fast path */
+                if (_PyUnicodeWriter_WriteStr(&writer, temp) == -1)
+                    goto onError;
+                goto nextarg;
+            }
+
             if (PyUnicode_READY(temp) == -1) {
                 Py_CLEAR(temp);
                 goto onError;
             if (!(flags & F_LJUST)) {
                 if (sign) {
                     if ((width-1) > len)
-                        bufmaxchar = Py_MAX(bufmaxchar, fill);
+                        bufmaxchar = MAX_MAXCHAR(bufmaxchar, fill);
                 }
                 else {
                     if (width > len)
-                        bufmaxchar = Py_MAX(bufmaxchar, fill);
+                        bufmaxchar = MAX_MAXCHAR(bufmaxchar, fill);
                 }
             }
             maxchar = _PyUnicode_FindMaxChar(temp, 0, pindex+len);
-            bufmaxchar = Py_MAX(bufmaxchar, maxchar);
+            bufmaxchar = MAX_MAXCHAR(bufmaxchar, maxchar);
 
             buflen = width;
             if (sign && len == width)

File Python/formatter_unicode.c

         goto done;
     }
 
+    if (format->width == -1 && format->precision == -1) {
+        /* Fast path */
+        return _PyUnicodeWriter_WriteStr(writer, value);
+    }
+
     /* if precision is specified, output no more that format.precision
        characters */
     if (format->precision >= 0 && len >= format->precision) {
 {
     InternalFormatSpec format;
 
+    assert(PyUnicode_Check(obj));
+
     /* check for the special case of zero length format spec, make
        it equivalent to str(obj) */
     if (start == end) {