This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Author vstinner
Recipients loewis, mark.dickinson, pitrou, python-dev, serhiy.storchaka, vstinner
Date 2012-05-24.09:57:29
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <CAMpsgwYbvCO46NCZzc_NKM5T0tzj58bFQS-z=1FF1rsQ7VjvzA@mail.gmail.com>
In-reply-to <1337846325.2589.30.camel@raxxla>
Content
>> For Python 3.3, _PyUnicodeWriter API is faster than the Py_UCS4 buffer API and PyAccu API in quite all cases, with a speedup between 30% and 100%. But there are some cases where the _PyUnicodeWriter API is slower:
>
> Perhaps most of these problems can be solved if instead of the boolean
> flag (overallocate/no overallocate) to use the Py_ssize_t parameter that
> indicates by how much should you overallocate (it is the length of the
> suffix in the format).

There is not only a flag (flags.overallocate): there is also the
min_length, which is used and helps for str%args and str.format(args).

My patch contains a lot of "tricks" to limit overallocation, e.g.
don't overallocate if we are writing the last part of the output.

Computing exactly the size of the buffer gives the best performance
because it avoids a resize in _PyUnicodeWriter_Finish(). I tried for
example to modify PyUnicode_Format() to parse the format string twice:
first to compute the size of the output buffer, second to write
characters. In my experience, parsing the format string twice is more
expensive than reallocating the buffer (PyUnicode_READ is expensive),
especially on short and simple format strings.

I tried different methods to allocate the buffer of _PyUnicodeWriter:
change the overallocation factor (0%, 25%, 50%, 100%), only
overallocate +100 characters, etc. But I failed to find something
better than the proposed patch.

At least I can say than always disabling overallocation slows done
many cases: when there is a suffix after an argument, or when there
are more than one argument.

Feel free to experiment other methods to estimate the size of the output buffer.
History
Date User Action Args
2012-05-24 09:57:30vstinnersetrecipients: + vstinner, loewis, mark.dickinson, pitrou, python-dev, serhiy.storchaka
2012-05-24 09:57:29vstinnerlinkissue14744 messages
2012-05-24 09:57:29vstinnercreate