Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1m0s0O-0007vy-HJ for pgsql-hackers@arkaria.postgresql.org; Tue, 06 Jul 2021 20:49:20 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1m0s0N-0002l1-7j for pgsql-hackers@arkaria.postgresql.org; Tue, 06 Jul 2021 20:49:19 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1m0s0N-0002ks-07 for pgsql-hackers@lists.postgresql.org; Tue, 06 Jul 2021 20:49:19 +0000 Received: from sfr-1.cri.mines-paristech.fr ([77.158.180.225] helo=smtp.cri.ensmp.fr) by magus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1m0s0G-0002R3-Io for pgsql-hackers@lists.postgresql.org; Tue, 06 Jul 2021 20:49:18 +0000 Received: from pseudo (185.141.6.84.rev.sfr.net [84.6.141.185]) by smtp.cri.ensmp.fr (Postfix) with ESMTPSA id 70A08C00D4; Tue, 6 Jul 2021 22:51:11 +0200 (CEST) Date: Tue, 6 Jul 2021 22:49:07 +0200 (CEST) From: Fabien COELHO X-X-Sender: fabien@pseudo To: Yura Sokolov cc: Dean Rasheed , Tom Lane , Aleksander Alekseev , PostgreSQL Developers Subject: Re: rand48 replacement In-Reply-To: <3b12e2dea4c3c33665c33b40c95349ef@postgrespro.ru> Message-ID: References: <746421.1625164869@sss.pgh.pa.us> <3136e2672d5ab395521bae05a80df469@postgrespro.ru> <3b12e2dea4c3c33665c33b40c95349ef@postgrespro.ru> User-Agent: Alpine 2.22 (DEB 394 2020-01-19) MIME-Version: 1.0 Content-Type: text/plain; format=flowed; charset=US-ASCII List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk Hello Yura, >> 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. Hmmm. ISTM that a branch predictor should predict that unknown < small should probably be false, so a hint should be given that it is really true. > 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. On average the bitmask is the better unbiased method, if the online figures are to be trusted. Also, as already said, I do not really want to add code complexity, especially to get lower average performance, and especially with code like "threshold = - range % range", where both variables are unsigned, I have a headache just looking at it:-) > And __builtin_clzl is not free lunch either, it has latency 3-4 cycles > on modern processor. Well, % is not cheap either. > 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. Yep, but do we need them? Who is likely to want 32 bits pseudo random ints in a range? pgbench needs 64 bits. So I'm still inclined to just keep the faster-on-average bitmask method, despite that it may be slower for some ranges. The average cost for the worst case in PRNG calls is, if I'm not mistaken: 1 * 0.5 + 2 * 0.25 + 3 * 0.125 + ... ~ 2 which does not look too bad. -- Fabien.