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 1m0xnz-0002Ma-J1 for pgsql-hackers@arkaria.postgresql.org; Wed, 07 Jul 2021 03:00:56 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1m0xnw-000248-CJ for pgsql-hackers@arkaria.postgresql.org; Wed, 07 Jul 2021 03:00:52 +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 1m0xnw-000240-0z for pgsql-hackers@lists.postgresql.org; Wed, 07 Jul 2021 03:00:52 +0000 Received: from mail.postgrespro.ru ([93.174.131.139]) by magus.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1m0xns-0005YC-NB for pgsql-hackers@lists.postgresql.org; Wed, 07 Jul 2021 03:00:51 +0000 Received: from mail.postgrespro.ru (cyclops.postgrespro.ru [93.174.131.138]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (Client did not present a certificate) by mail.postgrespro.ru (Postfix) with ESMTPSA id 6C4D721C900C; Wed, 7 Jul 2021 06:00:47 +0300 (MSK) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=postgrespro.ru; s=mail; t=1625626847; bh=vGS1cmkkjsHvpjQ3VYuTreOKLaUL8H6YGucPStjP5K0=; h=Date:From:To:Cc:Subject:In-Reply-To:References; b=LYM9s/skJLimPjov8gEpiPnx21PqacvapwJ3vPACQ0O0no4CUtB3rVV1kPV1iCmQg eBoIa9M66sgujh3SoXK2YeZXHxHDiwae2Q11IRQXMEZQV78g4Af4XK5WIaLKbCQhHO OrTa94bbw31LgKjFiktXzi98/n5Uc6r5Ai/ldSrk= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit Date: Wed, 07 Jul 2021 06:00:47 +0300 From: Yura Sokolov To: Fabien COELHO Cc: Dean Rasheed , Tom Lane , Aleksander Alekseev , PostgreSQL Developers Subject: Re: rand48 replacement In-Reply-To: References: <746421.1625164869@sss.pgh.pa.us> <3136e2672d5ab395521bae05a80df469@postgrespro.ru> <3b12e2dea4c3c33665c33b40c95349ef@postgrespro.ru> User-Agent: Roundcube Webmail/1.4.0 Message-ID: <8904daa4a916b4990b11d7140e0ff81f@postgrespro.ru> X-Sender: y.sokolov@postgrespro.ru List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk Fabien COELHO писал 2021-07-06 23:49: > 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. Why? Branch predictor is history based: if it were usually true here then it will be true this time either. unknown < small is usually true therefore branch predictor will assume it is true. I put `likely` for compiler: compiler then puts `likely` path closer. > >> 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:-) If you mention https://www.pcg-random.org/posts/bounded-rands.html then 1. first graphs are made with not exact Lemire's code. Last code from https://lemire.me/blog/2016/06/30/fast-random-shuffling/ (which I derived from) performs modulo operation only if `(leftover < range)`. Even with `rand_range(1000000)` it is just once in four thousands runs. 2. You can see "Small-Constant Benchmark" at that page, Debiased Int is 1.5 times faster. And even in "Small-Shuffle" benchmark their unoptimized version is on-par with mask method. 3. If you go to "Making Improvements/Faster Threshold-Based Discarding" section, then you'll see code my version is matched with. It is twice faster than masked method in Small-Shuffle benchmark, and just a bit slower in Large-Shuffle. > >> 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. You doesn't count cost of branch-misprediction. https://stackoverflow.com/questions/11227809/why-is-processing-a-sorted-array-faster-than-processing-an-unsorted-array https://lemire.me/blog/2019/10/15/mispredicted-branches-can-multiply-your-running-times/ Therefore calculation should be at least: 1 * 0.5 + 0.5 * (3 + 0.5 * (3 + ...)) = 6 By the way, we have 64bit random. If we use 44bit from it for range <= (1<<20), then bias will be less than 1/(2**24). Could we just ignore it (given it is not crypto strong random)? uint64 pg_prng_u64_range(pg_prng_state *state, uint64 range) { uint64 val = xoroshiro128ss(state); uint64 m; if ((range & (range-1) == 0) /* handle all power 2 cases */ return range != 0 ? val & (range-1) : 0; if (likely(range < (1<<20))) /* * While multiply method is biased, bias will be smaller than 1/(1<<24) for * such small ranges. Lets ignore it. */ return ((val >> 20) * range) >> 44; /* Apple's mask method */ m = mask_u64(range-1); val &= m; while (val >= range) val = xoroshiro128ss(state) & m; return val; } Anyway, excuse me for heating this discussion cause of such non-essential issue. I'll try to control myself and don't proceed it further. regards, Sokolov Yura.