PyUnicode_Format() creates short temporary substrings. Attached patch tries to avoid substrings. For example, it avoids write of 1 character and repetition of 1 character like a space. PyUnicode_Format() now works in two steps:
- computes the maximum character and the length of the output string
- write characters into the output string
I'm not sure that my patch is correct, nor that the change does really speed-up Python.