public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
From: Eelis van der Weegen <eelis@eelis.net>
Cc: libstdc++ <libstdc++@gcc.gnu.org>, gcc-patches <gcc-patches@gcc.gnu.org>
Subject: Re: [patch, libstdc++] std::shuffle: Generate two swap positions at a time if possible
Date: Tue, 03 May 2016 14:42:00 -0000	[thread overview]
Message-ID: <5728B8CC.7030405@eelis.net> (raw)
In-Reply-To: <CAH6eHdTnASg7A5USA_sTAHXPfUFmkrZvF27tZUJhj5ywwDVDag@mail.gmail.com>

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

Ah, thanks, I forgot to re-attach when I sent to include the libstdc++ list.

On 2016-05-03 14:38, Jonathan Wakely wrote:
> ENOPATCH
>
> On 1 May 2016 at 15:21, Eelis <eelis@eelis.net> wrote:
>> Sorry, forgot to include the libstdc++ list.
>>
>> On 2016-05-01 16:18, Eelis wrote:
>>>
>>> Hi,
>>>
>>> The attached patch optimizes std::shuffle for the very common case
>>> where the generator range is large enough that a single invocation
>>> can produce two swap positions.
>>>
>>> This reduces the runtime of the following testcase by 37% on my machine:
>>>
>>>       int main()
>>>       {
>>>           std::mt19937 gen;
>>>
>>>           std::vector<int> v;
>>>           v.reserve(10000);
>>>           for (int i = 0; i != 10000; ++i)
>>>           {
>>>               v.push_back(i);
>>>               std::shuffle(v.begin(), v.end(), gen);
>>>           }
>>>
>>>           std::cout << v.front() << '\n';
>>>       }
>>>
>>> Thoughts?
>>>
>>> Thanks,
>>>
>>> Eelis
>>>
>>
>>


[-- Attachment #2: double-step-shuffle.patch --]
[-- Type: text/x-patch, Size: 2440 bytes --]

Index: libstdc++-v3/include/bits/stl_algo.h
===================================================================
--- libstdc++-v3/include/bits/stl_algo.h	(revision 235680)
+++ libstdc++-v3/include/bits/stl_algo.h	(working copy)
@@ -3708,6 +3708,22 @@
 #endif
 
 #ifdef _GLIBCXX_USE_C99_STDINT_TR1
+
+  template<typename _IntType, typename _UniformRandomNumberGenerator>
+    inline _IntType
+    __generate_random_index_below(_IntType __bound, _UniformRandomNumberGenerator& __g)
+    {
+      const _IntType __urngrange = __g.max() - __g.min() + 1;
+      const _IntType __scaling = __urngrange / __bound;
+      const _IntType __past = __bound * __scaling;
+
+      for (;;)
+      {
+	const _IntType __r = _IntType(__g()) - __g.min();
+	if (__r < __past) return __r / __scaling;
+      }
+    }
+
   /**
    *  @brief Shuffle the elements of a sequence using a uniform random
    *         number generator.
@@ -3740,6 +3756,40 @@
       typedef typename std::make_unsigned<_DistanceType>::type __ud_type;
       typedef typename std::uniform_int_distribution<__ud_type> __distr_type;
       typedef typename __distr_type::param_type __p_type;
+
+      typedef typename std::remove_reference<_UniformRandomNumberGenerator>::type _Gen;
+      typedef typename std::common_type<typename _Gen::result_type, __ud_type>::type __uc_type;
+
+      const __uc_type __urngrange = _Gen::max() - _Gen::min() + 1;
+      const __uc_type __urange = __uc_type(__last - __first);
+
+      if (__urngrange / __urange >= __urange)
+        // I.e. (__urngrange >= __urange * __urange) but without wrap issues.
+      {
+	for (_RandomAccessIterator __i = __first + 1; __i != __last; )
+	{
+	  const __uc_type __swap_range = __uc_type(__i - __first) + 1;
+
+	  if (__i + 1 == __last)
+	  {
+	    const __uc_type __pos = __generate_random_index_below(__swap_range, __g);
+	    std::iter_swap(__i, __first + __pos);
+	    return;
+	  }
+
+	  // Use a single generator invocation to produce swap positions for
+	  // both of the next two elements:
+
+	  const __uc_type __comp_range = __swap_range * (__swap_range + 1);
+	  const __uc_type __pospos = __generate_random_index_below(__comp_range, __g);
+
+	  std::iter_swap(__i++, __first + (__pospos % __swap_range));
+	  std::iter_swap(__i++, __first + (__pospos / __swap_range));
+	}
+
+	return;
+      }
+
       __distr_type __d;
 
       for (_RandomAccessIterator __i = __first + 1; __i != __last; ++__i)

  reply	other threads:[~2016-05-03 14:42 UTC|newest]

Thread overview: 16+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2016-05-01 14:18 Eelis
2016-05-01 14:30 ` Eelis
2016-05-03 12:39   ` Jonathan Wakely
2016-05-03 14:42     ` Eelis van der Weegen [this message]
2016-05-25 19:54       ` Eelis
2016-05-25 20:45         ` Eelis
2016-08-31 12:45       ` Jonathan Wakely
2016-09-01 15:14         ` Jonathan Wakely
2016-09-01 15:27           ` Marc Glisse
2016-09-01 15:35             ` Jonathan Wakely
2016-09-01 15:31           ` Eelis van der Weegen
2016-09-01 15:37             ` Jonathan Wakely
2016-09-02 18:20         ` Eelis van der Weegen
2016-09-02 18:53           ` Eelis
2016-09-02 19:27             ` Eelis
2016-10-14 19:42             ` 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=5728B8CC.7030405@eelis.net \
    --to=eelis@eelis.net \
    --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).