Skip to content

Commit

Permalink
Add cache to CQuorumManager::ScanQuorums (#2856)
Browse files Browse the repository at this point in the history
  • Loading branch information
codablock authored and UdjinM6 committed Apr 11, 2019
1 parent 241f76f commit ae78360
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 1 deletion.
31 changes: 30 additions & 1 deletion src/llmq/quorums.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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;
}

Expand Down
3 changes: 3 additions & 0 deletions src/llmq/quorums.h
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -85,6 +87,7 @@ class CQuorumManager

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);
Expand Down

0 comments on commit ae78360

Please sign in to comment.