summaryrefslogtreecommitdiff
path: root/src/backend/optimizer
diff options
context:
space:
mode:
authorTom Lane <tgl@sss.pgh.pa.us>2021-11-28 21:32:36 -0500
committerTom Lane <tgl@sss.pgh.pa.us>2021-11-28 21:33:07 -0500
commit3804539e48e794781c6145c7f988f5d507418fa8 (patch)
tree317904b43ca8c1d510b23cb8fdd7b05a75e971bc /src/backend/optimizer
parentf44ceb46ec2d8da48f6e145bf462d5620c25e079 (diff)
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
Diffstat (limited to 'src/backend/optimizer')
-rw-r--r--src/backend/optimizer/geqo/geqo_random.c23
-rw-r--r--src/backend/optimizer/geqo/geqo_selection.c14
2 files changed, 19 insertions, 18 deletions
diff --git a/src/backend/optimizer/geqo/geqo_random.c b/src/backend/optimizer/geqo/geqo_random.c
index f21bc047e68..4d9432c0dfe 100644
--- a/src/backend/optimizer/geqo/geqo_random.c
+++ b/src/backend/optimizer/geqo/geqo_random.c
@@ -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);
}
diff --git a/src/backend/optimizer/geqo/geqo_selection.c b/src/backend/optimizer/geqo/geqo_selection.c
index 66b6c8ae38e..68a2e4e9f15 100644
--- a/src/backend/optimizer/geqo/geqo_selection.c
+++ b/src/backend/optimizer/geqo/geqo_selection.c
@@ -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
{