2021-04-20 21:33:02 +02:00
|
|
|
// Copyright (c) 2019-2021 The Dash Core developers
|
2019-03-11 06:33:46 +01:00
|
|
|
// Distributed under the MIT software license, see the accompanying
|
|
|
|
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
|
|
|
|
2018-04-02 00:30:17 +02:00
|
|
|
#ifndef BITCOIN_UNORDERED_LRU_CACHE_H
|
|
|
|
#define BITCOIN_UNORDERED_LRU_CACHE_H
|
2019-03-11 06:33:46 +01:00
|
|
|
|
2021-07-31 20:29:12 +02:00
|
|
|
#include <algorithm>
|
|
|
|
#include <cassert>
|
2019-03-11 06:33:46 +01:00
|
|
|
#include <unordered_map>
|
2021-07-31 20:29:12 +02:00
|
|
|
#include <vector>
|
2019-03-11 06:33:46 +01:00
|
|
|
|
|
|
|
template<typename Key, typename Value, typename Hasher, size_t MaxSize = 0, size_t TruncateThreshold = 0>
|
|
|
|
class unordered_lru_cache
|
|
|
|
{
|
|
|
|
private:
|
|
|
|
typedef std::unordered_map<Key, std::pair<Value, int64_t>, Hasher> MapType;
|
|
|
|
|
|
|
|
MapType cacheMap;
|
|
|
|
size_t maxSize;
|
|
|
|
size_t truncateThreshold;
|
|
|
|
int64_t accessCounter{0};
|
|
|
|
|
|
|
|
public:
|
2020-01-05 01:55:41 +01:00
|
|
|
explicit unordered_lru_cache(size_t _maxSize = MaxSize, size_t _truncateThreshold = TruncateThreshold) :
|
2019-03-11 06:33:46 +01:00
|
|
|
maxSize(_maxSize),
|
|
|
|
truncateThreshold(_truncateThreshold == 0 ? _maxSize * 2 : _truncateThreshold)
|
|
|
|
{
|
2021-07-17 21:15:21 +02:00
|
|
|
// either specify maxSize through template arguments or the constructor and fail otherwise
|
2019-03-11 06:33:46 +01:00
|
|
|
assert(_maxSize != 0);
|
|
|
|
}
|
|
|
|
|
2021-01-25 10:22:28 +01:00
|
|
|
size_t max_size() const { return maxSize; }
|
2019-03-11 06:33:46 +01:00
|
|
|
|
|
|
|
template<typename Value2>
|
|
|
|
void _emplace(const Key& key, Value2&& v)
|
|
|
|
{
|
|
|
|
truncate_if_needed();
|
|
|
|
auto it = cacheMap.find(key);
|
|
|
|
if (it == cacheMap.end()) {
|
|
|
|
cacheMap.emplace(key, std::make_pair(std::forward<Value2>(v), accessCounter++));
|
|
|
|
} else {
|
|
|
|
it->second.first = std::forward<Value2>(v);
|
|
|
|
it->second.second = accessCounter++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void emplace(const Key& key, Value&& v)
|
|
|
|
{
|
|
|
|
_emplace(key, v);
|
|
|
|
}
|
|
|
|
|
|
|
|
void insert(const Key& key, const Value& v)
|
|
|
|
{
|
|
|
|
_emplace(key, v);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool get(const Key& key, Value& value)
|
|
|
|
{
|
|
|
|
auto it = cacheMap.find(key);
|
|
|
|
if (it != cacheMap.end()) {
|
|
|
|
it->second.second = accessCounter++;
|
|
|
|
value = it->second.first;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool exists(const Key& key)
|
|
|
|
{
|
|
|
|
auto it = cacheMap.find(key);
|
|
|
|
if (it != cacheMap.end()) {
|
|
|
|
it->second.second = accessCounter++;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void erase(const Key& key)
|
|
|
|
{
|
|
|
|
cacheMap.erase(key);
|
|
|
|
}
|
|
|
|
|
|
|
|
void clear()
|
|
|
|
{
|
|
|
|
cacheMap.clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
void truncate_if_needed()
|
|
|
|
{
|
|
|
|
typedef typename MapType::iterator Iterator;
|
|
|
|
|
|
|
|
if (cacheMap.size() <= truncateThreshold) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::vector<Iterator> vec;
|
|
|
|
vec.reserve(cacheMap.size());
|
|
|
|
for (auto it = cacheMap.begin(); it != cacheMap.end(); ++it) {
|
|
|
|
vec.emplace_back(it);
|
|
|
|
}
|
|
|
|
// sort by last access time (descending order)
|
|
|
|
std::sort(vec.begin(), vec.end(), [](const Iterator& it1, const Iterator& it2) {
|
|
|
|
return it1->second.second > it2->second.second;
|
|
|
|
});
|
|
|
|
|
|
|
|
for (size_t i = maxSize; i < vec.size(); i++) {
|
|
|
|
cacheMap.erase(vec[i]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2018-04-02 00:30:17 +02:00
|
|
|
#endif // BITCOIN_UNORDERED_LRU_CACHE_H
|