Skip to content

Commit

Permalink
Replace std::map with std::unordered_map.
Browse files Browse the repository at this point in the history
  • Loading branch information
denravonska committed Nov 12, 2017
1 parent 2f5149b commit ddb2dda
Show file tree
Hide file tree
Showing 11 changed files with 110 additions and 112 deletions.
4 changes: 2 additions & 2 deletions src/checkpoints.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -77,14 +77,14 @@ namespace Checkpoints
return checkpoints.rbegin()->first;
}

CBlockIndex* GetLastCheckpoint(const std::map<uint256, CBlockIndex*>& mapBlockIndex)
CBlockIndex* GetLastCheckpoint(const BlockMap& mapBlockIndex)
{
MapCheckpoints& checkpoints = (fTestNet ? mapCheckpointsTestnet : mapCheckpoints);

for (auto const& i : boost::adaptors::reverse(checkpoints))
{
const uint256& hash = i.second;
std::map<uint256, CBlockIndex*>::const_iterator t = mapBlockIndex.find(hash);
BlockMap::const_iterator t = mapBlockIndex.find(hash);
if (t != mapBlockIndex.end())
return t->second;
}
Expand Down
6 changes: 2 additions & 4 deletions src/checkpoints.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,7 @@
#ifndef BITCOIN_CHECKPOINT_H
#define BITCOIN_CHECKPOINT_H

#include <map>
#include "net.h"
#include "util.h"
#include "main.h"

class uint256;
class CBlockIndex;
Expand All @@ -23,7 +21,7 @@ namespace Checkpoints
int GetTotalBlocksEstimate();

// Returns last CBlockIndex* in mapBlockIndex that is a checkpoint
CBlockIndex* GetLastCheckpoint(const std::map<uint256, CBlockIndex*>& mapBlockIndex);
CBlockIndex* GetLastCheckpoint(const BlockMap& mapBlockIndex);

bool CheckSync(int nHeight);
}
Expand Down
2 changes: 1 addition & 1 deletion src/init.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -826,7 +826,7 @@ bool AppInit2(ThreadHandlerPtr threads)
{
string strMatch = mapArgs["-printblock"];
int nFound = 0;
for (map<uint256, CBlockIndex*>::iterator mi = mapBlockIndex.begin(); mi != mapBlockIndex.end(); ++mi)
for (BlockMap::iterator mi = mapBlockIndex.begin(); mi != mapBlockIndex.end(); ++mi)
{
uint256 hash = (*mi).first;
if (strncmp(hash.ToString().c_str(), strMatch.c_str(), strMatch.size()) == 0)
Expand Down
171 changes: 85 additions & 86 deletions src/main.cpp

Large diffs are not rendered by default.

17 changes: 12 additions & 5 deletions src/main.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
#include "global_objects_noui.hpp"

#include <map>
#include <unordered_map>
#include <set>

class CWallet;
Expand Down Expand Up @@ -142,10 +143,16 @@ extern std::map<std::string, MiningCPID> mvBlockIndex;
typedef std::set<uint256> HashSet;
extern std::map<std::string, HashSet> mvCPIDBlockHashes;

struct BlockHasher
{
size_t operator()(const uint256& hash) const { return hash.Get64(); }
};

typedef std::unordered_map<uint256, CBlockIndex*, BlockHasher> BlockMap;

extern CScript COINBASE_FLAGS;
extern CCriticalSection cs_main;
extern std::map<uint256, CBlockIndex*> mapBlockIndex;
extern BlockMap mapBlockIndex;
extern std::set<std::pair<COutPoint, unsigned int> > setStakeSeen;
extern CBlockIndex* pindexGenesisBlock;
extern unsigned int nStakeMinAge;
Expand Down Expand Up @@ -1698,7 +1705,7 @@ class CBlockLocator

explicit CBlockLocator(uint256 hashBlock)
{
std::map<uint256, CBlockIndex*>::iterator mi = mapBlockIndex.find(hashBlock);
BlockMap::iterator mi = mapBlockIndex.find(hashBlock);
if (mi != mapBlockIndex.end())
Set((*mi).second);
}
Expand Down Expand Up @@ -1749,7 +1756,7 @@ class CBlockLocator
int nStep = 1;
for (auto const& hash : vHave)
{
std::map<uint256, CBlockIndex*>::iterator mi = mapBlockIndex.find(hash);
BlockMap::iterator mi = mapBlockIndex.find(hash);
if (mi != mapBlockIndex.end())
{
CBlockIndex* pindex = (*mi).second;
Expand All @@ -1768,7 +1775,7 @@ class CBlockLocator
// Find the first block the caller has in the main chain
for (auto const& hash : vHave)
{
std::map<uint256, CBlockIndex*>::iterator mi = mapBlockIndex.find(hash);
BlockMap::iterator mi = mapBlockIndex.find(hash);
if (mi != mapBlockIndex.end())
{
CBlockIndex* pindex = (*mi).second;
Expand All @@ -1784,7 +1791,7 @@ class CBlockLocator
// Find the first block the caller has in the main chain
for (auto const& hash : vHave)
{
std::map<uint256, CBlockIndex*>::iterator mi = mapBlockIndex.find(hash);
BlockMap::iterator mi = mapBlockIndex.find(hash);
if (mi != mapBlockIndex.end())
{
CBlockIndex* pindex = (*mi).second;
Expand Down
2 changes: 1 addition & 1 deletion src/qt/transactionrecord.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ void TransactionRecord::updateStatus(const CWalletTx &wtx)

// Find the block the tx is in
CBlockIndex* pindex = NULL;
std::map<uint256, CBlockIndex*>::iterator mi = mapBlockIndex.find(wtx.hashBlock);
BlockMap::iterator mi = mapBlockIndex.find(wtx.hashBlock);
if (mi != mapBlockIndex.end())
pindex = (*mi).second;

Expand Down
7 changes: 2 additions & 5 deletions src/rpcrawtransaction.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,12 +45,10 @@ std::vector<std::pair<std::string, std::string>> GetTxStakeBoincHashInfo(const C
CBlockIndex* pindex = NULL;
CBlock block;
{
map<uint256, CBlockIndex*>::iterator mi = mapBlockIndex.find(mtx.hashBlock);

BlockMap::iterator mi = mapBlockIndex.find(mtx.hashBlock);
if (mi == mapBlockIndex.end())
{
res.push_back(std::make_pair(_("ERROR"), _("Block not in index")));

return res;
}

Expand All @@ -59,7 +57,6 @@ std::vector<std::pair<std::string, std::string>> GetTxStakeBoincHashInfo(const C
if (!block.ReadFromDisk(pindex))
{
res.push_back(std::make_pair(_("ERROR"), _("Block read failed")));

return res;
}
}
Expand Down Expand Up @@ -598,7 +595,7 @@ void TxToJSON(const CTransaction& tx, const uint256 hashBlock, Object& entry)
if (hashBlock != 0)
{
entry.push_back(Pair("blockhash", hashBlock.GetHex()));
map<uint256, CBlockIndex*>::iterator mi = mapBlockIndex.find(hashBlock);
BlockMap::iterator mi = mapBlockIndex.find(hashBlock);
if (mi != mapBlockIndex.end() && (*mi).second)
{
CBlockIndex* pindex = (*mi).second;
Expand Down
4 changes: 2 additions & 2 deletions src/rpcwallet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1568,7 +1568,7 @@ Value listsinceblock(const Array& params, bool fHelp)
uint256 blockId = 0;

blockId.SetHex(params[0].get_str());
std::map<uint256, CBlockIndex*>::iterator it = mapBlockIndex.find(blockId);
BlockMap::iterator it = mapBlockIndex.find(blockId);
if (it != mapBlockIndex.end())
pindex = it->second;

Expand Down Expand Up @@ -1668,7 +1668,7 @@ Value gettransaction(const Array& params, bool fHelp)
else
{
entry.push_back(Pair("blockhash", hashBlock.GetHex()));
map<uint256, CBlockIndex*>::iterator mi = mapBlockIndex.find(hashBlock);
BlockMap::iterator mi = mapBlockIndex.find(hashBlock);
if (mi != mapBlockIndex.end() && (*mi).second)
{
CBlockIndex* pindex = (*mi).second;
Expand Down
2 changes: 1 addition & 1 deletion src/txdb-leveldb.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -296,7 +296,7 @@ static CBlockIndex *InsertBlockIndex(const uint256& hash)
return NULL;

// Return existing
map<uint256, CBlockIndex*>::iterator mi = mapBlockIndex.find(hash);
BlockMap::iterator mi = mapBlockIndex.find(hash);
if (mi != mapBlockIndex.end())
return (*mi).second;

Expand Down
5 changes: 1 addition & 4 deletions src/uint256.h
Original file line number Diff line number Diff line change
Expand Up @@ -260,10 +260,7 @@ class base_uint

friend inline bool operator==(const base_uint& a, const base_uint& b)
{
for (int i = 0; i < base_uint::WIDTH; i++)
if (a.pn[i] != b.pn[i])
return false;
return true;
return memcmp(a.pn, b.pn, sizeof(a.pn)) == 0;
}

friend inline bool operator==(const base_uint& a, uint64_t b)
Expand Down
2 changes: 1 addition & 1 deletion src/wallet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2592,7 +2592,7 @@ void CWallet::GetKeyBirthTimes(std::map<CKeyID, int64_t> &mapKeyBirth) const {
for (std::map<uint256, CWalletTx>::const_iterator it = mapWallet.begin(); it != mapWallet.end(); it++) {
// iterate over all wallet transactions...
const CWalletTx &wtx = (*it).second;
std::map<uint256, CBlockIndex*>::const_iterator blit = mapBlockIndex.find(wtx.hashBlock);
BlockMap::iterator blit = mapBlockIndex.find(wtx.hashBlock);
if (blit != mapBlockIndex.end() && blit->second->IsInMainChain()) {
// ... which are already in a block
int nHeight = blit->second->nHeight;
Expand Down

0 comments on commit ddb2dda

Please sign in to comment.