public inbox for libstdc++-cvs@sourceware.org help / color / mirror / Atom feed
From: Jonathan Wakely <redi@gcc.gnu.org> To: gcc-cvs@gcc.gnu.org, libstdc++-cvs@gcc.gnu.org Subject: [gcc r10-10302] libstdc++: Optimize std::seed_seq construction Date: Fri, 26 Nov 2021 15:41:03 +0000 (GMT) [thread overview] Message-ID: <20211126154103.8D5EC3857C69@sourceware.org> (raw) https://gcc.gnu.org/g:11f4502ce55b1a833ed6988e03499d06213395c4 commit r10-10302-g11f4502ce55b1a833ed6988e03499d06213395c4 Author: Antony Polukhin <antoshkka@gmail.com> Date: Tue Aug 17 13:50:53 2021 +0100 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> (cherry picked from commit 174f9257a75dec93221eca26c236e0a6346c9dfd) Diff: --- libstdc++-v3/include/bits/random.tcc | 4 ++++ libstdc++-v3/testsuite/26_numerics/random/pr60037-neg.cc | 2 +- 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/libstdc++-v3/include/bits/random.tcc b/libstdc++-v3/include/bits/random.tcc index d7503ee7094..74ee6ada13a 100644 --- a/libstdc++-v3/include/bits/random.tcc +++ b/libstdc++-v3/include/bits/random.tcc @@ -3200,6 +3200,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)); @@ -3208,6 +3209,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 9cffc3d06f9..1291967b0d1 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 *-*-* } 167 } -// { dg-error "static assertion failed: template argument must be a floating point type" "" { target *-*-* } 3284 } +// { dg-error "static assertion failed: template argument must be a floating point type" "" { target *-*-* } 3288 }
reply other threads:[~2021-11-26 15:41 UTC|newest] Thread overview: [no followups] expand[flat|nested] mbox.gz Atom feed
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=20211126154103.8D5EC3857C69@sourceware.org \ --to=redi@gcc.gnu.org \ --cc=gcc-cvs@gcc.gnu.org \ --cc=libstdc++-cvs@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: linkBe 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).