Skip to content

Commit ba3cecf

Browse files
committed
Share unused mempool memory with coincache
If the mempool is not completely full, treat the difference between the maximum size and the actual usage as available for the coin cache. This also changes the early flush trigger from (usage > 0.9 * space) to (usage > 0.9 * space && usage > space - 100MB). This means we're not permanently leaving 10% of the space unused when the space is large.
1 parent e8cfe1e commit ba3cecf

File tree

2 files changed

+9
-5
lines changed

2 files changed

+9
-5
lines changed

src/init.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1330,10 +1330,11 @@ bool AppInitMain(boost::thread_group& threadGroup, CScheduler& scheduler)
13301330
nCoinDBCache = std::min(nCoinDBCache, nMaxCoinsDBCache << 20); // cap total coins db cache
13311331
nTotalCache -= nCoinDBCache;
13321332
nCoinCacheUsage = nTotalCache; // the rest goes to in-memory cache
1333+
int64_t nMempoolSizeMax = GetArg("-maxmempool", DEFAULT_MAX_MEMPOOL_SIZE) * 1000000;
13331334
LogPrintf("Cache configuration:\n");
13341335
LogPrintf("* Using %.1fMiB for block index database\n", nBlockTreeDBCache * (1.0 / 1024 / 1024));
13351336
LogPrintf("* Using %.1fMiB for chain state database\n", nCoinDBCache * (1.0 / 1024 / 1024));
1336-
LogPrintf("* Using %.1fMiB for in-memory UTXO set\n", nCoinCacheUsage * (1.0 / 1024 / 1024));
1337+
LogPrintf("* Using %.1fMiB for in-memory UTXO set (plus up to %.1fMiB of unused mempool space)\n", nCoinCacheUsage * (1.0 / 1024 / 1024), nMempoolSizeMax * (1.0 / 1024 / 1024));
13371338

13381339
bool fLoaded = false;
13391340
while (!fLoaded) {

src/validation.cpp

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1916,6 +1916,7 @@ bool ConnectBlock(const CBlock& block, CValidationState& state, CBlockIndex* pin
19161916
* or always and in all cases if we're in prune mode and are deleting files.
19171917
*/
19181918
bool static FlushStateToDisk(CValidationState &state, FlushStateMode mode) {
1919+
int64_t nMempoolUsage = mempool.DynamicMemoryUsage();
19191920
const CChainParams& chainparams = Params();
19201921
LOCK2(cs_main, cs_LastBlockFile);
19211922
static int64_t nLastWrite = 0;
@@ -1946,11 +1947,13 @@ bool static FlushStateToDisk(CValidationState &state, FlushStateMode mode) {
19461947
if (nLastSetChain == 0) {
19471948
nLastSetChain = nNow;
19481949
}
1949-
size_t cacheSize = pcoinsTip->DynamicMemoryUsage();
1950-
// The cache is large and close to the limit, but we have time now (not in the middle of a block processing).
1951-
bool fCacheLarge = mode == FLUSH_STATE_PERIODIC && cacheSize * (10.0/9) > nCoinCacheUsage;
1950+
int64_t nMempoolSizeMax = GetArg("-maxmempool", DEFAULT_MAX_MEMPOOL_SIZE) * 1000000;
1951+
int64_t cacheSize = pcoinsTip->DynamicMemoryUsage();
1952+
int64_t nTotalSpace = nCoinCacheUsage + std::max<int64_t>(nMempoolSizeMax - nMempoolUsage, 0);
1953+
// The cache is large and we're within 10% and 100 MiB of the limit, but we have time now (not in the middle of a block processing).
1954+
bool fCacheLarge = mode == FLUSH_STATE_PERIODIC && cacheSize > std::max((9 * nTotalSpace) / 10, nTotalSpace - 100 * 1024 * 1024);
19521955
// The cache is over the limit, we have to write now.
1953-
bool fCacheCritical = mode == FLUSH_STATE_IF_NEEDED && cacheSize > nCoinCacheUsage;
1956+
bool fCacheCritical = mode == FLUSH_STATE_IF_NEEDED && cacheSize > nTotalSpace;
19541957
// It's been a while since we wrote the block index to disk. Do this frequently, so we don't need to redownload after a crash.
19551958
bool fPeriodicWrite = mode == FLUSH_STATE_PERIODIC && nNow > nLastWrite + (int64_t)DATABASE_WRITE_INTERVAL * 1000000;
19561959
// It's been very long since we flushed the cache. Do this infrequently, to optimize cache usage.

0 commit comments

Comments
 (0)