Repository navigation
Expand file tree
/
Copy pathSimpleApproximateMap.hpp
More file actions
71 lines (59 loc) · 2.04 KB
/
Copy pathSimpleApproximateMap.hpp
File metadata and controls
71 lines (59 loc) · 2.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#pragma once
#include <atomic>
#include <cstdint>
#include <memory>
class SimpleApproximateMap {
using key_t = uint64_t;
static constexpr size_t SIZE = 1.6e9;
static constexpr size_t EPOCH_BITS = 24;
static constexpr uint64_t EPOCH_RANGE = uint64_t(1) << EPOCH_BITS;
struct Entry {
uint64_t value : 8;
uint64_t epoch : EPOCH_BITS;
uint64_t fingerprint : 32;
};
static_assert(sizeof(Entry) == sizeof(uint64_t));
std::unique_ptr<std::atomic<Entry>[]> map;
uint64_t epoch = 1;
uint64_t levelStartEpoch = 1;
static uint32_t fingerprint(key_t key) {
return key >> 32;
}
public:
SimpleApproximateMap() : map(new std::atomic<Entry>[SIZE]()) {
}
void prefetch(key_t key) {
__builtin_prefetch(&map[key % SIZE]);
}
void insert(key_t key, uint8_t value) {
map[key % SIZE].store({ value, epoch % EPOCH_RANGE, fingerprint(key) }, std::memory_order_relaxed);
}
struct Result {
bool found;
uint8_t value;
bool isSameEpoch;
};
Result get(key_t key) {
Entry entry = map[key % SIZE].load(std::memory_order_relaxed);
uint64_t age = (epoch - entry.epoch) % EPOCH_RANGE;
if (entry.fingerprint == fingerprint(key) && age <= epoch - levelStartEpoch) {
return { true, static_cast<uint8_t>(entry.value), age == 0 };
}
return { false, 0, false };
}
void clear() {
nextEpoch();
levelStartEpoch = epoch;
}
void nextEpoch() {
epoch++;
if (epoch % EPOCH_RANGE == 0) {
// Stored epochs would become ambiguous after wrapping around
for (size_t i = 0; i < SIZE; i++) {
map[i].store({}, std::memory_order_relaxed);
}
epoch++;
levelStartEpoch = epoch;
}
}
};