
Comments on optimizations around string concatenation - jbredeche
https://gist.github.com/llllllllll/7ad5905275233f1fb3868f4a67793616
======
jfk13
Would be nice if the title indicated this is specifically about Python. [edit:
about CPython, actually]

------
judofyr
I saw someone on Twitter complaining that this behavior can be confusing since
without being aware of it you can create drastically slower code. However, I
must say that I find this approach quite pragmatic and neat. In most cases,
string concatenation is not going to be a performance issue, and then you
never care about these details at all. If you're in a performance-oriented
setting you need to be aware of this, but as long as it's well-understood and
documented (which it unfortunately doesn't seem to be) then it's quite easy to
make it fast.

The big advantage is that there is just a single "string" type. Compare this
with Java or Rust where you have separate types for mutable strings
(StringBuilder in Java; String in Rust) and immutable strings (String in Java;
str in Rust). This is nice both for newcomers (which don't have to learn about
all the details about how to optimize concatenation) and if you're just
writing code which is never going to be in the hot path anyway.

I understand that in some settings (when you really care about performance)
it's good to have an explicit difference between these, but I quite like the
reduced complexity.

~~~
chrisseaton
> In most cases, string concatenation is not going to be a performance issue,
> and then you never care about these details at all.

I would have thought if the performance of anything in a language like Python
mattered, it'd be how quickly you can build up a text response, like a JSON
document or rendered HTML?

In my implementation of Ruby I use immutable ropes for strings - so instead of
mutable arrays of characters I have persistent trees of immutable arrays of
characters. This makes a massive literal 10x impact on real-world code like
template rendering.

~~~
jfkebwjsbx
That depends on what you are building with the language. There are a lot of
use cases outside web apps in Python. The majority of them, I'd argue.

------
fovc
It's crazy that even on the 'fast path' there are so many checks being run
over and over. Provides an interesting alternative to the JavaScript Core
article from a couple of days ago [https://webkit.org/blog/10308/speculation-
in-javascriptcore/](https://webkit.org/blog/10308/speculation-in-
javascriptcore/)

------
scott31
Article is good, but string concatenation is a code smell, and if you ever
need to concatenate strings, you are probably doing something very wrong.

------
jstrong
the phrase "rearranging deck chairs on the Titanic" comes to mind...

