Skip to content

Commit cdee8c7

Browse files
authored
Merge pull request #2507 from jamescowens/enhance_dbcache
util: Change default -dbcache to 100 MB and also implement -txindexdbcache
2 parents d07bdb0 + f4a23b4 commit cdee8c7

File tree

4 files changed

+23
-3
lines changed

4 files changed

+23
-3
lines changed

src/dbwrapper.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,9 @@ leveldb::DB *txdb; // global pointer for LevelDB object instance
2929

3030
static leveldb::Options GetOptions() {
3131
leveldb::Options options;
32-
int nCacheSizeMB = gArgs.GetArg("-dbcache", 25);
32+
33+
int nCacheSizeMB = std::clamp<int>(gArgs.GetArg("-txindexdbcache", nDefaultDbCache), nMinDbCache, nMaxTxIndexCache);
34+
3335
options.block_cache = leveldb::NewLRUCache(nCacheSizeMB * 1048576);
3436
options.filter_policy = leveldb::NewBloomFilterPolicy(10);
3537
return options;

src/dbwrapper.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,18 @@
1414
#include <leveldb/db.h>
1515
#include <leveldb/write_batch.h>
1616

17+
// Note in Bitcoin these are in txdb.h, and we will eventually move them there when the db code is refactored.
18+
//! -dbcache default (MiB). This is for both bdb (the wallet) and leveldb (the transaction db).
19+
static const int64_t nDefaultDbCache = 100;
20+
//! max. -dbcache (MiB)
21+
//! Note that Bitcoin uses sizeof(void*) > 4 ? 16384 : 1024, but this includes the mempool. In Gridcoin it does not,
22+
//! so we use 1024 as the max.
23+
static const int64_t nMaxDbCache = 1024;
24+
//! min. -dbcache (MiB)
25+
static const int64_t nMinDbCache = 4;
26+
//! Max memory allocated to block tree DB (leveldb) cache. There is little performance gain over 1024 MB.
27+
static const int64_t nMaxTxIndexCache = 1024;
28+
1729
// Class that provides access to a LevelDB. Note that this class is frequently
1830
// instantiated on the stack and then destroyed again, so instantiation has to
1931
// be very cheap. Unfortunately that means, a CTxDB instance is actually just a

src/init.cpp

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -298,7 +298,12 @@ void SetupServerArgs()
298298
argsman.AddArg("-datadir=<dir>", "Specify data directory", ArgsManager::ALLOW_ANY, OptionsCategory::OPTIONS);
299299
argsman.AddArg("-wallet=<dir>", "Specify wallet file (within data directory)",
300300
ArgsManager::ALLOW_ANY, OptionsCategory::OPTIONS);
301-
argsman.AddArg("-dbcache=<n>", "Set database cache size in megabytes (default: 25)",
301+
argsman.AddArg("-dbcache=<n>", strprintf("Set database cache size in megabytes (%d to %d, default: %d)",
302+
nMinDbCache, nMaxDbCache, nDefaultDbCache),
303+
ArgsManager::ALLOW_ANY, OptionsCategory::OPTIONS);
304+
argsman.AddArg("-txindexdbcache=<n>", strprintf("Set txindex (leveldb) database cache size in megabytes "
305+
"(%d to %d, default: %d)",
306+
nMinDbCache, nMaxTxIndexCache, nDefaultDbCache),
302307
ArgsManager::ALLOW_ANY, OptionsCategory::OPTIONS);
303308
argsman.AddArg("-dblogsize=<n>", "Set database disk log size in megabytes (default: 100)",
304309
ArgsManager::ALLOW_ANY, OptionsCategory::OPTIONS);

src/wallet/db.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
// file COPYING or https://opensource.org/licenses/mit-license.php.
55

66
#include "wallet/db.h"
7+
#include "dbwrapper.h"
78
#include "net.h"
89
#include "main.h"
910
#include "node/ui_interface.h"
@@ -74,7 +75,7 @@ bool CDBEnv::Open(fs::path pathEnv_)
7475
if (gArgs.GetBoolArg("-privdb", true))
7576
nEnvFlags |= DB_PRIVATE;
7677

77-
int nDbCache = gArgs.GetArg("-dbcache", 25);
78+
int nDbCache = std::clamp<int>(gArgs.GetArg("-dbcache", nDefaultDbCache), nMinDbCache, nMaxDbCache);
7879
dbenv.set_lg_dir(pathLogDir.string().c_str());
7980
dbenv.set_cachesize(nDbCache / 1024, (nDbCache % 1024)*1048576, 1);
8081
dbenv.set_lg_bsize(1048576);

0 commit comments

Comments
 (0)