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 1m0fN3-0007mm-69 for pgsql-hackers@arkaria.postgresql.org; Tue, 06 Jul 2021 07:19:53 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1m0fN2-0004Ps-2V for pgsql-hackers@arkaria.postgresql.org; Tue, 06 Jul 2021 07:19:52 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1m0fN1-0004Pj-JQ for pgsql-hackers@lists.postgresql.org; Tue, 06 Jul 2021 07:19:51 +0000 Received: from mail.postgrespro.ru ([93.174.131.139]) by makus.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1m0fMy-0005Pq-QB for pgsql-hackers@lists.postgresql.org; Tue, 06 Jul 2021 07:19:50 +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 11A9C21C8BEC; Tue, 6 Jul 2021 10:19:46 +0300 (MSK) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=postgrespro.ru; s=mail; t=1625555986; bh=jNTCMYq0gFBlXzZsgmO+CMj/9SzKAeRRW3sduE9z5Xg=; h=Date:From:To:Cc:Subject:In-Reply-To:References; b=IZ3nNnF8Rb9srFrCu0G+tTiTXlMnAzAklt5benOzyfEobjUQlfCcYbOtu274QhFAa jXgshHiwgKmCbp56kxRyDiv2t2Daguh6oRaZEll5zx5Rfzc23+YM1J9y3F0lnXzo+N hVei4Ih/NRZCXihKfgMTFYCGuVqQArtHQGz1Rw34= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit Date: Tue, 06 Jul 2021 10:19:45 +0300 From: Yura Sokolov To: Fabien COELHO Cc: Dean Rasheed , Tom Lane , Aleksander Alekseev , PostgreSQL Developers Subject: Re: rand48 replacement In-Reply-To: References: <699567.1625156441@sss.pgh.pa.us> <746421.1625164869@sss.pgh.pa.us> <3136e2672d5ab395521bae05a80df469@postgrespro.ru> User-Agent: Roundcube Webmail/1.4.0 Message-ID: <3b12e2dea4c3c33665c33b40c95349ef@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 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.