2010-02-11 02:37:56 +08:00
|
|
|
/*
|
|
|
|
*******************************************************************************
|
|
|
|
* Implementation of (2^1+,2) cuckoo hashing, where 2^1+ indicates that each
|
|
|
|
* hash bucket contains 2^n cells, for n >= 1, and 2 indicates that two hash
|
|
|
|
* functions are employed. The original cuckoo hashing algorithm was described
|
|
|
|
* in:
|
|
|
|
*
|
|
|
|
* Pagh, R., F.F. Rodler (2004) Cuckoo Hashing. Journal of Algorithms
|
|
|
|
* 51(2):122-144.
|
|
|
|
*
|
|
|
|
* Generalization of cuckoo hashing was discussed in:
|
|
|
|
*
|
|
|
|
* Erlingsson, U., M. Manasse, F. McSherry (2006) A cool and practical
|
|
|
|
* alternative to traditional hash tables. In Proceedings of the 7th
|
|
|
|
* Workshop on Distributed Data and Structures (WDAS'06), Santa Clara, CA,
|
|
|
|
* January 2006.
|
|
|
|
*
|
|
|
|
* This implementation uses precisely two hash functions because that is the
|
|
|
|
* fewest that can work, and supporting multiple hashes is an implementation
|
|
|
|
* burden. Here is a reproduction of Figure 1 from Erlingsson et al. (2006)
|
|
|
|
* that shows approximate expected maximum load factors for various
|
|
|
|
* configurations:
|
|
|
|
*
|
|
|
|
* | #cells/bucket |
|
|
|
|
* #hashes | 1 | 2 | 4 | 8 |
|
|
|
|
* --------+-------+-------+-------+-------+
|
|
|
|
* 1 | 0.006 | 0.006 | 0.03 | 0.12 |
|
|
|
|
* 2 | 0.49 | 0.86 |>0.93< |>0.96< |
|
|
|
|
* 3 | 0.91 | 0.97 | 0.98 | 0.999 |
|
|
|
|
* 4 | 0.97 | 0.99 | 0.999 | |
|
|
|
|
*
|
|
|
|
* The number of cells per bucket is chosen such that a bucket fits in one cache
|
|
|
|
* line. So, on 32- and 64-bit systems, we use (8,2) and (4,2) cuckoo hashing,
|
|
|
|
* respectively.
|
|
|
|
*
|
|
|
|
******************************************************************************/
|
2017-01-20 13:41:41 +08:00
|
|
|
#define JEMALLOC_CKH_C_
|
2017-04-11 09:17:55 +08:00
|
|
|
#include "jemalloc/internal/jemalloc_preamble.h"
|
2017-04-20 05:56:42 +08:00
|
|
|
|
|
|
|
#include "jemalloc/internal/ckh.h"
|
|
|
|
|
2017-04-11 09:17:55 +08:00
|
|
|
#include "jemalloc/internal/jemalloc_internal_includes.h"
|
2010-02-11 02:37:56 +08:00
|
|
|
|
2017-04-12 05:43:12 +08:00
|
|
|
#include "jemalloc/internal/assert.h"
|
2017-04-25 08:16:36 +08:00
|
|
|
#include "jemalloc/internal/hash.h"
|
2017-04-12 04:06:31 +08:00
|
|
|
#include "jemalloc/internal/malloc_io.h"
|
2017-04-20 03:48:50 +08:00
|
|
|
#include "jemalloc/internal/prng.h"
|
2017-04-12 04:31:16 +08:00
|
|
|
#include "jemalloc/internal/util.h"
|
2017-04-12 04:06:31 +08:00
|
|
|
|
2010-02-11 02:37:56 +08:00
|
|
|
/******************************************************************************/
|
|
|
|
/* Function prototypes for non-inline static functions. */
|
|
|
|
|
2016-10-21 14:59:12 +08:00
|
|
|
static bool ckh_grow(tsd_t *tsd, ckh_t *ckh);
|
|
|
|
static void ckh_shrink(tsd_t *tsd, ckh_t *ckh);
|
2010-02-11 02:37:56 +08:00
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Search bucket for key and return the cell number if found; SIZE_T_MAX
|
|
|
|
* otherwise.
|
|
|
|
*/
|
2017-04-22 00:37:34 +08:00
|
|
|
static size_t
|
2017-01-16 08:56:30 +08:00
|
|
|
ckh_bucket_search(ckh_t *ckh, size_t bucket, const void *key) {
|
2010-02-11 02:37:56 +08:00
|
|
|
ckhc_t *cell;
|
|
|
|
unsigned i;
|
|
|
|
|
|
|
|
for (i = 0; i < (ZU(1) << LG_CKH_BUCKET_CELLS); i++) {
|
|
|
|
cell = &ckh->tab[(bucket << LG_CKH_BUCKET_CELLS) + i];
|
2017-01-16 08:56:30 +08:00
|
|
|
if (cell->key != NULL && ckh->keycomp(key, cell->key)) {
|
2017-01-20 10:15:45 +08:00
|
|
|
return (bucket << LG_CKH_BUCKET_CELLS) + i;
|
2017-01-16 08:56:30 +08:00
|
|
|
}
|
2010-02-11 02:37:56 +08:00
|
|
|
}
|
|
|
|
|
2017-01-20 10:15:45 +08:00
|
|
|
return SIZE_T_MAX;
|
2010-02-11 02:37:56 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Search table for key and return cell number if found; SIZE_T_MAX otherwise.
|
|
|
|
*/
|
2017-04-22 00:37:34 +08:00
|
|
|
static size_t
|
2017-01-16 08:56:30 +08:00
|
|
|
ckh_isearch(ckh_t *ckh, const void *key) {
|
2013-01-23 04:02:08 +08:00
|
|
|
size_t hashes[2], bucket, cell;
|
2010-02-11 02:37:56 +08:00
|
|
|
|
|
|
|
assert(ckh != NULL);
|
|
|
|
|
2013-01-23 04:02:08 +08:00
|
|
|
ckh->hash(key, hashes);
|
2010-02-11 02:37:56 +08:00
|
|
|
|
|
|
|
/* Search primary bucket. */
|
2013-01-23 04:02:08 +08:00
|
|
|
bucket = hashes[0] & ((ZU(1) << ckh->lg_curbuckets) - 1);
|
2010-02-11 02:37:56 +08:00
|
|
|
cell = ckh_bucket_search(ckh, bucket, key);
|
2017-01-16 08:56:30 +08:00
|
|
|
if (cell != SIZE_T_MAX) {
|
2017-01-20 10:15:45 +08:00
|
|
|
return cell;
|
2017-01-16 08:56:30 +08:00
|
|
|
}
|
2010-02-11 02:37:56 +08:00
|
|
|
|
|
|
|
/* Search secondary bucket. */
|
2013-01-23 04:02:08 +08:00
|
|
|
bucket = hashes[1] & ((ZU(1) << ckh->lg_curbuckets) - 1);
|
2010-02-11 02:37:56 +08:00
|
|
|
cell = ckh_bucket_search(ckh, bucket, key);
|
2017-01-20 10:15:45 +08:00
|
|
|
return cell;
|
2010-02-11 02:37:56 +08:00
|
|
|
}
|
|
|
|
|
2017-04-22 00:37:34 +08:00
|
|
|
static bool
|
2010-02-11 02:37:56 +08:00
|
|
|
ckh_try_bucket_insert(ckh_t *ckh, size_t bucket, const void *key,
|
2017-01-16 08:56:30 +08:00
|
|
|
const void *data) {
|
2010-02-11 02:37:56 +08:00
|
|
|
ckhc_t *cell;
|
|
|
|
unsigned offset, i;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Cycle through the cells in the bucket, starting at a random position.
|
|
|
|
* The randomness avoids worst-case search overhead as buckets fill up.
|
|
|
|
*/
|
2016-11-08 02:52:44 +08:00
|
|
|
offset = (unsigned)prng_lg_range_u64(&ckh->prng_state,
|
|
|
|
LG_CKH_BUCKET_CELLS);
|
2010-02-11 02:37:56 +08:00
|
|
|
for (i = 0; i < (ZU(1) << LG_CKH_BUCKET_CELLS); i++) {
|
|
|
|
cell = &ckh->tab[(bucket << LG_CKH_BUCKET_CELLS) +
|
|
|
|
((i + offset) & ((ZU(1) << LG_CKH_BUCKET_CELLS) - 1))];
|
|
|
|
if (cell->key == NULL) {
|
|
|
|
cell->key = key;
|
|
|
|
cell->data = data;
|
|
|
|
ckh->count++;
|
2017-01-20 10:15:45 +08:00
|
|
|
return false;
|
2010-02-11 02:37:56 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-20 10:15:45 +08:00
|
|
|
return true;
|
2010-02-11 02:37:56 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* No space is available in bucket. Randomly evict an item, then try to find an
|
|
|
|
* alternate location for that item. Iteratively repeat this
|
|
|
|
* eviction/relocation procedure until either success or detection of an
|
|
|
|
* eviction/relocation bucket cycle.
|
|
|
|
*/
|
2017-04-22 00:37:34 +08:00
|
|
|
static bool
|
2010-02-11 02:37:56 +08:00
|
|
|
ckh_evict_reloc_insert(ckh_t *ckh, size_t argbucket, void const **argkey,
|
2017-01-16 08:56:30 +08:00
|
|
|
void const **argdata) {
|
2010-02-11 02:37:56 +08:00
|
|
|
const void *key, *data, *tkey, *tdata;
|
|
|
|
ckhc_t *cell;
|
2013-01-23 04:02:08 +08:00
|
|
|
size_t hashes[2], bucket, tbucket;
|
2010-02-11 02:37:56 +08:00
|
|
|
unsigned i;
|
|
|
|
|
|
|
|
bucket = argbucket;
|
|
|
|
key = *argkey;
|
|
|
|
data = *argdata;
|
|
|
|
while (true) {
|
|
|
|
/*
|
|
|
|
* Choose a random item within the bucket to evict. This is
|
|
|
|
* critical to correct function, because without (eventually)
|
|
|
|
* evicting all items within a bucket during iteration, it
|
|
|
|
* would be possible to get stuck in an infinite loop if there
|
|
|
|
* were an item for which both hashes indicated the same
|
|
|
|
* bucket.
|
|
|
|
*/
|
2016-11-08 02:52:44 +08:00
|
|
|
i = (unsigned)prng_lg_range_u64(&ckh->prng_state,
|
|
|
|
LG_CKH_BUCKET_CELLS);
|
2010-02-11 02:37:56 +08:00
|
|
|
cell = &ckh->tab[(bucket << LG_CKH_BUCKET_CELLS) + i];
|
|
|
|
assert(cell->key != NULL);
|
|
|
|
|
|
|
|
/* Swap cell->{key,data} and {key,data} (evict). */
|
|
|
|
tkey = cell->key; tdata = cell->data;
|
|
|
|
cell->key = key; cell->data = data;
|
|
|
|
key = tkey; data = tdata;
|
|
|
|
|
|
|
|
#ifdef CKH_COUNT
|
|
|
|
ckh->nrelocs++;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* Find the alternate bucket for the evicted item. */
|
2013-01-23 04:02:08 +08:00
|
|
|
ckh->hash(key, hashes);
|
|
|
|
tbucket = hashes[1] & ((ZU(1) << ckh->lg_curbuckets) - 1);
|
2010-02-11 02:37:56 +08:00
|
|
|
if (tbucket == bucket) {
|
2013-01-23 04:02:08 +08:00
|
|
|
tbucket = hashes[0] & ((ZU(1) << ckh->lg_curbuckets)
|
|
|
|
- 1);
|
2010-02-11 02:37:56 +08:00
|
|
|
/*
|
|
|
|
* It may be that (tbucket == bucket) still, if the
|
|
|
|
* item's hashes both indicate this bucket. However,
|
|
|
|
* we are guaranteed to eventually escape this bucket
|
|
|
|
* during iteration, assuming pseudo-random item
|
|
|
|
* selection (true randomness would make infinite
|
|
|
|
* looping a remote possibility). The reason we can
|
|
|
|
* never get trapped forever is that there are two
|
|
|
|
* cases:
|
|
|
|
*
|
|
|
|
* 1) This bucket == argbucket, so we will quickly
|
|
|
|
* detect an eviction cycle and terminate.
|
|
|
|
* 2) An item was evicted to this bucket from another,
|
|
|
|
* which means that at least one item in this bucket
|
|
|
|
* has hashes that indicate distinct buckets.
|
|
|
|
*/
|
|
|
|
}
|
|
|
|
/* Check for a cycle. */
|
|
|
|
if (tbucket == argbucket) {
|
|
|
|
*argkey = key;
|
|
|
|
*argdata = data;
|
2017-01-20 10:15:45 +08:00
|
|
|
return true;
|
2010-02-11 02:37:56 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
bucket = tbucket;
|
2017-01-16 08:56:30 +08:00
|
|
|
if (!ckh_try_bucket_insert(ckh, bucket, key, data)) {
|
2017-01-20 10:15:45 +08:00
|
|
|
return false;
|
2017-01-16 08:56:30 +08:00
|
|
|
}
|
2010-02-11 02:37:56 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-22 00:37:34 +08:00
|
|
|
static bool
|
2017-01-16 08:56:30 +08:00
|
|
|
ckh_try_insert(ckh_t *ckh, void const**argkey, void const**argdata) {
|
2013-01-23 04:02:08 +08:00
|
|
|
size_t hashes[2], bucket;
|
2010-02-11 02:37:56 +08:00
|
|
|
const void *key = *argkey;
|
|
|
|
const void *data = *argdata;
|
|
|
|
|
2013-01-23 04:02:08 +08:00
|
|
|
ckh->hash(key, hashes);
|
2010-02-11 02:37:56 +08:00
|
|
|
|
|
|
|
/* Try to insert in primary bucket. */
|
2013-01-23 04:02:08 +08:00
|
|
|
bucket = hashes[0] & ((ZU(1) << ckh->lg_curbuckets) - 1);
|
2017-01-16 08:56:30 +08:00
|
|
|
if (!ckh_try_bucket_insert(ckh, bucket, key, data)) {
|
2017-01-20 10:15:45 +08:00
|
|
|
return false;
|
2017-01-16 08:56:30 +08:00
|
|
|
}
|
2010-02-11 02:37:56 +08:00
|
|
|
|
|
|
|
/* Try to insert in secondary bucket. */
|
2013-01-23 04:02:08 +08:00
|
|
|
bucket = hashes[1] & ((ZU(1) << ckh->lg_curbuckets) - 1);
|
2017-01-16 08:56:30 +08:00
|
|
|
if (!ckh_try_bucket_insert(ckh, bucket, key, data)) {
|
2017-01-20 10:15:45 +08:00
|
|
|
return false;
|
2017-01-16 08:56:30 +08:00
|
|
|
}
|
2010-02-11 02:37:56 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Try to find a place for this item via iterative eviction/relocation.
|
|
|
|
*/
|
2017-01-20 10:15:45 +08:00
|
|
|
return ckh_evict_reloc_insert(ckh, bucket, argkey, argdata);
|
2010-02-11 02:37:56 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Try to rebuild the hash table from scratch by inserting all items from the
|
|
|
|
* old table into the new.
|
|
|
|
*/
|
2017-04-22 00:37:34 +08:00
|
|
|
static bool
|
2017-01-16 08:56:30 +08:00
|
|
|
ckh_rebuild(ckh_t *ckh, ckhc_t *aTab) {
|
2010-02-11 02:37:56 +08:00
|
|
|
size_t count, i, nins;
|
|
|
|
const void *key, *data;
|
|
|
|
|
|
|
|
count = ckh->count;
|
|
|
|
ckh->count = 0;
|
|
|
|
for (i = nins = 0; nins < count; i++) {
|
|
|
|
if (aTab[i].key != NULL) {
|
|
|
|
key = aTab[i].key;
|
|
|
|
data = aTab[i].data;
|
|
|
|
if (ckh_try_insert(ckh, &key, &data)) {
|
|
|
|
ckh->count = count;
|
2017-01-20 10:15:45 +08:00
|
|
|
return true;
|
2010-02-11 02:37:56 +08:00
|
|
|
}
|
|
|
|
nins++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-20 10:15:45 +08:00
|
|
|
return false;
|
2010-02-11 02:37:56 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static bool
|
2017-01-16 08:56:30 +08:00
|
|
|
ckh_grow(tsd_t *tsd, ckh_t *ckh) {
|
2010-02-11 02:37:56 +08:00
|
|
|
bool ret;
|
|
|
|
ckhc_t *tab, *ttab;
|
2016-02-25 04:42:23 +08:00
|
|
|
unsigned lg_prevbuckets, lg_curcells;
|
2010-02-11 02:37:56 +08:00
|
|
|
|
|
|
|
#ifdef CKH_COUNT
|
|
|
|
ckh->ngrows++;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/*
|
|
|
|
* It is possible (though unlikely, given well behaved hashes) that the
|
|
|
|
* table will have to be doubled more than once in order to create a
|
|
|
|
* usable table.
|
|
|
|
*/
|
|
|
|
lg_prevbuckets = ckh->lg_curbuckets;
|
|
|
|
lg_curcells = ckh->lg_curbuckets + LG_CKH_BUCKET_CELLS;
|
|
|
|
while (true) {
|
2011-03-23 15:37:29 +08:00
|
|
|
size_t usize;
|
|
|
|
|
2010-02-11 02:37:56 +08:00
|
|
|
lg_curcells++;
|
2017-05-31 01:45:37 +08:00
|
|
|
usize = sz_sa2u(sizeof(ckhc_t) << lg_curcells, CACHELINE);
|
2017-12-15 04:46:39 +08:00
|
|
|
if (unlikely(usize == 0
|
2018-07-12 07:05:58 +08:00
|
|
|
|| usize > SC_LARGE_MAXCLASS)) {
|
2011-03-23 15:37:29 +08:00
|
|
|
ret = true;
|
2012-04-11 06:07:44 +08:00
|
|
|
goto label_return;
|
2011-03-23 15:37:29 +08:00
|
|
|
}
|
2016-10-21 14:59:12 +08:00
|
|
|
tab = (ckhc_t *)ipallocztm(tsd_tsdn(tsd), usize, CACHELINE,
|
|
|
|
true, NULL, true, arena_ichoose(tsd, NULL));
|
2010-02-11 02:37:56 +08:00
|
|
|
if (tab == NULL) {
|
|
|
|
ret = true;
|
2012-04-11 06:07:44 +08:00
|
|
|
goto label_return;
|
2010-02-11 02:37:56 +08:00
|
|
|
}
|
|
|
|
/* Swap in new table. */
|
|
|
|
ttab = ckh->tab;
|
|
|
|
ckh->tab = tab;
|
|
|
|
tab = ttab;
|
|
|
|
ckh->lg_curbuckets = lg_curcells - LG_CKH_BUCKET_CELLS;
|
|
|
|
|
2014-10-04 01:16:09 +08:00
|
|
|
if (!ckh_rebuild(ckh, tab)) {
|
2017-04-08 05:12:30 +08:00
|
|
|
idalloctm(tsd_tsdn(tsd), tab, NULL, NULL, true, true);
|
2010-02-11 02:37:56 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Rebuilding failed, so back out partially rebuilt table. */
|
2017-04-08 05:12:30 +08:00
|
|
|
idalloctm(tsd_tsdn(tsd), ckh->tab, NULL, NULL, true, true);
|
2010-02-11 02:37:56 +08:00
|
|
|
ckh->tab = tab;
|
|
|
|
ckh->lg_curbuckets = lg_prevbuckets;
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = false;
|
2012-04-11 06:07:44 +08:00
|
|
|
label_return:
|
2017-01-20 10:15:45 +08:00
|
|
|
return ret;
|
2010-02-11 02:37:56 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2017-01-16 08:56:30 +08:00
|
|
|
ckh_shrink(tsd_t *tsd, ckh_t *ckh) {
|
2010-02-11 02:37:56 +08:00
|
|
|
ckhc_t *tab, *ttab;
|
2016-02-25 04:42:23 +08:00
|
|
|
size_t usize;
|
|
|
|
unsigned lg_prevbuckets, lg_curcells;
|
2010-02-11 02:37:56 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* It is possible (though unlikely, given well behaved hashes) that the
|
|
|
|
* table rebuild will fail.
|
|
|
|
*/
|
|
|
|
lg_prevbuckets = ckh->lg_curbuckets;
|
|
|
|
lg_curcells = ckh->lg_curbuckets + LG_CKH_BUCKET_CELLS - 1;
|
2017-05-31 01:45:37 +08:00
|
|
|
usize = sz_sa2u(sizeof(ckhc_t) << lg_curcells, CACHELINE);
|
2018-07-12 07:05:58 +08:00
|
|
|
if (unlikely(usize == 0 || usize > SC_LARGE_MAXCLASS)) {
|
2011-03-23 15:37:29 +08:00
|
|
|
return;
|
2017-01-16 08:56:30 +08:00
|
|
|
}
|
2016-10-21 14:59:12 +08:00
|
|
|
tab = (ckhc_t *)ipallocztm(tsd_tsdn(tsd), usize, CACHELINE, true, NULL,
|
|
|
|
true, arena_ichoose(tsd, NULL));
|
2010-02-11 02:37:56 +08:00
|
|
|
if (tab == NULL) {
|
|
|
|
/*
|
|
|
|
* An OOM error isn't worth propagating, since it doesn't
|
|
|
|
* prevent this or future operations from proceeding.
|
|
|
|
*/
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
/* Swap in new table. */
|
|
|
|
ttab = ckh->tab;
|
|
|
|
ckh->tab = tab;
|
|
|
|
tab = ttab;
|
|
|
|
ckh->lg_curbuckets = lg_curcells - LG_CKH_BUCKET_CELLS;
|
|
|
|
|
2014-10-04 01:16:09 +08:00
|
|
|
if (!ckh_rebuild(ckh, tab)) {
|
2017-04-08 05:12:30 +08:00
|
|
|
idalloctm(tsd_tsdn(tsd), tab, NULL, NULL, true, true);
|
2010-02-11 02:37:56 +08:00
|
|
|
#ifdef CKH_COUNT
|
|
|
|
ckh->nshrinks++;
|
|
|
|
#endif
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Rebuilding failed, so back out partially rebuilt table. */
|
2017-04-08 05:12:30 +08:00
|
|
|
idalloctm(tsd_tsdn(tsd), ckh->tab, NULL, NULL, true, true);
|
2010-02-11 02:37:56 +08:00
|
|
|
ckh->tab = tab;
|
|
|
|
ckh->lg_curbuckets = lg_prevbuckets;
|
|
|
|
#ifdef CKH_COUNT
|
|
|
|
ckh->nshrinkfails++;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2016-10-21 14:59:12 +08:00
|
|
|
ckh_new(tsd_t *tsd, ckh_t *ckh, size_t minitems, ckh_hash_t *hash,
|
2017-01-16 08:56:30 +08:00
|
|
|
ckh_keycomp_t *keycomp) {
|
2010-02-11 02:37:56 +08:00
|
|
|
bool ret;
|
2011-03-23 15:37:29 +08:00
|
|
|
size_t mincells, usize;
|
2010-02-11 02:37:56 +08:00
|
|
|
unsigned lg_mincells;
|
|
|
|
|
|
|
|
assert(minitems > 0);
|
|
|
|
assert(hash != NULL);
|
|
|
|
assert(keycomp != NULL);
|
|
|
|
|
|
|
|
#ifdef CKH_COUNT
|
|
|
|
ckh->ngrows = 0;
|
|
|
|
ckh->nshrinks = 0;
|
|
|
|
ckh->nshrinkfails = 0;
|
|
|
|
ckh->ninserts = 0;
|
|
|
|
ckh->nrelocs = 0;
|
|
|
|
#endif
|
2012-03-03 07:59:45 +08:00
|
|
|
ckh->prng_state = 42; /* Value doesn't really matter. */
|
2010-02-11 02:37:56 +08:00
|
|
|
ckh->count = 0;
|
|
|
|
|
|
|
|
/*
|
2014-12-09 06:40:14 +08:00
|
|
|
* Find the minimum power of 2 that is large enough to fit minitems
|
2010-02-11 02:37:56 +08:00
|
|
|
* entries. We are using (2+,2) cuckoo hashing, which has an expected
|
|
|
|
* maximum load factor of at least ~0.86, so 0.75 is a conservative load
|
2014-12-09 06:40:14 +08:00
|
|
|
* factor that will typically allow mincells items to fit without ever
|
2010-02-11 02:37:56 +08:00
|
|
|
* growing the table.
|
|
|
|
*/
|
|
|
|
assert(LG_CKH_BUCKET_CELLS > 0);
|
|
|
|
mincells = ((minitems + (3 - (minitems % 3))) / 3) << 2;
|
|
|
|
for (lg_mincells = LG_CKH_BUCKET_CELLS;
|
|
|
|
(ZU(1) << lg_mincells) < mincells;
|
2017-01-16 08:56:30 +08:00
|
|
|
lg_mincells++) {
|
|
|
|
/* Do nothing. */
|
|
|
|
}
|
2010-02-11 02:37:56 +08:00
|
|
|
ckh->lg_minbuckets = lg_mincells - LG_CKH_BUCKET_CELLS;
|
|
|
|
ckh->lg_curbuckets = lg_mincells - LG_CKH_BUCKET_CELLS;
|
|
|
|
ckh->hash = hash;
|
|
|
|
ckh->keycomp = keycomp;
|
|
|
|
|
2017-05-31 01:45:37 +08:00
|
|
|
usize = sz_sa2u(sizeof(ckhc_t) << lg_mincells, CACHELINE);
|
2018-07-12 07:05:58 +08:00
|
|
|
if (unlikely(usize == 0 || usize > SC_LARGE_MAXCLASS)) {
|
2011-03-23 15:37:29 +08:00
|
|
|
ret = true;
|
2012-04-11 06:07:44 +08:00
|
|
|
goto label_return;
|
2011-03-23 15:37:29 +08:00
|
|
|
}
|
2016-10-21 14:59:12 +08:00
|
|
|
ckh->tab = (ckhc_t *)ipallocztm(tsd_tsdn(tsd), usize, CACHELINE, true,
|
|
|
|
NULL, true, arena_ichoose(tsd, NULL));
|
2010-02-11 02:37:56 +08:00
|
|
|
if (ckh->tab == NULL) {
|
|
|
|
ret = true;
|
2012-04-11 06:07:44 +08:00
|
|
|
goto label_return;
|
2010-02-11 02:37:56 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
ret = false;
|
2012-04-11 06:07:44 +08:00
|
|
|
label_return:
|
2017-01-20 10:15:45 +08:00
|
|
|
return ret;
|
2010-02-11 02:37:56 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2017-01-16 08:56:30 +08:00
|
|
|
ckh_delete(tsd_t *tsd, ckh_t *ckh) {
|
2010-02-11 02:37:56 +08:00
|
|
|
assert(ckh != NULL);
|
|
|
|
|
|
|
|
#ifdef CKH_VERBOSE
|
|
|
|
malloc_printf(
|
2015-07-24 04:56:25 +08:00
|
|
|
"%s(%p): ngrows: %"FMTu64", nshrinks: %"FMTu64","
|
|
|
|
" nshrinkfails: %"FMTu64", ninserts: %"FMTu64","
|
|
|
|
" nrelocs: %"FMTu64"\n", __func__, ckh,
|
2010-02-11 02:37:56 +08:00
|
|
|
(unsigned long long)ckh->ngrows,
|
|
|
|
(unsigned long long)ckh->nshrinks,
|
|
|
|
(unsigned long long)ckh->nshrinkfails,
|
|
|
|
(unsigned long long)ckh->ninserts,
|
|
|
|
(unsigned long long)ckh->nrelocs);
|
|
|
|
#endif
|
|
|
|
|
2017-04-08 05:12:30 +08:00
|
|
|
idalloctm(tsd_tsdn(tsd), ckh->tab, NULL, NULL, true, true);
|
2017-01-16 08:56:30 +08:00
|
|
|
if (config_debug) {
|
2016-03-28 14:28:39 +08:00
|
|
|
memset(ckh, JEMALLOC_FREE_JUNK, sizeof(ckh_t));
|
2017-01-16 08:56:30 +08:00
|
|
|
}
|
2010-02-11 02:37:56 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
size_t
|
2017-01-16 08:56:30 +08:00
|
|
|
ckh_count(ckh_t *ckh) {
|
2010-02-11 02:37:56 +08:00
|
|
|
assert(ckh != NULL);
|
|
|
|
|
2017-01-20 10:15:45 +08:00
|
|
|
return ckh->count;
|
2010-02-11 02:37:56 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2017-01-16 08:56:30 +08:00
|
|
|
ckh_iter(ckh_t *ckh, size_t *tabind, void **key, void **data) {
|
2010-02-11 02:37:56 +08:00
|
|
|
size_t i, ncells;
|
|
|
|
|
|
|
|
for (i = *tabind, ncells = (ZU(1) << (ckh->lg_curbuckets +
|
|
|
|
LG_CKH_BUCKET_CELLS)); i < ncells; i++) {
|
|
|
|
if (ckh->tab[i].key != NULL) {
|
2017-01-16 08:56:30 +08:00
|
|
|
if (key != NULL) {
|
2010-02-11 02:37:56 +08:00
|
|
|
*key = (void *)ckh->tab[i].key;
|
2017-01-16 08:56:30 +08:00
|
|
|
}
|
|
|
|
if (data != NULL) {
|
2010-02-11 02:37:56 +08:00
|
|
|
*data = (void *)ckh->tab[i].data;
|
2017-01-16 08:56:30 +08:00
|
|
|
}
|
2010-02-11 02:37:56 +08:00
|
|
|
*tabind = i + 1;
|
2017-01-20 10:15:45 +08:00
|
|
|
return false;
|
2010-02-11 02:37:56 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-20 10:15:45 +08:00
|
|
|
return true;
|
2010-02-11 02:37:56 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2017-01-16 08:56:30 +08:00
|
|
|
ckh_insert(tsd_t *tsd, ckh_t *ckh, const void *key, const void *data) {
|
2010-02-11 02:37:56 +08:00
|
|
|
bool ret;
|
|
|
|
|
|
|
|
assert(ckh != NULL);
|
|
|
|
assert(ckh_search(ckh, key, NULL, NULL));
|
|
|
|
|
|
|
|
#ifdef CKH_COUNT
|
|
|
|
ckh->ninserts++;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
while (ckh_try_insert(ckh, &key, &data)) {
|
2016-10-21 14:59:12 +08:00
|
|
|
if (ckh_grow(tsd, ckh)) {
|
2010-02-11 02:37:56 +08:00
|
|
|
ret = true;
|
2012-04-11 06:07:44 +08:00
|
|
|
goto label_return;
|
2010-02-11 02:37:56 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = false;
|
2012-04-11 06:07:44 +08:00
|
|
|
label_return:
|
2017-01-20 10:15:45 +08:00
|
|
|
return ret;
|
2010-02-11 02:37:56 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2016-10-21 14:59:12 +08:00
|
|
|
ckh_remove(tsd_t *tsd, ckh_t *ckh, const void *searchkey, void **key,
|
2017-01-16 08:56:30 +08:00
|
|
|
void **data) {
|
2010-02-11 02:37:56 +08:00
|
|
|
size_t cell;
|
|
|
|
|
|
|
|
assert(ckh != NULL);
|
|
|
|
|
|
|
|
cell = ckh_isearch(ckh, searchkey);
|
|
|
|
if (cell != SIZE_T_MAX) {
|
2017-01-16 08:56:30 +08:00
|
|
|
if (key != NULL) {
|
2010-02-11 02:37:56 +08:00
|
|
|
*key = (void *)ckh->tab[cell].key;
|
2017-01-16 08:56:30 +08:00
|
|
|
}
|
|
|
|
if (data != NULL) {
|
2010-02-11 02:37:56 +08:00
|
|
|
*data = (void *)ckh->tab[cell].data;
|
2017-01-16 08:56:30 +08:00
|
|
|
}
|
2010-02-11 02:37:56 +08:00
|
|
|
ckh->tab[cell].key = NULL;
|
|
|
|
ckh->tab[cell].data = NULL; /* Not necessary. */
|
|
|
|
|
|
|
|
ckh->count--;
|
|
|
|
/* Try to halve the table if it is less than 1/4 full. */
|
|
|
|
if (ckh->count < (ZU(1) << (ckh->lg_curbuckets
|
|
|
|
+ LG_CKH_BUCKET_CELLS - 2)) && ckh->lg_curbuckets
|
|
|
|
> ckh->lg_minbuckets) {
|
|
|
|
/* Ignore error due to OOM. */
|
2016-10-21 14:59:12 +08:00
|
|
|
ckh_shrink(tsd, ckh);
|
2010-02-11 02:37:56 +08:00
|
|
|
}
|
|
|
|
|
2017-01-20 10:15:45 +08:00
|
|
|
return false;
|
2010-02-11 02:37:56 +08:00
|
|
|
}
|
|
|
|
|
2017-01-20 10:15:45 +08:00
|
|
|
return true;
|
2010-02-11 02:37:56 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2017-01-16 08:56:30 +08:00
|
|
|
ckh_search(ckh_t *ckh, const void *searchkey, void **key, void **data) {
|
2010-02-11 02:37:56 +08:00
|
|
|
size_t cell;
|
|
|
|
|
|
|
|
assert(ckh != NULL);
|
|
|
|
|
|
|
|
cell = ckh_isearch(ckh, searchkey);
|
|
|
|
if (cell != SIZE_T_MAX) {
|
2017-01-16 08:56:30 +08:00
|
|
|
if (key != NULL) {
|
2010-02-11 02:37:56 +08:00
|
|
|
*key = (void *)ckh->tab[cell].key;
|
2017-01-16 08:56:30 +08:00
|
|
|
}
|
|
|
|
if (data != NULL) {
|
2010-02-11 02:37:56 +08:00
|
|
|
*data = (void *)ckh->tab[cell].data;
|
2017-01-16 08:56:30 +08:00
|
|
|
}
|
2017-01-20 10:15:45 +08:00
|
|
|
return false;
|
2010-02-11 02:37:56 +08:00
|
|
|
}
|
|
|
|
|
2017-01-20 10:15:45 +08:00
|
|
|
return true;
|
2010-02-11 02:37:56 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2017-01-16 08:56:30 +08:00
|
|
|
ckh_string_hash(const void *key, size_t r_hash[2]) {
|
2013-01-23 04:02:08 +08:00
|
|
|
hash(key, strlen((const char *)key), 0x94122f33U, r_hash);
|
2010-02-11 02:37:56 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2017-01-16 08:56:30 +08:00
|
|
|
ckh_string_keycomp(const void *k1, const void *k2) {
|
2017-01-14 02:34:50 +08:00
|
|
|
assert(k1 != NULL);
|
|
|
|
assert(k2 != NULL);
|
2010-02-11 02:37:56 +08:00
|
|
|
|
2017-01-20 10:15:45 +08:00
|
|
|
return !strcmp((char *)k1, (char *)k2);
|
2010-02-11 02:37:56 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2017-01-16 08:56:30 +08:00
|
|
|
ckh_pointer_hash(const void *key, size_t r_hash[2]) {
|
2010-09-21 10:20:48 +08:00
|
|
|
union {
|
|
|
|
const void *v;
|
2013-01-23 04:02:08 +08:00
|
|
|
size_t i;
|
2010-09-21 10:20:48 +08:00
|
|
|
} u;
|
2010-02-11 02:37:56 +08:00
|
|
|
|
2010-09-21 10:20:48 +08:00
|
|
|
assert(sizeof(u.v) == sizeof(u.i));
|
|
|
|
u.v = key;
|
2013-01-23 04:02:08 +08:00
|
|
|
hash(&u.i, sizeof(u.i), 0xd983396eU, r_hash);
|
2010-02-11 02:37:56 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2017-01-16 08:56:30 +08:00
|
|
|
ckh_pointer_keycomp(const void *k1, const void *k2) {
|
2017-01-20 10:15:45 +08:00
|
|
|
return (k1 == k2);
|
2010-02-11 02:37:56 +08:00
|
|
|
}
|