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

Antoine Pitrou solipsis at pitrou.net
Thu Aug 25 18:58:24 CEST 2011

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


> Since Pypy does not use refcounting, it can't do that as a rule (it might be
> possible to handle it for a limited number of cases via escape analysis,
> proving there can be only one reference to the string, but I'd say there
> are more interesting things to use escape analysis for).

The CPython optimization itself works in a limited number of cases,
because having a refcount of 1 essentially means it's a local variable,
and because of the result having to be stored back immediately in the
same local variable (otherwise you can't recycle the original object's



More information about the Python-ideas mailing list