|
34 | 34 |
|
35 | 35 | #include "lwan-private.h"
|
36 | 36 | #include "hash.h"
|
37 |
| -#include "murmur3.h" |
38 | 37 |
|
39 | 38 | struct hash_bucket {
|
40 | 39 | void **keys;
|
@@ -81,11 +80,12 @@ struct hash_entry {
|
81 | 80 | static_assert((MIN_BUCKETS & (MIN_BUCKETS - 1)) == 0,
|
82 | 81 | "Bucket size is power of 2");
|
83 | 82 |
|
| 83 | +static inline unsigned int hash_fnv1a_32(const void *keyptr); |
84 | 84 | static inline unsigned int hash_int_shift_mult(const void *keyptr);
|
85 | 85 | static inline unsigned int hash_int64_shift_mult(const void *keyptr);
|
86 | 86 |
|
87 | 87 | static unsigned int odd_constant = DEFAULT_ODD_CONSTANT;
|
88 |
| -static unsigned (*hash_str)(const void *key) = murmur3_simple; |
| 88 | +static unsigned (*hash_str)(const void *key) = hash_fnv1a_32; |
89 | 89 | static unsigned (*hash_int)(const void *key) = hash_int_shift_mult;
|
90 | 90 | static unsigned (*hash_int64)(const void *key) = hash_int64_shift_mult;
|
91 | 91 |
|
@@ -115,28 +115,21 @@ static bool resize_bucket(struct hash_bucket *bucket, unsigned int new_size)
|
115 | 115 | return false;
|
116 | 116 | }
|
117 | 117 |
|
| 118 | +static inline unsigned int hash_fnv1a_32(const void *keyptr) |
| 119 | +{ |
| 120 | + return fnv1a_32(keyptr, strlen(keyptr)); |
| 121 | +} |
| 122 | + |
118 | 123 | static inline unsigned int hash_int_shift_mult(const void *keyptr)
|
119 | 124 | {
|
120 |
| - /* http://www.concentric.net/~Ttwang/tech/inthash.htm */ |
121 | 125 | unsigned int key = (unsigned int)(long)keyptr;
|
122 |
| - unsigned int c2 = odd_constant; |
123 |
| - |
124 |
| - key = (key ^ 61) ^ (key >> 16); |
125 |
| - key += key << 3; |
126 |
| - key ^= key >> 4; |
127 |
| - key *= c2; |
128 |
| - key ^= key >> 15; |
129 |
| - return key; |
| 126 | + return fnv1a_32(&key, sizeof(key)); |
130 | 127 | }
|
131 | 128 |
|
132 | 129 | static inline unsigned int hash_int64_shift_mult(const void *keyptr)
|
133 | 130 | {
|
134 | 131 | const uint64_t key = (uint64_t)(uintptr_t)keyptr;
|
135 |
| - uint32_t key_low = (uint32_t)(key & 0xffffffff); |
136 |
| - uint32_t key_high = (uint32_t)(key >> 32); |
137 |
| - |
138 |
| - return hash_int_shift_mult((void *)(uintptr_t)key_low) ^ |
139 |
| - hash_int_shift_mult((void *)(uintptr_t)key_high); |
| 132 | + return fnv1a_32(&key, sizeof(key)); |
140 | 133 | }
|
141 | 134 |
|
142 | 135 | #if defined(LWAN_HAVE_BUILTIN_CPU_INIT) && defined(LWAN_HAVE_BUILTIN_IA32_CRC32)
|
@@ -200,14 +193,19 @@ static inline unsigned int hash_int64_crc32(const void *keyptr)
|
200 | 193 |
|
201 | 194 | #endif
|
202 | 195 |
|
| 196 | +uint64_t fnv1a_64_seed = 0xcbf29ce484222325ull; |
| 197 | +uint32_t fnv1a_32_seed = 0x811c9dc5u; |
| 198 | + |
203 | 199 | __attribute__((constructor(65535))) static void initialize_odd_constant(void)
|
204 | 200 | {
|
205 | 201 | /* This constant is randomized in order to mitigate the DDoS attack
|
206 | 202 | * described by Crosby and Wallach in UsenixSec2003. */
|
207 | 203 | if (lwan_getentropy(&odd_constant, sizeof(odd_constant), 0) < 0)
|
208 | 204 | odd_constant = DEFAULT_ODD_CONSTANT;
|
209 | 205 | odd_constant |= 1;
|
210 |
| - murmur3_set_seed(odd_constant); |
| 206 | + |
| 207 | + fnv1a_64_seed = fnv1a_64(&odd_constant, sizeof(odd_constant)); |
| 208 | + fnv1a_32_seed = fnv1a_32(&odd_constant, sizeof(odd_constant)); |
211 | 209 |
|
212 | 210 | #if defined(LWAN_HAVE_BUILTIN_CPU_INIT) && defined(LWAN_HAVE_BUILTIN_IA32_CRC32)
|
213 | 211 | __builtin_cpu_init();
|
|
0 commit comments