Skip to content

Commit

Permalink
hash_id: Change to xorshift*.
Browse files Browse the repository at this point in the history
katef's testing with words.sh found some suspicious timing, profiling
with callgrind showed there's still some kind of bad collision behavior
doing PHI64(a) ^ PHI64(b) with exactly two IDs. It's probably still a
bad idea to combine multiple Fibonacci hashes, even with xor-ing rather
than adding.

Changing to xorshift* (another fast, high quality hash function for
64-bit ints) immediately makes the issue go away, so do that.
  • Loading branch information
silentbicycle committed Jan 3, 2024
1 parent 7dd550a commit 7e9b517
Showing 1 changed file with 7 additions and 4 deletions.
11 changes: 7 additions & 4 deletions include/adt/hash.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,17 @@
#define FSM_PHI_32 0x9e3779b9UL
#define FSM_PHI_64 (uint64_t)0x9e3779b97f4a7c15UL

/* A suitable hash function for individual sequentially allocated
* identifiers. See Knuth 6.4, Fibonacci hashing. */

SUPPRESS_EXPECTED_UNSIGNED_INTEGER_OVERFLOW()
static __inline__ uint64_t
hash_id(unsigned id)
{
return FSM_PHI_64 * (uint64_t)(id + (unsigned)1);
/* xorshift* A1(12,25,27),
* from http://vigna.di.unimi.it/ftp/papers/xorshift.pdf */
uint64_t x = id + 1;
x ^= x >> 12; // a
x ^= x << 25; // b
x ^= x >> 27; // c
return x * 2685821657736338717LLU;
}

/* FNV-1a hash function, 32 and 64 bit versions. This is in the public
Expand Down

0 comments on commit 7e9b517

Please sign in to comment.