[Python-ideas] Create a StringBuilder class and use it everywhere

Stefan Behnel stefan_ml at behnel.de
Thu Aug 25 19:21:45 CEST 2011


Antoine Pitrou, 25.08.2011 18:58:
> Le jeudi 25 août 2011 à 18:50 +0200, Masklinn a écrit :
>> On 2011-08-25, at 18:40 , Antoine Pitrou wrote:
>>>> The most popular (as from what I can see) thing right now where people start seeing
>>>> that += is slow is when they try to do that on PyPy (which doesn't have hack like CPython,
>>>> who is still slow) and ask "why my pypy code is sooooo slow".
>>>
>>> Different implementations having different performance characteristics
>>> is not totally unexpected, is it?
>>> (and I'm sure the PyPy developers wouldn't mind adding another hack)
>> This one can not be done, as it relies on knowing there's only one reference
>> to the string (so it can be realloc'd in place), therefore on using a
>> refcounting GC.
>
> Ah, you're right.
> However, PyPy has another (and quite broader) set of optimizations
> available:
> http://codespeak.net/pypy/dist/pypy/doc/interpreter-optimizations.html#string-optimizations

And its JIT could potentially just enable its string-join optimisation 
automatically when it sees that a variable holds a string, and is never 
being assigned to inside of a loop or sequence of operations except for the 
+= operator. Any other operation on the string would then just turn it back 
into a normal string by joining it first.

But this is seriously getting off-topic now.

Stefan




More information about the Python-ideas mailing list