MDEV-35247: ut_hash_ulint() is a waste

ut_hash_ulint(): Remove. The exclusive OR before a modulus operation
does not serve any useful purpose; it is only obfuscating code and
wasting some CPU cycles.

Reviewed by: Debarun Banerjee
This commit is contained in:
Marko Mäkelä 2024-11-21 08:59:31 +02:00
parent a9b0a1c5d0
commit df3855a471
5 changed files with 5 additions and 44 deletions

View file

@ -1648,13 +1648,10 @@ public:
return 1 + latches + empty_slots + h;
}
private:
/** @return the hash value before any ELEMENTS_PER_LATCH padding */
static ulint hash(ulint fold, ulint n) { return ut_hash_ulint(fold, n); }
/** @return the index of an array element */
static ulint calc_hash(ulint fold, ulint n_cells)
static ulint calc_hash(ulint fold, ulint n_cells) noexcept
{
return pad(hash(fold, n_cells));
return pad(fold % n_cells);
}
public:
/** @return the latch covering a hash table chain */

View file

@ -135,8 +135,7 @@ struct hash_table_t
/** Free the hash table. */
void free() noexcept { ut_free(array); array= nullptr; }
ulint calc_hash(ulint fold) const noexcept
{ return ut_hash_ulint(fold, n_cells); }
ulint calc_hash(ulint fold) const noexcept { return fold % n_cells; }
hash_cell_t *cell_get(ulint fold) const noexcept
{ return &array[calc_hash(fold)]; }

View file

@ -717,13 +717,10 @@ public:
#endif
private:
/** @return the hash value before any ELEMENTS_PER_LATCH padding */
static ulint hash(ulint fold, ulint n) { return ut_hash_ulint(fold, n); }
/** @return the index of an array element */
static ulint calc_hash(ulint fold, ulint n_cells)
static ulint calc_hash(ulint fold, ulint n_cells) noexcept
{
return pad(hash(fold, n_cells));
return pad(fold % n_cells);
}
};

View file

@ -70,17 +70,6 @@ inline ulint ut_rnd_interval(ulint n)
return n > 1 ? static_cast<ulint>(ut_rnd_gen() % n) : 0;
}
/*******************************************************//**
The following function generates a hash value for a ulint integer
to a hash table of size table_size, which should be a prime or some
random number to work reliably.
@return hash value */
UNIV_INLINE
ulint
ut_hash_ulint(
/*==========*/
ulint key, /*!< in: value to be hashed */
ulint table_size); /*!< in: hash table size */
# if SIZEOF_SIZE_T < 8
inline size_t ut_fold_ull(uint64_t d) noexcept
{

View file

@ -27,27 +27,6 @@ Created 5/30/1994 Heikki Tuuri
#define UT_HASH_RANDOM_MASK 1463735687
#define UT_HASH_RANDOM_MASK2 1653893711
#ifndef UNIV_INNOCHECKSUM
/*******************************************************//**
The following function generates a hash value for a ulint integer
to a hash table of size table_size, which should be a prime
or some random number for the hash table to work reliably.
@return hash value */
UNIV_INLINE
ulint
ut_hash_ulint(
/*==========*/
ulint key, /*!< in: value to be hashed */
ulint table_size) /*!< in: hash table size */
{
ut_ad(table_size);
key = key ^ UT_HASH_RANDOM_MASK2;
return(key % table_size);
}
#endif /* !UNIV_INNOCHECKSUM */
/*************************************************************//**
Folds a pair of ulints.
@return folded value */