Skip to content

Commit

Permalink
Slightly more efficient mapBlockIndex usage.
Browse files Browse the repository at this point in the history
Instead of counting items and then accessing, thus causing two map searches
instead of one, just use the result of a find operation instead.
  • Loading branch information
denravonska committed Oct 24, 2017
1 parent 1c671cf commit 4755eee
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 14 deletions.
10 changes: 6 additions & 4 deletions src/kernel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -76,9 +76,10 @@ static bool SelectBlockFromCandidates(vector<pair<int64_t, uint256> >& 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)
Expand Down Expand Up @@ -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();
Expand Down
22 changes: 14 additions & 8 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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(),
Expand Down
5 changes: 3 additions & 2 deletions src/wallet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 4755eee

Please sign in to comment.