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 1ll8Kp-0000cc-QW for pgsql-hackers@arkaria.postgresql.org; Mon, 24 May 2021 11:01:23 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1ll8Gu-0006KH-S5 for pgsql-hackers@arkaria.postgresql.org; Mon, 24 May 2021 10:57:20 +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 1ll8Gu-0006K9-J0 for pgsql-hackers@lists.postgresql.org; Mon, 24 May 2021 10:57:20 +0000 Received: from mail-ed1-x532.google.com ([2a00:1450:4864:20::532]) by makus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1ll8Gs-0002RP-2J for pgsql-hackers@lists.postgresql.org; Mon, 24 May 2021 10:57:19 +0000 Received: by mail-ed1-x532.google.com with SMTP id y7so13880734eda.2 for ; Mon, 24 May 2021 03:57:17 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=enterprisedb-com.20150623.gappssmtp.com; s=20150623; h=subject:to:references:from:message-id:date:user-agent:mime-version :in-reply-to:content-language:content-transfer-encoding; bh=Un8vW7JcKT8Yp/+pS2dAymWZbGBbrbrAc8UxWRvJFbI=; b=ilafzSB9n0QE3Af9th3KYafnYhiOInoZEzkGBGPP9eeAwLCyArKd3Zij+lzu4Oygud ltjHa8v95rkfMp3yi1cSH7Nhal8LHjEvi0OS9z/h41m0HDRhRmGaQIAQSMrSFfy07aCI VWGEgyzCTEjs5XW0p1Yuk7fv7UW/qMzG0ECfnJ2F7h4GcMZMaQnBwe9+kBTJOxi+JfkD 26G4DTP7orgAfY2HpWtdHO6OJ1TDWfmcQyIKBsW5jubQ/Oy3YZBzj5dx55UFY8ZVLwQ2 DAk8LBBEuOcdurMZLLQPL7hTqDmJL33eQdhcfl25uKWWvoPo2aE01QFELH4VXy/OfzNz KQFw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:subject:to:references:from:message-id:date :user-agent:mime-version:in-reply-to:content-language :content-transfer-encoding; bh=Un8vW7JcKT8Yp/+pS2dAymWZbGBbrbrAc8UxWRvJFbI=; b=kpiT0dWgx1Stxh5sGptKeyndAR23clXJEmxBVbUGiSfgs2/BoVgn9m/kqeTDFQOG0t Sn7EY7wJCfvXB0PzPpmdf/UkgdsMvlHMIQTKCcQzPpBQn6MyGZ4hojr7lrafuQU1zfUQ RDnDtVrtxAIiY592uK++4W99nKAmpTXa3u9BF+JLo3QPzXcw9ZIPVnI2mGDQpDfX9WGf bzje9WIBLFTieUKkxHs5wpF7i5Wp2HDQJANBg8teKrz/yFQSQdxt3h0XBokzPBsm2RHb mtrI3UoUlJbxtdaHmrXw3lFT77rfiNNeElzB9MExkf/L8csTCby4G3pDza6AgvM/8V4s XZXw== X-Gm-Message-State: AOAM53028hnyyWHfC5GfefC8vphxLzEjW+4R58MMJf5kN5otvjsxV3UR KaJ/NiqRXEBWNzojjnZQpVwnFZu1hS2X8678cnfWyKzZ+FsMU7qG1Fo1jqDNgX+mtCZYRLNMOQ4 io5IGmkZp1+Dyd7I41wwJTOblIGSzM3xQvZX3VG5Hn3iLF+twiVgDU/cmkZXM19udEMDMv+1cgE fyldZPAZiZzB/j/PpPX3/XmGZpzQNoQue7Myk7JGzsYG5toIbJxQWVwy9J8LHAO33YjNYQsf0Xn WG3UVFA3OmNazCZkl5qc+ebc4I4GtJLebJgDZl19TJ4HxVhXeCgSRL5 X-Google-Smtp-Source: ABdhPJw8XLVcj2TVxX6Eh/DojgD6mNWj6cT1TKyFD+xLKXGFn9Fw9W9g/gOa0WDTdD5SgNSXW+0QJQ== X-Received: by 2002:a05:6402:5201:: with SMTP id s1mr25204681edd.86.1621853835683; Mon, 24 May 2021 03:57:15 -0700 (PDT) Received: from [10.137.0.19] (ip-86-49-254-183.net.upcbroadband.cz. [86.49.254.183]) by smtp.gmail.com with ESMTPSA id c3sm9611419edn.16.2021.05.24.03.57.15 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Mon, 24 May 2021 03:57:15 -0700 (PDT) Subject: Re: rand48 replacement To: Fabien COELHO , PostgreSQL Developers References: From: Tomas Vondra Message-ID: <43a6e30b-542f-671a-e7bb-b7f78cf90e8c@enterprisedb.com> Date: Mon, 24 May 2021 12:57:13 +0200 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:78.0) Gecko/20100101 Thunderbird/78.10.1 MIME-Version: 1.0 In-Reply-To: Content-Type: text/plain; charset=windows-1252; format=flowed Content-Language: en-US Content-Transfer-Encoding: 8bit X-CLOUD-SEC-AV-Info: enterprisedb,google_mail,monitor X-CLOUD-SEC-AV-Sent: true X-Gm-Spam: 0 X-Gm-Phishy: 0 List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk Hi, On 5/24/21 12:31 PM, Fabien COELHO wrote: > > Hello pg-devs, > > I have given a go at proposing a replacement for rand48. > So what is the motivation for replacing rand48? Speed, quality of produced random numbers, features rand48 can't provide, or what? > POSIX 1988 (?) rand48 is a LCG PRNG designed to generate 32 bits > integers or floats based on a 48 bits state on 16 or 32 bits > architectures. LCG cycles on the low bits, which can be quite annoying. > Given that we run on 64 bits architectures and that we need to generate > 64 bits ints or doubles, IMHO it makes very little sense to stick to that. > > We should (probably) want: >  - one reasonable default PRNG for all pg internal uses. >  - NOT to invent a new design! >  - something fast, close to rand48 (which basically does 2 arithmetic >    ops, so it is hard to compete) >    no need for something cryptographic though, which would imply slow >  - to produce 64 bits integers & doubles with a 52 bits mantissa, >    so state size > 64 bits. >  - a small state though, because we might generate quite a few of them >    for different purposes so state size <= 256 or even <= 128 bits >  - the state to be aligned to whatever => 128 bits >  - 64 bits operations for efficiency on modern architectures, >    but not 128 bits operations. >  - not to depend on special hardware for speed (eg MMX/SSE/AES). >  - not something with obvious known and relevant defects. >  - not something with "rights" attached. > > These constraints reduce drastically the available options from > https://en.wikipedia.org/wiki/List_of_random_number_generators > > The attached patch removes "rand48" and adds a "pg_prng" implementation > based on xoroshiro128ss, and replaces it everywhere. In pgbench, the non > portable double-relying code is replaced by hopefully portable ints. The > interface makes it easy to replace the underlying PRNG if something else > is desired. > xoroshiro seems reasonable. How does it compare to rand48? Does it need much less/more state, is it faster/slower, etc.? I'd expect that it produces better random sequence, considering rand48 is a LCG, which is fairly simple decades old design. regards -- Tomas Vondra EnterpriseDB: http://www.enterprisedb.com The Enterprise PostgreSQL Company