https://gcc.gnu.org/bugzilla/show_bug.cgi?id=108645

--- Comment #4 from Jonathan Wakely <redi at gcc dot gnu.org> ---
(In reply to Jonathan Wakely from comment #3)
>         const auto __size = __lhs.size() + __rhs.size();
>         if (__size > __lhs.capacity() && __size <= __rhs.capacity())
>           return std::move(__rhs.insert(0, __lhs));

It would be possible to change that logic to:

          const auto __size = __lhs.size() + __rhs.size();
          if (__size <= __rhs.capacity() && __rhs.capacity() >
__lhs.capacity())
            return std::move(__rhs.insert(0, __lhs));

That way if both strings have sufficient capacity, we would return the larger
of the two capacities. In general, the returned string is more likely to be the
one that gets reused, and this way it would be more likely to have additional
spare capacity.

In some usage patterns this would be a pessimization, because it would tend to
coalesce all the larger capacities into one string, leaving smaller, less
useful strings behind. Also, inserting into the beginning of the RHS takes more
work than simply appending to the end of the LHS. So I don't think it's clear
that this would be a definite improvement.

It wouldn't change anything in your example anyway, because there is no
dynamically allocated memory anywhere except the accumulator value, which is
always the LHS.

Reply via email to