Software Bits Newsletter

Share this post
operator+ vs string append
softwarebits.substack.com

operator+ vs string append

Or don't forget to make a reservation.

Taras Tsugrii
Dec 4, 2021
Comment
Share

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.

CommentComment
ShareShare

Create your profile

0 subscriptions will be displayed on your profile (edit)

Skip for now

Only paid subscribers can comment on this post

Already a paid subscriber? Sign in

Check your email

For your security, we need to re-authenticate you.

Click the link we sent to , or click here to sign in.

TopNewCommunity

No posts

Ready for more?

© 2022 Taras Tsugrii
Privacy ∙ Terms ∙ Collection notice
Publish on Substack Get the app
Substack is the home for great writing