Skip to content

Commit aa849a4

Browse files
committed
Cache heavy parts of CalcCbTxMerkleRoot*
1 parent 2652030 commit aa849a4

File tree

1 file changed

+32
-10
lines changed

1 file changed

+32
-10
lines changed

src/evo/cbtx.cpp

+32-10
Original file line numberDiff line numberDiff line change
@@ -92,27 +92,49 @@ bool CalcCbTxMerkleRootMNList(const CBlock& block, const CBlockIndex* pindexPrev
9292

9393
CSimplifiedMNList sml(tmpMNList);
9494

95+
static CSimplifiedMNList smlCached;
96+
static uint256 merkleRootCached;
97+
static bool mutatedCached{false};
98+
99+
if (sml.mnList == smlCached.mnList) {
100+
merkleRootRet = merkleRootCached;
101+
return !mutatedCached;
102+
}
103+
95104
bool mutated = false;
96105
merkleRootRet = sml.CalcMerkleRoot(&mutated);
106+
smlCached = std::move(sml);
107+
merkleRootCached = merkleRootRet;
108+
mutatedCached = mutated;
109+
97110
return !mutated;
98111
}
99112

100113
bool CalcCbTxMerkleRootQuorums(const CBlock& block, const CBlockIndex* pindexPrev, uint256& merkleRootRet, CValidationState& state)
101114
{
115+
static std::map<Consensus::LLMQType, std::vector<const CBlockIndex*>> quorumsCached;
116+
static std::map<Consensus::LLMQType, std::vector<uint256>> qcHashesCached;
117+
102118
auto quorums = llmq::quorumBlockProcessor->GetMinedAndActiveCommitmentsUntilBlock(pindexPrev);
103119
std::map<Consensus::LLMQType, std::vector<uint256>> qcHashes;
104120
size_t hashCount = 0;
105-
for (const auto& p : quorums) {
106-
auto& v = qcHashes[p.first];
107-
v.reserve(p.second.size());
108-
for (const auto& p2 : p.second) {
109-
llmq::CFinalCommitment qc;
110-
uint256 minedBlockHash;
111-
bool found = llmq::quorumBlockProcessor->GetMinedCommitment(p.first, p2->GetBlockHash(), qc, minedBlockHash);
112-
assert(found);
113-
v.emplace_back(::SerializeHash(qc));
114-
hashCount++;
121+
if (quorums == quorumsCached) {
122+
qcHashes = qcHashesCached;
123+
} else {
124+
for (const auto& p : quorums) {
125+
auto& v = qcHashes[p.first];
126+
v.reserve(p.second.size());
127+
for (const auto& p2 : p.second) {
128+
llmq::CFinalCommitment qc;
129+
uint256 minedBlockHash;
130+
bool found = llmq::quorumBlockProcessor->GetMinedCommitment(p.first, p2->GetBlockHash(), qc, minedBlockHash);
131+
assert(found);
132+
v.emplace_back(::SerializeHash(qc));
133+
hashCount++;
134+
}
115135
}
136+
quorumsCached = quorums;
137+
qcHashesCached = qcHashes;
116138
}
117139

118140
// now add the commitments from the current block, which are not returned by GetMinedAndActiveCommitmentsUntilBlock

0 commit comments

Comments
 (0)