Skip to content

Commit

Permalink
Merge pull request #1981 from sipa/caches
Browse files Browse the repository at this point in the history
Cache size optimizations
  • Loading branch information
sipa committed Nov 8, 2012
2 parents 86406da + 1c83b0a commit 16d9d61
Show file tree
Hide file tree
Showing 7 changed files with 27 additions and 14 deletions.
17 changes: 14 additions & 3 deletions src/init.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,6 @@ std::string HelpMessage()
" -gen=0 " + _("Don't generate coins") + "\n" +
" -datadir=<dir> " + _("Specify data directory") + "\n" +
" -dbcache=<n> " + _("Set database cache size in megabytes (default: 25)") + "\n" +
" -dblogsize=<n> " + _("Set database disk log size in megabytes (default: 100)") + "\n" +
" -timeout=<n> " + _("Specify connection timeout in milliseconds (default: 5000)") + "\n" +
" -proxy=<ip:port> " + _("Connect through socks proxy") + "\n" +
" -socks=<n> " + _("Select the version of socks proxy to use (4-5, default: 5)") + "\n" +
Expand Down Expand Up @@ -651,11 +650,23 @@ bool AppInit2()
return InitError(msg);
}

// cache size calculations
size_t nTotalCache = GetArg("-dbcache", 25) << 20;
if (nTotalCache < (1 << 22))
nTotalCache = (1 << 22); // total cache cannot be less than 4 MiB
size_t nBlockTreeDBCache = nTotalCache / 8;
if (nBlockTreeDBCache > (1 << 21))
nBlockTreeDBCache = (1 << 21); // block tree db cache shouldn't be larger than 2 MiB
nTotalCache -= nBlockTreeDBCache;
size_t nCoinDBCache = nTotalCache / 2; // use half of the remaining cache for coindb cache
nTotalCache -= nCoinDBCache;
nCoinCacheSize = nTotalCache / 300; // coins in memory require around 300 bytes

uiInterface.InitMessage(_("Loading block index..."));
printf("Loading block index...\n");
nStart = GetTimeMillis();
pblocktree = new CBlockTreeDB();
pcoinsdbview = new CCoinsViewDB();
pblocktree = new CBlockTreeDB(nBlockTreeDBCache);
pcoinsdbview = new CCoinsViewDB(nCoinDBCache);
pcoinsTip = new CCoinsViewCache(*pcoinsdbview);

if (!LoadBlockIndex())
Expand Down
10 changes: 5 additions & 5 deletions src/leveldb.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,22 +12,22 @@

#include <boost/filesystem.hpp>

static leveldb::Options GetOptions() {
static leveldb::Options GetOptions(size_t nCacheSize) {
leveldb::Options options;
int nCacheSizeMB = GetArg("-dbcache", 25);
options.block_cache = leveldb::NewLRUCache(nCacheSizeMB * 1048576);
options.block_cache = leveldb::NewLRUCache(nCacheSize / 2);
options.write_buffer_size = nCacheSize / 4; // up to two write buffers may be held in memory simultaneously
options.filter_policy = leveldb::NewBloomFilterPolicy(10);
options.compression = leveldb::kNoCompression;
return options;
}

CLevelDB::CLevelDB(const boost::filesystem::path &path, bool fMemory) {
CLevelDB::CLevelDB(const boost::filesystem::path &path, size_t nCacheSize, bool fMemory) {
penv = NULL;
readoptions.verify_checksums = true;
iteroptions.verify_checksums = true;
iteroptions.fill_cache = false;
syncoptions.sync = true;
options = GetOptions();
options = GetOptions(nCacheSize);
options.create_if_missing = true;
if (fMemory) {
penv = leveldb::NewMemEnv(leveldb::Env::Default());
Expand Down
2 changes: 1 addition & 1 deletion src/leveldb.h
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ class CLevelDB
leveldb::DB *pdb;

public:
CLevelDB(const boost::filesystem::path &path, bool fMemory = false);
CLevelDB(const boost::filesystem::path &path, size_t nCacheSize, bool fMemory = false);
~CLevelDB();

template<typename K, typename V> bool Read(const K& key, V& value) {
Expand Down
3 changes: 2 additions & 1 deletion src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ CBlockIndex* pindexBest = NULL;
set<CBlockIndex*, CBlockIndexWorkComparator> setBlockIndexValid; // may contain all CBlockIndex*'s that have validness >=BLOCK_VALID_TRANSACTIONS, and must contain those who aren't failed
int64 nTimeBestReceived = 0;
bool fImporting = false;
unsigned int nCoinCacheSize = 5000;

CMedianFilter<int> cPeerBlockCounts(5, 0); // Amount of blocks that other nodes claim to have

Expand Down Expand Up @@ -1735,7 +1736,7 @@ bool SetBestChain(CBlockIndex* pindexNew)

// Make sure it's successfully written to disk before changing memory structure
bool fIsInitialDownload = IsInitialBlockDownload();
if (!fIsInitialDownload || view.GetCacheSize()>5000) {
if (!fIsInitialDownload || view.GetCacheSize() > nCoinCacheSize) {
FlushBlockFile();
pblocktree->Sync();
if (!view.Flush())
Expand Down
1 change: 1 addition & 0 deletions src/main.h
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ extern CCriticalSection cs_setpwalletRegistered;
extern std::set<CWallet*> setpwalletRegistered;
extern unsigned char pchMessageStart[4];
extern bool fImporting;
extern unsigned int nCoinCacheSize;

// Settings
extern int64 nTransactionFee;
Expand Down
4 changes: 2 additions & 2 deletions src/txdb.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ void static BatchWriteHashBestChain(CLevelDBBatch &batch, const uint256 &hash) {
batch.Write('B', hash);
}

CCoinsViewDB::CCoinsViewDB(bool fMemory) : db(GetDataDir() / "coins", fMemory) {
CCoinsViewDB::CCoinsViewDB(size_t nCacheSize, bool fMemory) : db(GetDataDir() / "coins", nCacheSize, fMemory) {
}

bool CCoinsViewDB::GetCoins(uint256 txid, CCoins &coins) {
Expand Down Expand Up @@ -64,7 +64,7 @@ bool CCoinsViewDB::BatchWrite(const std::map<uint256, CCoins> &mapCoins, CBlockI
return db.WriteBatch(batch);
}

CBlockTreeDB::CBlockTreeDB(bool fMemory) : CLevelDB(GetDataDir() / "blktree", fMemory) {
CBlockTreeDB::CBlockTreeDB(size_t nCacheSize, bool fMemory) : CLevelDB(GetDataDir() / "blktree", nCacheSize, fMemory) {
}

bool CBlockTreeDB::WriteBlockIndex(const CDiskBlockIndex& blockindex)
Expand Down
4 changes: 2 additions & 2 deletions src/txdb.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class CCoinsViewDB : public CCoinsView
protected:
CLevelDB db;
public:
CCoinsViewDB(bool fMemory = false);
CCoinsViewDB(size_t nCacheSize, bool fMemory = false);

bool GetCoins(uint256 txid, CCoins &coins);
bool SetCoins(uint256 txid, const CCoins &coins);
Expand All @@ -29,7 +29,7 @@ class CCoinsViewDB : public CCoinsView
class CBlockTreeDB : public CLevelDB
{
public:
CBlockTreeDB(bool fMemory = false);
CBlockTreeDB(size_t nCacheSize, bool fMemory = false);
private:
CBlockTreeDB(const CBlockTreeDB&);
void operator=(const CBlockTreeDB&);
Expand Down

0 comments on commit 16d9d61

Please sign in to comment.