PATCH: Speed up direct string concatenation by 20+%!

Larry Hastings larry at
Tue Oct 3 19:18:59 CEST 2006

Istvan Albert wrote:
> I remember a similar patch from some time ago:
> >

That patch addressed the same general problem, but took a completely
different approach.  For the record, this patch (#980695) optimized "x
+= a" by examining the next instruction after the addition.  If it's a
store instruction, and the object being stored to holds a reference to
the left side of the concatenation, it makes the object drop the
reference *before* doing the concatenation.  In the case where there
was only one reference to the string on the left side (that being "x"),
this allows the string concatenation function to use a very fast
shortcut: resize the left side and concatenate the right side directly
to the end.

The patch was accepted in August of 2004, so it might have been folded
in to CPython during the 2.3 era; certainly it was part of CPython 2.4.

And, happily, that optimization is complimentary to mine.  In my patch,
when the left side only has one reference and it's a "string
concatenation object", it will often have space left in its internal
array of strings objects.   In that case I just append the string.
Speeds things up immensely.



More information about the Python-list mailing list