-
Notifications
You must be signed in to change notification settings - Fork 33
/
lru_cache.hpp
206 lines (179 loc) · 5.61 KB
/
lru_cache.hpp
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
/**
* Copyright Quadrivium LLC
* All Rights Reserved
* SPDX-License-Identifier: Apache-2.0
*/
#pragma once
#include <algorithm>
#include <cstdint>
#include <functional>
#include <memory>
#include <optional>
#include <vector>
#include <boost/assert.hpp>
#include "outcome/outcome.hpp"
#include "utils/retain_if.hpp"
namespace kagome {
template <template <bool> class Lockable, bool IsLockable>
struct LockGuard {
inline LockGuard(const Lockable<IsLockable> &) {}
};
template <template <bool> class Lockable>
struct LockGuard<Lockable, true> {
LockGuard(const Lockable<true> &protected_object)
: protected_object_(protected_object) {
protected_object_.lock();
}
~LockGuard() {
protected_object_.unlock();
}
private:
const Lockable<true> &protected_object_;
};
template <bool IsLockable>
class Lockable {
protected:
friend struct LockGuard<Lockable, IsLockable>;
inline void lock() const {}
inline void unlock() const {}
};
template <>
class Lockable<true> {
protected:
friend struct LockGuard<Lockable, true>;
inline void lock() const {
mutex_.lock();
}
inline void unlock() const {
mutex_.unlock();
}
mutable std::mutex mutex_;
};
/**
* LRU cache designed for small amounts of data (as its get() is O(N))
*/
template <typename Key,
typename Value,
bool ThreadSafe = false,
typename PriorityType = uint64_t>
struct SmallLruCache final : public Lockable<ThreadSafe> {
public:
static_assert(std::is_unsigned_v<PriorityType>);
using Mutex = Lockable<ThreadSafe>;
struct CacheEntry {
Key key;
std::shared_ptr<Value> value;
PriorityType latest_use_tick_;
bool operator<(const CacheEntry &rhs) const {
return latest_use_tick_ < rhs.latest_use_tick_;
}
};
SmallLruCache(size_t max_size) : kMaxSize{max_size} {
BOOST_ASSERT(kMaxSize > 0);
cache_.reserve(kMaxSize);
}
std::optional<std::shared_ptr<const Value>> get(const Key &key) {
LockGuard lg(*this);
if (++ticks_ == 0) {
handleTicksOverflow();
}
for (auto &entry : cache_) {
if (entry.key == key) {
entry.latest_use_tick_ = ticks_;
return entry.value;
}
}
return std::nullopt;
}
template <typename ValueArg>
std::shared_ptr<const Value> put(const Key &key, ValueArg &&value) {
LockGuard lg(*this);
static_assert(std::is_convertible_v<ValueArg, Value>
|| std::is_constructible_v<ValueArg, Value>);
if (cache_.size() >= kMaxSize) {
auto min = std::min_element(cache_.begin(), cache_.end());
cache_.erase(min);
}
if (++ticks_ == 0) {
handleTicksOverflow();
}
if constexpr (std::is_same_v<ValueArg, Value>) {
auto it = std::ranges::find_if(
cache_.begin(), cache_.end(), [&](const auto &item) {
return *item.value == value;
});
if (it != cache_.end()) {
auto &entry = cache_.emplace_back(CacheEntry{key, it->value, ticks_});
return entry.value;
}
auto &entry = cache_.emplace_back(
CacheEntry{key,
std::make_shared<Value>(std::forward<ValueArg>(value)),
ticks_});
return entry.value;
} else {
auto value_sptr =
std::make_shared<Value>(std::forward<ValueArg>(value));
auto it = std::ranges::find_if(
cache_.begin(), cache_.end(), [&](const auto &item) {
return *item.value == *value_sptr;
});
if (it != cache_.end()) {
auto &entry = cache_.emplace_back(CacheEntry{key, it->value, ticks_});
return entry.value;
}
auto &entry =
cache_.emplace_back(CacheEntry{key, std::move(value_sptr), ticks_});
return entry.value;
}
}
outcome::result<std::shared_ptr<const Value>> get_else(
const Key &key, const std::function<outcome::result<Value>()> &func) {
if (auto opt = get(key); opt.has_value()) {
return opt.value();
}
auto res = func();
if (res.has_value()) {
return put(key, std::move(res.value()));
}
return res.as_failure();
}
void erase(const Key &key) {
LockGuard lg(*this);
auto it = std::ranges::find_if(
cache_.begin(), cache_.end(), [&](const auto &item) {
return item.key == key;
});
if (it != cache_.end()) {
cache_.erase(it);
}
}
void erase_if(const std::function<bool(const Key &key, const Value &value)>
&predicate) {
LockGuard lg(*this);
retain_if(cache_, [&](const CacheEntry &item) {
return not predicate(item.key, *item.value);
});
}
private:
void handleTicksOverflow() {
// 'compress' timestamps of entries in the cache (works because we care
// only about their order, not actual timestamps)
std::sort(cache_.begin(), cache_.end());
for (auto &entry : cache_) {
entry.latest_use_tick_ = ticks_;
ticks_++;
}
}
const size_t kMaxSize;
// an abstract representation of time to implement 'recency' without
// depending on real time. Incremented on each cache access
PriorityType ticks_{};
std::vector<CacheEntry> cache_;
};
template <typename Key,
typename Value,
bool ThreadSafe = true,
typename PriorityType = uint64_t>
using LruCache = SmallLruCache<Key, Value, ThreadSafe, PriorityType>;
} // namespace kagome