Skip to content

Commit 5e7733b

Browse files
author
Mikael Ronstrom
committed
A minor change to MySQL's hash where calculation of hash can be done separately to avoid doing it under contended mutex locks
1 parent 7810f50 commit 5e7733b

3 files changed

Lines changed: 43 additions & 4 deletions

File tree

include/hash.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ extern "C" {
4545
#define hash_element my_hash_element
4646
#define hash_search my_hash_search
4747
#define hash_first my_hash_first
48+
#define hash_first_from_hash_value my_hash_first_from_hash_value
4849
#define hash_next my_hash_next
4950
#define hash_insert my_hash_insert
5051
#define hash_delete my_hash_delete
@@ -94,8 +95,16 @@ void my_hash_free(HASH *tree);
9495
void my_hash_reset(HASH *hash);
9596
uchar *my_hash_element(HASH *hash, ulong idx);
9697
uchar *my_hash_search(const HASH *info, const uchar *key, size_t length);
98+
uchar *my_hash_search_using_hash_value(const HASH *info, uint hash_value,
99+
const uchar *key, size_t length);
100+
uint my_calc_hash(const HASH *info, const uchar *key, size_t length);
97101
uchar *my_hash_first(const HASH *info, const uchar *key, size_t length,
98102
HASH_SEARCH_STATE *state);
103+
uchar *my_hash_first_from_hash_value(const HASH *info,
104+
uint hash_value,
105+
const uchar *key,
106+
size_t length,
107+
HASH_SEARCH_STATE *state);
99108
uchar *my_hash_next(const HASH *info, const uchar *key, size_t length,
100109
HASH_SEARCH_STATE *state);
101110
my_bool my_hash_insert(HASH *info, const uchar *data);

mysys/hash.c

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,20 @@ uchar* my_hash_search(const HASH *hash, const uchar *key, size_t length)
214214
return my_hash_first(hash, key, length, &state);
215215
}
216216

217+
uchar* my_hash_search_using_hash_value(const HASH *hash,
218+
uint hash_value,
219+
const uchar *key,
220+
size_t length)
221+
{
222+
HASH_SEARCH_STATE state;
223+
return my_hash_first_from_hash_value(hash, hash_value,
224+
key, length, &state);
225+
}
226+
227+
uint my_calc_hash(const HASH *hash, const uchar *key, size_t length)
228+
{
229+
return calc_hash(hash, key, length ? length : hash->key_length);
230+
}
217231
/*
218232
Search after a record based on a key
219233
@@ -223,15 +237,26 @@ uchar* my_hash_search(const HASH *hash, const uchar *key, size_t length)
223237

224238
uchar* my_hash_first(const HASH *hash, const uchar *key, size_t length,
225239
HASH_SEARCH_STATE *current_record)
240+
{
241+
return my_hash_first_from_hash_value(hash,
242+
calc_hash(hash, key, length ? length : hash->key_length),
243+
key, length, current_record);
244+
}
245+
246+
uchar* my_hash_first_from_hash_value(const HASH *hash,
247+
uint hash_value,
248+
const uchar *key,
249+
size_t length,
250+
HASH_SEARCH_STATE *current_record)
226251
{
227252
HASH_LINK *pos;
228253
uint flag,idx;
229-
DBUG_ENTER("my_hash_first");
254+
DBUG_ENTER("my_hash_first_from_hash_value");
230255

231256
flag=1;
232257
if (hash->records)
233258
{
234-
idx= my_hash_mask(calc_hash(hash, key, length ? length : hash->key_length),
259+
idx= my_hash_mask(hash_value,
235260
hash->blength, hash->records);
236261
do
237262
{

sql/sql_base.cc

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2513,6 +2513,7 @@ TABLE *open_table(THD *thd, TABLE_LIST *table_list, MEM_ROOT *mem_root,
25132513
char key[MAX_DBKEY_LENGTH];
25142514
uint key_length;
25152515
char *alias= table_list->alias;
2516+
uint hash_value;
25162517
HASH_SEARCH_STATE state;
25172518
DBUG_ENTER("open_table");
25182519

@@ -2702,6 +2703,7 @@ TABLE *open_table(THD *thd, TABLE_LIST *table_list, MEM_ROOT *mem_root,
27022703
on disk.
27032704
*/
27042705

2706+
hash_value= my_calc_hash(&open_cache, (uchar*) key, key_length);
27052707
VOID(pthread_mutex_lock(&LOCK_open));
27062708

27072709
/*
@@ -2744,8 +2746,11 @@ TABLE *open_table(THD *thd, TABLE_LIST *table_list, MEM_ROOT *mem_root,
27442746
an implicit "pending locks queue" - see
27452747
wait_for_locked_table_names for details.
27462748
*/
2747-
for (table= (TABLE*) hash_first(&open_cache, (uchar*) key, key_length,
2748-
&state);
2749+
for (table= (TABLE*) hash_first_from_hash_value(&open_cache,
2750+
hash_value,
2751+
(uchar*) key,
2752+
key_length,
2753+
&state);
27492754
table && table->in_use ;
27502755
table= (TABLE*) hash_next(&open_cache, (uchar*) key, key_length,
27512756
&state))

0 commit comments

Comments
 (0)