From 120282a66050c86717fb1b34fe502c621ca797d6 Mon Sep 17 00:00:00 2001 From: Prasanna Loganathar Date: Tue, 24 May 2022 21:00:59 +0530 Subject: [PATCH] Minimally intrusive conservative cache tuning (#1269) * Minimally intrusive conservative cache tuning * Update txdb.h * Update dbwrapper.cpp * Tune to use powers of 2 * Update dbwrapper.cpp * Add casts * Update dbwrapper.cpp * Update dbwrapper.cpp --- src/dbwrapper.cpp | 12 ++++++++++-- src/txdb.h | 4 ++-- 2 files changed, 12 insertions(+), 4 deletions(-) diff --git a/src/dbwrapper.cpp b/src/dbwrapper.cpp index 54e6263cf59..a3a770e2978 100644 --- a/src/dbwrapper.cpp +++ b/src/dbwrapper.cpp @@ -99,10 +99,18 @@ static void SetMaxOpenFiles(leveldb::Options *options) { static leveldb::Options GetOptions(size_t nCacheSize) { + const auto ceil_power_of_two = [](size_t v) { + v--; + for (const size_t i: {1, 2, 4, 6, 16}) v |= v >> i; + v++; + return v; + }; + leveldb::Options options; 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.write_buffer_size = ceil_power_of_two(std::min(static_cast(64) + << 20, nCacheSize / 4)); // Max of 64mb -more is not useful + options.filter_policy = leveldb::NewBloomFilterPolicy(16); options.compression = leveldb::kNoCompression; options.info_log = new CDefiLevelDBLogger(); if (leveldb::kMajorVersion > 1 || (leveldb::kMajorVersion == 1 && leveldb::kMinorVersion >= 16)) { diff --git a/src/txdb.h b/src/txdb.h index d25b412a979..a4584eb916a 100644 --- a/src/txdb.h +++ b/src/txdb.h @@ -24,7 +24,7 @@ class uint256; //! No need to periodic flush if at least this much space still available. static constexpr int MAX_BLOCK_COINSDB_USAGE = 10; //! -dbcache default (MiB) -static const int64_t nDefaultDbCache = 450; +static const int64_t nDefaultDbCache = 512; //! -dbbatchsize default (bytes) static const int64_t nDefaultDbBatchSize = 16 << 20; //! max. -dbcache (MiB) @@ -32,7 +32,7 @@ static const int64_t nMaxDbCache = sizeof(void*) > 4 ? 16384 : 1024; //! min. -dbcache (MiB) static const int64_t nMinDbCache = 4; //! Max memory allocated to block tree DB specific cache, if no -txindex (MiB) -static const int64_t nMaxBlockDBCache = 2; +static const int64_t nMaxBlockDBCache = 4; //! Max memory allocated to block tree DB specific cache, if -txindex (MiB) // Unlike for the UTXO database, for the txindex scenario the leveldb cache make // a meaningful difference: https://github.com/bitcoin/bitcoin/pull/8273#issuecomment-229601991