Bitcoin ABC 0.32.4
P2P Digital Currency
cuckoocache.h
Go to the documentation of this file.
1// Copyright (c) 2016 Jeremy Rubin
2// Distributed under the MIT software license, see the accompanying
3// file COPYING or http://www.opensource.org/licenses/mit-license.php.
4
5#ifndef BITCOIN_CUCKOOCACHE_H
6#define BITCOIN_CUCKOOCACHE_H
7
8#include <util/fastrange.h>
9
10#include <algorithm> // std::find
11#include <array>
12#include <atomic>
13#include <cmath>
14#include <cstring>
15#include <limits>
16#include <memory>
17#include <utility>
18#include <vector>
19
32namespace CuckooCache {
47 std::unique_ptr<std::atomic<uint8_t>[]> mem;
48
49public:
52
64 explicit bit_packed_atomic_flags(uint32_t size) {
65 // pad out the size if needed
66 size = (size + 7) / 8;
67 mem.reset(new std::atomic<uint8_t>[size]);
68 for (uint32_t i = 0; i < size; ++i) {
69 mem[i].store(0xFF);
70 }
71 };
72
83 inline void setup(uint32_t b) {
85 std::swap(mem, d.mem);
86 }
87
95 inline void bit_set(uint32_t s) {
96 mem[s >> 3].fetch_or(uint8_t(1 << (s & 7)), std::memory_order_relaxed);
97 }
98
106 inline void bit_unset(uint32_t s) {
107 mem[s >> 3].fetch_and(uint8_t(~(1 << (s & 7))),
108 std::memory_order_relaxed);
109 }
110
117 inline bool bit_is_set(uint32_t s) const {
118 return (1 << (s & 7)) & mem[s >> 3].load(std::memory_order_relaxed);
119 }
120};
121
166template <typename Element, typename Hash> class cache {
167private:
169 std::vector<Element> table;
170
172 uint32_t size;
173
179
185 mutable std::vector<bool> epoch_flags;
186
194
204 uint32_t epoch_size;
205
210 uint8_t depth_limit;
211
218
222 using Key = typename Element::KeyType;
223
261 inline std::array<uint32_t, 8> compute_hashes(const Key &k) const {
262 return {{FastRange32(hash_function.template operator()<0>(k), size),
263 FastRange32(hash_function.template operator()<1>(k), size),
264 FastRange32(hash_function.template operator()<2>(k), size),
265 FastRange32(hash_function.template operator()<3>(k), size),
266 FastRange32(hash_function.template operator()<4>(k), size),
267 FastRange32(hash_function.template operator()<5>(k), size),
268 FastRange32(hash_function.template operator()<6>(k), size),
269 FastRange32(hash_function.template operator()<7>(k), size)}};
270 }
271
276 constexpr uint32_t invalid() const { return ~uint32_t(0); }
277
283 inline void allow_erase(uint32_t n) const { collection_flags.bit_set(n); }
284
290 inline void please_keep(uint32_t n) const { collection_flags.bit_unset(n); }
291
302 void epoch_check() {
303 if (epoch_heuristic_counter != 0) {
305 return;
306 }
307 // count the number of elements from the latest epoch which have not
308 // been erased.
309 uint32_t epoch_unused_count = 0;
310 for (uint32_t i = 0; i < size; ++i) {
311 epoch_unused_count +=
313 }
314 // If there are more non-deleted entries in the current epoch than the
315 // epoch size, then allow_erase on all elements in the old epoch (marked
316 // false) and move all elements in the current epoch to the old epoch
317 // but do not call allow_erase on their indices.
318 if (epoch_unused_count >= epoch_size) {
319 for (uint32_t i = 0; i < size; ++i) {
320 if (epoch_flags[i]) {
321 epoch_flags[i] = false;
322 } else {
323 allow_erase(i);
324 }
325 }
327 } else {
328 // reset the epoch_heuristic_counter to next do a scan when worst
329 // case behavior (no intermittent erases) would exceed epoch size,
330 // with a reasonable minimum scan size. Ordinarily, we would have to
331 // sanity check std::min(epoch_size, epoch_unused_count), but we
332 // already know that `epoch_unused_count < epoch_size` in this
333 // branch
334 epoch_heuristic_counter = std::max(
335 1u, std::max(epoch_size / 16, epoch_size - epoch_unused_count));
336 }
337 }
338
339public:
347 hash_function() {}
348
358 uint32_t setup(uint32_t new_size) {
359 // depth_limit must be at least one otherwise errors can occur.
360 size = std::max<uint32_t>(2, new_size);
361 depth_limit = static_cast<uint8_t>(std::log2(static_cast<float>(size)));
362 table.resize(size);
364 epoch_flags.resize(size);
365 // Set to 45% as described above
366 epoch_size = std::max((uint32_t)1, (45 * size) / 100);
367 // Initially set to wait for a whole epoch
369 return size;
370 }
371
386 std::pair<uint32_t, size_t> setup_bytes(size_t bytes) {
387 uint32_t requested_num_elems(std::min<size_t>(
388 bytes / sizeof(Element), std::numeric_limits<uint32_t>::max()));
389
390 auto num_elems = setup(requested_num_elems);
391
392 size_t approx_size_bytes = num_elems * sizeof(Element);
393 return std::make_pair(num_elems, approx_size_bytes);
394 }
395
421 inline void insert(Element e, bool replace = false) {
422 epoch_check();
423 uint32_t last_loc = invalid();
424 bool last_epoch = true;
425 std::array<uint32_t, 8> locs = compute_hashes(e.getKey());
426 // Make sure we have not already inserted this element.
427 // If we have, make sure that it does not get deleted.
428 for (const uint32_t loc : locs) {
429 if (table[loc].getKey() == e.getKey()) {
430 if (replace) {
431 table[loc] = std::move(e);
432 }
433 please_keep(loc);
434 epoch_flags[loc] = last_epoch;
435 return;
436 }
437 }
438 for (uint8_t depth = 0; depth < depth_limit; ++depth) {
439 // First try to insert to an empty slot, if one exists
440 for (const uint32_t loc : locs) {
441 if (!collection_flags.bit_is_set(loc)) {
442 continue;
443 }
444 table[loc] = std::move(e);
445 please_keep(loc);
446 epoch_flags[loc] = last_epoch;
447 return;
448 }
464 last_loc =
465 locs[(1 + (std::find(locs.begin(), locs.end(), last_loc) -
466 locs.begin())) &
467 7];
468 std::swap(table[last_loc], e);
469 // Can't std::swap a std::vector<bool>::reference and a bool&.
470 bool epoch = last_epoch;
471 last_epoch = epoch_flags[last_loc];
472 epoch_flags[last_loc] = epoch;
473
474 // Recompute the locs -- unfortunately happens one too many times!
475 locs = compute_hashes(e.getKey());
476 }
477 }
478
507 bool contains(const Key &k, const bool erase) const {
508 return find(k, erase) != nullptr;
509 }
510
523 bool get(Element &e, const bool erase) const {
524 if (const Element *eptr = find(e.getKey(), erase)) {
525 e = *eptr;
526 return true;
527 }
528
529 return false;
530 }
531
532private:
533 const Element *find(const Key &k, const bool erase) const {
534 std::array<uint32_t, 8> locs = compute_hashes(k);
535 for (const uint32_t loc : locs) {
536 if (table[loc].getKey() == k) {
537 if (erase) {
538 allow_erase(loc);
539 }
540 return &table[loc];
541 }
542 }
543 return nullptr;
544 }
545};
546
550template <typename T> struct KeyOnly : public T {
551 // For contains.
552 using KeyType = T;
553
554 // Ensure implicit conversion from T.
555 KeyOnly() = default;
556 KeyOnly(const T &x) : T(x) {}
557
558 // Implement required features.
559 const T &getKey() const { return *this; }
560};
561
562} // namespace CuckooCache
563
564#endif // BITCOIN_CUCKOOCACHE_H
bit_packed_atomic_flags implements a container for garbage collection flags that is only thread unsaf...
Definition: cuckoocache.h:46
void bit_set(uint32_t s)
bit_set sets an entry as discardable.
Definition: cuckoocache.h:95
void setup(uint32_t b)
setup marks all entries and ensures that bit_packed_atomic_flags can store at least b entries.
Definition: cuckoocache.h:83
bit_packed_atomic_flags()=delete
No default constructor, as there must be some size.
bool bit_is_set(uint32_t s) const
bit_is_set queries the table for discardability at s.
Definition: cuckoocache.h:117
void bit_unset(uint32_t s)
bit_unset marks an entry as something that should not be overwritten.
Definition: cuckoocache.h:106
bit_packed_atomic_flags(uint32_t size)
bit_packed_atomic_flags constructor creates memory to sufficiently keep track of garbage collection i...
Definition: cuckoocache.h:64
std::unique_ptr< std::atomic< uint8_t >[]> mem
Definition: cuckoocache.h:47
cache implements a cache with properties similar to a cuckoo-set.
Definition: cuckoocache.h:166
uint32_t size
size stores the total available slots in the hash table
Definition: cuckoocache.h:172
std::pair< uint32_t, size_t > setup_bytes(size_t bytes)
setup_bytes is a convenience function which accounts for internal memory usage when deciding how many...
Definition: cuckoocache.h:386
uint8_t depth_limit
depth_limit determines how many elements insert should try to replace.
Definition: cuckoocache.h:210
std::vector< Element > table
table stores all the elements
Definition: cuckoocache.h:169
typename Element::KeyType Key
Key is the key type for this map or set.
Definition: cuckoocache.h:222
uint32_t epoch_heuristic_counter
epoch_heuristic_counter is used to determine when an epoch might be aged & an expensive scan should b...
Definition: cuckoocache.h:193
std::array< uint32_t, 8 > compute_hashes(const Key &k) const
compute_hashes is convenience for not having to write out this expression everywhere we use the hash ...
Definition: cuckoocache.h:261
uint32_t setup(uint32_t new_size)
setup initializes the container to store no more than new_size elements and no less than 2 elements.
Definition: cuckoocache.h:358
void epoch_check()
epoch_check handles the changing of epochs for elements stored in the cache.
Definition: cuckoocache.h:302
bool get(Element &e, const bool erase) const
get is almost identical to contains(), with the difference that it obtains the found element (for Ele...
Definition: cuckoocache.h:523
uint32_t epoch_size
epoch_size is set to be the number of elements supposed to be in a epoch.
Definition: cuckoocache.h:204
std::vector< bool > epoch_flags
epoch_flags tracks how recently an element was inserted into the cache.
Definition: cuckoocache.h:185
bit_packed_atomic_flags collection_flags
The bit_packed_atomic_flags array is marked mutable because we want garbage collection to be allowed ...
Definition: cuckoocache.h:178
const Element * find(const Key &k, const bool erase) const
Definition: cuckoocache.h:533
bool contains(const Key &k, const bool erase) const
contains iterates through the hash locations for a given element and checks to see if it is present.
Definition: cuckoocache.h:507
constexpr uint32_t invalid() const
invalid returns a special index that can never be inserted to
Definition: cuckoocache.h:276
const Hash hash_function
hash_function is a const instance of the hash function.
Definition: cuckoocache.h:217
void allow_erase(uint32_t n) const
allow_erase marks the element at index n as discardable.
Definition: cuckoocache.h:283
void insert(Element e, bool replace=false)
insert loops at most depth_limit times trying to insert a hash at various locations in the table via ...
Definition: cuckoocache.h:421
void please_keep(uint32_t n) const
please_keep marks the element at index n as an entry that should be kept.
Definition: cuckoocache.h:290
cache()
You must always construct a cache with some elements via a subsequent call to setup or setup_bytes,...
Definition: cuckoocache.h:344
static uint32_t FastRange32(uint32_t x, uint32_t n)
This file offers implementations of the fast range reduction technique described in https://lemire....
Definition: fastrange.h:22
uint256 Hash(const T &in1)
Compute the 256-bit hash of an object.
Definition: hash.h:75
High-performance cache primitives.
Definition: cuckoocache.h:32
Helper class used when we only want the cache to be a set rather than a map.
Definition: cuckoocache.h:550
KeyOnly(const T &x)
Definition: cuckoocache.h:556
const T & getKey() const
Definition: cuckoocache.h:559