From patchwork Mon Aug 22 18:15:09 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Will Hawkins X-Patchwork-Id: 656 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:adf:ecc5:0:0:0:0:0 with SMTP id s5csp615312wro; Mon, 22 Aug 2022 11:17:08 -0700 (PDT) X-Google-Smtp-Source: AA6agR6YXuNFPqwxPCcDDBCth+RIdE7wnenRwbC1PjBLVAhumlGAShR5/zbUH0PqdaJVbmvjrmVF X-Received: by 2002:a17:907:6e0c:b0:73d:8b64:5c3 with SMTP id sd12-20020a1709076e0c00b0073d8b6405c3mr2375685ejc.297.1661192228582; Mon, 22 Aug 2022 11:17:08 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1661192228; cv=none; d=google.com; s=arc-20160816; b=Q3cO+azSZRH+DgBELPM31omVZj4HZziL4M8jIXnq79EQ0vt9qTsCJcQVsDsPCJocA2 6FTxs+Q58gJjPqlVbzm15msDWCijKLZ01/Gx00/ErY6CQHkCodzYX57XusfGrtURL2U5 +eJhxXSQU1eip8gqNWNJ4kQM/d5dGPzhBu66bk4yFRHm8+7aQVS+UHFgWUlejkjD8i8U h3d3hkNXyykDA1uoXGYi10luQBQp67U6H6jzENZbUjaJPM+hdPyAy+ZWrGW0574LjypE 0QtL3Pvsw2JFemxKwhlz94p6KEgEOBufan4PAKRLmZjgkSmwVpBYkDxN0av0R0p0F1Pp IVsw== 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:references:in-reply-to:message-id:date:subject:to:from :dkim-signature:dmarc-filter:delivered-to; bh=EEIRdxltZb9NXhNvpzyfGB5FoY2truarP1iWA+J1Qws=; b=qncYLUWk9Wg4uJ2IPKhYh/pxbs4Jtb/C+J8Z1r8ff13U4FPlPbEJeOb1zR0efxggLi 0kF0S375SLShtKCyZfAKw27bR2cuP2MsZy5jEnyxDLwoZjACHcgqZpu4RQsIlvVrmY5P UQQHv5TeGefJqfbrv2OOb+tPXDT80VkNveO70WXbZNjlpfS+pLDDULX+3+xUh6hNwbdc ePPNvDsPllCCA9rkV3nVvaV1JwScctq9s3Hizqc9nTjiWL72Y/QUuWFXXD1wqRZ7DN8j 6vesnv/MkjTl6LB2DXetN2xkLfhvBnueST7q4ILsHn1Z7ZvGZjobx1Hr3h2ivQkVr0yY AAvg== ARC-Authentication-Results: i=1; mx.google.com; dkim=fail header.i=@obs-cr.20210112.gappssmtp.com header.s=20210112 header.b="sYf/3Fmo"; spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org" Received: from sourceware.org (server2.sourceware.org. [8.43.85.97]) by mx.google.com with ESMTPS id d2-20020aa7d682000000b0044628c209c3si84360edr.632.2022.08.22.11.17.08 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 22 Aug 2022 11:17:08 -0700 (PDT) Received-SPF: pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 as permitted sender) client-ip=8.43.85.97; Authentication-Results: mx.google.com; dkim=fail header.i=@obs-cr.20210112.gappssmtp.com header.s=20210112 header.b="sYf/3Fmo"; spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 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 12F963853559 for ; Mon, 22 Aug 2022 18:15:52 +0000 (GMT) X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from mail-qk1-x730.google.com (mail-qk1-x730.google.com [IPv6:2607:f8b0:4864:20::730]) by sourceware.org (Postfix) with ESMTPS id DF46C3857BBC for ; Mon, 22 Aug 2022 18:15:24 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org DF46C3857BBC 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-x730.google.com with SMTP id j6so8497982qkl.10 for ; Mon, 22 Aug 2022 11:15:24 -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=xaX8VLYYhkZ00HLqtFI5Dx5ETLCPLZFI5Y8ysX5Eid/PH/Z3LFMlqd/yeE3WT0EULx fq3M+3dFSkxAIzoJl1sXRPgcrrtQpQStR0mbIsGsd7hiU4p6wS2Jpu+S7CCUDWWw6Y8t Xp7031ZCeoJ3YgGEbkrjY3zMFE+Ihb887CIqfwKu5Zp2J0aOWb9JnSIVGwuOLeEIvS+F ABxyVVAMqPPFQKcHfbjEbtsJpw32ow/BqYc/5KdcVOH13spZ6V278yYR3JnNmzVsDGqX uY1LY8FZXQjRMANnsJZVlYOS9iI7ATmzqWAKHxZlRgebjTmwvWH0+ITbd8kZN6ucMq99 SmwQ== X-Gm-Message-State: ACgBeo3HAShy7/ajnhccKb3DGF828D5in/dQa3bwpwtxvwSwJgDCOPMw o12o32MRPWyYYldVf/uFesIjQg== 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 X-Spam-Status: No, score=-10.1 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=ham 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?1741886282648641643?= X-GMAIL-MSGID: =?utf-8?q?1741886302491551144?= 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