From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-qt1-x82f.google.com (mail-qt1-x82f.google.com [IPv6:2607:f8b0:4864:20::82f]) by sourceware.org (Postfix) with ESMTPS id BE578385703E for ; Wed, 24 Aug 2022 06:16:59 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org BE578385703E Authentication-Results: sourceware.org; dmarc=none (p=none dis=none) header.from=obs.cr Authentication-Results: sourceware.org; spf=none smtp.mailfrom=obs.cr Received: by mail-qt1-x82f.google.com with SMTP id cr9so12012814qtb.13 for ; Tue, 23 Aug 2022 23:16:59 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=obs-cr.20210112.gappssmtp.com; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc; bh=UxOxuiikWxHrqz6k6C8dSil//9BkU3bPox9qIEOpTPA=; b=hA/F8sosHy3Gz4HGnn+yvBbFAiIBR/1OQJN+KNfebbVAogc8BlDJTgQagi0ZnmrWqu Sf+xNvA/iPW+HvuRRUZRQ7r7N0Gh6psmI0JGm26hQsH1YYtq01f9I0Iqnc3TRqp36vCS 6ZWFY1lqZe5DokpJfOfMYd3vvW597RoTCk27FOvJ1cLHMds7ZXTRVORkP5mO79/bjcan EpfN8v45r93+onZYCkQz6o6KIB5tvQ+F5VzlfH6z1TYz1hVm5zJPndX/Ozpy2L0qypN+ rSdhzXUnocp4laoBhrCfduRKieG4lgYlxjzhtCcQZTkQG58Uy/i+1rNgTeqIfWtG0YQr sBJw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc; bh=UxOxuiikWxHrqz6k6C8dSil//9BkU3bPox9qIEOpTPA=; b=0bLMyF9Gbm+Uu+/inD5xcxR8xWef3o6QNIhhTFn9sFyS2av6v8dN1NpSwRjSwR/o3D bDYPkyuogTUlY31HaLbXl97IRxNEIsZfi+SEXv+R9OdMtYYbmqSAQ/QmbdECsHO/z+mq uGOHifiQLIs8cs4jq0gA5XVySlBtOoXb4D497bEF41GTlQrTFWiJFSGL9X3yNzxE6N3L xqpwyc980hR3mHtyr1UCcZZruALMgCtp1CbuGON1uSQZ1Z2fvHT4ejcrbBOSFpNhM2Ue enPm04gL/7y9cuqNc2GPJ3OSYvlqWIYCegWvp52mwrrht8c28NgB7tgUqjmMmhhDMgEy 834w== X-Gm-Message-State: ACgBeo3vX/klWqiikFR4d23vX/Kqv80ljG7+QAsmeazA12Oa4U5RkQB6 lxZxbmkSHf5s6GeziVck+P8SoP99GIbr6C0h X-Google-Smtp-Source: AA6agR51L/O2I2v/2eYxcL/JduRPj9PtBbXrkPlcD8hRWGr+PMjGuBMmJLFyu9I0qasrHpOulFyfpg== X-Received: by 2002:ac8:5f07:0:b0:344:a21c:1e02 with SMTP id x7-20020ac85f07000000b00344a21c1e02mr17938494qta.140.1661321818861; Tue, 23 Aug 2022 23:16:58 -0700 (PDT) Received: from localhost.localdomain (ip-192-24-137-251.dynamic.fuse.net. [192.24.137.251]) by smtp.gmail.com with ESMTPSA id y16-20020ac85f50000000b0031ecddf2278sm13306657qta.37.2022.08.23.23.16.57 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 23 Aug 2022 23:16:58 -0700 (PDT) From: whh8b@obs.cr To: libstdc++@gcc.gnu.org, gcc-patches@gcc.gnu.org Cc: Will Hawkins Subject: [PATCH] libstdc++: Optimize operator+(string/char*,string/char*) equally Date: Wed, 24 Aug 2022 02:16:48 -0400 Message-Id: <20220824061648.1119635-2-whh8b@obs.cr> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20220824061648.1119635-1-whh8b@obs.cr> References: <20220822181509.1032874-2-whh8b@obs.cr> <20220824061648.1119635-1-whh8b@obs.cr> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-12.8 required=5.0 tests=BAYES_00,DKIM_SIGNED,DKIM_VALID,GIT_PATCH_0,RCVD_IN_DNSWL_NONE,SPF_HELO_NONE,SPF_NONE,TXREP,T_SCC_BODY_TEXT_LINE autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org List-Id: From: Will Hawkins 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. libstdc++-v3/ChangeLog: * libstdc++-v3/include/bits/basic_string.h (operator+(string, char*)): Remove naive implementation. * libstdc++-v3/include/bits/basic_string.tcc (operator+(string, char*)): Add single-allocation implementation. Signed-off-by: Will Hawkins --- 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 -- 2.34.1 From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-qt1-x82f.google.com (mail-qt1-x82f.google.com [IPv6:2607:f8b0:4864:20::82f]) by sourceware.org (Postfix) with ESMTPS id BE578385703E for ; Wed, 24 Aug 2022 06:16:59 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org BE578385703E Authentication-Results: sourceware.org; dmarc=none (p=none dis=none) header.from=obs.cr Authentication-Results: sourceware.org; spf=none smtp.mailfrom=obs.cr Received: by mail-qt1-x82f.google.com with SMTP id cr9so12012814qtb.13 for ; Tue, 23 Aug 2022 23:16:59 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=obs-cr.20210112.gappssmtp.com; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc; bh=UxOxuiikWxHrqz6k6C8dSil//9BkU3bPox9qIEOpTPA=; b=hA/F8sosHy3Gz4HGnn+yvBbFAiIBR/1OQJN+KNfebbVAogc8BlDJTgQagi0ZnmrWqu Sf+xNvA/iPW+HvuRRUZRQ7r7N0Gh6psmI0JGm26hQsH1YYtq01f9I0Iqnc3TRqp36vCS 6ZWFY1lqZe5DokpJfOfMYd3vvW597RoTCk27FOvJ1cLHMds7ZXTRVORkP5mO79/bjcan EpfN8v45r93+onZYCkQz6o6KIB5tvQ+F5VzlfH6z1TYz1hVm5zJPndX/Ozpy2L0qypN+ rSdhzXUnocp4laoBhrCfduRKieG4lgYlxjzhtCcQZTkQG58Uy/i+1rNgTeqIfWtG0YQr sBJw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc; bh=UxOxuiikWxHrqz6k6C8dSil//9BkU3bPox9qIEOpTPA=; b=0bLMyF9Gbm+Uu+/inD5xcxR8xWef3o6QNIhhTFn9sFyS2av6v8dN1NpSwRjSwR/o3D bDYPkyuogTUlY31HaLbXl97IRxNEIsZfi+SEXv+R9OdMtYYbmqSAQ/QmbdECsHO/z+mq uGOHifiQLIs8cs4jq0gA5XVySlBtOoXb4D497bEF41GTlQrTFWiJFSGL9X3yNzxE6N3L xqpwyc980hR3mHtyr1UCcZZruALMgCtp1CbuGON1uSQZ1Z2fvHT4ejcrbBOSFpNhM2Ue enPm04gL/7y9cuqNc2GPJ3OSYvlqWIYCegWvp52mwrrht8c28NgB7tgUqjmMmhhDMgEy 834w== X-Gm-Message-State: ACgBeo3vX/klWqiikFR4d23vX/Kqv80ljG7+QAsmeazA12Oa4U5RkQB6 lxZxbmkSHf5s6GeziVck+P8SoP99GIbr6C0h X-Google-Smtp-Source: AA6agR51L/O2I2v/2eYxcL/JduRPj9PtBbXrkPlcD8hRWGr+PMjGuBMmJLFyu9I0qasrHpOulFyfpg== X-Received: by 2002:ac8:5f07:0:b0:344:a21c:1e02 with SMTP id x7-20020ac85f07000000b00344a21c1e02mr17938494qta.140.1661321818861; Tue, 23 Aug 2022 23:16:58 -0700 (PDT) Received: from localhost.localdomain (ip-192-24-137-251.dynamic.fuse.net. [192.24.137.251]) by smtp.gmail.com with ESMTPSA id y16-20020ac85f50000000b0031ecddf2278sm13306657qta.37.2022.08.23.23.16.57 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 23 Aug 2022 23:16:58 -0700 (PDT) From: whh8b@obs.cr To: libstdc++@gcc.gnu.org, gcc-patches@gcc.gnu.org Subject: [PATCH] libstdc++: Optimize operator+(string/char*, string/char*) equally Date: Wed, 24 Aug 2022 02:16:48 -0400 Message-ID: <20220824061648.1119635-2-whh8b@obs.cr> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20220824061648.1119635-1-whh8b@obs.cr> References: <20220822181509.1032874-2-whh8b@obs.cr> <20220824061648.1119635-1-whh8b@obs.cr> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-12.8 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, GIT_PATCH_0, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_NONE, TXREP, T_SCC_BODY_TEXT_LINE autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org X-BeenThere: libstdc++@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Libstdc++ mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Wed, 24 Aug 2022 06:17:00 -0000 Message-ID: <20220824061648.SARL5lJ4-2Ln9hfg4wm9pIbKNzyUaSi4xvTATJQcgqM@z> From: Will Hawkins 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. libstdc++-v3/ChangeLog: * libstdc++-v3/include/bits/basic_string.h (operator+(string, char*)): Remove naive implementation. * libstdc++-v3/include/bits/basic_string.tcc (operator+(string, char*)): Add single-allocation implementation. Signed-off-by: Will Hawkins --- 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 -- 2.34.1