summaryrefslogtreecommitdiff
path: root/erts/emulator/beam/hash.h
diff options
context:
space:
mode:
authorLukas Larsson <lukas@erlang.org>2019-08-09 09:04:46 +0200
committerLukas Larsson <lukas@erlang.org>2019-08-09 15:21:42 +0200
commit5ad0c86aa11af73950804cadc284b2242684a231 (patch)
tree6689ebf6d24cc3aad8b30e2fea65a81c79ff6fba /erts/emulator/beam/hash.h
parent0cf06f07be27de4219743df3098c8cf66b3848c2 (diff)
downloaderlang-5ad0c86aa11af73950804cadc284b2242684a231.tar.gz
erts: Change the default hash implementation to use fibonacci hash
Fibonacci hashing is a variant of multiplicative hashing that uses a constant based on the golden ratio.
Diffstat (limited to 'erts/emulator/beam/hash.h')
-rw-r--r--erts/emulator/beam/hash.h46
1 files changed, 38 insertions, 8 deletions
diff --git a/erts/emulator/beam/hash.h b/erts/emulator/beam/hash.h
index 38b401548a..cbd75f3025 100644
--- a/erts/emulator/beam/hash.h
+++ b/erts/emulator/beam/hash.h
@@ -18,16 +18,16 @@
* %CopyrightEnd%
*/
-/*
-** General hash functions
-**
-*/
+/**
+ * General hash functions
+ *
+ **/
#ifndef __HASH_H__
#define __HASH_H__
#include "sys.h"
-typedef unsigned long HashValue;
+typedef UWord HashValue;
typedef struct hash Hash;
typedef int (*HCMP_FUN)(void*, void*);
@@ -76,11 +76,10 @@ struct hash
int is_allocated; /* 0 iff hash structure is on stack or is static */
int meta_alloc_type; /* argument to pass to meta_alloc and meta_free */
char* name; /* Table name (static string, for debugging) */
- int size; /* Number of slots */
+ int shift; /* How much to shift the hash value */
+ int max_shift; /* Never shift more than this value */
int shrink_threshold;
int grow_threshold;
- int size_ix; /* Size index in size table */
- int min_size_ix; /* Never shrink table smaller than this */
int nobjs; /* Number of objects in table */
HashBucket** bucket; /* Vector of bucket pointers (objects) */
};
@@ -99,4 +98,35 @@ void* hash_erase(Hash*, void*);
void* hash_remove(Hash*, void*);
void hash_foreach(Hash*, HFOREACH_FUN, void *);
+ERTS_GLB_INLINE Uint hash_get_slot(Hash *h, HashValue hv);
+
+#if ERTS_GLB_INLINE_INCL_FUNC_DEF
+
+ERTS_GLB_INLINE Uint
+hash_get_slot(Hash *h, HashValue hv)
+{
+ /* This slot mapping function uses fibonacci hashing in order to
+ * protect itself against a very bad hash function. This is not
+ * a hash function, so the user of hash.h should still spend time
+ * to figure out a good hash function for its data.
+ *
+ * See https://probablydance.com/2018/06/16/fibonacci-hashing-the-optimization-that-the-world-forgot-or-a-better-alternative-to-integer-modulo/
+ * for some thoughts and ideas about fibonacci hashing.
+ */
+
+ /* This is not strictly part of the fibonacci hashing algorithm
+ * but it does help to spread the values of the mapping function better.
+ */
+ hv ^= hv >> h->shift;
+#ifdef ARCH_64
+ /* 2^64 / 1.61803398875 = 11400714819323198485.... */
+ return (UWORD_CONSTANT(11400714819323198485) * hv) >> h->shift;
+#else
+ /* 2^32 / 1.61803398875 = 2654435769.... */
+ return (UWORD_CONSTANT(2654435769) * hv) >> h->shift;
+#endif
+}
+
+#endif /* ERTS_GLB_INLINE_INCL_FUNC_DEF */
+
#endif