2 ms·
I think Python's string type has an optimization that concatenation on a string where the LHS only has a single reference to it will reuse the existing string w
by munificent 10y ago
I think Python's string type has an optimization that concatenation on a string where the LHS only has a single reference to it will reuse the existing string when possible. You still have to grow the buffer periodically, of course, but it's not creating as much garbage as it looks like.
- masklinn 10y ago> I think Python's string type has an optimization that concatenation on a string where the LHS only has a single reference to it will reuse the existing string when possible. It's specifically a CPython optimisation, it may (and usually will) not hold up on alternate implementations like pypy or jyton.
- munificent 10y agoSure, but the function in question lives in CPython's own implementation of its core libraries, so it seems reasonable for it to rely on that implementation. If PyPy or Jython want to reuse that core library code, it's on them to make whatever changes are needed to make it fast on those implementations.