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 43F863858C50 for ; Tue, 23 Aug 2022 16:33:39 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 43F863858C50 Received: from mail-qk1-f197.google.com (mail-qk1-f197.google.com [209.85.222.197]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_128_GCM_SHA256) id us-mta-657-RQTUkz34Or-e_UktW5jt6Q-1; Tue, 23 Aug 2022 12:33:37 -0400 X-MC-Unique: RQTUkz34Or-e_UktW5jt6Q-1 Received: by mail-qk1-f197.google.com with SMTP id bp11-20020a05620a458b00b006bbeffab91dso8153413qkb.11 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=Tds4l3385lTqYpWzdXMjZ6M5l8uQy9wLq9rML1/Gbh6tr8Smf6F0Zah6pYJmaSrwVR 81Yrv31EEwhmEHa5rpkCSmheotmZbGITsig/cOYDqiEYZq5dibF1bXahclEnsfRFChVL THUaSxi6XVaYT5vzKWc6wzWIcEKOp0qzD0o9gSdug9gJn9/Jho7oUQnAC65zm8NDljO9 EaK6zEdmkCz1bImE19oedmyHsXUnAHUkpo1ik/bKW9e4VlRZPTG2CGOWXfAs2bnZErd6 6xyIPlf04Vqn6rgwK28CmfGMQ+pdAgt+vpYkDBfRRH8bNQXTJjHQdy9LbtJJ1Xz/bf/j sFRQ== X-Gm-Message-State: ACgBeo0aZSUaVZN8fvQCAPxfZEQyiiesbFxrt8b6O7o/7b4GlEtAz7w2 9opAGj+BjspOcBlT3S/5JQOwWwGI3CTSA3Hb5pBiu65NLqrgZt3DkalsS7nogrjv89XDZAlyRFZ Kx91A4NdeV1SWa/2HrPiXRXGrplRXJqM= X-Received: by 2002:ae9:e312:0:b0:6bb:b0c9:a41d with SMTP id v18-20020ae9e312000000b006bbb0c9a41dmr17021621qkf.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=-13.0 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: 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: 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 >