Examining Python's string concatenation optimizationIn the comments on the other day's 'minimizing object churn' entry, it was noted that Python string concatenation had been optimized some in the recent 2.4 release of the CPython interpreter. The 2.4 release notes say (from here):
But what is 'more efficiently' and what are the 'certain circumstances'? What does it take to get the optimized behavior? Here's the summary: counting on this optimization is unwise, as it turns out to depend on low-level details of system memory allocation that can vary from system to system and workload to workload. Also, this is only for plain (byte) strings, not for Unicode strings; as of Python 2.4.2, Unicode string concatenation remains un-optimized. The bits of string concatenation that theoretically can be optimized away are allocating a new string object, copying the one of the old string objects to the new one, and freeing the old object. (No matter what, CPython has to copy the data from one of the new string objects into the other.) The new 2.4 optimization attempts to reuse the left side string object and enlarge it in place. For this to be possible, the object needs to have a reference count of one (and not be intern'd). To help create this situation, the CPython interpreter peeks ahead to see if the object has exactly two references and the next bytecode is an assignment to a local variable that currently points to the same object; if so, the variable gets zapped on the spot, dropping the reference count to one. (Nit: this assignment reference dropping also happens for module-level code that refers to global variables.) Even when the left side string object can be reused, we're not
actually saving anything unless it can be enlarged in place. To be
enlarged in place it needs to be at least about 235 bytes long (on
32-bit systems), and for the C library (CPython uses an internal allocator that always grows things by
copying for all allocations of 256 bytes or less; string objects have
about 21 bytes of overhead on a normal 32-bit platform. For more
details, see the CPython source. If you're interested in the gory
details, start with the I suspect that the optimization is most likely to trigger when you are repeatedly growing a fairly large string by a small bit, as C libraries often use allocation strategies that leave reasonable amounts of space free after large objects. If your program mostly deals with smaller strings, you may not benefit much (if at all) from this optimization. One consequence of all this is that none of the following will be optimized, because the left side reference (implicit in the case of '+=') cannot be reduced to a reference count of one, because the assignment is of the wrong type: def foo(self, alst, a): global bar; bar += a self.foo += a alst[0] += a This isn't too surprising, since both However, ' Sidebar: what about optimizing string prepending?By string prepending, I mean ' You can't significantly optimize string prepending in CPython, because
you always have to move (I suspect that string prepending is also uncommon in Python code.) (One comment.)
|
These are my WanderingThoughts GettingAround This is part of CSpace, and is written by ChrisSiebenmann. * * * Atom feeds are available; see the bottom of most pages. Categories: links, linux, programming, python, snark, solaris, spam, sysadmin, tech, unix, web |