From: Will Hawkins <hawkinsw@obs.cr>
To: Jonathan Wakely <jwakely@redhat.com>
Cc: libstdc++@gcc.gnu.org, gcc-patches@gcc.gnu.org
Subject: Re: [PATCH] libstdc++: Optimize operator+(string/char*, string/char*) equally
Date: Wed, 24 Aug 2022 02:18:32 -0400 [thread overview]
Message-ID: <CADx9qWh5AbBJ3mEe9XuSXqkp_rZvA=OrE1p=sOkNcaXu72aedA@mail.gmail.com> (raw)
In-Reply-To: <CACb0b4mH-OU8GkrWBjusaw3Q8vZR-pUVY69yXtMRMTc7_w5nuQ@mail.gmail.com>
On Tue, Aug 23, 2022 at 12:33 PM Jonathan Wakely <jwakely@redhat.com> 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<typename _CharT, typename _Traits, typename _Alloc>
> > + _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<typename _CharT, typename _Traits, typename _Alloc>
> > _GLIBCXX_STRING_CONSTEXPR
> > typename basic_string<_CharT, _Traits, _Alloc>::size_type
> > --
> > 2.34.1
> >
>
next prev parent reply other threads:[~2022-08-24 6:18 UTC|newest]
Thread overview: 13+ messages / expand[flat|nested] mbox.gz Atom feed top
2022-08-22 18:15 whh8b
2022-08-22 18:15 ` whh8b
2022-08-23 16:33 ` Jonathan Wakely
2022-08-24 6:18 ` Will Hawkins [this message]
2022-08-24 9:49 ` Jonathan Wakely
2022-08-24 6:16 ` [PATCH v2] libstdc++: Optimize operator+(string/char*,string/char*) whh8b
2022-08-24 6:16 ` [PATCH] libstdc++: Optimize operator+(string/char*,string/char*) equally whh8b
2022-08-24 6:16 ` [PATCH] libstdc++: Optimize operator+(string/char*, string/char*) equally whh8b
2022-08-24 14:24 ` Jonathan Wakely
2022-08-24 22:39 ` Alexandre Oliva
2022-08-24 22:47 ` Jonathan Wakely
2022-08-24 23:02 ` Jonathan Wakely
2022-08-25 5:52 ` Will Hawkins
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to='CADx9qWh5AbBJ3mEe9XuSXqkp_rZvA=OrE1p=sOkNcaXu72aedA@mail.gmail.com' \
--to=hawkinsw@obs.cr \
--cc=gcc-patches@gcc.gnu.org \
--cc=jwakely@redhat.com \
--cc=libstdc++@gcc.gnu.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).