public inbox for libstdc++@gcc.gnu.org
 help / color / mirror / Atom feed
From: Jonathan Wakely <jwakely@redhat.com>
To: Antony Polukhin <antoshkka@gmail.com>
Cc: "libstdc++" <libstdc++@gcc.gnu.org>,
	gcc-patches List <gcc-patches@gcc.gnu.org>
Subject: Re: [PATCH] Optimize seed_seq construction
Date: Tue, 17 Aug 2021 17:23:54 +0100	[thread overview]
Message-ID: <CACb0b4=jiPOFQ_qFqHFF7MLzz3JTQxbFS-wCbxfSE6qwARZe2w@mail.gmail.com> (raw)
In-Reply-To: <CAKqmYPaK+DM+CUFCboWyV018LYN9jwzTsvEt1-7BVaw_hUS6sQ@mail.gmail.com>

[-- Attachment #1: Type: text/plain, Size: 1148 bytes --]

On Tue, 17 Aug 2021 at 14:40, Antony Polukhin <antoshkka@gmail.com> wrote:
>
> вт, 17 авг. 2021 г. в 16:37, Jonathan Wakely <jwakely@redhat.com>:
> <...>
> > Thanks, this is a nice improvement. We can avoid tag dispatching to
> > make it simpler though:
> >
> > @@ -3248,6 +3249,9 @@ namespace __detail
> >   template<typename _InputIterator>
> >     seed_seq::seed_seq(_InputIterator __begin, _InputIterator __end)
> >     {
> > +      if _GLIBCXX17_CONSTEXPR
> > (__is_random_access_iter<_InputIterator>::value)
> > +       _M_v.reserve(std::distance(__begin, __end));
> > +
> >       for (_InputIterator __iter = __begin; __iter != __end; ++__iter)
> >        _M_v.push_back(__detail::__mod<result_type,
> >                       __detail::_Shift<result_type, 32>::__value>(*__iter));
> >
> > The call to std::distance is well-formed for input iterators, but we
> > won't actually call it unless we have random access iterators.
> >
> > Unless you see a problem with this that I'm missing, I'll go with that version.
>
> Looks much better. Thanks!

Here's what I've tested and pushed to trunk.

Thanks again!

[-- Attachment #2: patch.txt --]
[-- Type: text/plain, Size: 2521 bytes --]

commit 174f9257a75dec93221eca26c236e0a6346c9dfd
Author: Antony Polukhin <antoshkka@gmail.com>
Date:   Tue Aug 17 13:50:53 2021

    libstdc++: Optimize std::seed_seq construction
    
    When std::seed_seq is constructed from random access iterators we can
    detect the internal vector size in O(1). Reserving memory for elements
    in such cases may avoid multiple memory allocations.
    
    Signed-off-by: Jonathan Wakely <jwakely@redhat.com>
    
    libstdc++-v3/ChangeLog:
    
            * include/bits/random.tcc (seed_seq::seed_seq): Reserve capacity
            if distance is O(1).
            * testsuite/26_numerics/random/pr60037-neg.cc: Adjust dg-error
            line number.
    
    Co-authored-by: Jonathan Wakely <jwakely@redhat.com>

diff --git a/libstdc++-v3/include/bits/random.tcc b/libstdc++-v3/include/bits/random.tcc
index 0be50d90e8a..023fded7f5d 100644
--- a/libstdc++-v3/include/bits/random.tcc
+++ b/libstdc++-v3/include/bits/random.tcc
@@ -3240,6 +3240,7 @@ namespace __detail
   template<typename _IntType, typename>
     seed_seq::seed_seq(std::initializer_list<_IntType> __il)
     {
+      _M_v.reserve(__il.size());
       for (auto __iter = __il.begin(); __iter != __il.end(); ++__iter)
 	_M_v.push_back(__detail::__mod<result_type,
 		       __detail::_Shift<result_type, 32>::__value>(*__iter));
@@ -3248,6 +3249,9 @@ namespace __detail
   template<typename _InputIterator>
     seed_seq::seed_seq(_InputIterator __begin, _InputIterator __end)
     {
+      if _GLIBCXX17_CONSTEXPR (__is_random_access_iter<_InputIterator>::value)
+	_M_v.reserve(std::distance(__begin, __end));
+
       for (_InputIterator __iter = __begin; __iter != __end; ++__iter)
 	_M_v.push_back(__detail::__mod<result_type,
 		       __detail::_Shift<result_type, 32>::__value>(*__iter));
diff --git a/libstdc++-v3/testsuite/26_numerics/random/pr60037-neg.cc b/libstdc++-v3/testsuite/26_numerics/random/pr60037-neg.cc
index 8fba7144d8a..3ab9c44232e 100644
--- a/libstdc++-v3/testsuite/26_numerics/random/pr60037-neg.cc
+++ b/libstdc++-v3/testsuite/26_numerics/random/pr60037-neg.cc
@@ -12,4 +12,4 @@ auto x = std::generate_canonical<std::size_t,
 
 // { dg-error "static assertion failed: template argument must be a floating point type" "" { target *-*-* } 169 }
 
-// { dg-error "static assertion failed: template argument must be a floating point type" "" { target *-*-* } 3352 }
+// { dg-error "static assertion failed: template argument must be a floating point type" "" { target *-*-* } 3356 }

      reply	other threads:[~2021-08-17 16:24 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-08-17  8:41 Antony Polukhin
2021-08-17 13:36 ` Jonathan Wakely
2021-08-17 13:40   ` Antony Polukhin
2021-08-17 16:23     ` Jonathan Wakely [this message]

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='CACb0b4=jiPOFQ_qFqHFF7MLzz3JTQxbFS-wCbxfSE6qwARZe2w@mail.gmail.com' \
    --to=jwakely@redhat.com \
    --cc=antoshkka@gmail.com \
    --cc=gcc-patches@gcc.gnu.org \
    --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).