From 4755eeebbb979e0c9f3aa1030300f3551475cec6 Mon Sep 17 00:00:00 2001 From: Marco Nilsson Date: Tue, 24 Oct 2017 13:12:46 +0200 Subject: [PATCH] Slightly more efficient mapBlockIndex usage. Instead of counting items and then accessing, thus causing two map searches instead of one, just use the result of a find operation instead. --- src/kernel.cpp | 10 ++++++---- src/main.cpp | 22 ++++++++++++++-------- src/wallet.cpp | 5 +++-- 3 files changed, 23 insertions(+), 14 deletions(-) diff --git a/src/kernel.cpp b/src/kernel.cpp index 37f0f77e7d..6148eba2fc 100644 --- a/src/kernel.cpp +++ b/src/kernel.cpp @@ -76,9 +76,10 @@ static bool SelectBlockFromCandidates(vector >& vSortedBy *pindexSelected = (const CBlockIndex*) 0; for (auto const& item : vSortedByTimestamp) { - if (!mapBlockIndex.count(item.second)) + const auto mapItem = mapBlockIndex.find(item.second); + if (mapItem == mapBlockIndex.end()) return error("SelectBlockFromCandidates: failed to find block index for candidate block %s", item.second.ToString().c_str()); - const CBlockIndex* pindex = mapBlockIndex[item.second]; + const CBlockIndex* pindex = mapItem->second; if (fSelected && pindex->GetBlockTime() > nSelectionIntervalStop) break; if (mapSelectedBlocks.count(pindex->GetBlockHash()) > 0) @@ -224,9 +225,10 @@ bool ComputeNextStakeModifier(const CBlockIndex* pindexPrev, uint64_t& nStakeMod static bool GetKernelStakeModifier(uint256 hashBlockFrom, uint64_t& nStakeModifier, int& nStakeModifierHeight, int64_t& nStakeModifierTime, bool fPrintProofOfStake) { nStakeModifier = 0; - if (!mapBlockIndex.count(hashBlockFrom)) + const auto mapItem = mapBlockIndex.find(hashBlockFrom); + if (mapItem == mapBlockIndex.end()) return error("GetKernelStakeModifier() : block not indexed"); - const CBlockIndex* pindexFrom = mapBlockIndex[hashBlockFrom]; + const CBlockIndex* pindexFrom = mapItem->second; nStakeModifierHeight = pindexFrom->nHeight; nStakeModifierTime = pindexFrom->GetBlockTime(); int64_t nStakeModifierSelectionInterval = GetStakeModifierSelectionInterval(); diff --git a/src/main.cpp b/src/main.cpp index bc9b6a6fbf..ebcf3fd061 100755 --- a/src/main.cpp +++ b/src/main.cpp @@ -3436,14 +3436,15 @@ bool ForceReorganizeToHash(uint256 NewHash) { CTxDB txdb; - if(!mapBlockIndex.count(NewHash)) + auto mapItem = mapBlockIndex.find(NewHash); + if(mapItem == mapBlockIndex.end()) return error("ForceReorganizeToHash: failed to find requested block in block index"); CBlockIndex* pindexCur = pindexBest; - CBlockIndex* pindexNew = mapBlockIndex[NewHash]; + CBlockIndex* pindexNew = mapItem->second; printf("\r\n** Force Reorganize **\r\n"); - printf(" Current best height %f hash %s\n",(double)pindexCur->nHeight,pindexCur->GetBlockHash().GetHex().c_str()); - printf(" Target height %f hash %s\n",(double)pindexNew->nHeight,pindexNew->GetBlockHash().GetHex().c_str()); + printf(" Current best height %i hash %s\n", pindexCur->nHeight,pindexCur->GetBlockHash().GetHex().c_str()); + printf(" Target height %i hash %s\n", pindexNew->nHeight,pindexNew->GetBlockHash().GetHex().c_str()); CBlock blockNew; if (!blockNew.ReadFromDisk(pindexNew)) @@ -5224,11 +5225,12 @@ StructCPID GetLifetimeCPID(const std::string& cpid, const std::string& sCalledFr if (fDebug10) printf("GetLifetimeCPID: trying %s\n",uHash.GetHex().c_str()); // Ensure that we have this block. - if (mapBlockIndex.count(uHash) == 0) + auto mapItem = mapBlockIndex.find(uHash); + if (mapItem == mapBlockIndex.end()) continue; // Ensure that the block is valid - CBlockIndex* pblockindex = mapBlockIndex[uHash]; + CBlockIndex* pblockindex = mapItem->second; if(pblockindex == NULL || pblockindex->IsInMainChain() == false || pblockindex->GetCPID() != cpid) @@ -8355,8 +8357,12 @@ CBlockIndex* GetHistoricalMagnitude(std::string cpid) if (!stCPID.BlockHash.empty()) { uint256 hash(stCPID.BlockHash); - if (mapBlockIndex.count(hash) == 0) return pindexGenesisBlock; - CBlockIndex* pblockindex = mapBlockIndex[hash]; + + auto mapItem = mapBlockIndex.find(hash); + if (mapItem == mapBlockIndex.end()) + return pindexGenesisBlock; + + CBlockIndex* pblockindex = mapItem->second; if(!pblockindex->pnext) printf("GetHistoricalMagnitude: WARNING index {%s %d} for cpid %s, " "has no next pointer (not n main chain)\n",pblockindex->GetBlockHash().GetHex().c_str(), diff --git a/src/wallet.cpp b/src/wallet.cpp index 926e6a4f60..aac1c108b2 100644 --- a/src/wallet.cpp +++ b/src/wallet.cpp @@ -463,7 +463,8 @@ bool CWallet::AddToWallet(const CWalletTx& wtxIn) wtx.nTimeSmart = wtx.nTimeReceived; if (wtxIn.hashBlock != 0) { - if (mapBlockIndex.count(wtxIn.hashBlock)) + auto mapItem = mapBlockIndex.find(wtxIn.hashBlock); + if (mapItem != mapBlockIndex.end()) { unsigned int latestNow = wtx.nTimeReceived; unsigned int latestEntry = 0; @@ -497,7 +498,7 @@ bool CWallet::AddToWallet(const CWalletTx& wtxIn) } } - unsigned int& blocktime = mapBlockIndex[wtxIn.hashBlock]->nTime; + unsigned int& blocktime = mapItem->second->nTime; wtx.nTimeSmart = std::max(latestEntry, std::min(blocktime, latestNow)); } else