From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 2181) id 8E9ED385C32C; Wed, 24 Aug 2022 14:23:23 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 8E9ED385C32C DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1661351003; bh=ATO/oAy9/Tw9VLpToekhmosIwoCcmhXw3S1zAGtd4qk=; h=From:To:Subject:Date:From; b=k48NscvHCibJIRmzR+xDIsd90dSbBd/n0EHHVFf0CZ9poFvKuLyG1fiM5AeGd3X03 CmY8u8z/ULvBeWRI0TZ0n1gcOwZC3EmOfh4kpiEnqCrtsojTSNEJicfqPSSIoCB4kj mAbpXsmAf35OAwiAJTFYN4z93R6xI5JwSOrlNwx4= MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Content-Type: text/plain; charset="utf-8" From: Jonathan Wakely To: gcc-cvs@gcc.gnu.org, libstdc++-cvs@gcc.gnu.org Subject: [gcc r13-2175] libstdc++: Optimize operator+(string/char*, char*/string) equally X-Act-Checkin: gcc X-Git-Author: Will Hawkins X-Git-Refname: refs/heads/master X-Git-Oldrev: 02de9d26b1820e4af35ebdd271c3a788e3c99035 X-Git-Newrev: 0b7c9254998b3fb2c39f6b86b5b196f415530205 Message-Id: <20220824142323.8E9ED385C32C@sourceware.org> Date: Wed, 24 Aug 2022 14:23:23 +0000 (GMT) List-Id: https://gcc.gnu.org/g:0b7c9254998b3fb2c39f6b86b5b196f415530205 commit r13-2175-g0b7c9254998b3fb2c39f6b86b5b196f415530205 Author: Will Hawkins Date: Wed Aug 24 02:16:48 2022 -0400 libstdc++: Optimize operator+(string/char*, char*/string) equally Until now operator+(char*, const string&) and operator+(const string&, char*) had different performance characteristics. The former required a single memory allocation and the latter required two. This patch makes the performance equal. libstdc++-v3/ChangeLog: * include/bits/basic_string.h (operator+(const string&, const char*)): Remove naive implementation. * include/bits/basic_string.tcc (operator+(const string&, const char*)): Add single-allocation implementation. Signed-off-by: Will Hawkins Diff: --- libstdc++-v3/include/bits/basic_string.h | 9 ++------- libstdc++-v3/include/bits/basic_string.tcc | 21 +++++++++++++++++++++ 2 files changed, 23 insertions(+), 7 deletions(-) diff --git a/libstdc++-v3/include/bits/basic_string.h b/libstdc++-v3/include/bits/basic_string.h index b04fba95678..fa6738925bb 100644 --- a/libstdc++-v3/include/bits/basic_string.h +++ b/libstdc++-v3/include/bits/basic_string.h @@ -3521,14 +3521,9 @@ _GLIBCXX_END_NAMESPACE_CXX11 */ template _GLIBCXX20_CONSTEXPR - inline basic_string<_CharT, _Traits, _Alloc> + basic_string<_CharT, _Traits, _Alloc> operator+(const basic_string<_CharT, _Traits, _Alloc>& __lhs, - const _CharT* __rhs) - { - basic_string<_CharT, _Traits, _Alloc> __str(__lhs); - __str.append(__rhs); - return __str; - } + const _CharT* __rhs); /** * @brief Concatenate string and character. diff --git a/libstdc++-v3/include/bits/basic_string.tcc b/libstdc++-v3/include/bits/basic_string.tcc index 4563c61429a..95ba8e503e9 100644 --- a/libstdc++-v3/include/bits/basic_string.tcc +++ b/libstdc++-v3/include/bits/basic_string.tcc @@ -640,6 +640,27 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION return __str; } + template + _GLIBCXX20_CONSTEXPR + basic_string<_CharT, _Traits, _Alloc> + operator+(const basic_string<_CharT, _Traits, _Alloc>& __lhs, + const _CharT* __rhs) + { + __glibcxx_requires_string(__rhs); + typedef basic_string<_CharT, _Traits, _Alloc> __string_type; + typedef typename __string_type::size_type __size_type; + typedef typename __gnu_cxx::__alloc_traits<_Alloc>::template + rebind<_CharT>::other _Char_alloc_type; + typedef __gnu_cxx::__alloc_traits<_Char_alloc_type> _Alloc_traits; + const __size_type __len = _Traits::length(__rhs); + __string_type __str(_Alloc_traits::_S_select_on_copy( + __lhs.get_allocator())); + __str.reserve(__len + __lhs.size()); + __str.append(__lhs); + __str.append(__rhs, __len); + return __str; + } + template _GLIBCXX_STRING_CONSTEXPR typename basic_string<_CharT, _Traits, _Alloc>::size_type