From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-lj1-x22f.google.com (mail-lj1-x22f.google.com [IPv6:2a00:1450:4864:20::22f]) by sourceware.org (Postfix) with ESMTPS id 74B5D3857034 for ; Wed, 24 Aug 2022 06:18:45 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 74B5D3857034 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-lj1-x22f.google.com with SMTP id z20so15475645ljq.3 for ; Tue, 23 Aug 2022 23:18:45 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=obs-cr.20210112.gappssmtp.com; s=20210112; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:from:to:cc; bh=igWWoVNyrq/f/FcdhfoXz5KNkrRvxDnGtoEMZDYveRo=; b=BMBWTp5pFUkZJI4Y+NC+fJc8DBElh1YA0XvYXdL2G5+fO3SFD7rzp10kTWu/vTxb2o ryq5UNsNar8AQvXRZ+AR1qb8RLbMaP8UU3uDxh/HkUNpd/YuPWsl2SCIODxg9a5Ws5fD GCY/PF0wy6JSqbK/7PwCMJQQhYWftwhumiAKYLkQRNdErlxfkrNcSGIEWkftjCYJ8TAf /mhqIEnYusAC3gndywcWAx0OHXDS8VkWgmLeMjipa8OpxbDwULx+tp+EOe8+/aFH4FAT 1Bv/+4sUUXuhTo2un/fNAOMbMkbheN1oGJ8FzuILBG9AwewwUW5Et9Cw9ns9p91ttIzA dH+w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:x-gm-message-state:from:to:cc; bh=igWWoVNyrq/f/FcdhfoXz5KNkrRvxDnGtoEMZDYveRo=; b=7cKXP8mhdXJ8UKiJ76TvDyJataA1S053Z09oBdPC+1Q4K742u4BmvhlaX4IvasxOpA vBwzDAJxHhIuHlZyo9FLZxGA8fwzaVUcWSV6aOOmbPoQB/gkEjBiMQbd+wCftKLblcUp hNTmpL/xVt3g8m7pR6H8ejdsifnovxDGY3G5INUnddT2qFVIvvIL6FhDI0UIrt9yrKmN gzRDpM4lpIjWHS237xemcPPhLfirmTZMAypD/NcCooDg+jXDabjwPRviLk4aAa6M5CAY aVwPLrf1cM38buIKh9UgmbYDwErlPiOx09LWjc22LNKElN8vLJCHrudqAdpSLBb9mS2n wUJw== X-Gm-Message-State: ACgBeo2b2YqClWi9NEO28K0MQh4SRxUHs8+BLZNR6Fs3jjd1J9s9TpDY XZ5nK7M8vj746CsCIxCoHgh45IspBEYCEnJj75EFZp8Dzn40fg== X-Google-Smtp-Source: AA6agR6eaXDYk/x6Vdv7uzykizwWJn8ViVUIik0J2Yd7QLhL2v9QXw11a9TyinlqoSPa2erZsUryaJZLog9Khymg3tw= X-Received: by 2002:a2e:b8c6:0:b0:25f:dedf:efbc with SMTP id s6-20020a2eb8c6000000b0025fdedfefbcmr7779733ljp.37.1661321923871; Tue, 23 Aug 2022 23:18:43 -0700 (PDT) MIME-Version: 1.0 References: <20220822181509.1032874-1-whh8b@obs.cr> <20220822181509.1032874-2-whh8b@obs.cr> In-Reply-To: From: Will Hawkins Date: Wed, 24 Aug 2022 02:18:32 -0400 Message-ID: Subject: Re: [PATCH] libstdc++: Optimize operator+(string/char*, string/char*) equally To: Jonathan Wakely Cc: libstdc++@gcc.gnu.org, gcc-patches@gcc.gnu.org Content-Type: text/plain; charset="UTF-8" X-Spam-Status: No, score=-6.5 required=5.0 tests=BAYES_00,DKIM_SIGNED,DKIM_VALID,GIT_PATCH_0,KAM_SHORT,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 List-Id: On Tue, Aug 23, 2022 at 12:33 PM Jonathan Wakely wrote: > > On Mon, 22 Aug 2022 at 19:15, Will Hawkins wrote: > > > > 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. > > If you don't have a GCC copyright assignment on file with the FSF then > please follow the procedure described at https://gcc.gnu.org/dco.html Thank you. > > > > 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> > > Please remove the 'inline' specifier here, since you're moving the > definition into the non-inline .tcc file. > > There's a separate discussion to be had about whether these operator+ > overloads *should* be inline. But for the purposes of this change, we > want these two operator+ overloads to be consistent, and so they > should both be non-inline. Thank you for the feedback. I sent out a v2 of the patch. Again, I hope that I followed the proper procedure by having my mailer put the patch in reply to my previous message. Thank you again! Will > > > 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 > > >