Skip to content

Commit dd638dd

Browse files
committed
typedef std::map<uint256, CCoins> to CCoinsMap
This makes it possible to switch to a more efficient map type without changing all occurences manually. Merges half of #4413.
1 parent 8d9cc7d commit dd638dd

File tree

4 files changed

+17
-16
lines changed

4 files changed

+17
-16
lines changed

src/coins.cpp

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ bool CCoinsView::SetCoins(const uint256 &txid, const CCoins &coins) { return fal
5555
bool CCoinsView::HaveCoins(const uint256 &txid) { return false; }
5656
uint256 CCoinsView::GetBestBlock() { return uint256(0); }
5757
bool CCoinsView::SetBestBlock(const uint256 &hashBlock) { return false; }
58-
bool CCoinsView::BatchWrite(const std::map<uint256, CCoins> &mapCoins, const uint256 &hashBlock) { return false; }
58+
bool CCoinsView::BatchWrite(const CCoinsMap &mapCoins, const uint256 &hashBlock) { return false; }
5959
bool CCoinsView::GetStats(CCoinsStats &stats) { return false; }
6060

6161

@@ -66,7 +66,7 @@ bool CCoinsViewBacked::HaveCoins(const uint256 &txid) { return base->HaveCoins(t
6666
uint256 CCoinsViewBacked::GetBestBlock() { return base->GetBestBlock(); }
6767
bool CCoinsViewBacked::SetBestBlock(const uint256 &hashBlock) { return base->SetBestBlock(hashBlock); }
6868
void CCoinsViewBacked::SetBackend(CCoinsView &viewIn) { base = &viewIn; }
69-
bool CCoinsViewBacked::BatchWrite(const std::map<uint256, CCoins> &mapCoins, const uint256 &hashBlock) { return base->BatchWrite(mapCoins, hashBlock); }
69+
bool CCoinsViewBacked::BatchWrite(const CCoinsMap &mapCoins, const uint256 &hashBlock) { return base->BatchWrite(mapCoins, hashBlock); }
7070
bool CCoinsViewBacked::GetStats(CCoinsStats &stats) { return base->GetStats(stats); }
7171

7272
CCoinsViewCache::CCoinsViewCache(CCoinsView &baseIn, bool fDummy) : CCoinsViewBacked(baseIn), hashBlock(0) { }
@@ -83,20 +83,20 @@ bool CCoinsViewCache::GetCoins(const uint256 &txid, CCoins &coins) {
8383
return false;
8484
}
8585

86-
std::map<uint256,CCoins>::iterator CCoinsViewCache::FetchCoins(const uint256 &txid) {
87-
std::map<uint256,CCoins>::iterator it = cacheCoins.lower_bound(txid);
86+
CCoinsMap::iterator CCoinsViewCache::FetchCoins(const uint256 &txid) {
87+
CCoinsMap::iterator it = cacheCoins.lower_bound(txid);
8888
if (it != cacheCoins.end() && it->first == txid)
8989
return it;
9090
CCoins tmp;
9191
if (!base->GetCoins(txid,tmp))
9292
return cacheCoins.end();
93-
std::map<uint256,CCoins>::iterator ret = cacheCoins.insert(it, std::make_pair(txid, CCoins()));
93+
CCoinsMap::iterator ret = cacheCoins.insert(it, std::make_pair(txid, CCoins()));
9494
tmp.swap(ret->second);
9595
return ret;
9696
}
9797

9898
CCoins &CCoinsViewCache::GetCoins(const uint256 &txid) {
99-
std::map<uint256,CCoins>::iterator it = FetchCoins(txid);
99+
CCoinsMap::iterator it = FetchCoins(txid);
100100
assert(it != cacheCoins.end());
101101
return it->second;
102102
}
@@ -121,8 +121,8 @@ bool CCoinsViewCache::SetBestBlock(const uint256 &hashBlockIn) {
121121
return true;
122122
}
123123

124-
bool CCoinsViewCache::BatchWrite(const std::map<uint256, CCoins> &mapCoins, const uint256 &hashBlockIn) {
125-
for (std::map<uint256, CCoins>::const_iterator it = mapCoins.begin(); it != mapCoins.end(); it++)
124+
bool CCoinsViewCache::BatchWrite(const CCoinsMap &mapCoins, const uint256 &hashBlockIn) {
125+
for (CCoinsMap::const_iterator it = mapCoins.begin(); it != mapCoins.end(); it++)
126126
cacheCoins[it->first] = it->second;
127127
hashBlock = hashBlockIn;
128128
return true;

src/coins.h

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -239,6 +239,7 @@ class CCoins
239239
}
240240
};
241241

242+
typedef std::map<uint256,CCoins> CCoinsMap;
242243

243244
struct CCoinsStats
244245
{
@@ -275,7 +276,7 @@ class CCoinsView
275276
virtual bool SetBestBlock(const uint256 &hashBlock);
276277

277278
// Do a bulk modification (multiple SetCoins + one SetBestBlock)
278-
virtual bool BatchWrite(const std::map<uint256, CCoins> &mapCoins, const uint256 &hashBlock);
279+
virtual bool BatchWrite(const CCoinsMap &mapCoins, const uint256 &hashBlock);
279280

280281
// Calculate statistics about the unspent transaction output set
281282
virtual bool GetStats(CCoinsStats &stats);
@@ -299,7 +300,7 @@ class CCoinsViewBacked : public CCoinsView
299300
uint256 GetBestBlock();
300301
bool SetBestBlock(const uint256 &hashBlock);
301302
void SetBackend(CCoinsView &viewIn);
302-
bool BatchWrite(const std::map<uint256, CCoins> &mapCoins, const uint256 &hashBlock);
303+
bool BatchWrite(const CCoinsMap &mapCoins, const uint256 &hashBlock);
303304
bool GetStats(CCoinsStats &stats);
304305
};
305306

@@ -309,7 +310,7 @@ class CCoinsViewCache : public CCoinsViewBacked
309310
{
310311
protected:
311312
uint256 hashBlock;
312-
std::map<uint256,CCoins> cacheCoins;
313+
CCoinsMap cacheCoins;
313314

314315
public:
315316
CCoinsViewCache(CCoinsView &baseIn, bool fDummy = false);
@@ -320,7 +321,7 @@ class CCoinsViewCache : public CCoinsViewBacked
320321
bool HaveCoins(const uint256 &txid);
321322
uint256 GetBestBlock();
322323
bool SetBestBlock(const uint256 &hashBlock);
323-
bool BatchWrite(const std::map<uint256, CCoins> &mapCoins, const uint256 &hashBlock);
324+
bool BatchWrite(const CCoinsMap &mapCoins, const uint256 &hashBlock);
324325

325326
// Return a modifiable reference to a CCoins. Check HaveCoins first.
326327
// Many methods explicitly require a CCoinsViewCache because of this method, to reduce
@@ -352,7 +353,7 @@ class CCoinsViewCache : public CCoinsViewBacked
352353
const CTxOut &GetOutputFor(const CTxIn& input);
353354

354355
private:
355-
std::map<uint256,CCoins>::iterator FetchCoins(const uint256 &txid);
356+
CCoinsMap::iterator FetchCoins(const uint256 &txid);
356357
};
357358

358359
#endif

src/txdb.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,11 +54,11 @@ bool CCoinsViewDB::SetBestBlock(const uint256 &hashBlock) {
5454
return db.WriteBatch(batch);
5555
}
5656

57-
bool CCoinsViewDB::BatchWrite(const std::map<uint256, CCoins> &mapCoins, const uint256 &hashBlock) {
57+
bool CCoinsViewDB::BatchWrite(const CCoinsMap &mapCoins, const uint256 &hashBlock) {
5858
LogPrint("coindb", "Committing %u changed transactions to coin database...\n", (unsigned int)mapCoins.size());
5959

6060
CLevelDBBatch batch;
61-
for (std::map<uint256, CCoins>::const_iterator it = mapCoins.begin(); it != mapCoins.end(); it++)
61+
for (CCoinsMap::const_iterator it = mapCoins.begin(); it != mapCoins.end(); it++)
6262
BatchWriteCoins(batch, it->first, it->second);
6363
if (hashBlock != uint256(0))
6464
BatchWriteHashBestChain(batch, hashBlock);

src/txdb.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ class CCoinsViewDB : public CCoinsView
3737
bool HaveCoins(const uint256 &txid);
3838
uint256 GetBestBlock();
3939
bool SetBestBlock(const uint256 &hashBlock);
40-
bool BatchWrite(const std::map<uint256, CCoins> &mapCoins, const uint256 &hashBlock);
40+
bool BatchWrite(const CCoinsMap &mapCoins, const uint256 &hashBlock);
4141
bool GetStats(CCoinsStats &stats);
4242
};
4343

0 commit comments

Comments
 (0)