Commit 51fc8ab7 authored by Marko Mäkelä's avatar Marko Mäkelä

MDEV-21256: Reduce the use of ut_rnd_gen_next_ulint()

ut_rnd_set_seed(): Unused function; remove.

ut_rnd_gen(): Renamed from page_cur_lcg_prng().

ut_rnd_current: The internal state of ut_rnd_gen().

page_cur_open_on_rnd_user_rec(): Replace linear search with
page_rec_get_nth().
parent 4c0854f2
...@@ -444,26 +444,6 @@ pick_seeds( ...@@ -444,26 +444,6 @@ pick_seeds(
} }
} }
/*********************************************************//**
Generates a random iboolean value.
@return the random value */
static
ibool
ut_rnd_gen_ibool(void)
/*=================*/
{
ulint x;
x = ut_rnd_gen_ulint();
if (((x >> 20) + (x >> 15)) & 1) {
return(TRUE);
}
return(FALSE);
}
/*************************************************************//** /*************************************************************//**
Select next node and group where to add. */ Select next node and group where to add. */
static static
...@@ -500,8 +480,7 @@ pick_next( ...@@ -500,8 +480,7 @@ pick_next(
/* Introduce some randomness if the record /* Introduce some randomness if the record
is identical */ is identical */
if (diff == 0) { if (diff == 0) {
diff = static_cast<double>( diff = static_cast<double>(ut_rnd_gen() & 1);
ut_rnd_gen_ibool());
} }
*n_group = 1 + (diff > 0); *n_group = 1 + (diff > 0);
......
/***************************************************************************** /*****************************************************************************
Copyright (c) 1994, 2016, Oracle and/or its affiliates. All Rights Reserved. Copyright (c) 1994, 2016, Oracle and/or its affiliates. All Rights Reserved.
Copyright (c) 2019, MariaDB Corporation.
This program is free software; you can redistribute it and/or modify it under This program is free software; you can redistribute it and/or modify it under
the terms of the GNU General Public License as published by the Free Software the terms of the GNU General Public License as published by the Free Software
...@@ -27,37 +28,34 @@ Created 1/20/1994 Heikki Tuuri ...@@ -27,37 +28,34 @@ Created 1/20/1994 Heikki Tuuri
#define ut0rnd_h #define ut0rnd_h
#include "ut0byte.h" #include "ut0byte.h"
#include <my_sys.h>
#ifndef UNIV_INNOCHECKSUM #ifndef UNIV_INNOCHECKSUM
/** The 'character code' for end of field or string (used /** Seed value of ut_rnd_gen() */
in folding records */ extern uint64_t ut_rnd_current;
#define UT_END_OF_FIELD 257
/** @return a pseudo-random 64-bit number */
inline uint64_t ut_rnd_gen()
{
/*
This is a linear congruential pseudo random number generator.
The formula and the constants
being used are:
X[n+1] = (a * X[n] + c) mod m
where:
X[0] = my_interval_timer()
a = 1103515245 (3^5 * 5 * 7 * 129749)
c = 12345 (3 * 5 * 823)
m = 18446744073709551616 (1<<64), implicit */
if (UNIV_UNLIKELY(!ut_rnd_current)) {
ut_rnd_current = my_interval_timer();
}
ut_rnd_current = 1103515245 * ut_rnd_current + 12345;
return ut_rnd_current;
}
/********************************************************//**
This is used to set the random number seed. */
UNIV_INLINE
void
ut_rnd_set_seed(
/*============*/
ulint seed); /*!< in: seed */
/********************************************************//**
The following function generates a series of 'random' ulint integers.
@return the next 'random' number */
UNIV_INLINE
ulint
ut_rnd_gen_next_ulint(
/*==================*/
ulint rnd); /*!< in: the previous random number value */
/*********************************************************//**
The following function generates 'random' ulint integers which
enumerate the value space (let there be N of them) of ulint integers
in a pseudo-random fashion. Note that the same integer is repeated
always after N calls to the generator.
@return the 'random' number */
UNIV_INLINE
ulint
ut_rnd_gen_ulint(void);
/*==================*/
/********************************************************//** /********************************************************//**
Generates a random integer from a given interval. Generates a random integer from a given interval.
@return the 'random' number */ @return the 'random' number */
......
/***************************************************************************** /*****************************************************************************
Copyright (c) 1994, 2016, Oracle and/or its affiliates. All Rights Reserved. Copyright (c) 1994, 2016, Oracle and/or its affiliates. All Rights Reserved.
Copyright (c) 2017, MariaDB Corporation. Copyright (c) 2017, 2019, MariaDB Corporation.
This program is free software; you can redistribute it and/or modify it under This program is free software; you can redistribute it and/or modify it under
the terms of the GNU General Public License as published by the Free Software the terms of the GNU General Public License as published by the Free Software
...@@ -42,17 +42,6 @@ Created 5/30/1994 Heikki Tuuri ...@@ -42,17 +42,6 @@ Created 5/30/1994 Heikki Tuuri
/** Seed value of ut_rnd_gen_ulint() */ /** Seed value of ut_rnd_gen_ulint() */
extern ulint ut_rnd_ulint_counter; extern ulint ut_rnd_ulint_counter;
/********************************************************//**
This is used to set the random number seed. */
UNIV_INLINE
void
ut_rnd_set_seed(
/*============*/
ulint seed) /*!< in: seed */
{
ut_rnd_ulint_counter = seed;
}
/********************************************************//** /********************************************************//**
The following function generates a series of 'random' ulint integers. The following function generates a series of 'random' ulint integers.
@return the next 'random' number */ @return the next 'random' number */
......
...@@ -35,38 +35,6 @@ Created 10/4/1994 Heikki Tuuri ...@@ -35,38 +35,6 @@ Created 10/4/1994 Heikki Tuuri
#include <algorithm> #include <algorithm>
/*******************************************************************//**
This is a linear congruential generator PRNG. Returns a pseudo random
number between 0 and 2^64-1 inclusive. The formula and the constants
being used are:
X[n+1] = (a * X[n] + c) mod m
where:
X[0] = my_interval_timer()
a = 1103515245 (3^5 * 5 * 7 * 129749)
c = 12345 (3 * 5 * 823)
m = 18446744073709551616 (2^64)
@return number between 0 and 2^64-1 */
static
ib_uint64_t
page_cur_lcg_prng(void)
/*===================*/
{
#define LCG_a 1103515245
#define LCG_c 12345
static uint64_t lcg_current;
if (!lcg_current) {
lcg_current = my_interval_timer();
}
/* no need to "% 2^64" explicitly because lcg_current is
64 bit and this will be done anyway */
lcg_current = LCG_a * lcg_current + LCG_c;
return(lcg_current);
}
#ifdef BTR_CUR_HASH_ADAPT #ifdef BTR_CUR_HASH_ADAPT
# ifdef UNIV_SEARCH_PERF_STAT # ifdef UNIV_SEARCH_PERF_STAT
static ulint page_cur_short_succ; static ulint page_cur_short_succ;
...@@ -814,8 +782,7 @@ page_cur_open_on_rnd_user_rec( ...@@ -814,8 +782,7 @@ page_cur_open_on_rnd_user_rec(
buf_block_t* block, /*!< in: page */ buf_block_t* block, /*!< in: page */
page_cur_t* cursor) /*!< out: page cursor */ page_cur_t* cursor) /*!< out: page cursor */
{ {
ulint rnd; const ulint n_recs = page_get_n_recs(block->frame);
ulint n_recs = page_get_n_recs(buf_block_get_frame(block));
page_cur_set_before_first(block, cursor); page_cur_set_before_first(block, cursor);
...@@ -824,11 +791,9 @@ page_cur_open_on_rnd_user_rec( ...@@ -824,11 +791,9 @@ page_cur_open_on_rnd_user_rec(
return; return;
} }
rnd = (ulint) (page_cur_lcg_prng() % n_recs); cursor->rec = page_rec_get_nth(block->frame,
static_cast<ulint>
do { (ut_rnd_gen() % n_recs) + 1);
page_cur_move_to_next(cursor);
} while (rnd--);
} }
/** Write a redo log record of inserting a record into an index page. /** Write a redo log record of inserting a record into an index page.
...@@ -2426,18 +2391,17 @@ page_cur_delete_rec( ...@@ -2426,18 +2391,17 @@ page_cur_delete_rec(
#ifdef UNIV_COMPILE_TEST_FUNCS #ifdef UNIV_COMPILE_TEST_FUNCS
/*******************************************************************//** /*******************************************************************//**
Print the first n numbers, generated by page_cur_lcg_prng() to make sure Print the first n numbers, generated by ut_rnd_gen() to make sure
(visually) that it works properly. */ (visually) that it works properly. */
void void
test_page_cur_lcg_prng( test_ut_rnd_gen(
/*===================*/
int n) /*!< in: print first n numbers */ int n) /*!< in: print first n numbers */
{ {
int i; int i;
unsigned long long rnd; unsigned long long rnd;
for (i = 0; i < n; i++) { for (i = 0; i < n; i++) {
rnd = page_cur_lcg_prng(); rnd = ut_rnd_gen();
printf("%llu\t%%2=%llu %%3=%llu %%5=%llu %%7=%llu %%11=%llu\n", printf("%llu\t%%2=%llu %%3=%llu %%5=%llu %%7=%llu %%11=%llu\n",
rnd, rnd,
rnd % 2, rnd % 2,
......
/***************************************************************************** /*****************************************************************************
Copyright (c) 1994, 2016, Oracle and/or its affiliates. All Rights Reserved. Copyright (c) 1994, 2016, Oracle and/or its affiliates. All Rights Reserved.
Copyright (c) 2019, MariaDB Corporation.
This program is free software; you can redistribute it and/or modify it under This program is free software; you can redistribute it and/or modify it under
the terms of the GNU General Public License as published by the Free Software the terms of the GNU General Public License as published by the Free Software
...@@ -25,6 +26,9 @@ Created 5/11/1994 Heikki Tuuri ...@@ -25,6 +26,9 @@ Created 5/11/1994 Heikki Tuuri
#include "ut0rnd.h" #include "ut0rnd.h"
/** Seed value of ut_rnd_gen() */
uint64_t ut_rnd_current;
/** These random numbers are used in ut_find_prime */ /** These random numbers are used in ut_find_prime */
/*@{*/ /*@{*/
#define UT_RANDOM_1 1.0412321 #define UT_RANDOM_1 1.0412321
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment