Skip to content

Commit

Permalink
Merge pull request #2507 from jamescowens/enhance_dbcache
Browse files Browse the repository at this point in the history
util: Change default -dbcache to 100 MB and also implement -txindexdbcache
  • Loading branch information
jamescowens authored May 2, 2022
2 parents d07bdb0 + f4a23b4 commit cdee8c7
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 3 deletions.
4 changes: 3 additions & 1 deletion src/dbwrapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,9 @@ leveldb::DB *txdb; // global pointer for LevelDB object instance

static leveldb::Options GetOptions() {
leveldb::Options options;
int nCacheSizeMB = gArgs.GetArg("-dbcache", 25);

int nCacheSizeMB = std::clamp<int>(gArgs.GetArg("-txindexdbcache", nDefaultDbCache), nMinDbCache, nMaxTxIndexCache);

options.block_cache = leveldb::NewLRUCache(nCacheSizeMB * 1048576);
options.filter_policy = leveldb::NewBloomFilterPolicy(10);
return options;
Expand Down
12 changes: 12 additions & 0 deletions src/dbwrapper.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,18 @@
#include <leveldb/db.h>
#include <leveldb/write_batch.h>

// Note in Bitcoin these are in txdb.h, and we will eventually move them there when the db code is refactored.
//! -dbcache default (MiB). This is for both bdb (the wallet) and leveldb (the transaction db).
static const int64_t nDefaultDbCache = 100;
//! max. -dbcache (MiB)
//! Note that Bitcoin uses sizeof(void*) > 4 ? 16384 : 1024, but this includes the mempool. In Gridcoin it does not,
//! so we use 1024 as the max.
static const int64_t nMaxDbCache = 1024;
//! min. -dbcache (MiB)
static const int64_t nMinDbCache = 4;
//! Max memory allocated to block tree DB (leveldb) cache. There is little performance gain over 1024 MB.
static const int64_t nMaxTxIndexCache = 1024;

// Class that provides access to a LevelDB. Note that this class is frequently
// instantiated on the stack and then destroyed again, so instantiation has to
// be very cheap. Unfortunately that means, a CTxDB instance is actually just a
Expand Down
7 changes: 6 additions & 1 deletion src/init.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -298,7 +298,12 @@ void SetupServerArgs()
argsman.AddArg("-datadir=<dir>", "Specify data directory", ArgsManager::ALLOW_ANY, OptionsCategory::OPTIONS);
argsman.AddArg("-wallet=<dir>", "Specify wallet file (within data directory)",
ArgsManager::ALLOW_ANY, OptionsCategory::OPTIONS);
argsman.AddArg("-dbcache=<n>", "Set database cache size in megabytes (default: 25)",
argsman.AddArg("-dbcache=<n>", strprintf("Set database cache size in megabytes (%d to %d, default: %d)",
nMinDbCache, nMaxDbCache, nDefaultDbCache),
ArgsManager::ALLOW_ANY, OptionsCategory::OPTIONS);
argsman.AddArg("-txindexdbcache=<n>", strprintf("Set txindex (leveldb) database cache size in megabytes "
"(%d to %d, default: %d)",
nMinDbCache, nMaxTxIndexCache, nDefaultDbCache),
ArgsManager::ALLOW_ANY, OptionsCategory::OPTIONS);
argsman.AddArg("-dblogsize=<n>", "Set database disk log size in megabytes (default: 100)",
ArgsManager::ALLOW_ANY, OptionsCategory::OPTIONS);
Expand Down
3 changes: 2 additions & 1 deletion src/wallet/db.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
// file COPYING or https://opensource.org/licenses/mit-license.php.

#include "wallet/db.h"
#include "dbwrapper.h"
#include "net.h"
#include "main.h"
#include "node/ui_interface.h"
Expand Down Expand Up @@ -74,7 +75,7 @@ bool CDBEnv::Open(fs::path pathEnv_)
if (gArgs.GetBoolArg("-privdb", true))
nEnvFlags |= DB_PRIVATE;

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

0 comments on commit cdee8c7

Please sign in to comment.