Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Refactor: moved the hash function from assoc.c to hash.c

  • Loading branch information...
commit 1a070652ba97045c73b5e0f5237d35ea017bb04b 1 parent d2b97e6
authored March 02, 2009
8  Makefile.am
@@ -3,7 +3,13 @@ pkginclude_HEADERS = protocol_binary.h
3 3
 
4 4
 BUILT_SOURCES= @DTRACE_HEADER@
5 5
 
6  
-memcached_SOURCES = memcached.c slabs.c slabs.h items.c items.h assoc.c assoc.h memcached.h thread.c stats.c stats.h daemon.c
  6
+memcached_SOURCES = memcached.c memcached.h \
  7
+                    hash.c hash.h \
  8
+                    slabs.c slabs.h \
  9
+                    items.c items.h \
  10
+                    assoc.c assoc.h \
  11
+                    thread.c daemon.c \
  12
+                    stats.c stats.h
7 13
 memcached_debug_SOURCES = $(memcached_SOURCES)
8 14
 memcached_CPPFLAGS = -DNDEBUG
9 15
 memcached_LDADD = @DTRACE_OBJ@
419  assoc.c
@@ -30,425 +30,6 @@
30 30
 static pthread_cond_t maintenance_cond = PTHREAD_COND_INITIALIZER;
31 31
 
32 32
 
33  
-/*
34  
- * Since the hash function does bit manipulation, it needs to know
35  
- * whether it's big or little-endian. ENDIAN_LITTLE and ENDIAN_BIG
36  
- * are set in the configure script.
37  
- */
38  
-#if ENDIAN_BIG == 1
39  
-# define HASH_LITTLE_ENDIAN 0
40  
-# define HASH_BIG_ENDIAN 1
41  
-#else
42  
-# if ENDIAN_LITTLE == 1
43  
-#  define HASH_LITTLE_ENDIAN 1
44  
-#  define HASH_BIG_ENDIAN 0
45  
-# else
46  
-#  define HASH_LITTLE_ENDIAN 0
47  
-#  define HASH_BIG_ENDIAN 0
48  
-# endif
49  
-#endif
50  
-
51  
-#define rot(x,k) (((x)<<(k)) ^ ((x)>>(32-(k))))
52  
-
53  
-/*
54  
--------------------------------------------------------------------------------
55  
-mix -- mix 3 32-bit values reversibly.
56  
-
57  
-This is reversible, so any information in (a,b,c) before mix() is
58  
-still in (a,b,c) after mix().
59  
-
60  
-If four pairs of (a,b,c) inputs are run through mix(), or through
61  
-mix() in reverse, there are at least 32 bits of the output that
62  
-are sometimes the same for one pair and different for another pair.
63  
-This was tested for:
64  
-* pairs that differed by one bit, by two bits, in any combination
65  
-  of top bits of (a,b,c), or in any combination of bottom bits of
66  
-  (a,b,c).
67  
-* "differ" is defined as +, -, ^, or ~^.  For + and -, I transformed
68  
-  the output delta to a Gray code (a^(a>>1)) so a string of 1's (as
69  
-  is commonly produced by subtraction) look like a single 1-bit
70  
-  difference.
71  
-* the base values were pseudorandom, all zero but one bit set, or
72  
-  all zero plus a counter that starts at zero.
73  
-
74  
-Some k values for my "a-=c; a^=rot(c,k); c+=b;" arrangement that
75  
-satisfy this are
76  
-    4  6  8 16 19  4
77  
-    9 15  3 18 27 15
78  
-   14  9  3  7 17  3
79  
-Well, "9 15 3 18 27 15" didn't quite get 32 bits diffing
80  
-for "differ" defined as + with a one-bit base and a two-bit delta.  I
81  
-used http://burtleburtle.net/bob/hash/avalanche.html to choose
82  
-the operations, constants, and arrangements of the variables.
83  
-
84  
-This does not achieve avalanche.  There are input bits of (a,b,c)
85  
-that fail to affect some output bits of (a,b,c), especially of a.  The
86  
-most thoroughly mixed value is c, but it doesn't really even achieve
87  
-avalanche in c.
88  
-
89  
-This allows some parallelism.  Read-after-writes are good at doubling
90  
-the number of bits affected, so the goal of mixing pulls in the opposite
91  
-direction as the goal of parallelism.  I did what I could.  Rotates
92  
-seem to cost as much as shifts on every machine I could lay my hands
93  
-on, and rotates are much kinder to the top and bottom bits, so I used
94  
-rotates.
95  
--------------------------------------------------------------------------------
96  
-*/
97  
-#define mix(a,b,c) \
98  
-{ \
99  
-  a -= c;  a ^= rot(c, 4);  c += b; \
100  
-  b -= a;  b ^= rot(a, 6);  a += c; \
101  
-  c -= b;  c ^= rot(b, 8);  b += a; \
102  
-  a -= c;  a ^= rot(c,16);  c += b; \
103  
-  b -= a;  b ^= rot(a,19);  a += c; \
104  
-  c -= b;  c ^= rot(b, 4);  b += a; \
105  
-}
106  
-
107  
-/*
108  
--------------------------------------------------------------------------------
109  
-final -- final mixing of 3 32-bit values (a,b,c) into c
110  
-
111  
-Pairs of (a,b,c) values differing in only a few bits will usually
112  
-produce values of c that look totally different.  This was tested for
113  
-* pairs that differed by one bit, by two bits, in any combination
114  
-  of top bits of (a,b,c), or in any combination of bottom bits of
115  
-  (a,b,c).
116  
-* "differ" is defined as +, -, ^, or ~^.  For + and -, I transformed
117  
-  the output delta to a Gray code (a^(a>>1)) so a string of 1's (as
118  
-  is commonly produced by subtraction) look like a single 1-bit
119  
-  difference.
120  
-* the base values were pseudorandom, all zero but one bit set, or
121  
-  all zero plus a counter that starts at zero.
122  
-
123  
-These constants passed:
124  
- 14 11 25 16 4 14 24
125  
- 12 14 25 16 4 14 24
126  
-and these came close:
127  
-  4  8 15 26 3 22 24
128  
- 10  8 15 26 3 22 24
129  
- 11  8 15 26 3 22 24
130  
--------------------------------------------------------------------------------
131  
-*/
132  
-#define final(a,b,c) \
133  
-{ \
134  
-  c ^= b; c -= rot(b,14); \
135  
-  a ^= c; a -= rot(c,11); \
136  
-  b ^= a; b -= rot(a,25); \
137  
-  c ^= b; c -= rot(b,16); \
138  
-  a ^= c; a -= rot(c,4);  \
139  
-  b ^= a; b -= rot(a,14); \
140  
-  c ^= b; c -= rot(b,24); \
141  
-}
142  
-
143  
-#if HASH_LITTLE_ENDIAN == 1
144  
-uint32_t hash(
145  
-  const void *key,       /* the key to hash */
146  
-  size_t      length,    /* length of the key */
147  
-  const uint32_t    initval)   /* initval */
148  
-{
149  
-  uint32_t a,b,c;                                          /* internal state */
150  
-  union { const void *ptr; size_t i; } u;     /* needed for Mac Powerbook G4 */
151  
-
152  
-  /* Set up the internal state */
153  
-  a = b = c = 0xdeadbeef + ((uint32_t)length) + initval;
154  
-
155  
-  u.ptr = key;
156  
-  if (HASH_LITTLE_ENDIAN && ((u.i & 0x3) == 0)) {
157  
-    const uint32_t *k = key;                           /* read 32-bit chunks */
158  
-#ifdef VALGRIND
159  
-    const uint8_t  *k8;
160  
-#endif /* ifdef VALGRIND */
161  
-
162  
-    /*------ all but last block: aligned reads and affect 32 bits of (a,b,c) */
163  
-    while (length > 12)
164  
-    {
165  
-      a += k[0];
166  
-      b += k[1];
167  
-      c += k[2];
168  
-      mix(a,b,c);
169  
-      length -= 12;
170  
-      k += 3;
171  
-    }
172  
-
173  
-    /*----------------------------- handle the last (probably partial) block */
174  
-    /*
175  
-     * "k[2]&0xffffff" actually reads beyond the end of the string, but
176  
-     * then masks off the part it's not allowed to read.  Because the
177  
-     * string is aligned, the masked-off tail is in the same word as the
178  
-     * rest of the string.  Every machine with memory protection I've seen
179  
-     * does it on word boundaries, so is OK with this.  But VALGRIND will
180  
-     * still catch it and complain.  The masking trick does make the hash
181  
-     * noticably faster for short strings (like English words).
182  
-     */
183  
-#ifndef VALGRIND
184  
-
185  
-    switch(length)
186  
-    {
187  
-    case 12: c+=k[2]; b+=k[1]; a+=k[0]; break;
188  
-    case 11: c+=k[2]&0xffffff; b+=k[1]; a+=k[0]; break;
189  
-    case 10: c+=k[2]&0xffff; b+=k[1]; a+=k[0]; break;
190  
-    case 9 : c+=k[2]&0xff; b+=k[1]; a+=k[0]; break;
191  
-    case 8 : b+=k[1]; a+=k[0]; break;
192  
-    case 7 : b+=k[1]&0xffffff; a+=k[0]; break;
193  
-    case 6 : b+=k[1]&0xffff; a+=k[0]; break;
194  
-    case 5 : b+=k[1]&0xff; a+=k[0]; break;
195  
-    case 4 : a+=k[0]; break;
196  
-    case 3 : a+=k[0]&0xffffff; break;
197  
-    case 2 : a+=k[0]&0xffff; break;
198  
-    case 1 : a+=k[0]&0xff; break;
199  
-    case 0 : return c;  /* zero length strings require no mixing */
200  
-    }
201  
-
202  
-#else /* make valgrind happy */
203  
-
204  
-    k8 = (const uint8_t *)k;
205  
-    switch(length)
206  
-    {
207  
-    case 12: c+=k[2]; b+=k[1]; a+=k[0]; break;
208  
-    case 11: c+=((uint32_t)k8[10])<<16;  /* fall through */
209  
-    case 10: c+=((uint32_t)k8[9])<<8;    /* fall through */
210  
-    case 9 : c+=k8[8];                   /* fall through */
211  
-    case 8 : b+=k[1]; a+=k[0]; break;
212  
-    case 7 : b+=((uint32_t)k8[6])<<16;   /* fall through */
213  
-    case 6 : b+=((uint32_t)k8[5])<<8;    /* fall through */
214  
-    case 5 : b+=k8[4];                   /* fall through */
215  
-    case 4 : a+=k[0]; break;
216  
-    case 3 : a+=((uint32_t)k8[2])<<16;   /* fall through */
217  
-    case 2 : a+=((uint32_t)k8[1])<<8;    /* fall through */
218  
-    case 1 : a+=k8[0]; break;
219  
-    case 0 : return c;  /* zero length strings require no mixing */
220  
-    }
221  
-
222  
-#endif /* !valgrind */
223  
-
224  
-  } else if (HASH_LITTLE_ENDIAN && ((u.i & 0x1) == 0)) {
225  
-    const uint16_t *k = key;                           /* read 16-bit chunks */
226  
-    const uint8_t  *k8;
227  
-
228  
-    /*--------------- all but last block: aligned reads and different mixing */
229  
-    while (length > 12)
230  
-    {
231  
-      a += k[0] + (((uint32_t)k[1])<<16);
232  
-      b += k[2] + (((uint32_t)k[3])<<16);
233  
-      c += k[4] + (((uint32_t)k[5])<<16);
234  
-      mix(a,b,c);
235  
-      length -= 12;
236  
-      k += 6;
237  
-    }
238  
-
239  
-    /*----------------------------- handle the last (probably partial) block */
240  
-    k8 = (const uint8_t *)k;
241  
-    switch(length)
242  
-    {
243  
-    case 12: c+=k[4]+(((uint32_t)k[5])<<16);
244  
-             b+=k[2]+(((uint32_t)k[3])<<16);
245  
-             a+=k[0]+(((uint32_t)k[1])<<16);
246  
-             break;
247  
-    case 11: c+=((uint32_t)k8[10])<<16;     /* @fallthrough */
248  
-    case 10: c+=k[4];                       /* @fallthrough@ */
249  
-             b+=k[2]+(((uint32_t)k[3])<<16);
250  
-             a+=k[0]+(((uint32_t)k[1])<<16);
251  
-             break;
252  
-    case 9 : c+=k8[8];                      /* @fallthrough */
253  
-    case 8 : b+=k[2]+(((uint32_t)k[3])<<16);
254  
-             a+=k[0]+(((uint32_t)k[1])<<16);
255  
-             break;
256  
-    case 7 : b+=((uint32_t)k8[6])<<16;      /* @fallthrough */
257  
-    case 6 : b+=k[2];
258  
-             a+=k[0]+(((uint32_t)k[1])<<16);
259  
-             break;
260  
-    case 5 : b+=k8[4];                      /* @fallthrough */
261  
-    case 4 : a+=k[0]+(((uint32_t)k[1])<<16);
262  
-             break;
263  
-    case 3 : a+=((uint32_t)k8[2])<<16;      /* @fallthrough */
264  
-    case 2 : a+=k[0];
265  
-             break;
266  
-    case 1 : a+=k8[0];
267  
-             break;
268  
-    case 0 : return c;  /* zero length strings require no mixing */
269  
-    }
270  
-
271  
-  } else {                        /* need to read the key one byte at a time */
272  
-    const uint8_t *k = key;
273  
-
274  
-    /*--------------- all but the last block: affect some 32 bits of (a,b,c) */
275  
-    while (length > 12)
276  
-    {
277  
-      a += k[0];
278  
-      a += ((uint32_t)k[1])<<8;
279  
-      a += ((uint32_t)k[2])<<16;
280  
-      a += ((uint32_t)k[3])<<24;
281  
-      b += k[4];
282  
-      b += ((uint32_t)k[5])<<8;
283  
-      b += ((uint32_t)k[6])<<16;
284  
-      b += ((uint32_t)k[7])<<24;
285  
-      c += k[8];
286  
-      c += ((uint32_t)k[9])<<8;
287  
-      c += ((uint32_t)k[10])<<16;
288  
-      c += ((uint32_t)k[11])<<24;
289  
-      mix(a,b,c);
290  
-      length -= 12;
291  
-      k += 12;
292  
-    }
293  
-
294  
-    /*-------------------------------- last block: affect all 32 bits of (c) */
295  
-    switch(length)                   /* all the case statements fall through */
296  
-    {
297  
-    case 12: c+=((uint32_t)k[11])<<24;
298  
-    case 11: c+=((uint32_t)k[10])<<16;
299  
-    case 10: c+=((uint32_t)k[9])<<8;
300  
-    case 9 : c+=k[8];
301  
-    case 8 : b+=((uint32_t)k[7])<<24;
302  
-    case 7 : b+=((uint32_t)k[6])<<16;
303  
-    case 6 : b+=((uint32_t)k[5])<<8;
304  
-    case 5 : b+=k[4];
305  
-    case 4 : a+=((uint32_t)k[3])<<24;
306  
-    case 3 : a+=((uint32_t)k[2])<<16;
307  
-    case 2 : a+=((uint32_t)k[1])<<8;
308  
-    case 1 : a+=k[0];
309  
-             break;
310  
-    case 0 : return c;  /* zero length strings require no mixing */
311  
-    }
312  
-  }
313  
-
314  
-  final(a,b,c);
315  
-  return c;             /* zero length strings require no mixing */
316  
-}
317  
-
318  
-#elif HASH_BIG_ENDIAN == 1
319  
-/*
320  
- * hashbig():
321  
- * This is the same as hashword() on big-endian machines.  It is different
322  
- * from hashlittle() on all machines.  hashbig() takes advantage of
323  
- * big-endian byte ordering.
324  
- */
325  
-uint32_t hash( const void *key, size_t length, const uint32_t initval)
326  
-{
327  
-  uint32_t a,b,c;
328  
-  union { const void *ptr; size_t i; } u; /* to cast key to (size_t) happily */
329  
-
330  
-  /* Set up the internal state */
331  
-  a = b = c = 0xdeadbeef + ((uint32_t)length) + initval;
332  
-
333  
-  u.ptr = key;
334  
-  if (HASH_BIG_ENDIAN && ((u.i & 0x3) == 0)) {
335  
-    const uint32_t *k = key;                           /* read 32-bit chunks */
336  
-#ifdef VALGRIND
337  
-    const uint8_t  *k8;
338  
-#endif /* ifdef VALGRIND */
339  
-
340  
-    /*------ all but last block: aligned reads and affect 32 bits of (a,b,c) */
341  
-    while (length > 12)
342  
-    {
343  
-      a += k[0];
344  
-      b += k[1];
345  
-      c += k[2];
346  
-      mix(a,b,c);
347  
-      length -= 12;
348  
-      k += 3;
349  
-    }
350  
-
351  
-    /*----------------------------- handle the last (probably partial) block */
352  
-    /*
353  
-     * "k[2]<<8" actually reads beyond the end of the string, but
354  
-     * then shifts out the part it's not allowed to read.  Because the
355  
-     * string is aligned, the illegal read is in the same word as the
356  
-     * rest of the string.  Every machine with memory protection I've seen
357  
-     * does it on word boundaries, so is OK with this.  But VALGRIND will
358  
-     * still catch it and complain.  The masking trick does make the hash
359  
-     * noticably faster for short strings (like English words).
360  
-     */
361  
-#ifndef VALGRIND
362  
-
363  
-    switch(length)
364  
-    {
365  
-    case 12: c+=k[2]; b+=k[1]; a+=k[0]; break;
366  
-    case 11: c+=k[2]&0xffffff00; b+=k[1]; a+=k[0]; break;
367  
-    case 10: c+=k[2]&0xffff0000; b+=k[1]; a+=k[0]; break;
368  
-    case 9 : c+=k[2]&0xff000000; b+=k[1]; a+=k[0]; break;
369  
-    case 8 : b+=k[1]; a+=k[0]; break;
370  
-    case 7 : b+=k[1]&0xffffff00; a+=k[0]; break;
371  
-    case 6 : b+=k[1]&0xffff0000; a+=k[0]; break;
372  
-    case 5 : b+=k[1]&0xff000000; a+=k[0]; break;
373  
-    case 4 : a+=k[0]; break;
374  
-    case 3 : a+=k[0]&0xffffff00; break;
375  
-    case 2 : a+=k[0]&0xffff0000; break;
376  
-    case 1 : a+=k[0]&0xff000000; break;
377  
-    case 0 : return c;              /* zero length strings require no mixing */
378  
-    }
379  
-
380  
-#else  /* make valgrind happy */
381  
-
382  
-    k8 = (const uint8_t *)k;
383  
-    switch(length)                   /* all the case statements fall through */
384  
-    {
385  
-    case 12: c+=k[2]; b+=k[1]; a+=k[0]; break;
386  
-    case 11: c+=((uint32_t)k8[10])<<8;  /* fall through */
387  
-    case 10: c+=((uint32_t)k8[9])<<16;  /* fall through */
388  
-    case 9 : c+=((uint32_t)k8[8])<<24;  /* fall through */
389  
-    case 8 : b+=k[1]; a+=k[0]; break;
390  
-    case 7 : b+=((uint32_t)k8[6])<<8;   /* fall through */
391  
-    case 6 : b+=((uint32_t)k8[5])<<16;  /* fall through */
392  
-    case 5 : b+=((uint32_t)k8[4])<<24;  /* fall through */
393  
-    case 4 : a+=k[0]; break;
394  
-    case 3 : a+=((uint32_t)k8[2])<<8;   /* fall through */
395  
-    case 2 : a+=((uint32_t)k8[1])<<16;  /* fall through */
396  
-    case 1 : a+=((uint32_t)k8[0])<<24; break;
397  
-    case 0 : return c;
398  
-    }
399  
-
400  
-#endif /* !VALGRIND */
401  
-
402  
-  } else {                        /* need to read the key one byte at a time */
403  
-    const uint8_t *k = key;
404  
-
405  
-    /*--------------- all but the last block: affect some 32 bits of (a,b,c) */
406  
-    while (length > 12)
407  
-    {
408  
-      a += ((uint32_t)k[0])<<24;
409  
-      a += ((uint32_t)k[1])<<16;
410  
-      a += ((uint32_t)k[2])<<8;
411  
-      a += ((uint32_t)k[3]);
412  
-      b += ((uint32_t)k[4])<<24;
413  
-      b += ((uint32_t)k[5])<<16;
414  
-      b += ((uint32_t)k[6])<<8;
415  
-      b += ((uint32_t)k[7]);
416  
-      c += ((uint32_t)k[8])<<24;
417  
-      c += ((uint32_t)k[9])<<16;
418  
-      c += ((uint32_t)k[10])<<8;
419  
-      c += ((uint32_t)k[11]);
420  
-      mix(a,b,c);
421  
-      length -= 12;
422  
-      k += 12;
423  
-    }
424  
-
425  
-    /*-------------------------------- last block: affect all 32 bits of (c) */
426  
-    switch(length)                   /* all the case statements fall through */
427  
-    {
428  
-    case 12: c+=k[11];
429  
-    case 11: c+=((uint32_t)k[10])<<8;
430  
-    case 10: c+=((uint32_t)k[9])<<16;
431  
-    case 9 : c+=((uint32_t)k[8])<<24;
432  
-    case 8 : b+=k[7];
433  
-    case 7 : b+=((uint32_t)k[6])<<8;
434  
-    case 6 : b+=((uint32_t)k[5])<<16;
435  
-    case 5 : b+=((uint32_t)k[4])<<24;
436  
-    case 4 : a+=k[3];
437  
-    case 3 : a+=((uint32_t)k[2])<<8;
438  
-    case 2 : a+=((uint32_t)k[1])<<16;
439  
-    case 1 : a+=((uint32_t)k[0])<<24;
440  
-             break;
441  
-    case 0 : return c;
442  
-    }
443  
-  }
444  
-
445  
-  final(a,b,c);
446  
-  return c;
447  
-}
448  
-#else /* HASH_XXX_ENDIAN == 1 */
449  
-#error Must define HASH_BIG_ENDIAN or HASH_LITTLE_ENDIAN
450  
-#endif /* HASH_XXX_ENDIAN == 1 */
451  
-
452 33
 typedef  unsigned long  int  ub4;   /* unsigned 4-byte quantities */
453 34
 typedef  unsigned       char ub1;   /* unsigned 1-byte quantities */
454 35
 
1  assoc.h
@@ -4,7 +4,6 @@ item *assoc_find(const char *key, const size_t nkey);
4 4
 int assoc_insert(item *item);
5 5
 void assoc_delete(const char *key, const size_t nkey);
6 6
 void do_assoc_move_next_bucket(void);
7  
-uint32_t hash( const void *key, size_t length, const uint32_t initval);
8 7
 int start_assoc_maintenance_thread(void);
9 8
 void stop_assoc_maintenance_thread(void);
10 9
 
431  hash.c
... ...
@@ -0,0 +1,431 @@
  1
+/* -*- Mode: C; tab-width: 4; c-basic-offset: 4; indent-tabs-mode: nil -*- */
  2
+/*
  3
+ * Hash table
  4
+ *
  5
+ * The hash function used here is by Bob Jenkins, 1996:
  6
+ *    <http://burtleburtle.net/bob/hash/doobs.html>
  7
+ *       "By Bob Jenkins, 1996.  bob_jenkins@burtleburtle.net.
  8
+ *       You may use this code any way you wish, private, educational,
  9
+ *       or commercial.  It's free."
  10
+ *
  11
+ */
  12
+#include "memcached.h"
  13
+
  14
+/*
  15
+ * Since the hash function does bit manipulation, it needs to know
  16
+ * whether it's big or little-endian. ENDIAN_LITTLE and ENDIAN_BIG
  17
+ * are set in the configure script.
  18
+ */
  19
+#if ENDIAN_BIG == 1
  20
+# define HASH_LITTLE_ENDIAN 0
  21
+# define HASH_BIG_ENDIAN 1
  22
+#else
  23
+# if ENDIAN_LITTLE == 1
  24
+#  define HASH_LITTLE_ENDIAN 1
  25
+#  define HASH_BIG_ENDIAN 0
  26
+# else
  27
+#  define HASH_LITTLE_ENDIAN 0
  28
+#  define HASH_BIG_ENDIAN 0
  29
+# endif
  30
+#endif
  31
+
  32
+#define rot(x,k) (((x)<<(k)) ^ ((x)>>(32-(k))))
  33
+
  34
+/*
  35
+-------------------------------------------------------------------------------
  36
+mix -- mix 3 32-bit values reversibly.
  37
+
  38
+This is reversible, so any information in (a,b,c) before mix() is
  39
+still in (a,b,c) after mix().
  40
+
  41
+If four pairs of (a,b,c) inputs are run through mix(), or through
  42
+mix() in reverse, there are at least 32 bits of the output that
  43
+are sometimes the same for one pair and different for another pair.
  44
+This was tested for:
  45
+* pairs that differed by one bit, by two bits, in any combination
  46
+  of top bits of (a,b,c), or in any combination of bottom bits of
  47
+  (a,b,c).
  48
+* "differ" is defined as +, -, ^, or ~^.  For + and -, I transformed
  49
+  the output delta to a Gray code (a^(a>>1)) so a string of 1's (as
  50
+  is commonly produced by subtraction) look like a single 1-bit
  51
+  difference.
  52
+* the base values were pseudorandom, all zero but one bit set, or
  53
+  all zero plus a counter that starts at zero.
  54
+
  55
+Some k values for my "a-=c; a^=rot(c,k); c+=b;" arrangement that
  56
+satisfy this are
  57
+    4  6  8 16 19  4
  58
+    9 15  3 18 27 15
  59
+   14  9  3  7 17  3
  60
+Well, "9 15 3 18 27 15" didn't quite get 32 bits diffing
  61
+for "differ" defined as + with a one-bit base and a two-bit delta.  I
  62
+used http://burtleburtle.net/bob/hash/avalanche.html to choose
  63
+the operations, constants, and arrangements of the variables.
  64
+
  65
+This does not achieve avalanche.  There are input bits of (a,b,c)
  66
+that fail to affect some output bits of (a,b,c), especially of a.  The
  67
+most thoroughly mixed value is c, but it doesn't really even achieve
  68
+avalanche in c.
  69
+
  70
+This allows some parallelism.  Read-after-writes are good at doubling
  71
+the number of bits affected, so the goal of mixing pulls in the opposite
  72
+direction as the goal of parallelism.  I did what I could.  Rotates
  73
+seem to cost as much as shifts on every machine I could lay my hands
  74
+on, and rotates are much kinder to the top and bottom bits, so I used
  75
+rotates.
  76
+-------------------------------------------------------------------------------
  77
+*/
  78
+#define mix(a,b,c) \
  79
+{ \
  80
+  a -= c;  a ^= rot(c, 4);  c += b; \
  81
+  b -= a;  b ^= rot(a, 6);  a += c; \
  82
+  c -= b;  c ^= rot(b, 8);  b += a; \
  83
+  a -= c;  a ^= rot(c,16);  c += b; \
  84
+  b -= a;  b ^= rot(a,19);  a += c; \
  85
+  c -= b;  c ^= rot(b, 4);  b += a; \
  86
+}
  87
+
  88
+/*
  89
+-------------------------------------------------------------------------------
  90
+final -- final mixing of 3 32-bit values (a,b,c) into c
  91
+
  92
+Pairs of (a,b,c) values differing in only a few bits will usually
  93
+produce values of c that look totally different.  This was tested for
  94
+* pairs that differed by one bit, by two bits, in any combination
  95
+  of top bits of (a,b,c), or in any combination of bottom bits of
  96
+  (a,b,c).
  97
+* "differ" is defined as +, -, ^, or ~^.  For + and -, I transformed
  98
+  the output delta to a Gray code (a^(a>>1)) so a string of 1's (as
  99
+  is commonly produced by subtraction) look like a single 1-bit
  100
+  difference.
  101
+* the base values were pseudorandom, all zero but one bit set, or
  102
+  all zero plus a counter that starts at zero.
  103
+
  104
+These constants passed:
  105
+ 14 11 25 16 4 14 24
  106
+ 12 14 25 16 4 14 24
  107
+and these came close:
  108
+  4  8 15 26 3 22 24
  109
+ 10  8 15 26 3 22 24
  110
+ 11  8 15 26 3 22 24
  111
+-------------------------------------------------------------------------------
  112
+*/
  113
+#define final(a,b,c) \
  114
+{ \
  115
+  c ^= b; c -= rot(b,14); \
  116
+  a ^= c; a -= rot(c,11); \
  117
+  b ^= a; b -= rot(a,25); \
  118
+  c ^= b; c -= rot(b,16); \
  119
+  a ^= c; a -= rot(c,4);  \
  120
+  b ^= a; b -= rot(a,14); \
  121
+  c ^= b; c -= rot(b,24); \
  122
+}
  123
+
  124
+#if HASH_LITTLE_ENDIAN == 1
  125
+uint32_t hash(
  126
+  const void *key,       /* the key to hash */
  127
+  size_t      length,    /* length of the key */
  128
+  const uint32_t    initval)   /* initval */
  129
+{
  130
+  uint32_t a,b,c;                                          /* internal state */
  131
+  union { const void *ptr; size_t i; } u;     /* needed for Mac Powerbook G4 */
  132
+
  133
+  /* Set up the internal state */
  134
+  a = b = c = 0xdeadbeef + ((uint32_t)length) + initval;
  135
+
  136
+  u.ptr = key;
  137
+  if (HASH_LITTLE_ENDIAN && ((u.i & 0x3) == 0)) {
  138
+    const uint32_t *k = key;                           /* read 32-bit chunks */
  139
+#ifdef VALGRIND
  140
+    const uint8_t  *k8;
  141
+#endif /* ifdef VALGRIND */
  142
+
  143
+    /*------ all but last block: aligned reads and affect 32 bits of (a,b,c) */
  144
+    while (length > 12)
  145
+    {
  146
+      a += k[0];
  147
+      b += k[1];
  148
+      c += k[2];
  149
+      mix(a,b,c);
  150
+      length -= 12;
  151
+      k += 3;
  152
+    }
  153
+
  154
+    /*----------------------------- handle the last (probably partial) block */
  155
+    /*
  156
+     * "k[2]&0xffffff" actually reads beyond the end of the string, but
  157
+     * then masks off the part it's not allowed to read.  Because the
  158
+     * string is aligned, the masked-off tail is in the same word as the
  159
+     * rest of the string.  Every machine with memory protection I've seen
  160
+     * does it on word boundaries, so is OK with this.  But VALGRIND will
  161
+     * still catch it and complain.  The masking trick does make the hash
  162
+     * noticably faster for short strings (like English words).
  163
+     */
  164
+#ifndef VALGRIND
  165
+
  166
+    switch(length)
  167
+    {
  168
+    case 12: c+=k[2]; b+=k[1]; a+=k[0]; break;
  169
+    case 11: c+=k[2]&0xffffff; b+=k[1]; a+=k[0]; break;
  170
+    case 10: c+=k[2]&0xffff; b+=k[1]; a+=k[0]; break;
  171
+    case 9 : c+=k[2]&0xff; b+=k[1]; a+=k[0]; break;
  172
+    case 8 : b+=k[1]; a+=k[0]; break;
  173
+    case 7 : b+=k[1]&0xffffff; a+=k[0]; break;
  174
+    case 6 : b+=k[1]&0xffff; a+=k[0]; break;
  175
+    case 5 : b+=k[1]&0xff; a+=k[0]; break;
  176
+    case 4 : a+=k[0]; break;
  177
+    case 3 : a+=k[0]&0xffffff; break;
  178
+    case 2 : a+=k[0]&0xffff; break;
  179
+    case 1 : a+=k[0]&0xff; break;
  180
+    case 0 : return c;  /* zero length strings require no mixing */
  181
+    }
  182
+
  183
+#else /* make valgrind happy */
  184
+
  185
+    k8 = (const uint8_t *)k;
  186
+    switch(length)
  187
+    {
  188
+    case 12: c+=k[2]; b+=k[1]; a+=k[0]; break;
  189
+    case 11: c+=((uint32_t)k8[10])<<16;  /* fall through */
  190
+    case 10: c+=((uint32_t)k8[9])<<8;    /* fall through */
  191
+    case 9 : c+=k8[8];                   /* fall through */
  192
+    case 8 : b+=k[1]; a+=k[0]; break;
  193
+    case 7 : b+=((uint32_t)k8[6])<<16;   /* fall through */
  194
+    case 6 : b+=((uint32_t)k8[5])<<8;    /* fall through */
  195
+    case 5 : b+=k8[4];                   /* fall through */
  196
+    case 4 : a+=k[0]; break;
  197
+    case 3 : a+=((uint32_t)k8[2])<<16;   /* fall through */
  198
+    case 2 : a+=((uint32_t)k8[1])<<8;    /* fall through */
  199
+    case 1 : a+=k8[0]; break;
  200
+    case 0 : return c;  /* zero length strings require no mixing */
  201
+    }
  202
+
  203
+#endif /* !valgrind */
  204
+
  205
+  } else if (HASH_LITTLE_ENDIAN && ((u.i & 0x1) == 0)) {
  206
+    const uint16_t *k = key;                           /* read 16-bit chunks */
  207
+    const uint8_t  *k8;
  208
+
  209
+    /*--------------- all but last block: aligned reads and different mixing */
  210
+    while (length > 12)
  211
+    {
  212
+      a += k[0] + (((uint32_t)k[1])<<16);
  213
+      b += k[2] + (((uint32_t)k[3])<<16);
  214
+      c += k[4] + (((uint32_t)k[5])<<16);
  215
+      mix(a,b,c);
  216
+      length -= 12;
  217
+      k += 6;
  218
+    }
  219
+
  220
+    /*----------------------------- handle the last (probably partial) block */
  221
+    k8 = (const uint8_t *)k;
  222
+    switch(length)
  223
+    {
  224
+    case 12: c+=k[4]+(((uint32_t)k[5])<<16);
  225
+             b+=k[2]+(((uint32_t)k[3])<<16);
  226
+             a+=k[0]+(((uint32_t)k[1])<<16);
  227
+             break;
  228
+    case 11: c+=((uint32_t)k8[10])<<16;     /* @fallthrough */
  229
+    case 10: c+=k[4];                       /* @fallthrough@ */
  230
+             b+=k[2]+(((uint32_t)k[3])<<16);
  231
+             a+=k[0]+(((uint32_t)k[1])<<16);
  232
+             break;
  233
+    case 9 : c+=k8[8];                      /* @fallthrough */
  234
+    case 8 : b+=k[2]+(((uint32_t)k[3])<<16);
  235
+             a+=k[0]+(((uint32_t)k[1])<<16);
  236
+             break;
  237
+    case 7 : b+=((uint32_t)k8[6])<<16;      /* @fallthrough */
  238
+    case 6 : b+=k[2];
  239
+             a+=k[0]+(((uint32_t)k[1])<<16);
  240
+             break;
  241
+    case 5 : b+=k8[4];                      /* @fallthrough */
  242
+    case 4 : a+=k[0]+(((uint32_t)k[1])<<16);
  243
+             break;
  244
+    case 3 : a+=((uint32_t)k8[2])<<16;      /* @fallthrough */
  245
+    case 2 : a+=k[0];
  246
+             break;
  247
+    case 1 : a+=k8[0];
  248
+             break;
  249
+    case 0 : return c;  /* zero length strings require no mixing */
  250
+    }
  251
+
  252
+  } else {                        /* need to read the key one byte at a time */
  253
+    const uint8_t *k = key;
  254
+
  255
+    /*--------------- all but the last block: affect some 32 bits of (a,b,c) */
  256
+    while (length > 12)
  257
+    {
  258
+      a += k[0];
  259
+      a += ((uint32_t)k[1])<<8;
  260
+      a += ((uint32_t)k[2])<<16;
  261
+      a += ((uint32_t)k[3])<<24;
  262
+      b += k[4];
  263
+      b += ((uint32_t)k[5])<<8;
  264
+      b += ((uint32_t)k[6])<<16;
  265
+      b += ((uint32_t)k[7])<<24;
  266
+      c += k[8];
  267
+      c += ((uint32_t)k[9])<<8;
  268
+      c += ((uint32_t)k[10])<<16;
  269
+      c += ((uint32_t)k[11])<<24;
  270
+      mix(a,b,c);
  271
+      length -= 12;
  272
+      k += 12;
  273
+    }
  274
+
  275
+    /*-------------------------------- last block: affect all 32 bits of (c) */
  276
+    switch(length)                   /* all the case statements fall through */
  277
+    {
  278
+    case 12: c+=((uint32_t)k[11])<<24;
  279
+    case 11: c+=((uint32_t)k[10])<<16;
  280
+    case 10: c+=((uint32_t)k[9])<<8;
  281
+    case 9 : c+=k[8];
  282
+    case 8 : b+=((uint32_t)k[7])<<24;
  283
+    case 7 : b+=((uint32_t)k[6])<<16;
  284
+    case 6 : b+=((uint32_t)k[5])<<8;
  285
+    case 5 : b+=k[4];
  286
+    case 4 : a+=((uint32_t)k[3])<<24;
  287
+    case 3 : a+=((uint32_t)k[2])<<16;
  288
+    case 2 : a+=((uint32_t)k[1])<<8;
  289
+    case 1 : a+=k[0];
  290
+             break;
  291
+    case 0 : return c;  /* zero length strings require no mixing */
  292
+    }
  293
+  }
  294
+
  295
+  final(a,b,c);
  296
+  return c;             /* zero length strings require no mixing */
  297
+}
  298
+
  299
+#elif HASH_BIG_ENDIAN == 1
  300
+/*
  301
+ * hashbig():
  302
+ * This is the same as hashword() on big-endian machines.  It is different
  303
+ * from hashlittle() on all machines.  hashbig() takes advantage of
  304
+ * big-endian byte ordering.
  305
+ */
  306
+uint32_t hash( const void *key, size_t length, const uint32_t initval)
  307
+{
  308
+  uint32_t a,b,c;
  309
+  union { const void *ptr; size_t i; } u; /* to cast key to (size_t) happily */
  310
+
  311
+  /* Set up the internal state */
  312
+  a = b = c = 0xdeadbeef + ((uint32_t)length) + initval;
  313
+
  314
+  u.ptr = key;
  315
+  if (HASH_BIG_ENDIAN && ((u.i & 0x3) == 0)) {
  316
+    const uint32_t *k = key;                           /* read 32-bit chunks */
  317
+#ifdef VALGRIND
  318
+    const uint8_t  *k8;
  319
+#endif /* ifdef VALGRIND */
  320
+
  321
+    /*------ all but last block: aligned reads and affect 32 bits of (a,b,c) */
  322
+    while (length > 12)
  323
+    {
  324
+      a += k[0];
  325
+      b += k[1];
  326
+      c += k[2];
  327
+      mix(a,b,c);
  328
+      length -= 12;
  329
+      k += 3;
  330
+    }
  331
+
  332
+    /*----------------------------- handle the last (probably partial) block */
  333
+    /*
  334
+     * "k[2]<<8" actually reads beyond the end of the string, but
  335
+     * then shifts out the part it's not allowed to read.  Because the
  336
+     * string is aligned, the illegal read is in the same word as the
  337
+     * rest of the string.  Every machine with memory protection I've seen
  338
+     * does it on word boundaries, so is OK with this.  But VALGRIND will
  339
+     * still catch it and complain.  The masking trick does make the hash
  340
+     * noticably faster for short strings (like English words).
  341
+     */
  342
+#ifndef VALGRIND
  343
+
  344
+    switch(length)
  345
+    {
  346
+    case 12: c+=k[2]; b+=k[1]; a+=k[0]; break;
  347
+    case 11: c+=k[2]&0xffffff00; b+=k[1]; a+=k[0]; break;
  348
+    case 10: c+=k[2]&0xffff0000; b+=k[1]; a+=k[0]; break;
  349
+    case 9 : c+=k[2]&0xff000000; b+=k[1]; a+=k[0]; break;
  350
+    case 8 : b+=k[1]; a+=k[0]; break;
  351
+    case 7 : b+=k[1]&0xffffff00; a+=k[0]; break;
  352
+    case 6 : b+=k[1]&0xffff0000; a+=k[0]; break;
  353
+    case 5 : b+=k[1]&0xff000000; a+=k[0]; break;
  354
+    case 4 : a+=k[0]; break;
  355
+    case 3 : a+=k[0]&0xffffff00; break;
  356
+    case 2 : a+=k[0]&0xffff0000; break;
  357
+    case 1 : a+=k[0]&0xff000000; break;
  358
+    case 0 : return c;              /* zero length strings require no mixing */
  359
+    }
  360
+
  361
+#else  /* make valgrind happy */
  362
+
  363
+    k8 = (const uint8_t *)k;
  364
+    switch(length)                   /* all the case statements fall through */
  365
+    {
  366
+    case 12: c+=k[2]; b+=k[1]; a+=k[0]; break;
  367
+    case 11: c+=((uint32_t)k8[10])<<8;  /* fall through */
  368
+    case 10: c+=((uint32_t)k8[9])<<16;  /* fall through */
  369
+    case 9 : c+=((uint32_t)k8[8])<<24;  /* fall through */
  370
+    case 8 : b+=k[1]; a+=k[0]; break;
  371
+    case 7 : b+=((uint32_t)k8[6])<<8;   /* fall through */
  372
+    case 6 : b+=((uint32_t)k8[5])<<16;  /* fall through */
  373
+    case 5 : b+=((uint32_t)k8[4])<<24;  /* fall through */
  374
+    case 4 : a+=k[0]; break;
  375
+    case 3 : a+=((uint32_t)k8[2])<<8;   /* fall through */
  376
+    case 2 : a+=((uint32_t)k8[1])<<16;  /* fall through */
  377
+    case 1 : a+=((uint32_t)k8[0])<<24; break;
  378
+    case 0 : return c;
  379
+    }
  380
+
  381
+#endif /* !VALGRIND */
  382
+
  383
+  } else {                        /* need to read the key one byte at a time */
  384
+    const uint8_t *k = key;
  385
+
  386
+    /*--------------- all but the last block: affect some 32 bits of (a,b,c) */
  387
+    while (length > 12)
  388
+    {
  389
+      a += ((uint32_t)k[0])<<24;
  390
+      a += ((uint32_t)k[1])<<16;
  391
+      a += ((uint32_t)k[2])<<8;
  392
+      a += ((uint32_t)k[3]);
  393
+      b += ((uint32_t)k[4])<<24;
  394
+      b += ((uint32_t)k[5])<<16;
  395
+      b += ((uint32_t)k[6])<<8;
  396
+      b += ((uint32_t)k[7]);
  397
+      c += ((uint32_t)k[8])<<24;
  398
+      c += ((uint32_t)k[9])<<16;
  399
+      c += ((uint32_t)k[10])<<8;
  400
+      c += ((uint32_t)k[11]);
  401
+      mix(a,b,c);
  402
+      length -= 12;
  403
+      k += 12;
  404
+    }
  405
+
  406
+    /*-------------------------------- last block: affect all 32 bits of (c) */
  407
+    switch(length)                   /* all the case statements fall through */
  408
+    {
  409
+    case 12: c+=k[11];
  410
+    case 11: c+=((uint32_t)k[10])<<8;
  411
+    case 10: c+=((uint32_t)k[9])<<16;
  412
+    case 9 : c+=((uint32_t)k[8])<<24;
  413
+    case 8 : b+=k[7];
  414
+    case 7 : b+=((uint32_t)k[6])<<8;
  415
+    case 6 : b+=((uint32_t)k[5])<<16;
  416
+    case 5 : b+=((uint32_t)k[4])<<24;
  417
+    case 4 : a+=k[3];
  418
+    case 3 : a+=((uint32_t)k[2])<<8;
  419
+    case 2 : a+=((uint32_t)k[1])<<16;
  420
+    case 1 : a+=((uint32_t)k[0])<<24;
  421
+             break;
  422
+    case 0 : return c;
  423
+    }
  424
+  }
  425
+
  426
+  final(a,b,c);
  427
+  return c;
  428
+}
  429
+#else /* HASH_XXX_ENDIAN == 1 */
  430
+#error Must define HASH_BIG_ENDIAN or HASH_LITTLE_ENDIAN
  431
+#endif /* HASH_XXX_ENDIAN == 1 */
15  hash.h
... ...
@@ -0,0 +1,15 @@
  1
+#ifndef HASH_H
  2
+#define    HASH_H
  3
+
  4
+#ifdef    __cplusplus
  5
+extern "C" {
  6
+#endif
  7
+
  8
+uint32_t hash(const void *key, size_t length, const uint32_t initval);
  9
+
  10
+#ifdef    __cplusplus
  11
+}
  12
+#endif
  13
+
  14
+#endif    /* HASH_H */
  15
+
1  memcached.h
@@ -304,6 +304,7 @@ extern int daemonize(int nochdir, int noclose);
304 304
 #include "assoc.h"
305 305
 #include "items.h"
306 306
 #include "trace.h"
  307
+#include "hash.h"
307 308
 
308 309
 
309 310
 /*

0 notes on commit 1a07065

Please sign in to comment.
Something went wrong with that request. Please try again.