]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
libstdc++: Optimize std::seed_seq construction
authorAntony Polukhin <antoshkka@gmail.com>
Tue, 17 Aug 2021 12:50:53 +0000 (13:50 +0100)
committerJonathan Wakely <jwakely@redhat.com>
Fri, 26 Nov 2021 13:38:41 +0000 (13:38 +0000)
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)

libstdc++-v3/include/bits/random.tcc
libstdc++-v3/testsuite/26_numerics/random/pr60037-neg.cc

index d7503ee70945aad296c495c74766696dc64400c5..74ee6ada13a758f0d25f3acfd326dccbcd2d9def 100644 (file)
@@ -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));
index 9cffc3d06f9342fd636e26ed9046d91701a253a9..1291967b0d127c701329180a5264838a5070cdf7 100644 (file)
@@ -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 }