public inbox for libstdc++@gcc.gnu.org
 help / color / mirror / Atom feed
From: Antony Polukhin <antoshkka@gmail.com>
To: "libstdc++" <libstdc++@gcc.gnu.org>,
	gcc-patches List <gcc-patches@gcc.gnu.org>
Subject: [PATCH] Optimize seed_seq construction
Date: Tue, 17 Aug 2021 11:41:08 +0300	[thread overview]
Message-ID: <CAKqmYPa137-NdG8a0Jds5GSv230ufazYscmepFBNUHZWoSoc0w@mail.gmail.com> (raw)

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

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.

-- 
Best regards,
Antony Polukhin

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

diff --git a/libstdc++-v3/include/bits/random.tcc b/libstdc++-v3/include/bits/random.tcc
index bf43970..816bfc1 100644
--- a/libstdc++-v3/include/bits/random.tcc
+++ b/libstdc++-v3/include/bits/random.tcc
@@ -3234,14 +3234,31 @@ namespace __detail
   template<typename _IntType>
     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));
     }
 
+  template<typename _Vector, typename _InputIterator>
+    void __reserve_if_distance_cheap(_Vector& __vec, _InputIterator __begin,
+      _InputIterator __end, random_access_iterator_tag)
+    {
+      __vec.reserve(__end - __begin);
+    }
+
+  template<typename _Vector, typename _InputIterator, typename _Tag>
+    void __reserve_if_distance_cheap(_Vector&, _InputIterator,
+      _InputIterator, _Tag)
+    {
+      // computing the distance between __begin and __end is not O(1)
+    }
+
   template<typename _InputIterator>
     seed_seq::seed_seq(_InputIterator __begin, _InputIterator __end)
     {
+      std::__reserve_if_distance_cheap(_M_v, __begin, __end,
+          typename iterator_traits<_InputIterator>::iterator_category());
       for (_InputIterator __iter = __begin; __iter != __end; ++__iter)
 	_M_v.push_back(__detail::__mod<result_type,
 		       __detail::_Shift<result_type, 32>::__value>(*__iter));

             reply	other threads:[~2021-08-17  8:41 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-08-17  8:41 Antony Polukhin [this message]
2021-08-17 13:36 ` Jonathan Wakely
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=CAKqmYPa137-NdG8a0Jds5GSv230ufazYscmepFBNUHZWoSoc0w@mail.gmail.com \
    --to=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).