pg.ddx.io  pgsql-hackers@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Yura Sokolov <y.sokolov@postgrespro.ru>
To: Fabien COELHO <coelho@cri.ensmp.fr>
Cc: Dean Rasheed <dean.a.rasheed@gmail.com>
Cc: Tom Lane <tgl@sss.pgh.pa.us>
Cc: Aleksander Alekseev <aleksander@timescale.com>
Cc: PostgreSQL Developers <pgsql-hackers@lists.postgresql.org>
Subject: Re: rand48 replacement
Date: Tue, 06 Jul 2021 10:19:45 +0300
Message-ID: <3b12e2dea4c3c33665c33b40c95349ef@postgrespro.ru> (raw)
In-Reply-To: <alpine.DEB.2.22.394.2107060805140.3858351@pseudo>
References: <alpine.DEB.2.22.394.2105241211230.165418@pseudo>
	<alpine.DEB.2.22.394.2105241509200.165418@pseudo>
	<699567.1625156441@sss.pgh.pa.us>
	<alpine.DEB.2.22.394.2107011842240.2143494@pseudo>
	<746421.1625164869@sss.pgh.pa.us>
	<CAEZATCXvzDSNhH2uRMUt5K_vmMBDrvo+D7Sn66--1Z_ELz==5Q@mail.gmail.com>
	<alpine.DEB.2.22.394.2107012212500.2248546@pseudo>
	<CAEZATCVpnOjrwU=4712OPjNDqV2sVkkK7XHDiR-nzYDL8N6z=Q@mail.gmail.com>
	<alpine.DEB.2.22.394.2107022340250.2359988@pseudo>
	<alpine.DEB.2.22.394.2107030902400.2359988@pseudo>
	<CAEZATCW76GZ-uA-i5_EL4ZukZuvWE_tTNPgRsPkR=0fyiiztTA@mail.gmail.com>
	<alpine.DEB.2.22.394.2107041115130.2359988@pseudo>
	<CAEZATCXcmccPQismOioD5kr2NMyMoeLpDaesizzoKh8M-BK5Dw@mail.gmail.com>
	<alpine.DEB.2.22.394.2107041718570.2359988@pseudo>
	<CAEZATCWnrVACs2FM219dSkh1ivic8KiGkPEVPtu-MO3nCOYm7A@mail.gmail.com>
	<alpine.DEB.2.22.394.2107042204240.2359988@pseudo>
	<3136e2672d5ab395521bae05a80df469@postgrespro.ru>
	<alpine.DEB.2.22.394.2107060805140.3858351@pseudo>

Fabien COELHO писал 2021-07-06 09:13:
> Hello Yura,
> 
>> I believe most "range" values are small, much smaller than UINT32_MAX.
>> In this case, according to [1] fastest method is Lemire's one (I'd 
>> take
>> original version from [2]) [...]
> 
> Yep.
> 
> I share your point that the range is more often 32 bits.
> 
> However, I'm not enthousiastic at combining two methods depending on
> the range, the function looks complex enough without that, so I would
> suggest not to take this option. Also, the decision process adds to
> the average cost, which is undesirable.

Given 99.99% cases will be in the likely case, branch predictor should
eliminate decision cost.

And as Dean Rasheed correctly mentioned, mask method will
have really bad pattern for branch predictor if range is not just below
or equal to power of two.
For example, rand_range(10000) will have 60% probability to pass through
`while (val > range)` and 40% probability to go to next loop iteration.
rand_range(100000) will have 76%/24% probabilities. Branch predictor
doesn't like it. Even rand_range(1000000), which is quite close to 2^20,
will have 95%/5%, and still not enough to please BP.

But with unbias multiply method it will be 0.0002%/99.9998% for 10000,
0,002%/99.998% for 100000 and 0.02%/99.98% for 1000000 - much-much 
better.
Branch predictor will make it almost free (i hope).

And __builtin_clzl is not free lunch either, it has latency 3-4 cycles
on modern processor. Well, probably it could run in parallel with some
part of xoroshiro, but it depends on how compiler will optimize this
function.

> I would certainly select the unbias multiply method if we want a u32 
> range variant.

There could be two functions.

regards,
Sokolov Yura.





view thread (59+ messages)  latest in thread

Message-ID: <3b12e2dea4c3c33665c33b40c95349ef@postgrespro.ru>
Permalink:  ../3b12e2dea4c3c33665c33b40c95349ef@postgrespro.ru/
Also on:    postgresql.org/message-id/3b12e2dea4c3c33665c33b40c95349ef@postgrespro.ru

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-hackers@postgresql.org
  Cc: y.sokolov@postgrespro.ru, coelho@cri.ensmp.fr, dean.a.rasheed@gmail.com, tgl@sss.pgh.pa.us, aleksander@timescale.com
  Subject: Re: rand48 replacement
  In-Reply-To: <3b12e2dea4c3c33665c33b40c95349ef@postgrespro.ru>
  References: <alpine.DEB.2.22.394.2105241211230.165418@pseudo> <699567.1625156441@sss.pgh.pa.us> <alpine.DEB.2.22.394.2107011842240.2143494@pseudo> <746421.1625164869@sss.pgh.pa.us> <CAEZATCXvzDSNhH2uRMUt5K_vmMBDrvo+D7Sn66--1Z_ELz==5Q@mail.gmail.com> <alpine.DEB.2.22.394.2107012212500.2248546@pseudo> <CAEZATCVpnOjrwU=4712OPjNDqV2sVkkK7XHDiR-nzYDL8N6z=Q@mail.gmail.com> <alpine.DEB.2.22.394.2107022340250.2359988@pseudo> <alpine.DEB.2.22.394.2107030902400.2359988@pseudo> <CAEZATCW76GZ-uA-i5_EL4ZukZuvWE_tTNPgRsPkR=0fyiiztTA@mail.gmail.com> <alpine.DEB.2.22.394.2107041115130.2359988@pseudo> <CAEZATCXcmccPQismOioD5kr2NMyMoeLpDaesizzoKh8M-BK5Dw@mail.gmail.com> <alpine.DEB.2.22.394.2107041718570.2359988@pseudo> <CAEZATCWnrVACs2FM219dSkh1ivic8KiGkPEVPtu-MO3nCOYm7A@mail.gmail.com> <alpine.DEB.2.22.394.2107042204240.2359988@pseudo> <3136e2672d5ab395521bae05a80df469@postgrespro.ru> <alpine.DEB.2.22.394.2107060805140.3858351@pseudo> <3b12e2dea4c3c33665c33b40c95349ef@postgrespro.ru>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox