Skip to content
Permalink
Browse files
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.
  • Loading branch information
montywi committed Nov 23, 2017
1 parent d8ada08 commit e69466d
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 33 deletions.
@@ -83,7 +83,6 @@ extern uchar *hp_search_next(HP_INFO *info, HP_KEYDEF *keyinfo,
const uchar *key, HASH_INFO *pos);
extern ulong hp_hashnr(HP_KEYDEF *keyinfo,const uchar *key);
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,
HASH_INFO *newlink);
extern int hp_rec_key_cmp(HP_KEYDEF *keydef,const uchar *rec1,
@@ -111,3 +110,22 @@ void init_heap_psi_keys();
#endif /* HAVE_PSI_INTERFACE */

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,
uint nextflag)
{
reg1 HASH_INFO *pos,*prev_ptr;
int flag;
uint old_nextflag;
HP_SHARE *share=info->s;
DBUG_ENTER("hp_search");
old_nextflag=nextflag;
flag=1;
prev_ptr=0;

if (share->records)
{
pos=hp_find_hash(&keyinfo->block, hp_mask(hp_hashnr(keyinfo, key),
share->blength, share->records));
ulong search_pos=
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
{
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,
}
}
}
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));
}

not_found:
my_errno=HA_ERR_KEY_NOT_FOUND;
if (nextflag == 2 && ! info->current_ptr)
{
@@ -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
next_link -> ... -> X -> pos

0 comments on commit e69466d

Please sign in to comment.