Skip to content

Commit 202553a

Browse files
author
Mikael Ronstrom
committed
WL#5137, Remove hash calculation from LOCK_open in open_table
2 parents ecb6228 + 1edf7e7 commit 202553a

File tree

3 files changed

+58
-11
lines changed

3 files changed

+58
-11
lines changed

include/hash.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ extern "C" {
3030
/* flags for hash_init */
3131
#define HASH_UNIQUE 1 /* hash_insert fails on duplicate key */
3232

33+
typedef uint my_hash_value_type;
3334
typedef uchar *(*my_hash_get_key)(const uchar *,size_t*,my_bool);
3435
typedef void (*my_hash_free_key)(void *);
3536

@@ -60,8 +61,18 @@ void my_hash_free(HASH *tree);
6061
void my_hash_reset(HASH *hash);
6162
uchar *my_hash_element(HASH *hash, ulong idx);
6263
uchar *my_hash_search(const HASH *info, const uchar *key, size_t length);
64+
uchar *my_hash_search_using_hash_value(const HASH *info,
65+
my_hash_value_type hash_value,
66+
const uchar *key, size_t length);
67+
my_hash_value_type my_calc_hash(const HASH *info,
68+
const uchar *key, size_t length);
6369
uchar *my_hash_first(const HASH *info, const uchar *key, size_t length,
6470
HASH_SEARCH_STATE *state);
71+
uchar *my_hash_first_from_hash_value(const HASH *info,
72+
my_hash_value_type hash_value,
73+
const uchar *key,
74+
size_t length,
75+
HASH_SEARCH_STATE *state);
6576
uchar *my_hash_next(const HASH *info, const uchar *key, size_t length,
6677
HASH_SEARCH_STATE *state);
6778
my_bool my_hash_insert(HASH *info, const uchar *data);

mysys/hash.c

Lines changed: 40 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -33,16 +33,18 @@ typedef struct st_hash_info {
3333
uchar *data; /* data for current entry */
3434
} HASH_LINK;
3535

36-
static uint my_hash_mask(size_t hashnr, size_t buffmax, size_t maxlength);
36+
static uint my_hash_mask(my_hash_value_type hashnr,
37+
size_t buffmax, size_t maxlength);
3738
static void movelink(HASH_LINK *array,uint pos,uint next_link,uint newlink);
3839
static int hashcmp(const HASH *hash, HASH_LINK *pos, const uchar *key,
3940
size_t length);
4041

41-
static uint calc_hash(const HASH *hash, const uchar *key, size_t length)
42+
static my_hash_value_type calc_hash(const HASH *hash,
43+
const uchar *key, size_t length)
4244
{
4345
ulong nr1=1, nr2=4;
4446
hash->charset->coll->hash_sort(hash->charset,(uchar*) key,length,&nr1,&nr2);
45-
return nr1;
47+
return (my_hash_value_type)nr1;
4648
}
4749

4850
/**
@@ -179,7 +181,8 @@ my_hash_key(const HASH *hash, const uchar *record, size_t *length,
179181

180182
/* Calculate pos according to keys */
181183

182-
static uint my_hash_mask(size_t hashnr, size_t buffmax, size_t maxlength)
184+
static uint my_hash_mask(my_hash_value_type hashnr, size_t buffmax,
185+
size_t maxlength)
183186
{
184187
if ((hashnr & (buffmax-1)) < maxlength) return (hashnr & (buffmax-1));
185188
return (hashnr & ((buffmax >> 1) -1));
@@ -200,7 +203,7 @@ static
200203
#if !defined(__USLC__) && !defined(__sgi)
201204
inline
202205
#endif
203-
unsigned int rec_hashnr(HASH *hash,const uchar *record)
206+
my_hash_value_type rec_hashnr(HASH *hash,const uchar *record)
204207
{
205208
size_t length;
206209
uchar *key= (uchar*) my_hash_key(hash, record, &length, 0);
@@ -214,6 +217,21 @@ uchar* my_hash_search(const HASH *hash, const uchar *key, size_t length)
214217
return my_hash_first(hash, key, length, &state);
215218
}
216219

220+
uchar* my_hash_search_using_hash_value(const HASH *hash,
221+
my_hash_value_type hash_value,
222+
const uchar *key,
223+
size_t length)
224+
{
225+
HASH_SEARCH_STATE state;
226+
return my_hash_first_from_hash_value(hash, hash_value,
227+
key, length, &state);
228+
}
229+
230+
my_hash_value_type my_calc_hash(const HASH *hash,
231+
const uchar *key, size_t length)
232+
{
233+
return calc_hash(hash, key, length ? length : hash->key_length);
234+
}
217235
/*
218236
Search after a record based on a key
219237
@@ -223,15 +241,26 @@ uchar* my_hash_search(const HASH *hash, const uchar *key, size_t length)
223241

224242
uchar* my_hash_first(const HASH *hash, const uchar *key, size_t length,
225243
HASH_SEARCH_STATE *current_record)
244+
{
245+
return my_hash_first_from_hash_value(hash,
246+
calc_hash(hash, key, length ? length : hash->key_length),
247+
key, length, current_record);
248+
}
249+
250+
uchar* my_hash_first_from_hash_value(const HASH *hash,
251+
my_hash_value_type hash_value,
252+
const uchar *key,
253+
size_t length,
254+
HASH_SEARCH_STATE *current_record)
226255
{
227256
HASH_LINK *pos;
228257
uint flag,idx;
229-
DBUG_ENTER("my_hash_first");
258+
DBUG_ENTER("my_hash_first_from_hash_value");
230259

231260
flag=1;
232261
if (hash->records)
233262
{
234-
idx= my_hash_mask(calc_hash(hash, key, length ? length : hash->key_length),
263+
idx= my_hash_mask(hash_value,
235264
hash->blength, hash->records);
236265
do
237266
{
@@ -331,7 +360,8 @@ static int hashcmp(const HASH *hash, HASH_LINK *pos, const uchar *key,
331360
my_bool my_hash_insert(HASH *info, const uchar *record)
332361
{
333362
int flag;
334-
size_t idx,halfbuff,hash_nr,first_index;
363+
size_t idx,halfbuff,first_index;
364+
my_hash_value_type hash_nr;
335365
uchar *UNINIT_VAR(ptr_to_rec),*UNINIT_VAR(ptr_to_rec2);
336366
HASH_LINK *data,*empty,*UNINIT_VAR(gpos),*UNINIT_VAR(gpos2),*pos;
337367

@@ -467,7 +497,8 @@ my_bool my_hash_insert(HASH *info, const uchar *record)
467497

468498
my_bool my_hash_delete(HASH *hash, uchar *record)
469499
{
470-
uint blength,pos2,pos_hashnr,lastpos_hashnr,idx,empty_index;
500+
uint blength,pos2,idx,empty_index;
501+
my_hash_value_type pos_hashnr, lastpos_hashnr;
471502
HASH_LINK *data,*lastpos,*gpos,*pos,*pos3,*empty;
472503
DBUG_ENTER("my_hash_delete");
473504
if (!hash->records)

sql/sql_base.cc

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2529,6 +2529,7 @@ TABLE *open_table(THD *thd, TABLE_LIST *table_list, MEM_ROOT *mem_root,
25292529
char key[MAX_DBKEY_LENGTH];
25302530
uint key_length;
25312531
char *alias= table_list->alias;
2532+
my_hash_value_type hash_value;
25322533
HASH_SEARCH_STATE state;
25332534
DBUG_ENTER("open_table");
25342535

@@ -2718,6 +2719,7 @@ TABLE *open_table(THD *thd, TABLE_LIST *table_list, MEM_ROOT *mem_root,
27182719
on disk.
27192720
*/
27202721

2722+
hash_value= my_calc_hash(&open_cache, (uchar*) key, key_length);
27212723
VOID(pthread_mutex_lock(&LOCK_open));
27222724

27232725
/*
@@ -2760,8 +2762,11 @@ TABLE *open_table(THD *thd, TABLE_LIST *table_list, MEM_ROOT *mem_root,
27602762
an implicit "pending locks queue" - see
27612763
wait_for_locked_table_names for details.
27622764
*/
2763-
for (table= (TABLE*) my_hash_first(&open_cache, (uchar*) key, key_length,
2764-
&state);
2765+
for (table= (TABLE*) my_hash_first_from_hash_value(&open_cache,
2766+
hash_value,
2767+
(uchar*) key,
2768+
key_length,
2769+
&state);
27652770
table && table->in_use ;
27662771
table= (TABLE*) my_hash_next(&open_cache, (uchar*) key, key_length,
27672772
&state))

0 commit comments

Comments
 (0)