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_preamble.h"
40 #include "jemalloc/internal/ckh.h"
42 #include "jemalloc/internal/jemalloc_internal_includes.h"
44 #include "jemalloc/internal/assert.h"
45 #include "jemalloc/internal/hash.h"
46 #include "jemalloc/internal/malloc_io.h"
47 #include "jemalloc/internal/prng.h"
48 #include "jemalloc/internal/util.h"
50 /******************************************************************************/
51 /* Function prototypes for non-inline static functions. */
53 static bool ckh_grow(tsd_t *tsd, ckh_t *ckh);
54 static void ckh_shrink(tsd_t *tsd, ckh_t *ckh);
56 /******************************************************************************/
59 * Search bucket for key and return the cell number if found; SIZE_T_MAX
63 ckh_bucket_search(ckh_t *ckh, size_t bucket, const void *key) {
67 for (i = 0; i < (ZU(1) << LG_CKH_BUCKET_CELLS); i++) {
68 cell = &ckh->tab[(bucket << LG_CKH_BUCKET_CELLS) + i];
69 if (cell->key != NULL && ckh->keycomp(key, cell->key)) {
70 return (bucket << LG_CKH_BUCKET_CELLS) + i;
78 * Search table for key and return cell number if found; SIZE_T_MAX otherwise.
81 ckh_isearch(ckh_t *ckh, const void *key) {
82 size_t hashes[2], bucket, cell;
86 ckh->hash(key, hashes);
88 /* Search primary bucket. */
89 bucket = hashes[0] & ((ZU(1) << ckh->lg_curbuckets) - 1);
90 cell = ckh_bucket_search(ckh, bucket, key);
91 if (cell != SIZE_T_MAX) {
95 /* Search secondary bucket. */
96 bucket = hashes[1] & ((ZU(1) << ckh->lg_curbuckets) - 1);
97 cell = ckh_bucket_search(ckh, bucket, key);
102 ckh_try_bucket_insert(ckh_t *ckh, size_t bucket, const void *key,
108 * Cycle through the cells in the bucket, starting at a random position.
109 * The randomness avoids worst-case search overhead as buckets fill up.
111 offset = (unsigned)prng_lg_range_u64(&ckh->prng_state,
112 LG_CKH_BUCKET_CELLS);
113 for (i = 0; i < (ZU(1) << LG_CKH_BUCKET_CELLS); i++) {
114 cell = &ckh->tab[(bucket << LG_CKH_BUCKET_CELLS) +
115 ((i + offset) & ((ZU(1) << LG_CKH_BUCKET_CELLS) - 1))];
116 if (cell->key == NULL) {
128 * No space is available in bucket. Randomly evict an item, then try to find an
129 * alternate location for that item. Iteratively repeat this
130 * eviction/relocation procedure until either success or detection of an
131 * eviction/relocation bucket cycle.
134 ckh_evict_reloc_insert(ckh_t *ckh, size_t argbucket, void const **argkey,
135 void const **argdata) {
136 const void *key, *data, *tkey, *tdata;
138 size_t hashes[2], bucket, tbucket;
146 * Choose a random item within the bucket to evict. This is
147 * critical to correct function, because without (eventually)
148 * evicting all items within a bucket during iteration, it
149 * would be possible to get stuck in an infinite loop if there
150 * were an item for which both hashes indicated the same
153 i = (unsigned)prng_lg_range_u64(&ckh->prng_state,
154 LG_CKH_BUCKET_CELLS);
155 cell = &ckh->tab[(bucket << LG_CKH_BUCKET_CELLS) + i];
156 assert(cell->key != NULL);
158 /* Swap cell->{key,data} and {key,data} (evict). */
159 tkey = cell->key; tdata = cell->data;
160 cell->key = key; cell->data = data;
161 key = tkey; data = tdata;
167 /* Find the alternate bucket for the evicted item. */
168 ckh->hash(key, hashes);
169 tbucket = hashes[1] & ((ZU(1) << ckh->lg_curbuckets) - 1);
170 if (tbucket == bucket) {
171 tbucket = hashes[0] & ((ZU(1) << ckh->lg_curbuckets)
174 * It may be that (tbucket == bucket) still, if the
175 * item's hashes both indicate this bucket. However,
176 * we are guaranteed to eventually escape this bucket
177 * during iteration, assuming pseudo-random item
178 * selection (true randomness would make infinite
179 * looping a remote possibility). The reason we can
180 * never get trapped forever is that there are two
183 * 1) This bucket == argbucket, so we will quickly
184 * detect an eviction cycle and terminate.
185 * 2) An item was evicted to this bucket from another,
186 * which means that at least one item in this bucket
187 * has hashes that indicate distinct buckets.
190 /* Check for a cycle. */
191 if (tbucket == argbucket) {
198 if (!ckh_try_bucket_insert(ckh, bucket, key, data)) {
205 ckh_try_insert(ckh_t *ckh, void const**argkey, void const**argdata) {
206 size_t hashes[2], bucket;
207 const void *key = *argkey;
208 const void *data = *argdata;
210 ckh->hash(key, hashes);
212 /* Try to insert in primary bucket. */
213 bucket = hashes[0] & ((ZU(1) << ckh->lg_curbuckets) - 1);
214 if (!ckh_try_bucket_insert(ckh, bucket, key, data)) {
218 /* Try to insert in secondary bucket. */
219 bucket = hashes[1] & ((ZU(1) << ckh->lg_curbuckets) - 1);
220 if (!ckh_try_bucket_insert(ckh, bucket, key, data)) {
225 * Try to find a place for this item via iterative eviction/relocation.
227 return ckh_evict_reloc_insert(ckh, bucket, argkey, argdata);
231 * Try to rebuild the hash table from scratch by inserting all items from the
232 * old table into the new.
235 ckh_rebuild(ckh_t *ckh, ckhc_t *aTab) {
236 size_t count, i, nins;
237 const void *key, *data;
241 for (i = nins = 0; nins < count; i++) {
242 if (aTab[i].key != NULL) {
245 if (ckh_try_insert(ckh, &key, &data)) {
257 ckh_grow(tsd_t *tsd, ckh_t *ckh) {
260 unsigned lg_prevbuckets, lg_curcells;
267 * It is possible (though unlikely, given well behaved hashes) that the
268 * table will have to be doubled more than once in order to create a
271 lg_prevbuckets = ckh->lg_curbuckets;
272 lg_curcells = ckh->lg_curbuckets + LG_CKH_BUCKET_CELLS;
277 usize = sz_sa2u(sizeof(ckhc_t) << lg_curcells, CACHELINE);
278 if (unlikely(usize == 0
279 || usize > SC_LARGE_MAXCLASS)) {
283 tab = (ckhc_t *)ipallocztm(tsd_tsdn(tsd), usize, CACHELINE,
284 true, NULL, true, arena_ichoose(tsd, NULL));
289 /* Swap in new table. */
293 ckh->lg_curbuckets = lg_curcells - LG_CKH_BUCKET_CELLS;
295 if (!ckh_rebuild(ckh, tab)) {
296 idalloctm(tsd_tsdn(tsd), tab, NULL, NULL, true, true);
300 /* Rebuilding failed, so back out partially rebuilt table. */
301 idalloctm(tsd_tsdn(tsd), ckh->tab, NULL, NULL, true, true);
303 ckh->lg_curbuckets = lg_prevbuckets;
312 ckh_shrink(tsd_t *tsd, ckh_t *ckh) {
315 unsigned lg_prevbuckets, lg_curcells;
318 * It is possible (though unlikely, given well behaved hashes) that the
319 * table rebuild will fail.
321 lg_prevbuckets = ckh->lg_curbuckets;
322 lg_curcells = ckh->lg_curbuckets + LG_CKH_BUCKET_CELLS - 1;
323 usize = sz_sa2u(sizeof(ckhc_t) << lg_curcells, CACHELINE);
324 if (unlikely(usize == 0 || usize > SC_LARGE_MAXCLASS)) {
327 tab = (ckhc_t *)ipallocztm(tsd_tsdn(tsd), usize, CACHELINE, true, NULL,
328 true, arena_ichoose(tsd, NULL));
331 * An OOM error isn't worth propagating, since it doesn't
332 * prevent this or future operations from proceeding.
336 /* Swap in new table. */
340 ckh->lg_curbuckets = lg_curcells - LG_CKH_BUCKET_CELLS;
342 if (!ckh_rebuild(ckh, tab)) {
343 idalloctm(tsd_tsdn(tsd), tab, NULL, NULL, true, true);
350 /* Rebuilding failed, so back out partially rebuilt table. */
351 idalloctm(tsd_tsdn(tsd), ckh->tab, NULL, NULL, true, true);
353 ckh->lg_curbuckets = lg_prevbuckets;
360 ckh_new(tsd_t *tsd, ckh_t *ckh, size_t minitems, ckh_hash_t *hash,
361 ckh_keycomp_t *keycomp) {
363 size_t mincells, usize;
364 unsigned lg_mincells;
366 assert(minitems > 0);
367 assert(hash != NULL);
368 assert(keycomp != NULL);
373 ckh->nshrinkfails = 0;
377 ckh->prng_state = 42; /* Value doesn't really matter. */
381 * Find the minimum power of 2 that is large enough to fit minitems
382 * entries. We are using (2+,2) cuckoo hashing, which has an expected
383 * maximum load factor of at least ~0.86, so 0.75 is a conservative load
384 * factor that will typically allow mincells items to fit without ever
387 assert(LG_CKH_BUCKET_CELLS > 0);
388 mincells = ((minitems + (3 - (minitems % 3))) / 3) << 2;
389 for (lg_mincells = LG_CKH_BUCKET_CELLS;
390 (ZU(1) << lg_mincells) < mincells;
394 ckh->lg_minbuckets = lg_mincells - LG_CKH_BUCKET_CELLS;
395 ckh->lg_curbuckets = lg_mincells - LG_CKH_BUCKET_CELLS;
397 ckh->keycomp = keycomp;
399 usize = sz_sa2u(sizeof(ckhc_t) << lg_mincells, CACHELINE);
400 if (unlikely(usize == 0 || usize > SC_LARGE_MAXCLASS)) {
404 ckh->tab = (ckhc_t *)ipallocztm(tsd_tsdn(tsd), usize, CACHELINE, true,
405 NULL, true, arena_ichoose(tsd, NULL));
406 if (ckh->tab == NULL) {
417 ckh_delete(tsd_t *tsd, ckh_t *ckh) {
422 "%s(%p): ngrows: %"FMTu64", nshrinks: %"FMTu64","
423 " nshrinkfails: %"FMTu64", ninserts: %"FMTu64","
424 " nrelocs: %"FMTu64"\n", __func__, ckh,
425 (unsigned long long)ckh->ngrows,
426 (unsigned long long)ckh->nshrinks,
427 (unsigned long long)ckh->nshrinkfails,
428 (unsigned long long)ckh->ninserts,
429 (unsigned long long)ckh->nrelocs);
432 idalloctm(tsd_tsdn(tsd), ckh->tab, NULL, NULL, true, true);
434 memset(ckh, JEMALLOC_FREE_JUNK, sizeof(ckh_t));
439 ckh_count(ckh_t *ckh) {
446 ckh_iter(ckh_t *ckh, size_t *tabind, void **key, void **data) {
449 for (i = *tabind, ncells = (ZU(1) << (ckh->lg_curbuckets +
450 LG_CKH_BUCKET_CELLS)); i < ncells; i++) {
451 if (ckh->tab[i].key != NULL) {
453 *key = (void *)ckh->tab[i].key;
456 *data = (void *)ckh->tab[i].data;
467 ckh_insert(tsd_t *tsd, ckh_t *ckh, const void *key, const void *data) {
471 assert(ckh_search(ckh, key, NULL, NULL));
477 while (ckh_try_insert(ckh, &key, &data)) {
478 if (ckh_grow(tsd, ckh)) {
490 ckh_remove(tsd_t *tsd, ckh_t *ckh, const void *searchkey, void **key,
496 cell = ckh_isearch(ckh, searchkey);
497 if (cell != SIZE_T_MAX) {
499 *key = (void *)ckh->tab[cell].key;
502 *data = (void *)ckh->tab[cell].data;
504 ckh->tab[cell].key = NULL;
505 ckh->tab[cell].data = NULL; /* Not necessary. */
508 /* Try to halve the table if it is less than 1/4 full. */
509 if (ckh->count < (ZU(1) << (ckh->lg_curbuckets
510 + LG_CKH_BUCKET_CELLS - 2)) && ckh->lg_curbuckets
511 > ckh->lg_minbuckets) {
512 /* Ignore error due to OOM. */
513 ckh_shrink(tsd, ckh);
523 ckh_search(ckh_t *ckh, const void *searchkey, void **key, void **data) {
528 cell = ckh_isearch(ckh, searchkey);
529 if (cell != SIZE_T_MAX) {
531 *key = (void *)ckh->tab[cell].key;
534 *data = (void *)ckh->tab[cell].data;
543 ckh_string_hash(const void *key, size_t r_hash[2]) {
544 hash(key, strlen((const char *)key), 0x94122f33U, r_hash);
548 ckh_string_keycomp(const void *k1, const void *k2) {
552 return !strcmp((char *)k1, (char *)k2);
556 ckh_pointer_hash(const void *key, size_t r_hash[2]) {
562 assert(sizeof(u.v) == sizeof(u.i));
564 hash(&u.i, sizeof(u.i), 0xd983396eU, r_hash);
568 ckh_pointer_keycomp(const void *k1, const void *k2) {