
On 2021-05-01 at 03:05:51 -0000, Valentin Berlier <berlier.v@gmail.com> wrote:
Also I haven't seen anyone acknowledge the potential performance benefits of string comprehensions. The "".join() idiom needs to go through the entire generator machinery to assemble the final string, whereas a decent implementation of string comprehensions would enable some pretty significant optimizations.
In certain special cases, maybe. In the general case, no. How much optimization can you do on something like the following: c"f(c) for c in some_string if g(c)" I'll even let you assume that f and g are pure functions (i.e., no side effects), but you can't assume that f always returns a string of length 1. Even the simpler c"c + c for c in some_string" at some point has to decide whether (a) to collect all the pieces in a temporary container and join them at the end, or (b) to suffer quadratic (or worse) behavior by appending the pieces to an intermediate accumulator as it iterates. Also, how often do any of the use cases come up in inner loops, where performance is important?