forked from hove-io/utils
-
Notifications
You must be signed in to change notification settings - Fork 0
/
lru.h
206 lines (175 loc) · 7.11 KB
/
lru.h
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 © 2001-2015, Hove and/or its affiliates. All rights reserved.
This file is part of Navitia,
the software to build cool stuff with public transport.
Hope you'll enjoy and contribute to this project,
powered by Hove (www.hove.com).
Help us simplify mobility and open public transport:
a non ending quest to the responsive locomotion way of traveling!
LICENCE: This program is free software; you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
Stay tuned using
twitter @navitia
IRC #navitia on freenode
https://groups.google.com/d/forum/navitia
www.navitia.io
*/
#include "functions.h"
#include <boost/multi_index_container.hpp>
#include <boost/multi_index/ordered_index.hpp>
#include <boost/multi_index/sequenced_index.hpp>
#include <boost/multi_index/member.hpp>
#include <boost/type_traits/remove_cv.hpp>
#include <boost/type_traits/remove_reference.hpp>
#include <boost/range/adaptor/reversed.hpp>
#include <mutex>
#include <future>
#include <stdexcept>
namespace navitia {
// forward declare
template <typename T>
struct ConcurrentLru;
// Encapsulate a unary function, and provide a least recently used
// cache. The function must be pure (same argument => same result),
// and a Lru object must not be shared across threads.
template <typename F>
class Lru {
private:
typedef typename boost::remove_cv<typename boost::remove_reference<typename F::argument_type>::type>::type key_type;
using mapped_type =
typename boost::remove_cv<typename boost::remove_reference<typename F::result_type>::type>::type;
using value_type = std::pair<const key_type, mapped_type>;
using Cache = boost::multi_index_container<
value_type,
boost::multi_index::indexed_by<
boost::multi_index::sequenced<>,
boost::multi_index::ordered_unique<
boost::multi_index::member<value_type, const key_type, &value_type::first>>>>;
// the encapsulate function
F f;
// maximal cached values
size_t max_cache;
// the cache, mutable because side effect are not visible from the
// exterior because of the purity of f
mutable Cache cache;
mutable size_t nb_cache_miss = 0;
mutable size_t nb_calls = 0;
std::vector<key_type> keys() const {
auto& list = cache.template get<0>();
std::vector<key_type> result;
for (const auto& p : list) {
result.push_back(p.first);
}
return result;
}
template <typename T>
friend struct ConcurrentLru;
public:
using result_type = const mapped_type&;
using argument_type = typename F::argument_type;
Lru(F fun, size_t max = 10) : f(std::move(fun)), max_cache(max) {
if (max < 1) {
throw std::invalid_argument("max (size of cache) must be strictly positive");
}
}
result_type operator()(argument_type arg) const {
++nb_calls;
auto& list = cache.template get<0>();
auto& map = cache.template get<1>();
const auto search = map.find(arg);
if (search != map.end()) {
// put the cached value at the begining of the cache
list.relocate(list.begin(), cache.template project<0>(search));
return search->second;
} else {
++nb_cache_miss;
// insert the new value at the begining of the cache
const auto ins = list.push_front(std::make_pair(arg, f(arg)));
// clean the cache by the end (where the entries are the
// older ones) until the requested size
while (list.size() > max_cache) {
list.pop_back();
}
return ins.first->second;
}
}
size_t get_nb_cache_miss() const { return nb_cache_miss; }
size_t get_nb_calls() const { return nb_calls; }
size_t get_max_size() const { return max_cache; }
};
template <typename F>
inline Lru<F> make_lru(F&& fun, size_t max = 10) {
return Lru<F>(std::forward<F>(fun), max);
}
template <typename F>
struct ConcurrentLru {
private:
struct SharedPtrF {
F f;
using argument_type = typename F::argument_type;
using underlying_type =
typename boost::remove_cv<typename boost::remove_reference<typename F::result_type>::type>::type const;
using result_type = std::shared_future<std::shared_ptr<underlying_type>>;
result_type operator()(argument_type arg) const {
// build a future that will be lazy initialized
return std::async(std::launch::deferred, [&]() { return std::make_shared<underlying_type>(f(arg)); })
.share();
}
};
Lru<SharedPtrF> lru;
std::unique_ptr<std::mutex> mutex{std::make_unique<std::mutex>()};
std::vector<typename Lru<SharedPtrF>::key_type> keys() const {
std::lock_guard<std::mutex> lock(*mutex);
return lru.keys();
}
public:
using result_type = typename std::shared_ptr<typename SharedPtrF::underlying_type>;
using argument_type = typename SharedPtrF::argument_type;
ConcurrentLru(F fun, size_t max = 10) : lru(SharedPtrF{std::move(fun)}, max) {}
ConcurrentLru(ConcurrentLru&&) = default; // NOLINT // needed by old version of gcc
result_type operator()(argument_type arg) const {
typename SharedPtrF::result_type future;
{
std::lock_guard<std::mutex> lock(*mutex);
future = lru(arg);
}
// As arg might be a reference, the maybe newly created future must be run
// before the end of the current method, else we can have a use after free.
return future.get();
}
// We add mutex lock in all get_nb_** functions :
// Without that a race condition could occurs if operator()
// or warmup() functions are used by a thread and get_nb_** functions by another thread
// in the same time
size_t get_nb_cache_miss() const {
std::lock_guard<std::mutex> lock(*mutex);
return lru.get_nb_cache_miss();
}
size_t get_nb_calls() const {
std::lock_guard<std::mutex> lock(*mutex);
return lru.get_nb_calls();
}
size_t get_max_size() const {
std::lock_guard<std::mutex> lock(*mutex);
return lru.get_max_size();
}
void warmup(const ConcurrentLru<F>& other) {
// we can't use the warmup of the lru direclty as it will mess with the future
auto keys = other.keys();
for (const auto& key : boost::adaptors::reverse(keys)) {
this->operator()(key);
}
}
};
template <typename F>
inline ConcurrentLru<F> make_concurrent_lru(F&& fun, size_t max = 10) {
return ConcurrentLru<F>(std::forward<F>(fun), max);
}
} // namespace navitia