ckh.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569
  1. /*
  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
  6. * in:
  7. *
  8. * Pagh, R., F.F. Rodler (2004) Cuckoo Hashing. Journal of Algorithms
  9. * 51(2):122-144.
  10. *
  11. * Generalization of cuckoo hashing was discussed in:
  12. *
  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,
  16. * January 2006.
  17. *
  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
  22. * configurations:
  23. *
  24. * | #cells/bucket |
  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 | |
  31. *
  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,
  34. * respectively.
  35. *
  36. ******************************************************************************/
  37. #include "jemalloc/internal/jemalloc_preamble.h"
  38. #include "jemalloc/internal/ckh.h"
  39. #include "jemalloc/internal/jemalloc_internal_includes.h"
  40. #include "jemalloc/internal/assert.h"
  41. #include "jemalloc/internal/hash.h"
  42. #include "jemalloc/internal/malloc_io.h"
  43. #include "jemalloc/internal/prng.h"
  44. #include "jemalloc/internal/util.h"
  45. /******************************************************************************/
  46. /* Function prototypes for non-inline static functions. */
  47. static bool ckh_grow(tsd_t *tsd, ckh_t *ckh);
  48. static void ckh_shrink(tsd_t *tsd, ckh_t *ckh);
  49. /******************************************************************************/
  50. /*
  51. * Search bucket for key and return the cell number if found; SIZE_T_MAX
  52. * otherwise.
  53. */
  54. static size_t
  55. ckh_bucket_search(ckh_t *ckh, size_t bucket, const void *key) {
  56. ckhc_t *cell;
  57. unsigned i;
  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;
  62. }
  63. }
  64. return SIZE_T_MAX;
  65. }
  66. /*
  67. * Search table for key and return cell number if found; SIZE_T_MAX otherwise.
  68. */
  69. static size_t
  70. ckh_isearch(ckh_t *ckh, const void *key) {
  71. size_t hashes[2], bucket, cell;
  72. assert(ckh != NULL);
  73. ckh->hash(key, hashes);
  74. /* Search primary bucket. */
  75. bucket = hashes[0] & ((ZU(1) << ckh->lg_curbuckets) - 1);
  76. cell = ckh_bucket_search(ckh, bucket, key);
  77. if (cell != SIZE_T_MAX) {
  78. return cell;
  79. }
  80. /* Search secondary bucket. */
  81. bucket = hashes[1] & ((ZU(1) << ckh->lg_curbuckets) - 1);
  82. cell = ckh_bucket_search(ckh, bucket, key);
  83. return cell;
  84. }
  85. static bool
  86. ckh_try_bucket_insert(ckh_t *ckh, size_t bucket, const void *key,
  87. const void *data) {
  88. ckhc_t *cell;
  89. unsigned offset, i;
  90. /*
  91. * Cycle through the cells in the bucket, starting at a random position.
  92. * The randomness avoids worst-case search overhead as buckets fill up.
  93. */
  94. offset = (unsigned)prng_lg_range_u64(&ckh->prng_state,
  95. LG_CKH_BUCKET_CELLS);
  96. for (i = 0; i < (ZU(1) << LG_CKH_BUCKET_CELLS); i++) {
  97. cell = &ckh->tab[(bucket << LG_CKH_BUCKET_CELLS) +
  98. ((i + offset) & ((ZU(1) << LG_CKH_BUCKET_CELLS) - 1))];
  99. if (cell->key == NULL) {
  100. cell->key = key;
  101. cell->data = data;
  102. ckh->count++;
  103. return false;
  104. }
  105. }
  106. return true;
  107. }
  108. /*
  109. * No space is available in bucket. Randomly evict an item, then try to find an
  110. * alternate location for that item. Iteratively repeat this
  111. * eviction/relocation procedure until either success or detection of an
  112. * eviction/relocation bucket cycle.
  113. */
  114. static bool
  115. ckh_evict_reloc_insert(ckh_t *ckh, size_t argbucket, void const **argkey,
  116. void const **argdata) {
  117. const void *key, *data, *tkey, *tdata;
  118. ckhc_t *cell;
  119. size_t hashes[2], bucket, tbucket;
  120. unsigned i;
  121. bucket = argbucket;
  122. key = *argkey;
  123. data = *argdata;
  124. while (true) {
  125. /*
  126. * Choose a random item within the bucket to evict. This is
  127. * critical to correct function, because without (eventually)
  128. * evicting all items within a bucket during iteration, it
  129. * would be possible to get stuck in an infinite loop if there
  130. * were an item for which both hashes indicated the same
  131. * bucket.
  132. */
  133. i = (unsigned)prng_lg_range_u64(&ckh->prng_state,
  134. LG_CKH_BUCKET_CELLS);
  135. cell = &ckh->tab[(bucket << LG_CKH_BUCKET_CELLS) + i];
  136. assert(cell->key != NULL);
  137. /* Swap cell->{key,data} and {key,data} (evict). */
  138. tkey = cell->key; tdata = cell->data;
  139. cell->key = key; cell->data = data;
  140. key = tkey; data = tdata;
  141. #ifdef CKH_COUNT
  142. ckh->nrelocs++;
  143. #endif
  144. /* Find the alternate bucket for the evicted item. */
  145. ckh->hash(key, hashes);
  146. tbucket = hashes[1] & ((ZU(1) << ckh->lg_curbuckets) - 1);
  147. if (tbucket == bucket) {
  148. tbucket = hashes[0] & ((ZU(1) << ckh->lg_curbuckets)
  149. - 1);
  150. /*
  151. * It may be that (tbucket == bucket) still, if the
  152. * item's hashes both indicate this bucket. However,
  153. * we are guaranteed to eventually escape this bucket
  154. * during iteration, assuming pseudo-random item
  155. * selection (true randomness would make infinite
  156. * looping a remote possibility). The reason we can
  157. * never get trapped forever is that there are two
  158. * cases:
  159. *
  160. * 1) This bucket == argbucket, so we will quickly
  161. * detect an eviction cycle and terminate.
  162. * 2) An item was evicted to this bucket from another,
  163. * which means that at least one item in this bucket
  164. * has hashes that indicate distinct buckets.
  165. */
  166. }
  167. /* Check for a cycle. */
  168. if (tbucket == argbucket) {
  169. *argkey = key;
  170. *argdata = data;
  171. return true;
  172. }
  173. bucket = tbucket;
  174. if (!ckh_try_bucket_insert(ckh, bucket, key, data)) {
  175. return false;
  176. }
  177. }
  178. }
  179. static bool
  180. ckh_try_insert(ckh_t *ckh, void const**argkey, void const**argdata) {
  181. size_t hashes[2], bucket;
  182. const void *key = *argkey;
  183. const void *data = *argdata;
  184. ckh->hash(key, hashes);
  185. /* Try to insert in primary bucket. */
  186. bucket = hashes[0] & ((ZU(1) << ckh->lg_curbuckets) - 1);
  187. if (!ckh_try_bucket_insert(ckh, bucket, key, data)) {
  188. return false;
  189. }
  190. /* Try to insert in secondary bucket. */
  191. bucket = hashes[1] & ((ZU(1) << ckh->lg_curbuckets) - 1);
  192. if (!ckh_try_bucket_insert(ckh, bucket, key, data)) {
  193. return false;
  194. }
  195. /*
  196. * Try to find a place for this item via iterative eviction/relocation.
  197. */
  198. return ckh_evict_reloc_insert(ckh, bucket, argkey, argdata);
  199. }
  200. /*
  201. * Try to rebuild the hash table from scratch by inserting all items from the
  202. * old table into the new.
  203. */
  204. static bool
  205. ckh_rebuild(ckh_t *ckh, ckhc_t *aTab) {
  206. size_t count, i, nins;
  207. const void *key, *data;
  208. count = ckh->count;
  209. ckh->count = 0;
  210. for (i = nins = 0; nins < count; i++) {
  211. if (aTab[i].key != NULL) {
  212. key = aTab[i].key;
  213. data = aTab[i].data;
  214. if (ckh_try_insert(ckh, &key, &data)) {
  215. ckh->count = count;
  216. return true;
  217. }
  218. nins++;
  219. }
  220. }
  221. return false;
  222. }
  223. static bool
  224. ckh_grow(tsd_t *tsd, ckh_t *ckh) {
  225. bool ret;
  226. ckhc_t *tab, *ttab;
  227. unsigned lg_prevbuckets, lg_curcells;
  228. #ifdef CKH_COUNT
  229. ckh->ngrows++;
  230. #endif
  231. /*
  232. * It is possible (though unlikely, given well behaved hashes) that the
  233. * table will have to be doubled more than once in order to create a
  234. * usable table.
  235. */
  236. lg_prevbuckets = ckh->lg_curbuckets;
  237. lg_curcells = ckh->lg_curbuckets + LG_CKH_BUCKET_CELLS;
  238. while (true) {
  239. size_t usize;
  240. lg_curcells++;
  241. usize = sz_sa2u(sizeof(ckhc_t) << lg_curcells, CACHELINE);
  242. if (unlikely(usize == 0
  243. || usize > SC_LARGE_MAXCLASS)) {
  244. ret = true;
  245. goto label_return;
  246. }
  247. tab = (ckhc_t *)ipallocztm(tsd_tsdn(tsd), usize, CACHELINE,
  248. true, NULL, true, arena_ichoose(tsd, NULL));
  249. if (tab == NULL) {
  250. ret = true;
  251. goto label_return;
  252. }
  253. /* Swap in new table. */
  254. ttab = ckh->tab;
  255. ckh->tab = tab;
  256. tab = ttab;
  257. ckh->lg_curbuckets = lg_curcells - LG_CKH_BUCKET_CELLS;
  258. if (!ckh_rebuild(ckh, tab)) {
  259. idalloctm(tsd_tsdn(tsd), tab, NULL, NULL, true, true);
  260. break;
  261. }
  262. /* Rebuilding failed, so back out partially rebuilt table. */
  263. idalloctm(tsd_tsdn(tsd), ckh->tab, NULL, NULL, true, true);
  264. ckh->tab = tab;
  265. ckh->lg_curbuckets = lg_prevbuckets;
  266. }
  267. ret = false;
  268. label_return:
  269. return ret;
  270. }
  271. static void
  272. ckh_shrink(tsd_t *tsd, ckh_t *ckh) {
  273. ckhc_t *tab, *ttab;
  274. size_t usize;
  275. unsigned lg_prevbuckets, lg_curcells;
  276. /*
  277. * It is possible (though unlikely, given well behaved hashes) that the
  278. * table rebuild will fail.
  279. */
  280. lg_prevbuckets = ckh->lg_curbuckets;
  281. lg_curcells = ckh->lg_curbuckets + LG_CKH_BUCKET_CELLS - 1;
  282. usize = sz_sa2u(sizeof(ckhc_t) << lg_curcells, CACHELINE);
  283. if (unlikely(usize == 0 || usize > SC_LARGE_MAXCLASS)) {
  284. return;
  285. }
  286. tab = (ckhc_t *)ipallocztm(tsd_tsdn(tsd), usize, CACHELINE, true, NULL,
  287. true, arena_ichoose(tsd, NULL));
  288. if (tab == NULL) {
  289. /*
  290. * An OOM error isn't worth propagating, since it doesn't
  291. * prevent this or future operations from proceeding.
  292. */
  293. return;
  294. }
  295. /* Swap in new table. */
  296. ttab = ckh->tab;
  297. ckh->tab = tab;
  298. tab = ttab;
  299. ckh->lg_curbuckets = lg_curcells - LG_CKH_BUCKET_CELLS;
  300. if (!ckh_rebuild(ckh, tab)) {
  301. idalloctm(tsd_tsdn(tsd), tab, NULL, NULL, true, true);
  302. #ifdef CKH_COUNT
  303. ckh->nshrinks++;
  304. #endif
  305. return;
  306. }
  307. /* Rebuilding failed, so back out partially rebuilt table. */
  308. idalloctm(tsd_tsdn(tsd), ckh->tab, NULL, NULL, true, true);
  309. ckh->tab = tab;
  310. ckh->lg_curbuckets = lg_prevbuckets;
  311. #ifdef CKH_COUNT
  312. ckh->nshrinkfails++;
  313. #endif
  314. }
  315. bool
  316. ckh_new(tsd_t *tsd, ckh_t *ckh, size_t minitems, ckh_hash_t *ckh_hash,
  317. ckh_keycomp_t *keycomp) {
  318. bool ret;
  319. size_t mincells, usize;
  320. unsigned lg_mincells;
  321. assert(minitems > 0);
  322. assert(ckh_hash != NULL);
  323. assert(keycomp != NULL);
  324. #ifdef CKH_COUNT
  325. ckh->ngrows = 0;
  326. ckh->nshrinks = 0;
  327. ckh->nshrinkfails = 0;
  328. ckh->ninserts = 0;
  329. ckh->nrelocs = 0;
  330. #endif
  331. ckh->prng_state = 42; /* Value doesn't really matter. */
  332. ckh->count = 0;
  333. /*
  334. * Find the minimum power of 2 that is large enough to fit minitems
  335. * entries. We are using (2+,2) cuckoo hashing, which has an expected
  336. * maximum load factor of at least ~0.86, so 0.75 is a conservative load
  337. * factor that will typically allow mincells items to fit without ever
  338. * growing the table.
  339. */
  340. assert(LG_CKH_BUCKET_CELLS > 0);
  341. mincells = ((minitems + (3 - (minitems % 3))) / 3) << 2;
  342. for (lg_mincells = LG_CKH_BUCKET_CELLS;
  343. (ZU(1) << lg_mincells) < mincells;
  344. lg_mincells++) {
  345. /* Do nothing. */
  346. }
  347. ckh->lg_minbuckets = lg_mincells - LG_CKH_BUCKET_CELLS;
  348. ckh->lg_curbuckets = lg_mincells - LG_CKH_BUCKET_CELLS;
  349. ckh->hash = ckh_hash;
  350. ckh->keycomp = keycomp;
  351. usize = sz_sa2u(sizeof(ckhc_t) << lg_mincells, CACHELINE);
  352. if (unlikely(usize == 0 || usize > SC_LARGE_MAXCLASS)) {
  353. ret = true;
  354. goto label_return;
  355. }
  356. ckh->tab = (ckhc_t *)ipallocztm(tsd_tsdn(tsd), usize, CACHELINE, true,
  357. NULL, true, arena_ichoose(tsd, NULL));
  358. if (ckh->tab == NULL) {
  359. ret = true;
  360. goto label_return;
  361. }
  362. ret = false;
  363. label_return:
  364. return ret;
  365. }
  366. void
  367. ckh_delete(tsd_t *tsd, ckh_t *ckh) {
  368. assert(ckh != NULL);
  369. #ifdef CKH_VERBOSE
  370. malloc_printf(
  371. "%s(%p): ngrows: %"FMTu64", nshrinks: %"FMTu64","
  372. " nshrinkfails: %"FMTu64", ninserts: %"FMTu64","
  373. " nrelocs: %"FMTu64"\n", __func__, ckh,
  374. (unsigned long long)ckh->ngrows,
  375. (unsigned long long)ckh->nshrinks,
  376. (unsigned long long)ckh->nshrinkfails,
  377. (unsigned long long)ckh->ninserts,
  378. (unsigned long long)ckh->nrelocs);
  379. #endif
  380. idalloctm(tsd_tsdn(tsd), ckh->tab, NULL, NULL, true, true);
  381. if (config_debug) {
  382. memset(ckh, JEMALLOC_FREE_JUNK, sizeof(ckh_t));
  383. }
  384. }
  385. size_t
  386. ckh_count(ckh_t *ckh) {
  387. assert(ckh != NULL);
  388. return ckh->count;
  389. }
  390. bool
  391. ckh_iter(ckh_t *ckh, size_t *tabind, void **key, void **data) {
  392. size_t i, ncells;
  393. for (i = *tabind, ncells = (ZU(1) << (ckh->lg_curbuckets +
  394. LG_CKH_BUCKET_CELLS)); i < ncells; i++) {
  395. if (ckh->tab[i].key != NULL) {
  396. if (key != NULL) {
  397. *key = (void *)ckh->tab[i].key;
  398. }
  399. if (data != NULL) {
  400. *data = (void *)ckh->tab[i].data;
  401. }
  402. *tabind = i + 1;
  403. return false;
  404. }
  405. }
  406. return true;
  407. }
  408. bool
  409. ckh_insert(tsd_t *tsd, ckh_t *ckh, const void *key, const void *data) {
  410. bool ret;
  411. assert(ckh != NULL);
  412. assert(ckh_search(ckh, key, NULL, NULL));
  413. #ifdef CKH_COUNT
  414. ckh->ninserts++;
  415. #endif
  416. while (ckh_try_insert(ckh, &key, &data)) {
  417. if (ckh_grow(tsd, ckh)) {
  418. ret = true;
  419. goto label_return;
  420. }
  421. }
  422. ret = false;
  423. label_return:
  424. return ret;
  425. }
  426. bool
  427. ckh_remove(tsd_t *tsd, ckh_t *ckh, const void *searchkey, void **key,
  428. void **data) {
  429. size_t cell;
  430. assert(ckh != NULL);
  431. cell = ckh_isearch(ckh, searchkey);
  432. if (cell != SIZE_T_MAX) {
  433. if (key != NULL) {
  434. *key = (void *)ckh->tab[cell].key;
  435. }
  436. if (data != NULL) {
  437. *data = (void *)ckh->tab[cell].data;
  438. }
  439. ckh->tab[cell].key = NULL;
  440. ckh->tab[cell].data = NULL; /* Not necessary. */
  441. ckh->count--;
  442. /* Try to halve the table if it is less than 1/4 full. */
  443. if (ckh->count < (ZU(1) << (ckh->lg_curbuckets
  444. + LG_CKH_BUCKET_CELLS - 2)) && ckh->lg_curbuckets
  445. > ckh->lg_minbuckets) {
  446. /* Ignore error due to OOM. */
  447. ckh_shrink(tsd, ckh);
  448. }
  449. return false;
  450. }
  451. return true;
  452. }
  453. bool
  454. ckh_search(ckh_t *ckh, const void *searchkey, void **key, void **data) {
  455. size_t cell;
  456. assert(ckh != NULL);
  457. cell = ckh_isearch(ckh, searchkey);
  458. if (cell != SIZE_T_MAX) {
  459. if (key != NULL) {
  460. *key = (void *)ckh->tab[cell].key;
  461. }
  462. if (data != NULL) {
  463. *data = (void *)ckh->tab[cell].data;
  464. }
  465. return false;
  466. }
  467. return true;
  468. }
  469. void
  470. ckh_string_hash(const void *key, size_t r_hash[2]) {
  471. hash(key, strlen((const char *)key), 0x94122f33U, r_hash);
  472. }
  473. bool
  474. ckh_string_keycomp(const void *k1, const void *k2) {
  475. assert(k1 != NULL);
  476. assert(k2 != NULL);
  477. return !strcmp((char *)k1, (char *)k2);
  478. }
  479. void
  480. ckh_pointer_hash(const void *key, size_t r_hash[2]) {
  481. union {
  482. const void *v;
  483. size_t i;
  484. } u;
  485. assert(sizeof(u.v) == sizeof(u.i));
  486. u.v = key;
  487. hash(&u.i, sizeof(u.i), 0xd983396eU, r_hash);
  488. }
  489. bool
  490. ckh_pointer_keycomp(const void *k1, const void *k2) {
  491. return (k1 == k2);
  492. }