python-dev Summary for 2004-08-01 through 2004-08-15

Raymond Hettinger python at rcn.com
Thu Aug 26 01:51:46 EDT 2004


jjl at pobox.com (John J. Lee) wrote in message news:<87y8k35ahi.fsf at pobox.com>...
> Richie Hindle <richie at entrian.com> writes:
> 
> > [Istvan]
> > > what does:
> > > 
> > >  > It is not to be used in the stdlib ever.
> > > 
> > > mean in this context?
> > 
> > As I understand it, it means that no standard library code should take
> > advantage of the fact that repeated s1 += s2 operations are fast in CPython
> > 2.4.  Instead, it should use the ''.join() idiom.
> [...]
> 
> The stdlib already *does* take advantage of the patch.
> 
> It began taking advantage of it the instant the code was committed,
> because, for example, httplib.HTTPResponse_read_chunked() uses string
> concatenation.

Yes, the standard library does benefit.  The += style is occasionally
used either out of necessity or for simplicity in some places.  For
the most part, whereever it really matters, ''.join() was always used.
 There are some places like the sre modules where the cost of building
up small lists and joining them does not pay for itself.  Also, I
believe (but haven't checked recently) that some SAX code also uses +=
because the consecutive additions take place across calls and the
string is expected to be available after each.  In some places, there
are just a small constant number of string adds that don't warrant
using less straight-forward coding for speed.

If you see a time critical section of library code that can profitably
be converted to ''.join(), then please submit a patch.  If it is not
time sensitive, resist the temptation to optimize it.

That guidance can also apply to non-library code.  The purpose of the
+= optimization was to make it less disasterous when someone forgets
to use ''.join().  Ideally, the experts will leaving their coding
styles unchanged.


Raymond



More information about the Python-list mailing list