1
0
mirror of https://github.com/postgres/postgres.git synced 2025-08-27 07:42:10 +03:00

Replace random(), pg_erand48(), etc with a better PRNG API and algorithm.

Standardize on xoroshiro128** as our basic PRNG algorithm, eliminating
a bunch of platform dependencies as well as fundamentally-obsolete PRNG
code.  In addition, this API replacement will ease replacing the
algorithm again in future, should that become necessary.

xoroshiro128** is a few percent slower than the drand48 family,
but it can produce full-width 64-bit random values not only 48-bit,
and it should be much more trustworthy.  It's likely to be noticeably
faster than the platform's random(), depending on which platform you
are thinking about; and we can have non-global state vectors easily,
unlike with random().  It is not cryptographically strong, but neither
are the functions it replaces.

Fabien Coelho, reviewed by Dean Rasheed, Aleksander Alekseev, and myself

Discussion: https://postgr.es/m/alpine.DEB.2.22.394.2105241211230.165418@pseudo
This commit is contained in:
Tom Lane
2021-11-28 21:32:36 -05:00
parent f44ceb46ec
commit 3804539e48
50 changed files with 543 additions and 480 deletions

View File

@@ -21,14 +21,7 @@ geqo_set_seed(PlannerInfo *root, double seed)
{
GeqoPrivateData *private = (GeqoPrivateData *) root->join_search_private;
/*
* XXX. This seeding algorithm could certainly be improved - but it is not
* critical to do so.
*/
memset(private->random_state, 0, sizeof(private->random_state));
memcpy(private->random_state,
&seed,
Min(sizeof(private->random_state), sizeof(seed)));
pg_prng_fseed(&private->random_state, seed);
}
double
@@ -36,5 +29,17 @@ geqo_rand(PlannerInfo *root)
{
GeqoPrivateData *private = (GeqoPrivateData *) root->join_search_private;
return pg_erand48(private->random_state);
return pg_prng_double(&private->random_state);
}
int
geqo_randint(PlannerInfo *root, int upper, int lower)
{
GeqoPrivateData *private = (GeqoPrivateData *) root->join_search_private;
/*
* In current usage, "lower" is never negative so we can just use
* pg_prng_uint64_range directly.
*/
return (int) pg_prng_uint64_range(&private->random_state, lower, upper);
}

View File

@@ -63,10 +63,6 @@ geqo_selection(PlannerInfo *root, Chromosome *momma, Chromosome *daddy,
/*
* Ensure we have selected different genes, except if pool size is only
* one, when we can't.
*
* This code was observed to hang up in an infinite loop when the
* platform's implementation of erand48() was broken. We now always use
* our own version.
*/
if (pool->size > 1)
{
@@ -95,11 +91,11 @@ linear_rand(PlannerInfo *root, int pool_size, double bias)
double max = (double) pool_size;
/*
* If geqo_rand() returns exactly 1.0 then we will get exactly max from
* this equation, whereas we need 0 <= index < max. Also it seems
* possible that roundoff error might deliver values slightly outside the
* range; in particular avoid passing a value slightly less than 0 to
* sqrt(). If we get a bad value just try again.
* geqo_rand() is not supposed to return 1.0, but if it does then we will
* get exactly max from this equation, whereas we need 0 <= index < max.
* Also it seems possible that roundoff error might deliver values
* slightly outside the range; in particular avoid passing a value
* slightly less than 0 to sqrt(). If we get a bad value just try again.
*/
do
{