libstdc++: Refactor implementation of operator+ for std::string
authorWill Hawkins <whh8b@obs.cr>
Thu, 20 Oct 2022 00:05:59 +0000 (20:05 -0400)
committerJonathan Wakely <jwakely@redhat.com>
Tue, 8 Nov 2022 17:35:14 +0000 (17:35 +0000)
commitc93baa93df2d454f329a38686affcfe1a1339766
tree7286e8a2461912156a1e524152ae9de8ea8547bf
parent564b11184650a494d97d9e89b984664ae142a24a
libstdc++: Refactor implementation of operator+ for std::string

Until now operator+(char*, string) and operator+(string, char*) had
different performance characteristics. The former required a single
memory allocation and the latter required two. This patch makes the
performance equal.

After consultation with Jonathan, it seemed like a good idea to create a
single function that performed one-allocation string concatenation that
could be used by various different version of operator+. This patch adds
such a function and calls it from the relevant implementations.

Co-authored-by: Jonathan Wakely <jwakely@redhat.com>
libstdc++-v3/ChangeLog:

* include/bits/basic_string.h (__str_cat): Add common function
that performs single-allocation string concatenation.
(operator+): Use __str_cat.
* include/bits/basic_string.tcc (operator+): Move to .h and
define inline using __str_cat.

Signed-off-by: Will Hawkins <whh8b@obs.cr>
libstdc++-v3/include/bits/basic_string.h
libstdc++-v3/include/bits/basic_string.tcc