From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by sourceware.org (Postfix) with ESMTPS id 547723858283 for ; Tue, 23 Aug 2022 16:33:39 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 547723858283 Received: from mail-qt1-f198.google.com (mail-qt1-f198.google.com [209.85.160.198]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_128_GCM_SHA256) id us-mta-209-eMA8qGg_PXmWzq1wx5lG0g-1; Tue, 23 Aug 2022 12:33:37 -0400 X-MC-Unique: eMA8qGg_PXmWzq1wx5lG0g-1 Received: by mail-qt1-f198.google.com with SMTP id o22-20020ac85a56000000b0034481129ce6so11011604qta.19 for ; Tue, 23 Aug 2022 09:33:37 -0700 (PDT) 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=K6NWppbRRBmpGU4sVb6MnK/zIC81WH6wGRiqXdzF8xw=; b=19HVvZpLeM9iMzY3EB02fIwmEAohXeDgFSCMM9WSqON/m7WwtoIUIavA+n3Y6WUzZa JhcyttzYHG5pgO06rQUDv/RYZIMUS/QuoJ/sOATUppVGAe306rAosopjnMS9MNWn5sQa N2/WX9WiurDz/ZlIfUrR07eju8l0+IgmMt5ncJyzVoVHfZaExTLz3gwPSpWACYP5uoMM sA+6rBG/dNCl14xSQAHCWHnl0DT9p4e7/+83tbCdVbtAP++wOfwMIdNzPVH+W2RfYWFc m/2UUew/b0Tp8J8nHYBdcfvh3oxXoKrxhSqags4U8XFh9sEW0NOBttKxUR61kgtTAbED VL0w== X-Gm-Message-State: ACgBeo18mBC1TAl0Aa8POoy/e/35Z9DPFnpAvl2Clx3fE0F4IfEwilU8 FWr9AHTa0nmlV7ETmE0WWhzBm7TNFWBJo3iN/70fl8ygSYJ6OA9URfIn8rq8NWU3IqFPsa5Zvve Aqo7f/lUI8W2efy/Ca+zcVpIatnUvr5au1w== X-Received: by 2002:ae9:e312:0:b0:6bb:b0c9:a41d with SMTP id v18-20020ae9e312000000b006bbb0c9a41dmr17021622qkf.436.1661272417500; Tue, 23 Aug 2022 09:33:37 -0700 (PDT) X-Google-Smtp-Source: AA6agR5jAO1aEbPZhAPzYFwQZzVnoJy8z+TFRu4PUW3GJXJmDMdQXKwQq+L+l8KraOFO9UgX++lSF68tvCgzw0LsuIw= X-Received: by 2002:ae9:e312:0:b0:6bb:b0c9:a41d with SMTP id v18-20020ae9e312000000b006bbb0c9a41dmr17021599qkf.436.1661272417195; Tue, 23 Aug 2022 09:33:37 -0700 (PDT) MIME-Version: 1.0 References: <20220822181509.1032874-1-whh8b@obs.cr> <20220822181509.1032874-2-whh8b@obs.cr> In-Reply-To: <20220822181509.1032874-2-whh8b@obs.cr> From: Jonathan Wakely Date: Tue, 23 Aug 2022 17:33:26 +0100 Message-ID: Subject: Re: [PATCH] libstdc++: Optimize operator+(string/char*, string/char*) equally To: whh8b@obs.cr Cc: libstdc++@gcc.gnu.org, gcc-patches@gcc.gnu.org X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Type: text/plain; charset="UTF-8" X-Spam-Status: No, score=-12.8 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, KAM_SHORT, RCVD_IN_DNSWL_LOW, 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: , X-List-Received-Date: Tue, 23 Aug 2022 16:33:40 -0000 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 > 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. > 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 >