]>
git.saurik.com Git - redis.git/blob - deps/jemalloc/src/ckh.c
2 *******************************************************************************
3 * Implementation of (2^1+,2) cuckoo hashing, where 2^1+ indicates that each
4 * hash bucket contains 2^n cells, for n >= 1, and 2 indicates that two hash
5 * functions are employed. The original cuckoo hashing algorithm was described
8 * Pagh, R., F.F. Rodler (2004) Cuckoo Hashing. Journal of Algorithms
11 * Generalization of cuckoo hashing was discussed in:
13 * Erlingsson, U., M. Manasse, F. McSherry (2006) A cool and practical
14 * alternative to traditional hash tables. In Proceedings of the 7th
15 * Workshop on Distributed Data and Structures (WDAS'06), Santa Clara, CA,
18 * This implementation uses precisely two hash functions because that is the
19 * fewest that can work, and supporting multiple hashes is an implementation
20 * burden. Here is a reproduction of Figure 1 from Erlingsson et al. (2006)
21 * that shows approximate expected maximum load factors for various
25 * #hashes | 1 | 2 | 4 | 8 |
26 * --------+-------+-------+-------+-------+
27 * 1 | 0.006 | 0.006 | 0.03 | 0.12 |
28 * 2 | 0.49 | 0.86 |>0.93< |>0.96< |
29 * 3 | 0.91 | 0.97 | 0.98 | 0.999 |
30 * 4 | 0.97 | 0.99 | 0.999 | |
32 * The number of cells per bucket is chosen such that a bucket fits in one cache
33 * line. So, on 32- and 64-bit systems, we use (8,2) and (4,2) cuckoo hashing,
36 ******************************************************************************/
37 #define JEMALLOC_CKH_C_
38 #include "jemalloc/internal/jemalloc_internal.h"
40 /******************************************************************************/
41 /* Function prototypes for non-inline static functions. */
43 static bool ckh_grow(ckh_t
*ckh
);
44 static void ckh_shrink(ckh_t
*ckh
);
46 /******************************************************************************/
49 * Search bucket for key and return the cell number if found; SIZE_T_MAX
52 JEMALLOC_INLINE
size_t
53 ckh_bucket_search(ckh_t
*ckh
, size_t bucket
, const void *key
)
58 for (i
= 0; i
< (ZU(1) << LG_CKH_BUCKET_CELLS
); i
++) {
59 cell
= &ckh
->tab
[(bucket
<< LG_CKH_BUCKET_CELLS
) + i
];
60 if (cell
->key
!= NULL
&& ckh
->keycomp(key
, cell
->key
))
61 return ((bucket
<< LG_CKH_BUCKET_CELLS
) + i
);
68 * Search table for key and return cell number if found; SIZE_T_MAX otherwise.
70 JEMALLOC_INLINE
size_t
71 ckh_isearch(ckh_t
*ckh
, const void *key
)
73 size_t hash1
, hash2
, bucket
, cell
;
76 dassert(ckh
->magic
== CKH_MAGIC
);
78 ckh
->hash(key
, ckh
->lg_curbuckets
, &hash1
, &hash2
);
80 /* Search primary bucket. */
81 bucket
= hash1
& ((ZU(1) << ckh
->lg_curbuckets
) - 1);
82 cell
= ckh_bucket_search(ckh
, bucket
, key
);
83 if (cell
!= SIZE_T_MAX
)
86 /* Search secondary bucket. */
87 bucket
= hash2
& ((ZU(1) << ckh
->lg_curbuckets
) - 1);
88 cell
= ckh_bucket_search(ckh
, bucket
, key
);
93 ckh_try_bucket_insert(ckh_t
*ckh
, size_t bucket
, const void *key
,
100 * Cycle through the cells in the bucket, starting at a random position.
101 * The randomness avoids worst-case search overhead as buckets fill up.
103 prn32(offset
, LG_CKH_BUCKET_CELLS
, ckh
->prn_state
, CKH_A
, CKH_C
);
104 for (i
= 0; i
< (ZU(1) << LG_CKH_BUCKET_CELLS
); i
++) {
105 cell
= &ckh
->tab
[(bucket
<< LG_CKH_BUCKET_CELLS
) +
106 ((i
+ offset
) & ((ZU(1) << LG_CKH_BUCKET_CELLS
) - 1))];
107 if (cell
->key
== NULL
) {
119 * No space is available in bucket. Randomly evict an item, then try to find an
120 * alternate location for that item. Iteratively repeat this
121 * eviction/relocation procedure until either success or detection of an
122 * eviction/relocation bucket cycle.
125 ckh_evict_reloc_insert(ckh_t
*ckh
, size_t argbucket
, void const **argkey
,
126 void const **argdata
)
128 const void *key
, *data
, *tkey
, *tdata
;
130 size_t hash1
, hash2
, bucket
, tbucket
;
138 * Choose a random item within the bucket to evict. This is
139 * critical to correct function, because without (eventually)
140 * evicting all items within a bucket during iteration, it
141 * would be possible to get stuck in an infinite loop if there
142 * were an item for which both hashes indicated the same
145 prn32(i
, LG_CKH_BUCKET_CELLS
, ckh
->prn_state
, CKH_A
, CKH_C
);
146 cell
= &ckh
->tab
[(bucket
<< LG_CKH_BUCKET_CELLS
) + i
];
147 assert(cell
->key
!= NULL
);
149 /* Swap cell->{key,data} and {key,data} (evict). */
150 tkey
= cell
->key
; tdata
= cell
->data
;
151 cell
->key
= key
; cell
->data
= data
;
152 key
= tkey
; data
= tdata
;
158 /* Find the alternate bucket for the evicted item. */
159 ckh
->hash(key
, ckh
->lg_curbuckets
, &hash1
, &hash2
);
160 tbucket
= hash2
& ((ZU(1) << ckh
->lg_curbuckets
) - 1);
161 if (tbucket
== bucket
) {
162 tbucket
= hash1
& ((ZU(1) << ckh
->lg_curbuckets
) - 1);
164 * It may be that (tbucket == bucket) still, if the
165 * item's hashes both indicate this bucket. However,
166 * we are guaranteed to eventually escape this bucket
167 * during iteration, assuming pseudo-random item
168 * selection (true randomness would make infinite
169 * looping a remote possibility). The reason we can
170 * never get trapped forever is that there are two
173 * 1) This bucket == argbucket, so we will quickly
174 * detect an eviction cycle and terminate.
175 * 2) An item was evicted to this bucket from another,
176 * which means that at least one item in this bucket
177 * has hashes that indicate distinct buckets.
180 /* Check for a cycle. */
181 if (tbucket
== argbucket
) {
188 if (ckh_try_bucket_insert(ckh
, bucket
, key
, data
) == false)
194 ckh_try_insert(ckh_t
*ckh
, void const**argkey
, void const**argdata
)
196 size_t hash1
, hash2
, bucket
;
197 const void *key
= *argkey
;
198 const void *data
= *argdata
;
200 ckh
->hash(key
, ckh
->lg_curbuckets
, &hash1
, &hash2
);
202 /* Try to insert in primary bucket. */
203 bucket
= hash1
& ((ZU(1) << ckh
->lg_curbuckets
) - 1);
204 if (ckh_try_bucket_insert(ckh
, bucket
, key
, data
) == false)
207 /* Try to insert in secondary bucket. */
208 bucket
= hash2
& ((ZU(1) << ckh
->lg_curbuckets
) - 1);
209 if (ckh_try_bucket_insert(ckh
, bucket
, key
, data
) == false)
213 * Try to find a place for this item via iterative eviction/relocation.
215 return (ckh_evict_reloc_insert(ckh
, bucket
, argkey
, argdata
));
219 * Try to rebuild the hash table from scratch by inserting all items from the
220 * old table into the new.
223 ckh_rebuild(ckh_t
*ckh
, ckhc_t
*aTab
)
225 size_t count
, i
, nins
;
226 const void *key
, *data
;
230 for (i
= nins
= 0; nins
< count
; i
++) {
231 if (aTab
[i
].key
!= NULL
) {
234 if (ckh_try_insert(ckh
, &key
, &data
)) {
251 unsigned lg_prevbuckets
;
258 * It is possible (though unlikely, given well behaved hashes) that the
259 * table will have to be doubled more than once in order to create a
262 lg_prevbuckets
= ckh
->lg_curbuckets
;
263 lg_curcells
= ckh
->lg_curbuckets
+ LG_CKH_BUCKET_CELLS
;
268 usize
= sa2u(sizeof(ckhc_t
) << lg_curcells
, CACHELINE
, NULL
);
273 tab
= (ckhc_t
*)ipalloc(usize
, CACHELINE
, true);
278 /* Swap in new table. */
282 ckh
->lg_curbuckets
= lg_curcells
- LG_CKH_BUCKET_CELLS
;
284 if (ckh_rebuild(ckh
, tab
) == false) {
289 /* Rebuilding failed, so back out partially rebuilt table. */
292 ckh
->lg_curbuckets
= lg_prevbuckets
;
301 ckh_shrink(ckh_t
*ckh
)
304 size_t lg_curcells
, usize
;
305 unsigned lg_prevbuckets
;
308 * It is possible (though unlikely, given well behaved hashes) that the
309 * table rebuild will fail.
311 lg_prevbuckets
= ckh
->lg_curbuckets
;
312 lg_curcells
= ckh
->lg_curbuckets
+ LG_CKH_BUCKET_CELLS
- 1;
313 usize
= sa2u(sizeof(ckhc_t
) << lg_curcells
, CACHELINE
, NULL
);
316 tab
= (ckhc_t
*)ipalloc(usize
, CACHELINE
, true);
319 * An OOM error isn't worth propagating, since it doesn't
320 * prevent this or future operations from proceeding.
324 /* Swap in new table. */
328 ckh
->lg_curbuckets
= lg_curcells
- LG_CKH_BUCKET_CELLS
;
330 if (ckh_rebuild(ckh
, tab
) == false) {
338 /* Rebuilding failed, so back out partially rebuilt table. */
341 ckh
->lg_curbuckets
= lg_prevbuckets
;
348 ckh_new(ckh_t
*ckh
, size_t minitems
, ckh_hash_t
*hash
, ckh_keycomp_t
*keycomp
)
351 size_t mincells
, usize
;
352 unsigned lg_mincells
;
354 assert(minitems
> 0);
355 assert(hash
!= NULL
);
356 assert(keycomp
!= NULL
);
361 ckh
->nshrinkfails
= 0;
365 ckh
->prn_state
= 42; /* Value doesn't really matter. */
369 * Find the minimum power of 2 that is large enough to fit aBaseCount
370 * entries. We are using (2+,2) cuckoo hashing, which has an expected
371 * maximum load factor of at least ~0.86, so 0.75 is a conservative load
372 * factor that will typically allow 2^aLgMinItems to fit without ever
375 assert(LG_CKH_BUCKET_CELLS
> 0);
376 mincells
= ((minitems
+ (3 - (minitems
% 3))) / 3) << 2;
377 for (lg_mincells
= LG_CKH_BUCKET_CELLS
;
378 (ZU(1) << lg_mincells
) < mincells
;
381 ckh
->lg_minbuckets
= lg_mincells
- LG_CKH_BUCKET_CELLS
;
382 ckh
->lg_curbuckets
= lg_mincells
- LG_CKH_BUCKET_CELLS
;
384 ckh
->keycomp
= keycomp
;
386 usize
= sa2u(sizeof(ckhc_t
) << lg_mincells
, CACHELINE
, NULL
);
391 ckh
->tab
= (ckhc_t
*)ipalloc(usize
, CACHELINE
, true);
392 if (ckh
->tab
== NULL
) {
397 #ifdef JEMALLOC_DEBUG
398 ckh
->magic
= CKH_MAGIC
;
407 ckh_delete(ckh_t
*ckh
)
411 dassert(ckh
->magic
== CKH_MAGIC
);
415 "%s(%p): ngrows: %"PRIu64
", nshrinks: %"PRIu64
","
416 " nshrinkfails: %"PRIu64
", ninserts: %"PRIu64
","
417 " nrelocs: %"PRIu64
"\n", __func__
, ckh
,
418 (unsigned long long)ckh
->ngrows
,
419 (unsigned long long)ckh
->nshrinks
,
420 (unsigned long long)ckh
->nshrinkfails
,
421 (unsigned long long)ckh
->ninserts
,
422 (unsigned long long)ckh
->nrelocs
);
426 #ifdef JEMALLOC_DEBUG
427 memset(ckh
, 0x5a, sizeof(ckh_t
));
432 ckh_count(ckh_t
*ckh
)
436 dassert(ckh
->magic
== CKH_MAGIC
);
442 ckh_iter(ckh_t
*ckh
, size_t *tabind
, void **key
, void **data
)
446 for (i
= *tabind
, ncells
= (ZU(1) << (ckh
->lg_curbuckets
+
447 LG_CKH_BUCKET_CELLS
)); i
< ncells
; i
++) {
448 if (ckh
->tab
[i
].key
!= NULL
) {
450 *key
= (void *)ckh
->tab
[i
].key
;
452 *data
= (void *)ckh
->tab
[i
].data
;
462 ckh_insert(ckh_t
*ckh
, const void *key
, const void *data
)
467 dassert(ckh
->magic
== CKH_MAGIC
);
468 assert(ckh_search(ckh
, key
, NULL
, NULL
));
474 while (ckh_try_insert(ckh
, &key
, &data
)) {
487 ckh_remove(ckh_t
*ckh
, const void *searchkey
, void **key
, void **data
)
492 dassert(ckh
->magic
== CKH_MAGIC
);
494 cell
= ckh_isearch(ckh
, searchkey
);
495 if (cell
!= SIZE_T_MAX
) {
497 *key
= (void *)ckh
->tab
[cell
].key
;
499 *data
= (void *)ckh
->tab
[cell
].data
;
500 ckh
->tab
[cell
].key
= NULL
;
501 ckh
->tab
[cell
].data
= NULL
; /* Not necessary. */
504 /* Try to halve the table if it is less than 1/4 full. */
505 if (ckh
->count
< (ZU(1) << (ckh
->lg_curbuckets
506 + LG_CKH_BUCKET_CELLS
- 2)) && ckh
->lg_curbuckets
507 > ckh
->lg_minbuckets
) {
508 /* Ignore error due to OOM. */
519 ckh_search(ckh_t
*ckh
, const void *searchkey
, void **key
, void **data
)
524 dassert(ckh
->magic
== CKH_MAGIC
);
526 cell
= ckh_isearch(ckh
, searchkey
);
527 if (cell
!= SIZE_T_MAX
) {
529 *key
= (void *)ckh
->tab
[cell
].key
;
531 *data
= (void *)ckh
->tab
[cell
].data
;
539 ckh_string_hash(const void *key
, unsigned minbits
, size_t *hash1
, size_t *hash2
)
544 assert(minbits
<= 32 || (SIZEOF_PTR
== 8 && minbits
<= 64));
545 assert(hash1
!= NULL
);
546 assert(hash2
!= NULL
);
548 h
= hash(key
, strlen((const char *)key
), 0x94122f335b332aeaLLU
);
551 * Avoid doing multiple hashes, since a single hash provides
554 ret1
= h
& ZU(0xffffffffU
);
558 ret2
= hash(key
, strlen((const char *)key
),
559 0x8432a476666bbc13LLU
);
567 ckh_string_keycomp(const void *k1
, const void *k2
)
573 return (strcmp((char *)k1
, (char *)k2
) ? false : true);
577 ckh_pointer_hash(const void *key
, unsigned minbits
, size_t *hash1
,
587 assert(minbits
<= 32 || (SIZEOF_PTR
== 8 && minbits
<= 64));
588 assert(hash1
!= NULL
);
589 assert(hash2
!= NULL
);
591 assert(sizeof(u
.v
) == sizeof(u
.i
));
592 #if (LG_SIZEOF_PTR != LG_SIZEOF_INT)
596 h
= hash(&u
.i
, sizeof(u
.i
), 0xd983396e68886082LLU
);
599 * Avoid doing multiple hashes, since a single hash provides
602 ret1
= h
& ZU(0xffffffffU
);
605 assert(SIZEOF_PTR
== 8);
607 ret2
= hash(&u
.i
, sizeof(u
.i
), 0x5e2be9aff8709a5dLLU
);
615 ckh_pointer_keycomp(const void *k1
, const void *k2
)
618 return ((k1
== k2
) ? true : false);