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(tsd_t *tsd, ckh_t *ckh);
44 static void ckh_shrink(tsd_t *tsd, ckh_t *ckh);
46 /******************************************************************************/
49 * Search bucket for key and return the cell number if found; SIZE_T_MAX
52 JEMALLOC_INLINE_C 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_C size_t
71 ckh_isearch(ckh_t *ckh, const void *key)
73 size_t hashes[2], bucket, cell;
77 ckh->hash(key, hashes);
79 /* Search primary bucket. */
80 bucket = hashes[0] & ((ZU(1) << ckh->lg_curbuckets) - 1);
81 cell = ckh_bucket_search(ckh, bucket, key);
82 if (cell != SIZE_T_MAX)
85 /* Search secondary bucket. */
86 bucket = hashes[1] & ((ZU(1) << ckh->lg_curbuckets) - 1);
87 cell = ckh_bucket_search(ckh, bucket, key);
91 JEMALLOC_INLINE_C bool
92 ckh_try_bucket_insert(ckh_t *ckh, size_t bucket, const void *key,
99 * Cycle through the cells in the bucket, starting at a random position.
100 * The randomness avoids worst-case search overhead as buckets fill up.
102 offset = (unsigned)prng_lg_range_u64(&ckh->prng_state,
103 LG_CKH_BUCKET_CELLS);
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.
124 JEMALLOC_INLINE_C bool
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 hashes[2], 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 i = (unsigned)prng_lg_range_u64(&ckh->prng_state,
146 LG_CKH_BUCKET_CELLS);
147 cell = &ckh->tab[(bucket << LG_CKH_BUCKET_CELLS) + i];
148 assert(cell->key != NULL);
150 /* Swap cell->{key,data} and {key,data} (evict). */
151 tkey = cell->key; tdata = cell->data;
152 cell->key = key; cell->data = data;
153 key = tkey; data = tdata;
159 /* Find the alternate bucket for the evicted item. */
160 ckh->hash(key, hashes);
161 tbucket = hashes[1] & ((ZU(1) << ckh->lg_curbuckets) - 1);
162 if (tbucket == bucket) {
163 tbucket = hashes[0] & ((ZU(1) << ckh->lg_curbuckets)
166 * It may be that (tbucket == bucket) still, if the
167 * item's hashes both indicate this bucket. However,
168 * we are guaranteed to eventually escape this bucket
169 * during iteration, assuming pseudo-random item
170 * selection (true randomness would make infinite
171 * looping a remote possibility). The reason we can
172 * never get trapped forever is that there are two
175 * 1) This bucket == argbucket, so we will quickly
176 * detect an eviction cycle and terminate.
177 * 2) An item was evicted to this bucket from another,
178 * which means that at least one item in this bucket
179 * has hashes that indicate distinct buckets.
182 /* Check for a cycle. */
183 if (tbucket == argbucket) {
190 if (!ckh_try_bucket_insert(ckh, bucket, key, data))
195 JEMALLOC_INLINE_C bool
196 ckh_try_insert(ckh_t *ckh, void const**argkey, void const**argdata)
198 size_t hashes[2], bucket;
199 const void *key = *argkey;
200 const void *data = *argdata;
202 ckh->hash(key, hashes);
204 /* Try to insert in primary bucket. */
205 bucket = hashes[0] & ((ZU(1) << ckh->lg_curbuckets) - 1);
206 if (!ckh_try_bucket_insert(ckh, bucket, key, data))
209 /* Try to insert in secondary bucket. */
210 bucket = hashes[1] & ((ZU(1) << ckh->lg_curbuckets) - 1);
211 if (!ckh_try_bucket_insert(ckh, bucket, key, data))
215 * Try to find a place for this item via iterative eviction/relocation.
217 return (ckh_evict_reloc_insert(ckh, bucket, argkey, argdata));
221 * Try to rebuild the hash table from scratch by inserting all items from the
222 * old table into the new.
224 JEMALLOC_INLINE_C bool
225 ckh_rebuild(ckh_t *ckh, ckhc_t *aTab)
227 size_t count, i, nins;
228 const void *key, *data;
232 for (i = nins = 0; nins < count; i++) {
233 if (aTab[i].key != NULL) {
236 if (ckh_try_insert(ckh, &key, &data)) {
248 ckh_grow(tsd_t *tsd, ckh_t *ckh)
252 unsigned lg_prevbuckets, lg_curcells;
259 * It is possible (though unlikely, given well behaved hashes) that the
260 * table will have to be doubled more than once in order to create a
263 lg_prevbuckets = ckh->lg_curbuckets;
264 lg_curcells = ckh->lg_curbuckets + LG_CKH_BUCKET_CELLS;
269 usize = sa2u(sizeof(ckhc_t) << lg_curcells, CACHELINE);
270 if (unlikely(usize == 0 || usize > HUGE_MAXCLASS)) {
274 tab = (ckhc_t *)ipallocztm(tsd_tsdn(tsd), usize, CACHELINE,
275 true, NULL, true, arena_ichoose(tsd, NULL));
280 /* Swap in new table. */
284 ckh->lg_curbuckets = lg_curcells - LG_CKH_BUCKET_CELLS;
286 if (!ckh_rebuild(ckh, tab)) {
287 idalloctm(tsd_tsdn(tsd), tab, NULL, true, true);
291 /* Rebuilding failed, so back out partially rebuilt table. */
292 idalloctm(tsd_tsdn(tsd), ckh->tab, NULL, true, true);
294 ckh->lg_curbuckets = lg_prevbuckets;
303 ckh_shrink(tsd_t *tsd, ckh_t *ckh)
307 unsigned lg_prevbuckets, lg_curcells;
310 * It is possible (though unlikely, given well behaved hashes) that the
311 * table rebuild will fail.
313 lg_prevbuckets = ckh->lg_curbuckets;
314 lg_curcells = ckh->lg_curbuckets + LG_CKH_BUCKET_CELLS - 1;
315 usize = sa2u(sizeof(ckhc_t) << lg_curcells, CACHELINE);
316 if (unlikely(usize == 0 || usize > HUGE_MAXCLASS))
318 tab = (ckhc_t *)ipallocztm(tsd_tsdn(tsd), usize, CACHELINE, true, NULL,
319 true, arena_ichoose(tsd, NULL));
322 * An OOM error isn't worth propagating, since it doesn't
323 * prevent this or future operations from proceeding.
327 /* Swap in new table. */
331 ckh->lg_curbuckets = lg_curcells - LG_CKH_BUCKET_CELLS;
333 if (!ckh_rebuild(ckh, tab)) {
334 idalloctm(tsd_tsdn(tsd), tab, NULL, true, true);
341 /* Rebuilding failed, so back out partially rebuilt table. */
342 idalloctm(tsd_tsdn(tsd), ckh->tab, NULL, true, true);
344 ckh->lg_curbuckets = lg_prevbuckets;
351 ckh_new(tsd_t *tsd, ckh_t *ckh, size_t minitems, ckh_hash_t *hash,
352 ckh_keycomp_t *keycomp)
355 size_t mincells, usize;
356 unsigned lg_mincells;
358 assert(minitems > 0);
359 assert(hash != NULL);
360 assert(keycomp != NULL);
365 ckh->nshrinkfails = 0;
369 ckh->prng_state = 42; /* Value doesn't really matter. */
373 * Find the minimum power of 2 that is large enough to fit minitems
374 * entries. We are using (2+,2) cuckoo hashing, which has an expected
375 * maximum load factor of at least ~0.86, so 0.75 is a conservative load
376 * factor that will typically allow mincells items to fit without ever
379 assert(LG_CKH_BUCKET_CELLS > 0);
380 mincells = ((minitems + (3 - (minitems % 3))) / 3) << 2;
381 for (lg_mincells = LG_CKH_BUCKET_CELLS;
382 (ZU(1) << lg_mincells) < mincells;
385 ckh->lg_minbuckets = lg_mincells - LG_CKH_BUCKET_CELLS;
386 ckh->lg_curbuckets = lg_mincells - LG_CKH_BUCKET_CELLS;
388 ckh->keycomp = keycomp;
390 usize = sa2u(sizeof(ckhc_t) << lg_mincells, CACHELINE);
391 if (unlikely(usize == 0 || usize > HUGE_MAXCLASS)) {
395 ckh->tab = (ckhc_t *)ipallocztm(tsd_tsdn(tsd), usize, CACHELINE, true,
396 NULL, true, arena_ichoose(tsd, NULL));
397 if (ckh->tab == NULL) {
408 ckh_delete(tsd_t *tsd, ckh_t *ckh)
415 "%s(%p): ngrows: %"FMTu64", nshrinks: %"FMTu64","
416 " nshrinkfails: %"FMTu64", ninserts: %"FMTu64","
417 " nrelocs: %"FMTu64"\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);
425 idalloctm(tsd_tsdn(tsd), ckh->tab, NULL, true, true);
427 memset(ckh, JEMALLOC_FREE_JUNK, sizeof(ckh_t));
431 ckh_count(ckh_t *ckh)
440 ckh_iter(ckh_t *ckh, size_t *tabind, void **key, void **data)
444 for (i = *tabind, ncells = (ZU(1) << (ckh->lg_curbuckets +
445 LG_CKH_BUCKET_CELLS)); i < ncells; i++) {
446 if (ckh->tab[i].key != NULL) {
448 *key = (void *)ckh->tab[i].key;
450 *data = (void *)ckh->tab[i].data;
460 ckh_insert(tsd_t *tsd, ckh_t *ckh, const void *key, const void *data)
465 assert(ckh_search(ckh, key, NULL, NULL));
471 while (ckh_try_insert(ckh, &key, &data)) {
472 if (ckh_grow(tsd, ckh)) {
484 ckh_remove(tsd_t *tsd, ckh_t *ckh, const void *searchkey, void **key,
491 cell = ckh_isearch(ckh, searchkey);
492 if (cell != SIZE_T_MAX) {
494 *key = (void *)ckh->tab[cell].key;
496 *data = (void *)ckh->tab[cell].data;
497 ckh->tab[cell].key = NULL;
498 ckh->tab[cell].data = NULL; /* Not necessary. */
501 /* Try to halve the table if it is less than 1/4 full. */
502 if (ckh->count < (ZU(1) << (ckh->lg_curbuckets
503 + LG_CKH_BUCKET_CELLS - 2)) && ckh->lg_curbuckets
504 > ckh->lg_minbuckets) {
505 /* Ignore error due to OOM. */
506 ckh_shrink(tsd, ckh);
516 ckh_search(ckh_t *ckh, const void *searchkey, void **key, void **data)
522 cell = ckh_isearch(ckh, searchkey);
523 if (cell != SIZE_T_MAX) {
525 *key = (void *)ckh->tab[cell].key;
527 *data = (void *)ckh->tab[cell].data;
535 ckh_string_hash(const void *key, size_t r_hash[2])
538 hash(key, strlen((const char *)key), 0x94122f33U, r_hash);
542 ckh_string_keycomp(const void *k1, const void *k2)
548 return (strcmp((char *)k1, (char *)k2) ? false : true);
552 ckh_pointer_hash(const void *key, size_t r_hash[2])
559 assert(sizeof(u.v) == sizeof(u.i));
561 hash(&u.i, sizeof(u.i), 0xd983396eU, r_hash);
565 ckh_pointer_keycomp(const void *k1, const void *k2)
568 return ((k1 == k2) ? true : false);