Recently I was looking for ways to speed up a performance critical section of the code and noticed that a signfiicant chunk of time was spent doing string concatenation. Since it was a plain old string concatenation
it would seem like there is not much to optimize here, but after looking at the perf reprot, I’ve realized that it’s one of those cases when clang didn’t meet my expectations and missed an important optimization - reserving enough memory to store the result of concatenation. Because of this, significant chunk of time was spent on internal buffer resizes, similar to what dynamic arrays have to do after exceeding their capacity.
But when we concatenate strings, their sizes are known, so why don’t we use this knowledge and make a reservation
Sure, it’s a bit more verbose, but let’s see if it makes any difference
Looks like it most certainly does, 1.4X difference, in fact!
As an extra bonus, this approach makes it possible to concatenate other string-like sequences, including string_views, so when dealing with performance sensitive sections of the code, don’t forget to make a reservation.