Issue #15609: Optimize str%args for integer argument

- Use _PyLong_FormatWriter() instead of formatlong() when possible, to avoid
   a temporary buffer
 - Enable the fast path when width is smaller or equals to the length,
   and when the precision is bigger or equals to the length
 - Add unit tests!
 - formatlong() uses PyUnicode_Resize() instead of _PyUnicode_FromASCII()
   to resize the output string
This commit is contained in:
Victor Stinner 2012-10-02 00:33:47 +02:00
parent fd0d3e5d25
commit 621ef3d84f
3 changed files with 119 additions and 68 deletions

View file

@ -757,7 +757,8 @@ format_string_internal(PyObject *value, const InternalFormatSpec *format,
goto done;
}
if (format->width == -1 && format->precision == -1) {
if ((format->width == -1 || format->width <= len)
&& (format->precision == -1 || format->precision >= len)) {
/* Fast path */
return _PyUnicodeWriter_WriteStr(writer, value);
}