forked from dashpay/dash
-
Notifications
You must be signed in to change notification settings - Fork 719
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge #2883: [DMNs] Implement lru cache
e3a9721 lru cache for scan quorums and get quorums (Alessandro Rezzi) 0e60d88 rlu cache quorum_hash->has_final_commitment (Alessandro Rezzi) 84cacd8 Add lru cache (Alessandro Rezzi) Pull request description: A LRU cache is an (unordered) map where, once it is full, the least recently used element is removed. This PR adds a LRU cache that maps `quorumHash` -> `bool` where `bool` is `true` iff the quorum has been mined on chain. It is a big optimization compared to checking the database each time ACKs for top commit: e3a9721 Fuzzbawls: ACK e3a9721 Liquid369: tACK e3a9721 Tree-SHA512: a90bf80b5ed6d9ed0caf384614fb55000c34c05088861ffc567a20015389ddd61c2dd8bd4b35110a835a06611da147344e201443b9d31b971313f76443821760
- Loading branch information
Showing
8 changed files
with
203 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,115 @@ | ||
// Copyright (c) 2019-2021 The Dash Core developers | ||
// Copyright (c) 2023 The Dash Core developers | ||
// Distributed under the MIT software license, see the accompanying | ||
// file COPYING or http://www.opensource.org/licenses/mit-license.php. | ||
|
||
#ifndef PIVX_UNORDERED_LRU_CACHE_H | ||
#define PIVX_UNORDERED_LRU_CACHE_H | ||
|
||
#include <algorithm> | ||
#include <cassert> | ||
#include <cstdint> | ||
#include <unordered_map> | ||
#include <vector> | ||
|
||
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: | ||
explicit unordered_lru_cache(size_t _maxSize = MaxSize, size_t _truncateThreshold = TruncateThreshold) : maxSize(_maxSize), | ||
truncateThreshold(_truncateThreshold == 0 ? _maxSize * 2 : _truncateThreshold) | ||
{ | ||
// either specify maxSize through template arguments or the constructor and fail otherwise | ||
assert(_maxSize != 0); | ||
} | ||
|
||
size_t max_size() const { return maxSize; } | ||
|
||
template<typename Value2> | ||
void _emplace(const Key& key, Value2&& v) | ||
{ | ||
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++; | ||
} | ||
truncate_if_needed(); | ||
} | ||
|
||
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]); | ||
} | ||
} | ||
}; | ||
|
||
#endif // PIVX_UNORDERED_LRU_CACHE_H |