summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMarko Mäkelä <marko.makela@mariadb.com>2019-12-09 12:17:12 +0200
committerMarko Mäkelä <marko.makela@mariadb.com>2019-12-10 16:58:28 +0200
commit51fc8ab73e3ee9712d1128fefc9831dcf891a3b5 (patch)
tree0c6eaa9608129cc440535b2be77f8c6bc71323ae
parent4c0854f2211a034683afd3a2c1e4f8d020c6785a (diff)
downloadmariadb-git-51fc8ab73e3ee9712d1128fefc9831dcf891a3b5.tar.gz
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().
-rw-r--r--storage/innobase/gis/gis0geo.cc23
-rw-r--r--storage/innobase/include/ut0rnd.h54
-rw-r--r--storage/innobase/include/ut0rnd.ic13
-rw-r--r--storage/innobase/page/page0cur.cc50
-rw-r--r--storage/innobase/ut/ut0rnd.cc4
5 files changed, 39 insertions, 105 deletions
diff --git a/storage/innobase/gis/gis0geo.cc b/storage/innobase/gis/gis0geo.cc
index fb55945549e..3a727185632 100644
--- a/storage/innobase/gis/gis0geo.cc
+++ b/storage/innobase/gis/gis0geo.cc
@@ -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. */
static
@@ -500,8 +480,7 @@ pick_next(
/* Introduce some randomness if the record
is identical */
if (diff == 0) {
- diff = static_cast<double>(
- ut_rnd_gen_ibool());
+ diff = static_cast<double>(ut_rnd_gen() & 1);
}
*n_group = 1 + (diff > 0);
diff --git a/storage/innobase/include/ut0rnd.h b/storage/innobase/include/ut0rnd.h
index b6d4d4abbd2..56b4a6159c1 100644
--- a/storage/innobase/include/ut0rnd.h
+++ b/storage/innobase/include/ut0rnd.h
@@ -1,6 +1,7 @@
/*****************************************************************************
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
the terms of the GNU General Public License as published by the Free Software
@@ -27,38 +28,35 @@ Created 1/20/1994 Heikki Tuuri
#define ut0rnd_h
#include "ut0byte.h"
+#include <my_sys.h>
#ifndef UNIV_INNOCHECKSUM
-/** The 'character code' for end of field or string (used
-in folding records */
-#define UT_END_OF_FIELD 257
+/** Seed value of ut_rnd_gen() */
+extern uint64_t ut_rnd_current;
+
+/** @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.
@return the 'random' number */
UNIV_INLINE
diff --git a/storage/innobase/include/ut0rnd.ic b/storage/innobase/include/ut0rnd.ic
index 0003c42baad..8b7ea5fe1e4 100644
--- a/storage/innobase/include/ut0rnd.ic
+++ b/storage/innobase/include/ut0rnd.ic
@@ -1,7 +1,7 @@
/*****************************************************************************
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
the terms of the GNU General Public License as published by the Free Software
@@ -43,17 +43,6 @@ Created 5/30/1994 Heikki Tuuri
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.
@return the next 'random' number */
UNIV_INLINE
diff --git a/storage/innobase/page/page0cur.cc b/storage/innobase/page/page0cur.cc
index b4ec3e6dcdf..a5a8c6c3d1e 100644
--- a/storage/innobase/page/page0cur.cc
+++ b/storage/innobase/page/page0cur.cc
@@ -35,38 +35,6 @@ Created 10/4/1994 Heikki Tuuri
#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 UNIV_SEARCH_PERF_STAT
static ulint page_cur_short_succ;
@@ -814,8 +782,7 @@ page_cur_open_on_rnd_user_rec(
buf_block_t* block, /*!< in: page */
page_cur_t* cursor) /*!< out: page cursor */
{
- ulint rnd;
- ulint n_recs = page_get_n_recs(buf_block_get_frame(block));
+ const ulint n_recs = page_get_n_recs(block->frame);
page_cur_set_before_first(block, cursor);
@@ -824,11 +791,9 @@ page_cur_open_on_rnd_user_rec(
return;
}
- rnd = (ulint) (page_cur_lcg_prng() % n_recs);
-
- do {
- page_cur_move_to_next(cursor);
- } while (rnd--);
+ cursor->rec = page_rec_get_nth(block->frame,
+ static_cast<ulint>
+ (ut_rnd_gen() % n_recs) + 1);
}
/** Write a redo log record of inserting a record into an index page.
@@ -2426,18 +2391,17 @@ page_cur_delete_rec(
#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. */
void
-test_page_cur_lcg_prng(
-/*===================*/
+test_ut_rnd_gen(
int n) /*!< in: print first n numbers */
{
int i;
unsigned long long rnd;
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",
rnd,
rnd % 2,
diff --git a/storage/innobase/ut/ut0rnd.cc b/storage/innobase/ut/ut0rnd.cc
index 3c1e51536a1..dcf43b9d7e1 100644
--- a/storage/innobase/ut/ut0rnd.cc
+++ b/storage/innobase/ut/ut0rnd.cc
@@ -1,6 +1,7 @@
/*****************************************************************************
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
the terms of the GNU General Public License as published by the Free Software
@@ -25,6 +26,9 @@ Created 5/11/1994 Heikki Tuuri
#include "ut0rnd.h"
+/** Seed value of ut_rnd_gen() */
+uint64_t ut_rnd_current;
+
/** These random numbers are used in ut_find_prime */
/*@{*/
#define UT_RANDOM_1 1.0412321