Commit db6d9322 authored by Bradley C. Kuszmaul's avatar Bradley C. Kuszmaul

Speed up toku_hash_rehash_everything by remembering the hash function

git-svn-id: file:///svn/tokudb@120 c7de825b-a66e-492c-adef-691d508d4ae1
parent 65c01f60
......@@ -38,13 +38,13 @@ pma-test: pma.o memory.o key.o ybt.o
pma.o: pma.h yerror.h pma-internal.h memory.h key.h ybt.h brttypes.h ../include/ydb-constants.h
ybt.o: ybt.h brttypes.h
ybt-test: ybt-test.o ybt.o memory.o
cachetable.o: cachetable.h
cachetable.o: cachetable.h hashfun.h
brt-test: ybt.o brt.o hashtable.o pma.o memory.o brt-serialize.o cachetable.o header-io.o ybt.o key.o
brt-test.o brt.o: brt.h hashtable.h pma.h brttypes.h cachetable.h
brt-serialize-test.o: pma.h yerror.h brt.h memory.h hashtable.h brttypes.h brt-internal.h
brt.o: brt.h mdict.h pma.h brttypes.h memory.h brt-internal.h cachetable.h hashtable.h
mdict.o: pma.h
hashtable.o: hashtable.h brttypes.h memory.h key.h yerror.h ../include/ydb-constants.h
hashtable.o: hashtable.h brttypes.h memory.h key.h yerror.h ../include/ydb-constants.h hashfun.h
memory.o: memory.h
hashtest: hashtable.o memory.o
brt-serialize.o: brt.h cachetable.h memory.h mdict.h pma.h brttypes.h brt-internal.h hashtable.h
......
......@@ -5,6 +5,9 @@ static inline unsigned int hash_key (const unsigned char *key, unsigned long key
unsigned int hash=0;
for (i=0; i<keylen; i++, key++) {
hash *= 16777619;
// GCC 4.1.2 -O2 and -O3 translates the following shifts back into the multiply shown on the line above here.
// So much for optimizing this multiplication...
//hash += (hash<<1) + (hash<<4) + (hash<<7) + (hash<<8) + (hash<<24);
hash ^= *key;
}
return hash;
......
......@@ -24,8 +24,8 @@ int toku_hashtable_create (HASHTABLE *h) {
return 0;
}
static void hash_find_internal (HASHTABLE tab, const unsigned char *key, ITEMLEN keylen, HASHELT *hashelt, HASHELT **prev_ptr) {
unsigned int h = hash_key (key, keylen) % tab->arraysize;
static void hash_find_internal (HASHTABLE tab, unsigned int hash, const unsigned char *key, ITEMLEN keylen, HASHELT *hashelt, HASHELT **prev_ptr) {
unsigned int h = hash % tab->arraysize;
HASHELT he;
HASHELT *prev = &tab->array[h];
for (he=*prev; he; prev=&he->next, he=*prev) {
......@@ -41,7 +41,7 @@ static void hash_find_internal (HASHTABLE tab, const unsigned char *key, ITEMLEN
int toku_hash_find (HASHTABLE tab, bytevec key, ITEMLEN keylen, bytevec *data, ITEMLEN *datalen) {
HASHELT he, *prev_ptr;
hash_find_internal(tab, key, keylen, &he, &prev_ptr);
hash_find_internal(tab, hash_key (key, keylen), key, keylen, &he, &prev_ptr);
if (he==0) {
return -1;
} else {
......@@ -59,7 +59,9 @@ int toku_hash_rehash_everything (HASHTABLE tab, int newarraysize) {
for (i=0; i<tab->arraysize; i++) {
HASHELT he;
while ((he=tab->array[i])!=0) {
unsigned int h = hash_key((unsigned char *)he->key, he->keylen)%newarraysize;
//unsigned int hk = hash_key((unsigned char *)he->key, he->keylen);
unsigned int h = he->hash%newarraysize;
//assert(he->hash==hk);
tab->array[i] = he->next;
he->next = newarray[h];
newarray[h] = he;
......@@ -75,10 +77,11 @@ int toku_hash_rehash_everything (HASHTABLE tab, int newarraysize) {
int toku_hash_insert (HASHTABLE tab, const void *key, ITEMLEN keylen, const void *val, ITEMLEN vallen)
{
unsigned int h = hash_key (key,keylen)%tab->arraysize;
unsigned int hk = hash_key (key,keylen);
unsigned int h = hk%tab->arraysize;
{
HASHELT he,*prev_ptr;
hash_find_internal(tab, key, keylen, &he, &prev_ptr);
hash_find_internal(tab, hk, key, keylen, &he, &prev_ptr);
if (he!=0) {
return BRT_ALREADY_THERE;
}
......@@ -90,6 +93,7 @@ int toku_hash_insert (HASHTABLE tab, const void *key, ITEMLEN keylen, const void
he->keylen = keylen;
he->val = memdup(val, vallen);
he->vallen = vallen;
he->hash = hk;
he->next = tab->array[h];
tab->array[h]=he;
tab->n_keys++;
......@@ -103,7 +107,7 @@ int toku_hash_insert (HASHTABLE tab, const void *key, ITEMLEN keylen, const void
int toku_hash_delete (HASHTABLE tab, const void *key, ITEMLEN keylen) {
HASHELT he, *prev_ptr;
//printf("%s:%d deleting %s (bucket %d)\n", __FILE__, __LINE__, key, hash_key(key,keylen)%tab->arraysize);
hash_find_internal(tab, key, keylen, &he, &prev_ptr);
hash_find_internal(tab, hash_key (key, keylen), key, keylen, &he, &prev_ptr);
if (he==0) return DB_NOTFOUND;
else {
//printf("%s:%d deleting %s %s\n", __FILE__, __LINE__, he->key, he->val);
......
......@@ -31,6 +31,7 @@ typedef struct hashelt *HASHELT;
struct hashelt {
char *key; ITEMLEN keylen; /* key is NULL for empty elements */
char *val; ITEMLEN vallen;
unsigned int hash;
HASHELT next;
};
......
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