public inbox for libstdc++@gcc.gnu.org
 help / color / mirror / Atom feed
From: Jonathan Wakely <jwakely.gcc@gmail.com>
To: Jakub Jelinek <jakub@redhat.com>
Cc: Martin Sebor <msebor@gmail.com>,
	"libstdc++" <libstdc++@gcc.gnu.org>,
	 gcc-patches <gcc-patches@gcc.gnu.org>,
	Jonathan Wakely <jwakely@redhat.com>
Subject: Re: [committed] libstdc++: Disable over-zealous warnings about std::string copies [PR103332]
Date: Fri, 10 Dec 2021 18:16:05 +0000	[thread overview]
Message-ID: <CAH6eHdRLEdcKEisxLTE7NvGG44aEaoZuKFiUEJ5WOjjGd5rO=g@mail.gmail.com> (raw)
In-Reply-To: <20211210171749.GR2646553@tucnak>

On Fri, 10 Dec 2021 at 17:17, Jakub Jelinek wrote:
>
> On Fri, Dec 10, 2021 at 10:11:04AM -0700, Martin Sebor via Gcc-patches wrote:
> > On 12/10/21 9:41 AM, Jakub Jelinek wrote:
> > > On Fri, Dec 10, 2021 at 09:35:50AM -0700, Martin Sebor via Gcc-patches wrote:
> > > > The above was just a quick proof of concept experiment.  You're
> > > > of course right that the final solution can't be so crude(*).
> > > > But if the required functions are always_inline (I think member
> > > > functions defined in the body of the class implicitly are
> > >
> > > They are not, and can't be, nothing says that such member functions
> > > can't use constructs that make it uninlinable (with always_inline
> > > that would be an error), or are way too large that inlining is not
> > > desirable, etc.  They are just implicitly inline.
> >
> > The functions we're talking about are the trivial max_size()
> > members of std::string and allocator traits.  They just return
> > a constant.
> >
> > But I see I was wrong, even member functions have to be explicitly
> > declared always_inline to be guaranteed to be inlined even at -O0.
> > I don't think that should be an issue for the trivial max_size()
> > (at least not for the std::string specialization).
>
> Note, if those functions are declared constexpr, without -fno-inline
> (default at -O0), then cp_fold will try to evaluate such calls to constant
> expressions already, effectively "inlining" them.

Every member function of std::string is constexpr in C++20, but not
before. But we could add constexpr to internal _M_xxx functions if
that benefits optimization.

For std::basic_string::max_size() it has to call another function
provided by the allocator, but for the std::string (i.e.
std::basic_string<char, char_traits<char>, allocator<char>>)
specialization we know what the allocator is going to tell us.

--- a/libstdc++-v3/include/bits/basic_string.h
+++ b/libstdc++-v3/include/bits/basic_string.h
@@ -1071,7 +1071,12 @@ _GLIBCXX_BEGIN_NAMESPACE_CXX11
      _GLIBCXX20_CONSTEXPR
      size_type
      max_size() const _GLIBCXX_NOEXCEPT
-      { return (_Alloc_traits::max_size(_M_get_allocator()) - 1) / 2; }
+      {
+       if _GLIBCXX17_CONSTEXPR (__are_same<allocator_type, allocator<char>>)
+         return size_t(-1) / 2;
+       else
+         return (_Alloc_traits::max_size(_M_get_allocator()) - 1) / 2;
+      }

      /**
       *  @brief  Resizes the %string to the specified number of characters.

  reply	other threads:[~2021-12-10 18:16 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-12-09 23:24 Jonathan Wakely
2021-12-10  0:38 ` Martin Sebor
2021-12-10  1:49   ` Martin Sebor
2021-12-10 10:12     ` Jonathan Wakely
2021-12-10 16:35       ` Martin Sebor
2021-12-10 16:41         ` Jakub Jelinek
2021-12-10 17:11           ` Martin Sebor
2021-12-10 17:17             ` Jakub Jelinek
2021-12-10 18:16               ` Jonathan Wakely [this message]
2021-12-10 18:10         ` Jonathan Wakely
2021-12-10  9:51   ` Jonathan Wakely

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='CAH6eHdRLEdcKEisxLTE7NvGG44aEaoZuKFiUEJ5WOjjGd5rO=g@mail.gmail.com' \
    --to=jwakely.gcc@gmail.com \
    --cc=gcc-patches@gcc.gnu.org \
    --cc=jakub@redhat.com \
    --cc=jwakely@redhat.com \
    --cc=libstdc++@gcc.gnu.org \
    --cc=msebor@gmail.com \
    /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).