Commit e69466d0 authored by Monty's avatar Monty

Improve performance of heap tables

- make hp_mask an inline function (short and called 16 times)
- Simplify hp_search. Biggest benefit is for doing key lookup
  without a matching row. Matching rows may be a bit slower, but
  is offseted by having hp_mask inlined.
parent d8ada081
...@@ -83,7 +83,6 @@ extern uchar *hp_search_next(HP_INFO *info, HP_KEYDEF *keyinfo, ...@@ -83,7 +83,6 @@ extern uchar *hp_search_next(HP_INFO *info, HP_KEYDEF *keyinfo,
const uchar *key, HASH_INFO *pos); const uchar *key, HASH_INFO *pos);
extern ulong hp_hashnr(HP_KEYDEF *keyinfo,const uchar *key); extern ulong hp_hashnr(HP_KEYDEF *keyinfo,const uchar *key);
extern ulong hp_rec_hashnr(HP_KEYDEF *keyinfo,const uchar *rec); extern ulong hp_rec_hashnr(HP_KEYDEF *keyinfo,const uchar *rec);
extern ulong hp_mask(ulong hashnr,ulong buffmax,ulong maxlength);
extern void hp_movelink(HASH_INFO *pos,HASH_INFO *next_link, extern void hp_movelink(HASH_INFO *pos,HASH_INFO *next_link,
HASH_INFO *newlink); HASH_INFO *newlink);
extern int hp_rec_key_cmp(HP_KEYDEF *keydef,const uchar *rec1, extern int hp_rec_key_cmp(HP_KEYDEF *keydef,const uchar *rec1,
...@@ -111,3 +110,22 @@ void init_heap_psi_keys(); ...@@ -111,3 +110,22 @@ void init_heap_psi_keys();
#endif /* HAVE_PSI_INTERFACE */ #endif /* HAVE_PSI_INTERFACE */
C_MODE_END C_MODE_END
/*
Calculate position number for hash value.
SYNOPSIS
hp_mask()
hashnr Hash value
buffmax Value such that
2^(n-1) < maxlength <= 2^n = buffmax
maxlength
RETURN
Array index, in [0..maxlength)
*/
static inline ulong hp_mask(ulong hashnr, ulong buffmax, ulong maxlength)
{
if ((hashnr & (buffmax-1)) < maxlength) return (hashnr & (buffmax-1));
return (hashnr & ((buffmax >> 1) -1));
}
...@@ -100,18 +100,20 @@ uchar *hp_search(HP_INFO *info, HP_KEYDEF *keyinfo, const uchar *key, ...@@ -100,18 +100,20 @@ uchar *hp_search(HP_INFO *info, HP_KEYDEF *keyinfo, const uchar *key,
uint nextflag) uint nextflag)
{ {
reg1 HASH_INFO *pos,*prev_ptr; reg1 HASH_INFO *pos,*prev_ptr;
int flag;
uint old_nextflag; uint old_nextflag;
HP_SHARE *share=info->s; HP_SHARE *share=info->s;
DBUG_ENTER("hp_search"); DBUG_ENTER("hp_search");
old_nextflag=nextflag; old_nextflag=nextflag;
flag=1;
prev_ptr=0; prev_ptr=0;
if (share->records) if (share->records)
{ {
pos=hp_find_hash(&keyinfo->block, hp_mask(hp_hashnr(keyinfo, key), ulong search_pos=
share->blength, share->records)); hp_mask(hp_hashnr(keyinfo, key), share->blength, share->records);
pos=hp_find_hash(&keyinfo->block, search_pos);
if (search_pos !=
hp_mask(pos->hash_of_key, share->blength, share->records))
goto not_found; /* Wrong link */
do do
{ {
if (!hp_key_cmp(keyinfo, pos->ptr_to_rec, key)) if (!hp_key_cmp(keyinfo, pos->ptr_to_rec, key))
...@@ -142,17 +144,11 @@ uchar *hp_search(HP_INFO *info, HP_KEYDEF *keyinfo, const uchar *key, ...@@ -142,17 +144,11 @@ uchar *hp_search(HP_INFO *info, HP_KEYDEF *keyinfo, const uchar *key,
} }
} }
} }
if (flag)
{
flag=0; /* Reset flag */
if (hp_find_hash(&keyinfo->block,
hp_mask(pos->hash_of_key,
share->blength, share->records)) != pos)
break; /* Wrong link */
}
} }
while ((pos=pos->next_key)); while ((pos=pos->next_key));
} }
not_found:
my_errno=HA_ERR_KEY_NOT_FOUND; my_errno=HA_ERR_KEY_NOT_FOUND;
if (nextflag == 2 && ! info->current_ptr) if (nextflag == 2 && ! info->current_ptr)
{ {
...@@ -194,26 +190,6 @@ uchar *hp_search_next(HP_INFO *info, HP_KEYDEF *keyinfo, const uchar *key, ...@@ -194,26 +190,6 @@ uchar *hp_search_next(HP_INFO *info, HP_KEYDEF *keyinfo, const uchar *key,
} }
/*
Calculate position number for hash value.
SYNOPSIS
hp_mask()
hashnr Hash value
buffmax Value such that
2^(n-1) < maxlength <= 2^n = buffmax
maxlength
RETURN
Array index, in [0..maxlength)
*/
ulong hp_mask(ulong hashnr, ulong buffmax, ulong maxlength)
{
if ((hashnr & (buffmax-1)) < maxlength) return (hashnr & (buffmax-1));
return (hashnr & ((buffmax >> 1) -1));
}
/* /*
Change Change
next_link -> ... -> X -> pos next_link -> ... -> X -> pos
......
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