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 14:36:58 +0100 [thread overview]
Message-ID: <CACb0b4=4E_+4ugV5RbzNMFCmAfnot_cW0s52JDfmCjkoD=0Ljw@mail.gmail.com> (raw)
In-Reply-To: <CAKqmYPa137-NdG8a0Jds5GSv230ufazYscmepFBNUHZWoSoc0w@mail.gmail.com>
On Tue, 17 Aug 2021 at 09:42, Antony Polukhin wrote:
>
> 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.
>
> libstdc++-v3/ChangeLog:
>
> * include/bits/random.tcc: Optimize seed_seq construction.
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.
next prev parent reply other threads:[~2021-08-17 13:37 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 [this message]
2021-08-17 13:40 ` Antony Polukhin
2021-08-17 16:23 ` 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='CACb0b4=4E_+4ugV5RbzNMFCmAfnot_cW0s52JDfmCjkoD=0Ljw@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).