From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-qk1-x729.google.com (mail-qk1-x729.google.com [IPv6:2607:f8b0:4864:20::729]) by sourceware.org (Postfix) with ESMTPS id 2A3233857C41 for ; Mon, 22 Aug 2022 18:15:25 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 2A3233857C41 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-x729.google.com with SMTP id a15so8513769qko.4 for ; Mon, 22 Aug 2022 11:15:25 -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=EEIRdxltZb9NXhNvpzyfGB5FoY2truarP1iWA+J1Qws=; b=sYf/3FmoDVa5KhSE608BvYRrwwS8OJg23eT3LQCeD0DD6LZG66bFGi/zVV6X4wIaf5 7zp+/lOtkFWB9ZrEwzbm97Q9Lv4utKbtZe9EaYyCY9ZpZGId4YPi5q3saYka7wqwOt/Q dhPN1qjY2ilxSaCRGjXBA8XJvc0pHnWTRJ4BuXMHUNpXGa8f+ACGEcM2UfpC7vz5Vpe5 OEC0dxE+R/xI5iRDoxgNzSIuljNEvOwfITfuJdhvVT0m27guhqjgxeT3pdh3Gw1vRtSv spjpAZDEEEo4fK6N7BCPEbdlWR9++BhCcg9gJyECrJjeChMYbO2LqRb5ERu0wXAV+Br1 X3Lw== 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=EEIRdxltZb9NXhNvpzyfGB5FoY2truarP1iWA+J1Qws=; b=VCDFJnq9dQ2q7X0TIg+L91rashq863Sj7GQqholYEFxDAATSsU/ylUp4aJL5ZnlBsp lSaFX0bnkooPdVPVfm9ZshtCrbScOoLDS8iH+TMSaj0Fi5Gg5Qyz6XgRlrejHfdiS5vY VAl9QibaT1UOPb0X5w5HA9pCDFFC2w5/Sf1PRGADBpStgWJsb3QDjd66pgv1Lj0lpg9q 8KldLRtpnIqwHFQEE+lO0ydGHR8nfYiBN7XWcPbALbPjMSVJqK07UwrGJkimo3U3lKBA m7OnjuKGEpYvN8qpFPUCUKGHxIhcyHUz0AqWLUpj+G6ADua9Hahamh3zbRiO1l51A0RZ zp7A== X-Gm-Message-State: ACgBeo3j1AsaDgKuTOSlQnPiyE+3u0DdA5TwFXrXwB7MQ+wDcaUDtiph Za4XreWJOyoJeqbETxta3ZZl1aClNuTfEm7s X-Google-Smtp-Source: AA6agR5L5XoNfrcNjts+ibUqT7zHzL5G+QJdrpJBWt/MKSu2tlYSX+SsMaKx3zBihQOZYiRIvV3eAA== X-Received: by 2002:ae9:ebd6:0:b0:6ba:eb9a:427e with SMTP id b205-20020ae9ebd6000000b006baeb9a427emr13655411qkg.705.1661192124179; Mon, 22 Aug 2022 11:15:24 -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 do14-20020a05620a2b0e00b006bb0e5ca4bbsm9834049qkb.85.2022.08.22.11.15.23 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 22 Aug 2022 11:15:23 -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: Mon, 22 Aug 2022 14:15:09 -0400 Message-Id: <20220822181509.1032874-2-whh8b@obs.cr> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20220822181509.1032874-1-whh8b@obs.cr> References: <20220822181509.1032874-1-whh8b@obs.cr> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-10.6 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: Mon, 22 Aug 2022 18:15:26 -0000 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. --- libstdc++-v3/include/bits/basic_string.h | 7 +------ libstdc++-v3/include/bits/basic_string.tcc | 21 +++++++++++++++++++++ 2 files changed, 22 insertions(+), 6 deletions(-) diff --git a/libstdc++-v3/include/bits/basic_string.h b/libstdc++-v3/include/bits/basic_string.h index b04fba95678..bc048fc0689 100644 --- a/libstdc++-v3/include/bits/basic_string.h +++ b/libstdc++-v3/include/bits/basic_string.h @@ -3523,12 +3523,7 @@ _GLIBCXX_END_NAMESPACE_CXX11 _GLIBCXX20_CONSTEXPR inline 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