Add cache to CQuorumManager::ScanQuorums (#2856)

This commit is contained in:
Alexander Block 2019-04-11 14:42:35 +02:00 committed by UdjinM6
parent 241f76f9bf
commit ae78360e5d
2 changed files with 33 additions and 1 deletions

View File

@ -311,9 +311,29 @@ std::vector<CQuorumCPtr> CQuorumManager::ScanQuorums(Consensus::LLMQType llmqTyp
{
auto& params = Params().GetConsensus().llmqs.at(llmqType);
auto quorumIndexes = quorumBlockProcessor->GetMinedCommitmentsUntilBlock(params.type, pindexStart, maxCount);
auto cacheKey = std::make_pair(llmqType, pindexStart->GetBlockHash());
const size_t cacheMaxSize = 25; // largest active set + 1
std::vector<CQuorumCPtr> result;
if (maxCount <= cacheMaxSize) {
LOCK(quorumsCacheCs);
if (scanQuorumsCache.get(cacheKey, result)) {
if (result.size() > maxCount) {
result.resize(maxCount);
}
return result;
}
}
bool storeCache = false;
size_t maxCount2 = maxCount;
if (maxCount2 <= cacheMaxSize) {
maxCount2 = cacheMaxSize;
storeCache = true;
}
auto quorumIndexes = quorumBlockProcessor->GetMinedCommitmentsUntilBlock(params.type, pindexStart, maxCount2);
result.reserve(quorumIndexes.size());
for (auto& quorumIndex : quorumIndexes) {
@ -323,6 +343,15 @@ std::vector<CQuorumCPtr> CQuorumManager::ScanQuorums(Consensus::LLMQType llmqTyp
result.emplace_back(quorum);
}
if (storeCache) {
LOCK(quorumsCacheCs);
scanQuorumsCache.insert(cacheKey, result);
}
if (result.size() > maxCount) {
result.resize(maxCount);
}
return result;
}

View File

@ -11,6 +11,8 @@
#include "validationinterface.h"
#include "consensus/params.h"
#include "saltedhasher.h"
#include "unordered_lru_cache.h"
#include "bls/bls.h"
#include "bls/bls_worker.h"
@ -85,6 +87,7 @@ private:
CCriticalSection quorumsCacheCs;
std::map<std::pair<Consensus::LLMQType, uint256>, CQuorumPtr> quorumsCache;
unordered_lru_cache<std::pair<Consensus::LLMQType, uint256>, std::vector<CQuorumCPtr>, StaticSaltedHasher, 32> scanQuorumsCache;
public:
CQuorumManager(CEvoDB& _evoDb, CBLSWorker& _blsWorker, CDKGSessionManager& _dkgManager);