Skip to content

Commit 4dee7c4

Browse files
authored
Cache heavy parts of CalcCbTxMerkleRoot* (#2885)
1 parent b3ed641 commit 4dee7c4

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
@@ -121,12 +121,25 @@ bool CalcCbTxMerkleRootMNList(const CBlock& block, const CBlockIndex* pindexPrev
121121
int64_t nTime3 = GetTimeMicros(); nTimeSMNL += nTime3 - nTime2;
122122
LogPrint("bench", " - CSimplifiedMNList: %.2fms [%.2fs]\n", 0.001 * (nTime3 - nTime2), nTimeSMNL * 0.000001);
123123

124+
static CSimplifiedMNList smlCached;
125+
static uint256 merkleRootCached;
126+
static bool mutatedCached{false};
127+
128+
if (sml.mnList == smlCached.mnList) {
129+
merkleRootRet = merkleRootCached;
130+
return !mutatedCached;
131+
}
132+
124133
bool mutated = false;
125134
merkleRootRet = sml.CalcMerkleRoot(&mutated);
126135

127136
int64_t nTime4 = GetTimeMicros(); nTimeMerkle += nTime4 - nTime3;
128137
LogPrint("bench", " - CalcMerkleRoot: %.2fms [%.2fs]\n", 0.001 * (nTime4 - nTime3), nTimeMerkle * 0.000001);
129138

139+
smlCached = std::move(sml);
140+
merkleRootCached = merkleRootRet;
141+
mutatedCached = mutated;
142+
130143
return !mutated;
131144
}
132145

@@ -139,24 +152,33 @@ bool CalcCbTxMerkleRootQuorums(const CBlock& block, const CBlockIndex* pindexPre
139152

140153
int64_t nTime1 = GetTimeMicros();
141154

155+
static std::map<Consensus::LLMQType, std::vector<const CBlockIndex*>> quorumsCached;
156+
static std::map<Consensus::LLMQType, std::vector<uint256>> qcHashesCached;
157+
142158
auto quorums = llmq::quorumBlockProcessor->GetMinedAndActiveCommitmentsUntilBlock(pindexPrev);
143159
std::map<Consensus::LLMQType, std::vector<uint256>> qcHashes;
144160
size_t hashCount = 0;
145161

146162
int64_t nTime2 = GetTimeMicros(); nTimeMinedAndActive += nTime2 - nTime1;
147163
LogPrint("bench", " - GetMinedAndActiveCommitmentsUntilBlock: %.2fms [%.2fs]\n", 0.001 * (nTime2 - nTime1), nTimeMinedAndActive * 0.000001);
148164

149-
for (const auto& p : quorums) {
150-
auto& v = qcHashes[p.first];
151-
v.reserve(p.second.size());
152-
for (const auto& p2 : p.second) {
153-
llmq::CFinalCommitment qc;
154-
uint256 minedBlockHash;
155-
bool found = llmq::quorumBlockProcessor->GetMinedCommitment(p.first, p2->GetBlockHash(), qc, minedBlockHash);
156-
assert(found);
157-
v.emplace_back(::SerializeHash(qc));
158-
hashCount++;
165+
if (quorums == quorumsCached) {
166+
qcHashes = qcHashesCached;
167+
} else {
168+
for (const auto& p : quorums) {
169+
auto& v = qcHashes[p.first];
170+
v.reserve(p.second.size());
171+
for (const auto& p2 : p.second) {
172+
llmq::CFinalCommitment qc;
173+
uint256 minedBlockHash;
174+
bool found = llmq::quorumBlockProcessor->GetMinedCommitment(p.first, p2->GetBlockHash(), qc, minedBlockHash);
175+
assert(found);
176+
v.emplace_back(::SerializeHash(qc));
177+
hashCount++;
178+
}
159179
}
180+
quorumsCached = quorums;
181+
qcHashesCached = qcHashes;
160182
}
161183

162184
int64_t nTime3 = GetTimeMicros(); nTimeMined += nTime3 - nTime2;

0 commit comments

Comments
 (0)