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.

Reply via email to