Skip to content

Commit

Permalink
Merge pull request #14 from paulborile/wyhash-seed-fix2
Browse files Browse the repository at this point in the history
fixed wyhash seeding
  • Loading branch information
paulborile authored Feb 2, 2024
2 parents fc9e43c + 0ab6967 commit 49ed4ab
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 21 deletions.
6 changes: 6 additions & 0 deletions libfh/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# Changelog

## 1.0.1 Sept Jan 2024

- fixed seeding (was not working properly). Tests now show taht seeding works
- breaking change on the hashfun signature. Additional void * used to pass fh_t where seed/secret is kept


## 1.0.0 Sept 2023

- introduced buckets for collisions (size 8) heavily inspired by go map source
Expand Down
22 changes: 12 additions & 10 deletions libfh/fh.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,21 +22,23 @@
#include "wyhash.h"

// version
static char version[] = "1.0.0";
static char version[] = "1.0.1";

static void wyhash_hash_init(fh_t *fh)
{
make_secret(time(NULL), fh->seed);
fh->seed = time(NULL);
make_secret(fh->seed, fh->secret);
}

uint64_t fh_default_hash(char *key)
uint64_t fh_default_hash(void *data, char *key)
{
fh_t *fh = (fh_t *)data;
if (key[0] == '\0' )
{
// return fixed value on empty string
return 1;
}
uint64_t h = wyhash(key, strlen(key), 0, _wyp);
uint64_t h = wyhash(key, strlen(key), fh->seed, fh->secret);
return h;
}

Expand Down Expand Up @@ -372,7 +374,7 @@ int fh_insert(fh_t *fh, char *key, void *block)
// uint8_t mini_hash;
// MAKE_MINIHASH(i, mini_hash);

uint64_t h = fh->hash_function(key);
uint64_t h = fh->hash_function(fh, key);
uint8_t mini_hash;
MAKE_MINIHASH(h, mini_hash); // get the 8 most sign bits
i = h & (fh->h_dim -1);
Expand Down Expand Up @@ -528,7 +530,7 @@ int fh_del(fh_t *fh, char *key)
FH_KEY_CHECK(key);

// find bucket
uint64_t h = fh->hash_function(key);
uint64_t h = fh->hash_function(fh, key);
uint8_t mini_hash;
MAKE_MINIHASH(h, mini_hash); // get the 8 most sign bits
bckt_num = h & (fh->h_dim -1);
Expand All @@ -553,7 +555,7 @@ int fh_dellocked(fh_t *fh, char *key, int locked_bucket)
FH_CHECK(fh);
FH_KEY_CHECK(key);

uint64_t h = fh->hash_function(key);
uint64_t h = fh->hash_function(fh, key);
uint8_t mini_hash;
MAKE_MINIHASH(h, mini_hash); // get the 8 most sign bits

Expand Down Expand Up @@ -700,7 +702,7 @@ int fh_search(fh_t *fh, char *key, void *block, int block_size)
}

// find bucket
uint64_t h = fh->hash_function(key);
uint64_t h = fh->hash_function(fh, key);
uint8_t mini_hash;
MAKE_MINIHASH(h, mini_hash); // get the 8 most sign bits
i = h & (fh->h_dim -1);
Expand Down Expand Up @@ -763,7 +765,7 @@ void *fh_get(fh_t *fh, char *key, int *error)
void *opaque = NULL;

// find bucket
uint64_t h = fh->hash_function(key);
uint64_t h = fh->hash_function(fh, key);
uint8_t mini_hash;
MAKE_MINIHASH(h, mini_hash); // get the 8 most sign bits
i = h & (fh->h_dim -1);
Expand Down Expand Up @@ -810,7 +812,7 @@ void *fh_searchlock(fh_t *fh, char *key, int *slot, int *error)
}

// find bucket
uint64_t h = fh->hash_function(key);
uint64_t h = fh->hash_function(fh, key);
uint8_t mini_hash;
MAKE_MINIHASH(h, mini_hash); // get the 8 most sign bits
i = h & (fh->h_dim -1);
Expand Down
8 changes: 4 additions & 4 deletions libfh/fh.h
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ struct _f_hash {
fh_bucket *h_bucket;
};
typedef struct _f_hash f_hash;
typedef uint64_t (*fh_hash_fun)(char *key);
typedef uint64_t (*fh_hash_fun)(void *data, char *key);

// fh object
struct _fh_t {
Expand All @@ -110,7 +110,8 @@ struct _fh_t {
pthread_mutex_t *h_lock;
int n_lock;
f_hash *hash_table;
uint64_t seed[4]; // wyhash seed
uint64_t seed; // wyhash seed
uint64_t secret[4]; // wyhash secret
};
typedef struct _fh_t fh_t;

Expand Down Expand Up @@ -139,8 +140,7 @@ void *fh_searchlock(fh_t *fh, char *key, int *slot, int *error);
int fh_dellocked(fh_t *fh, char *key, int locked_slot);
int fh_releaselock(fh_t *fh, int slot);

uint64_t fh_default_hash(char *key);
// compute the hash size given initial dimension
uint64_t fh_default_hash(void *data, char *key);// compute the hash size given initial dimension
unsigned int fh_hash_size(unsigned int s);

// elements in enumeration
Expand Down
12 changes: 7 additions & 5 deletions libfh/test/BenchFH.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@
#include "fh.h"

static void generate_random_str(int seed, char *str, int min_len, int max_len);
uint64_t murmur64a_hash(char *key);
uint64_t murmur64a_hash(void *data, char *key);
/*
* oat hash (one at a time hash), Bob Jenkins, used by cfu hash and perl
*/
unsigned int fh_default_hash(char *key, int dim);
uint64_t fh_default_hash(void *data, char *key);


PICOBENCH_SUITE("BenchFH");
Expand Down Expand Up @@ -84,14 +84,15 @@ static void HashFunctionsDefaultX100(picobench::state &s)
unsigned int hash;
char key[65];
int i = 0;
fh_t fh;

generate_random_str(i, key, 32, 64);

for (auto _ : s)
{
for (i = 0; i<100; i++)
{
hash += fh_default_hash(key) & 127;
hash += fh_default_hash(&fh, key) & 127;
}
}
// just to avoid optimizer removing all code..
Expand All @@ -108,14 +109,15 @@ static void HashFunctionsMurmurX100(picobench::state &s)
unsigned int hash;
char key[65];
int i = 0;
fh_t fh;

generate_random_str(i, key, 32, 64);

for (auto _ : s)
{
for (i = 0; i<100; i++)
{
hash += murmur64a_hash(key) & 127;
hash += murmur64a_hash(&fh, key) & 127;
}
}
// just to avoid optimizer removing all code..
Expand Down Expand Up @@ -148,7 +150,7 @@ PICOBENCH(GenerateRandomString).label("GenerateRandomString").samples(10);

#define BIG_CONSTANT(x) (x ## LLU)

uint64_t murmur64a_hash(char *key)
uint64_t murmur64a_hash(void *unused, char *key)
{
const uint64_t m = BIG_CONSTANT(0xc6a4a7935bd1e995);
const int r = 47;
Expand Down
4 changes: 2 additions & 2 deletions libfh/test/TestFH.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ void dofree(void *data)

// worst hash function of the history
// just to check if it works, all inserts will result in collisions
uint64_t custom_hash(char *key)
uint64_t custom_hash(void *data, char *key)
{
return 42;
}
Expand Down Expand Up @@ -480,7 +480,7 @@ TEST(FH, test_attr_methods)
attribute = 0;
result = fh_getattr(fhash, FH_ATTR_COLLISION, &attribute);
EXPECT_EQ(result, 1);
EXPECT_EQ(attribute, 3);
EXPECT_GE(attribute, 0);

// Destroy hash table
fh_destroy(fhash);
Expand Down

0 comments on commit 49ed4ab

Please sign in to comment.