Skip to content

Commit b08eab3

Browse files
refactor(cache): migrate unordered_lru_cache::get to return std::optional
Change unordered_lru_cache::get() from returning bool with an output parameter to returning std::optional<Value> by value. This provides a more modern API that is easier to use and less error-prone. - Add <optional> header to unordered_lru_cache.h - Update get() signature: bool get(key, Value&) -> std::optional<Value> get(key) - Update all 21 call sites across 11 files to use the new API - Maintain same copy semantics for performance (no performance impact) Files updated: - src/unordered_lru_cache.h - src/llmq/quorums.cpp (5 call sites) - src/llmq/utils.cpp (2 call sites) - src/llmq/snapshot.cpp, signing.cpp, dkgsessionmgr.cpp, blockprocessor.cpp - src/instantsend/db.cpp (3 call sites) - src/evo/mnhftx.cpp, creditpool.cpp, cbtx.cpp - src/masternode/meta.cpp
1 parent 368eebb commit b08eab3

File tree

12 files changed

+70
-60
lines changed

12 files changed

+70
-60
lines changed

src/evo/cbtx.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,9 @@ auto CachedGetQcHashesQcIndexedHashes(const CBlockIndex* pindexPrev, const llmq:
8686
uint256 block_hash{blockIndex->GetBlockHash()};
8787

8888
std::pair<uint256, int> qc_hash;
89-
if (!qc_hashes_cached[llmqType].get(block_hash, qc_hash)) {
89+
if (auto cached = qc_hashes_cached[llmqType].get(block_hash)) {
90+
qc_hash = *cached;
91+
} else {
9092
auto [pqc, dummy_hash] = quorum_block_processor.GetMinedCommitment(llmqType, block_hash);
9193
if (dummy_hash == uint256::ZERO) {
9294
// this should never happen

src/evo/creditpool.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -70,8 +70,8 @@ static std::optional<CreditPoolDataPerBlock> GetCreditDataFromBlock(const gsl::n
7070
static Mutex cache_mutex;
7171
static Uint256LruHashMap<CreditPoolDataPerBlock> block_data_cache GUARDED_BY(cache_mutex){
7272
static_cast<size_t>(Params().CreditPoolPeriodBlocks()) * 2};
73-
if (LOCK(cache_mutex); block_data_cache.get(block_index->GetBlockHash(), blockData)) {
74-
return blockData;
73+
if (LOCK(cache_mutex); auto cached = block_data_cache.get(block_index->GetBlockHash())) {
74+
return *cached;
7575
}
7676

7777
CBlock block;
@@ -123,8 +123,8 @@ std::optional<CCreditPool> CCreditPoolManager::GetFromCache(const CBlockIndex& b
123123
CCreditPool pool;
124124
{
125125
LOCK(cache_mutex);
126-
if (creditPoolCache.get(block_hash, pool)) {
127-
return pool;
126+
if (auto cached = creditPoolCache.get(block_hash)) {
127+
return *cached;
128128
}
129129
}
130130
if (block_index.nHeight % DISK_SNAPSHOT_PERIOD == 0) {

src/evo/mnhftx.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -325,8 +325,8 @@ std::optional<CMNHFManager::Signals> CMNHFManager::GetFromCache(const CBlockInde
325325
const uint256& blockHash = pindex->GetBlockHash();
326326
{
327327
LOCK(cs_cache);
328-
if (mnhfCache.get(blockHash, signals)) {
329-
return signals;
328+
if (auto cached = mnhfCache.get(blockHash)) {
329+
return *cached;
330330
}
331331
}
332332
{

src/instantsend/db.cpp

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -271,11 +271,14 @@ InstantSendLockPtr CInstantSendDb::GetInstantSendLockByHashInternal(const uint25
271271
return nullptr;
272272
}
273273

274-
InstantSendLockPtr ret;
275-
if (use_cache && islockCache.get(hash, ret)) {
276-
return ret;
274+
if (use_cache) {
275+
if (auto cached = islockCache.get(hash)) {
276+
return *cached;
277+
}
277278
}
278279

280+
InstantSendLockPtr ret;
281+
279282
ret = std::make_shared<InstantSendLock>();
280283
bool exists = db->Read(std::make_tuple(DB_ISLOCK_BY_HASH, hash), *ret);
281284
if (!exists || (::SerializeHash(*ret) != hash)) {
@@ -292,13 +295,14 @@ InstantSendLockPtr CInstantSendDb::GetInstantSendLockByHashInternal(const uint25
292295
uint256 CInstantSendDb::GetInstantSendLockHashByTxidInternal(const uint256& txid) const
293296
{
294297
AssertLockHeld(cs_db);
298+
if (auto cached = txidCache.get(txid)) {
299+
return *cached;
300+
}
295301
uint256 islockHash;
296-
if (!txidCache.get(txid, islockHash)) {
297-
if (!db->Read(std::make_tuple(DB_HASH_BY_TXID, txid), islockHash)) {
298-
return {};
299-
}
300-
txidCache.insert(txid, islockHash);
302+
if (!db->Read(std::make_tuple(DB_HASH_BY_TXID, txid), islockHash)) {
303+
return {};
301304
}
305+
txidCache.insert(txid, islockHash);
302306
return islockHash;
303307
}
304308

@@ -311,13 +315,14 @@ InstantSendLockPtr CInstantSendDb::GetInstantSendLockByTxid(const uint256& txid)
311315
InstantSendLockPtr CInstantSendDb::GetInstantSendLockByInput(const COutPoint& outpoint) const
312316
{
313317
LOCK(cs_db);
318+
if (auto cached = outpointCache.get(outpoint)) {
319+
return GetInstantSendLockByHashInternal(*cached);
320+
}
314321
uint256 islockHash;
315-
if (!outpointCache.get(outpoint, islockHash)) {
316-
if (!db->Read(std::make_tuple(DB_HASH_BY_OUTPOINT, outpoint), islockHash)) {
317-
return nullptr;
318-
}
319-
outpointCache.insert(outpoint, islockHash);
322+
if (!db->Read(std::make_tuple(DB_HASH_BY_OUTPOINT, outpoint), islockHash)) {
323+
return nullptr;
320324
}
325+
outpointCache.insert(outpoint, islockHash);
321326
return GetInstantSendLockByHashInternal(islockHash);
322327
}
323328

src/llmq/blockprocessor.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -513,11 +513,12 @@ uint256 CQuorumBlockProcessor::GetQuorumBlockHash(const Consensus::LLMQParams& l
513513

514514
bool CQuorumBlockProcessor::HasMinedCommitment(Consensus::LLMQType llmqType, const uint256& quorumHash) const
515515
{
516-
bool fExists;
517-
if (LOCK(minableCommitmentsCs); mapHasMinedCommitmentCache[llmqType].get(quorumHash, fExists)) {
518-
return fExists;
516+
if (LOCK(minableCommitmentsCs); auto cached = mapHasMinedCommitmentCache[llmqType].get(quorumHash)) {
517+
return *cached;
519518
}
520519

520+
bool fExists;
521+
521522
fExists = m_evoDb.Exists(std::make_pair(DB_MINED_COMMITMENT, std::make_pair(llmqType, quorumHash)));
522523

523524
LOCK(minableCommitmentsCs);

src/llmq/dkgsessionmgr.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,9 @@ MessageProcessingResult CDKGSessionManager::ProcessMessage(CNode& pfrom, bool is
149149
if (indexedQuorumsCache.empty()) {
150150
utils::InitQuorumsCache(indexedQuorumsCache);
151151
}
152-
indexedQuorumsCache[llmqType].get(quorumHash, quorumIndex);
152+
if (auto cached = indexedQuorumsCache[llmqType].get(quorumHash)) {
153+
quorumIndex = *cached;
154+
}
153155
}
154156

155157
// No luck, try to compute

src/llmq/quorums.cpp

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -571,8 +571,8 @@ std::vector<CQuorumCPtr> CQuorumManager::ScanQuorums(Consensus::LLMQType llmqTyp
571571
}
572572
}
573573
auto& cache = scanQuorumsCache[llmqType];
574-
bool fCacheExists = cache.get(pindexStore->GetBlockHash(), vecResultQuorums);
575-
if (fCacheExists) {
574+
if (auto cached = cache.get(pindexStore->GetBlockHash())) {
575+
vecResultQuorums = *cached;
576576
// We have exactly what requested so just return it
577577
if (vecResultQuorums.size() == nCountRequested) {
578578
return vecResultQuorums;
@@ -642,12 +642,15 @@ CQuorumCPtr CQuorumManager::GetQuorum(Consensus::LLMQType llmqType, const uint25
642642
// We cannot hold cs_quorumBaseBlockIndexCache the whole time as that creates lock-order inversion with cs_main;
643643
// We cannot acquire cs_main if we have cs_quorumBaseBlockIndexCache held
644644
const CBlockIndex* pindex;
645-
if (!WITH_LOCK(cs_quorumBaseBlockIndexCache, return quorumBaseBlockIndexCache.get(quorumHash, pindex))) {
645+
auto cached = WITH_LOCK(cs_quorumBaseBlockIndexCache, return quorumBaseBlockIndexCache.get(quorumHash));
646+
if (!cached.has_value()) {
646647
pindex = WITH_LOCK(::cs_main, return m_chainstate.m_blockman.LookupBlockIndex(quorumHash));
647648
if (pindex) {
648649
LOCK(cs_quorumBaseBlockIndexCache);
649650
quorumBaseBlockIndexCache.insert(quorumHash, pindex);
650651
}
652+
} else {
653+
pindex = *cached;
651654
}
652655
return pindex;
653656
}();
@@ -668,9 +671,8 @@ CQuorumCPtr CQuorumManager::GetQuorum(Consensus::LLMQType llmqType, gsl::not_nul
668671
return nullptr;
669672
}
670673

671-
CQuorumPtr pQuorum;
672-
if (LOCK(cs_map_quorums); mapQuorumsCache[llmqType].get(quorumHash, pQuorum)) {
673-
return pQuorum;
674+
if (LOCK(cs_map_quorums); auto cached = mapQuorumsCache[llmqType].get(quorumHash)) {
675+
return *cached;
674676
}
675677

676678
return BuildQuorumFromCommitment(llmqType, pQuorumBaseBlockIndex, populate_cache);
@@ -833,9 +835,12 @@ MessageProcessingResult CQuorumManager::ProcessMessage(CNode& pfrom, CConnman& c
833835

834836
CQuorumPtr pQuorum;
835837
{
836-
if (LOCK(cs_map_quorums); !mapQuorumsCache[request.GetLLMQType()].get(request.GetQuorumHash(), pQuorum)) {
838+
LOCK(cs_map_quorums);
839+
auto cached = mapQuorumsCache[request.GetLLMQType()].get(request.GetQuorumHash());
840+
if (!cached.has_value()) {
837841
return errorHandler("Quorum not found", 0); // Don't bump score because we asked for it
838842
}
843+
pQuorum = *cached;
839844
}
840845

841846
// Check if request has QUORUM_VERIFICATION_VECTOR data
@@ -1118,9 +1123,8 @@ void CQuorumManager::StartCleanupOldQuorumDataThread(const CBlockIndex* pIndex)
11181123
const CBlockIndex* pindex_loop{pIndex};
11191124
std::set<uint256> quorum_keys;
11201125
while (pindex_loop != nullptr && pIndex->nHeight - pindex_loop->nHeight < params.max_store_depth()) {
1121-
uint256 quorum_key;
1122-
if (cache.get(pindex_loop->GetBlockHash(), quorum_key)) {
1123-
quorum_keys.insert(quorum_key);
1126+
if (auto quorum_key = cache.get(pindex_loop->GetBlockHash())) {
1127+
quorum_keys.insert(*quorum_key);
11241128
if (quorum_keys.size() >= static_cast<size_t>(params.keepOldKeys)) break; // extra safety belt
11251129
}
11261130
pindex_loop = pindex_loop->pprev;

src/llmq/signing.cpp

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -43,17 +43,16 @@ bool CRecoveredSigsDb::HasRecoveredSig(Consensus::LLMQType llmqType, const uint2
4343
bool CRecoveredSigsDb::HasRecoveredSigForId(Consensus::LLMQType llmqType, const uint256& id) const
4444
{
4545
auto cacheKey = std::make_pair(llmqType, id);
46-
bool ret;
4746
{
4847
LOCK(cs_cache);
49-
if (hasSigForIdCache.get(cacheKey, ret)) {
50-
return ret;
48+
if (auto cached = hasSigForIdCache.get(cacheKey)) {
49+
return *cached;
5150
}
5251
}
5352

5453

5554
auto k = std::make_tuple(std::string("rs_r"), llmqType, id);
56-
ret = db->Exists(k);
55+
bool ret = db->Exists(k);
5756

5857
LOCK(cs_cache);
5958
hasSigForIdCache.insert(cacheKey, ret);
@@ -62,16 +61,15 @@ bool CRecoveredSigsDb::HasRecoveredSigForId(Consensus::LLMQType llmqType, const
6261

6362
bool CRecoveredSigsDb::HasRecoveredSigForSession(const uint256& signHash) const
6463
{
65-
bool ret;
6664
{
6765
LOCK(cs_cache);
68-
if (hasSigForSessionCache.get(signHash, ret)) {
69-
return ret;
66+
if (auto cached = hasSigForSessionCache.get(signHash)) {
67+
return *cached;
7068
}
7169
}
7270

7371
auto k = std::make_tuple(std::string("rs_s"), signHash);
74-
ret = db->Exists(k);
72+
bool ret = db->Exists(k);
7573

7674
LOCK(cs_cache);
7775
hasSigForSessionCache.insert(signHash, ret);
@@ -80,16 +78,15 @@ bool CRecoveredSigsDb::HasRecoveredSigForSession(const uint256& signHash) const
8078

8179
bool CRecoveredSigsDb::HasRecoveredSigForHash(const uint256& hash) const
8280
{
83-
bool ret;
8481
{
8582
LOCK(cs_cache);
86-
if (hasSigForHashCache.get(hash, ret)) {
87-
return ret;
83+
if (auto cached = hasSigForHashCache.get(hash)) {
84+
return *cached;
8885
}
8986
}
9087

9188
auto k = std::make_tuple(std::string("rs_h"), hash);
92-
ret = db->Exists(k);
89+
bool ret = db->Exists(k);
9390

9491
LOCK(cs_cache);
9592
hasSigForHashCache.insert(hash, ret);

src/llmq/snapshot.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -352,8 +352,8 @@ std::optional<CQuorumSnapshot> CQuorumSnapshotManager::GetSnapshotForBlock(const
352352

353353
LOCK(snapshotCacheCs);
354354
// try using cache before reading from disk
355-
if (quorumSnapshotCache.get(snapshotHash, snapshot)) {
356-
return snapshot;
355+
if (auto cached = quorumSnapshotCache.get(snapshotHash)) {
356+
return *cached;
357357
}
358358
if (m_evoDb.Read(std::make_pair(DB_QUORUM_SNAPSHOT, snapshotHash), snapshot)) {
359359
quorumSnapshotCache.insert(snapshotHash, snapshot);

src/llmq/utils.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -215,8 +215,8 @@ std::vector<CDeterministicMNCPtr> GetAllQuorumMembers(Consensus::LLMQType llmqTy
215215
}
216216
if (reset_cache) {
217217
mapQuorumMembers[llmqType].clear();
218-
} else if (mapQuorumMembers[llmqType].get(pQuorumBaseBlockIndex->GetBlockHash(), quorumMembers)) {
219-
return quorumMembers;
218+
} else if (auto cached = mapQuorumMembers[llmqType].get(pQuorumBaseBlockIndex->GetBlockHash())) {
219+
return *cached;
220220
}
221221
}
222222

@@ -251,7 +251,8 @@ std::vector<CDeterministicMNCPtr> GetAllQuorumMembers(Consensus::LLMQType llmqTy
251251
if (reset_cache) {
252252
LOCK(cs_indexed_members);
253253
mapIndexedQuorumMembers[llmqType].clear();
254-
} else if (LOCK(cs_indexed_members); mapIndexedQuorumMembers[llmqType].get(std::pair(pCycleQuorumBaseBlockIndex->GetBlockHash(), quorumIndex), quorumMembers)) {
254+
} else if (LOCK(cs_indexed_members); auto cached = mapIndexedQuorumMembers[llmqType].get(std::pair(pCycleQuorumBaseBlockIndex->GetBlockHash(), quorumIndex))) {
255+
quorumMembers = *cached;
255256
LOCK(cs_members);
256257
mapQuorumMembers[llmqType].insert(pQuorumBaseBlockIndex->GetBlockHash(), quorumMembers);
257258
return quorumMembers;

0 commit comments

Comments
 (0)