From patchwork Thu Oct 20 00:05:59 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Will Hawkins X-Patchwork-Id: 5942 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:4ac7:0:0:0:0:0 with SMTP id y7csp596613wrs; Wed, 19 Oct 2022 17:07:17 -0700 (PDT) X-Google-Smtp-Source: AMsMyM5vQG+Tz+7dYNMNXKfjlGRiuGqC94PcyX/+vCddjcyUW57JOerbLV+lliapCGSjps4O7tIm X-Received: by 2002:a17:906:cc0d:b0:78e:2c06:8e70 with SMTP id ml13-20020a170906cc0d00b0078e2c068e70mr8578487ejb.732.1666224437129; Wed, 19 Oct 2022 17:07:17 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1666224437; cv=none; d=google.com; s=arc-20160816; b=JlhsUQpahh0IZxJgci7X4N2VjpSoiADnSb4xtxkE8QwB4aLsGmzw8vfAVfOPwYnufl sDOcU6bzLu6YkLV8f8aJVFPm+yVVg/+HdR6ujXuwtuDVzpDbUR0boaqgL8UstoHgIoVX WhTOiIsHLZrdVoHj4U2d+B1iSIah9GTkD9iVxoJ4a7mVpd9aociWdaA+OSONktwh5eno +MS3pxm1PJxIr5gopeX538rT3Cco2PE3ynYjMIrUbxNcVk5noVwfK62ANrjDCo9xWkqL M0qhceyPptzM0aGfriXXp96Lx/I/kzDLNaTFYib7zkNc0QSVSeUjic0NmZlWG/c7b4kM q9ew== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:cc:list-subscribe:list-help:list-post:list-archive :list-unsubscribe:list-id:precedence:content-transfer-encoding :mime-version:message-id:date:subject:to:from:dkim-signature :dmarc-filter:delivered-to; bh=q3xUQY4rjXkhYpZvrNWsXeCSk25OQO+RDpus3ieUmWQ=; b=LPeYS511gwYzYECqVgmdsuAFZQcAH74RICxhnZc8yXftYzG6M8YQJU6sLlAtZYEc4m NeKbZQ52/ZTxNiGhP2RPO1PcNbTBTwZATwmRxFlODrdgMJegYmwTLJRemAyTpqsvUyaj CCcAx/PAb4WmAjDK2ADgk5t6lnUEC1Buwi9HTS15KF/4Y+uzWTLhS1eKuxKcYYXtXbCA N+utqOwo89nvhnTbAoUtDsRsjroJ804S4TsU1M0545bBfgmpswm+DHvCfQE1SipUNNVv idvoGneg9b+w+pmQ5Ndia4LLJI+ZdBGov1ZXB9wTAV00L5Bj0fBcHktnPMKGUk8LAuSq bsdQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@obs-cr.20210112.gappssmtp.com header.s=20210112 header.b=eCw30Gni; spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org" Received: from sourceware.org (server2.sourceware.org. [2620:52:3:1:0:246e:9693:128c]) by mx.google.com with ESMTPS id z9-20020a1709063a0900b00769d94690fesi13646681eje.326.2022.10.19.17.07.16 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 19 Oct 2022 17:07:17 -0700 (PDT) Received-SPF: pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) client-ip=2620:52:3:1:0:246e:9693:128c; Authentication-Results: mx.google.com; dkim=pass header.i=@obs-cr.20210112.gappssmtp.com header.s=20210112 header.b=eCw30Gni; spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org" Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id CA0D03851C0F for ; Thu, 20 Oct 2022 00:06:39 +0000 (GMT) X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from mail-qk1-x734.google.com (mail-qk1-x734.google.com [IPv6:2607:f8b0:4864:20::734]) by sourceware.org (Postfix) with ESMTPS id 971143854173 for ; Thu, 20 Oct 2022 00:06:12 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 971143854173 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-qk1-x734.google.com with SMTP id z30so11825623qkz.13 for ; Wed, 19 Oct 2022 17:06:12 -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:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to; bh=q3xUQY4rjXkhYpZvrNWsXeCSk25OQO+RDpus3ieUmWQ=; b=eCw30Gni1ZAiVlIiyR2BINOkHtPmwtN/+dJBXxtExAOTnvcUwyumuuoVpHLTapBZUo xXbU7YyK8Payag4EzqWJjqYsF9rgEMCKMr6EOlqqIbTINYAJwrKmQO+DnQ7ZKnMIWjfp UvrOOv4hK7swKBf5IrYw2p1iEeVdfFuh4IJhT8TASoSQjrywWbJD2xZTqiAJ+jlfZScn c+bvfxpMRz/TV+GsVbZx9NoqkGtRYn2/p9vycu9Bq7/MGwCfYtKSda52Kv9CveRnlS/1 DVc4a/LU7UhTzdUIJVbmOwj1JXNDdTq+aR+EeDCEegc1jbzdzEV4VyMmfcMBv88Vl2u5 PXGQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=q3xUQY4rjXkhYpZvrNWsXeCSk25OQO+RDpus3ieUmWQ=; b=IuRvjRu8hNDUexceu/piwsfAmO2OtU2jEyqYzxjIzU5xyUI4I97WQPlyiCjqDx/s8H /A4nhpA6iOecIvUocPlnUpCAgfPTndONfesWaoFchkyuRGwJjIJxDdV1y8g9UEp1mjMY GB3hUz259OV5rNNX0AtBUc3y6sXzdA+baTKYYE3h+9W4T9ONDMrVXZ+MsKUtT9FDM9dP s6xNAugaBwYVGkingeMM4k1VWkqc7GcRITrYLRYpo3UP7x4690T7T5HOvG7apC3wtymm 1C2btl1QQ8wG4Jddtltq0C6Xm9f61C3dud4nr0ckjWuDtLmF7drYWhmQqkQsZ1r6vrGx f3Jg== X-Gm-Message-State: ACrzQf2vkZramsywIAXQ0LaGSsS5EChY6HZvnyqr/u9vmYWpLpvc0Nvj 2rBP/mJzxE3tZXm+Pqqrl3NTHjRhxpwmsQ== X-Received: by 2002:a05:620a:4153:b0:6ee:cf6a:a82 with SMTP id k19-20020a05620a415300b006eecf6a0a82mr7435331qko.171.1666224371586; Wed, 19 Oct 2022 17:06:11 -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 t15-20020a05622a180f00b00399b73d06f0sm5243263qtc.38.2022.10.19.17.06.09 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 19 Oct 2022 17:06:09 -0700 (PDT) From: Will Hawkins To: gcc-patches@gcc.gnu.org, libstdc++@gcc.gnu.org Subject: [PATCH] libstdc++: Refactor implementation of operator+ for std::string Date: Wed, 19 Oct 2022 20:05:59 -0400 Message-Id: <20221020000559.371886-1-whh8b@obs.cr> X-Mailer: git-send-email 2.37.3 MIME-Version: 1.0 X-Spam-Status: No, score=-13.0 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, GIT_PATCH_0, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_NONE, TXREP 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: gcc-patches@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Will Hawkins Errors-To: gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org Sender: "Gcc-patches" X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1747162955162551731?= X-GMAIL-MSGID: =?utf-8?q?1747162955162551731?= Sorry for the delay. Tested on x86-64 Linux. -->8-- 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. libstdc++-v3/ChangeLog: * include/bits/basic_string.h: Add common function that performs single-allocation string concatenation. (__str_cat) Use __str_cat to perform optimized operator+, where relevant. * include/bits/basic_string.tcc:: Remove single-allocation implementation of operator+. Signed-off-by: Will Hawkins --- libstdc++-v3/include/bits/basic_string.h | 66 ++++++++++++++++------ libstdc++-v3/include/bits/basic_string.tcc | 41 -------------- 2 files changed, 49 insertions(+), 58 deletions(-) diff --git a/libstdc++-v3/include/bits/basic_string.h b/libstdc++-v3/include/bits/basic_string.h index cd244191df4..9c2b57f5a1d 100644 --- a/libstdc++-v3/include/bits/basic_string.h +++ b/libstdc++-v3/include/bits/basic_string.h @@ -3485,6 +3485,24 @@ _GLIBCXX_BEGIN_NAMESPACE_CXX11 _GLIBCXX_END_NAMESPACE_CXX11 #endif + template + _GLIBCXX20_CONSTEXPR + inline _Str + __str_concat(typename _Str::value_type const* __lhs, + typename _Str::size_type __lhs_len, + typename _Str::value_type const* __rhs, + typename _Str::size_type __rhs_len, + typename _Str::allocator_type const& __a) + { + typedef typename _Str::allocator_type allocator_type; + typedef __gnu_cxx::__alloc_traits _Alloc_traits; + _Str __str(_Alloc_traits::_S_select_on_copy(__a)); + __str.reserve(__lhs_len + __rhs_len); + __str.append(__lhs, __lhs_len); + __str.append(__rhs, __rhs_len); + return __str; + } + // operator+ /** * @brief Concatenate two strings. @@ -3494,13 +3512,14 @@ _GLIBCXX_END_NAMESPACE_CXX11 */ template _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR - basic_string<_CharT, _Traits, _Alloc> + inline basic_string<_CharT, _Traits, _Alloc> operator+(const basic_string<_CharT, _Traits, _Alloc>& __lhs, const basic_string<_CharT, _Traits, _Alloc>& __rhs) { - basic_string<_CharT, _Traits, _Alloc> __str(__lhs); - __str.append(__rhs); - return __str; + typedef basic_string<_CharT, _Traits, _Alloc> _Str; + return std::__str_concat<_Str>(__lhs.c_str(), __lhs.size(), + __rhs.c_str(), __rhs.size(), + __lhs.get_allocator()); } /** @@ -3511,9 +3530,16 @@ _GLIBCXX_END_NAMESPACE_CXX11 */ template _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR - basic_string<_CharT,_Traits,_Alloc> + inline basic_string<_CharT,_Traits,_Alloc> operator+(const _CharT* __lhs, - const basic_string<_CharT,_Traits,_Alloc>& __rhs); + const basic_string<_CharT,_Traits,_Alloc>& __rhs) + { + __glibcxx_requires_string(__lhs); + typedef basic_string<_CharT, _Traits, _Alloc> _Str; + return std::__str_concat<_Str>(__lhs, _Traits::length(__lhs), + __rhs.c_str(), __rhs.size(), + __rhs.get_allocator()); + } /** * @brief Concatenate character and string. @@ -3523,8 +3549,14 @@ _GLIBCXX_END_NAMESPACE_CXX11 */ template _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR - basic_string<_CharT,_Traits,_Alloc> - operator+(_CharT __lhs, const basic_string<_CharT,_Traits,_Alloc>& __rhs); + inline basic_string<_CharT,_Traits,_Alloc> + operator+(_CharT __lhs, const basic_string<_CharT,_Traits,_Alloc>& __rhs) + { + typedef basic_string<_CharT, _Traits, _Alloc> _Str; + return std::__str_concat<_Str>(__builtin_addressof(__lhs), 1, + __rhs.c_str(), __rhs.size(), + __rhs.get_allocator()); + } /** * @brief Concatenate string and C string. @@ -3538,11 +3570,12 @@ _GLIBCXX_END_NAMESPACE_CXX11 operator+(const basic_string<_CharT, _Traits, _Alloc>& __lhs, const _CharT* __rhs) { - basic_string<_CharT, _Traits, _Alloc> __str(__lhs); - __str.append(__rhs); - return __str; + __glibcxx_requires_string(__rhs); + typedef basic_string<_CharT, _Traits, _Alloc> _Str; + return std::__str_concat<_Str>(__lhs.c_str(), __lhs.size(), + __rhs, _Traits::length(__rhs), + __lhs.get_allocator()); } - /** * @brief Concatenate string and character. * @param __lhs First string. @@ -3554,11 +3587,10 @@ _GLIBCXX_END_NAMESPACE_CXX11 inline basic_string<_CharT, _Traits, _Alloc> operator+(const basic_string<_CharT, _Traits, _Alloc>& __lhs, _CharT __rhs) { - typedef basic_string<_CharT, _Traits, _Alloc> __string_type; - typedef typename __string_type::size_type __size_type; - __string_type __str(__lhs); - __str.append(__size_type(1), __rhs); - return __str; + typedef basic_string<_CharT, _Traits, _Alloc> _Str; + return std::__str_concat<_Str>(__lhs.c_str(), __lhs.size(), + __builtin_addressof(__rhs), 1, + __lhs.get_allocator()); } #if __cplusplus >= 201103L diff --git a/libstdc++-v3/include/bits/basic_string.tcc b/libstdc++-v3/include/bits/basic_string.tcc index 710c2dfe030..56114f120ba 100644 --- a/libstdc++-v3/include/bits/basic_string.tcc +++ b/libstdc++-v3/include/bits/basic_string.tcc @@ -605,47 +605,6 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION #else # define _GLIBCXX_STRING_CONSTEXPR #endif - - template - _GLIBCXX20_CONSTEXPR - basic_string<_CharT, _Traits, _Alloc> - operator+(const _CharT* __lhs, - const basic_string<_CharT, _Traits, _Alloc>& __rhs) - { - __glibcxx_requires_string(__lhs); - 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(__lhs); - __string_type __str(_Alloc_traits::_S_select_on_copy( - __rhs.get_allocator())); - __str.reserve(__len + __rhs.size()); - __str.append(__lhs, __len); - __str.append(__rhs); - return __str; - } - - template - _GLIBCXX20_CONSTEXPR - basic_string<_CharT, _Traits, _Alloc> - operator+(_CharT __lhs, const basic_string<_CharT, _Traits, _Alloc>& __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; - __string_type __str(_Alloc_traits::_S_select_on_copy( - __rhs.get_allocator())); - const __size_type __len = __rhs.size(); - __str.reserve(__len + 1); - __str.append(__size_type(1), __lhs); - __str.append(__rhs); - return __str; - } - template _GLIBCXX_STRING_CONSTEXPR typename basic_string<_CharT, _Traits, _Alloc>::size_type