Skip to content

Commit c252685

Browse files
committed
Merge #8610: Share unused mempool memory with coincache
ba3cecf Share unused mempool memory with coincache (Pieter Wuille)
2 parents a7d55c9 + ba3cecf commit c252685

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
@@ -1340,10 +1340,11 @@ bool AppInitMain(boost::thread_group& threadGroup, CScheduler& scheduler)
13401340
nCoinDBCache = std::min(nCoinDBCache, nMaxCoinsDBCache << 20); // cap total coins db cache
13411341
nTotalCache -= nCoinDBCache;
13421342
nCoinCacheUsage = nTotalCache; // the rest goes to in-memory cache
1343+
int64_t nMempoolSizeMax = GetArg("-maxmempool", DEFAULT_MAX_MEMPOOL_SIZE) * 1000000;
13431344
LogPrintf("Cache configuration:\n");
13441345
LogPrintf("* Using %.1fMiB for block index database\n", nBlockTreeDBCache * (1.0 / 1024 / 1024));
13451346
LogPrintf("* Using %.1fMiB for chain state database\n", nCoinDBCache * (1.0 / 1024 / 1024));
1346-
LogPrintf("* Using %.1fMiB for in-memory UTXO set\n", nCoinCacheUsage * (1.0 / 1024 / 1024));
1347+
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));
13471348

13481349
bool fLoaded = false;
13491350
while (!fLoaded) {

src/validation.cpp

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1917,6 +1917,7 @@ bool ConnectBlock(const CBlock& block, CValidationState& state, CBlockIndex* pin
19171917
* or always and in all cases if we're in prune mode and are deleting files.
19181918
*/
19191919
bool static FlushStateToDisk(CValidationState &state, FlushStateMode mode) {
1920+
int64_t nMempoolUsage = mempool.DynamicMemoryUsage();
19201921
const CChainParams& chainparams = Params();
19211922
LOCK2(cs_main, cs_LastBlockFile);
19221923
static int64_t nLastWrite = 0;
@@ -1947,11 +1948,13 @@ bool static FlushStateToDisk(CValidationState &state, FlushStateMode mode) {
19471948
if (nLastSetChain == 0) {
19481949
nLastSetChain = nNow;
19491950
}
1950-
size_t cacheSize = pcoinsTip->DynamicMemoryUsage();
1951-
// The cache is large and close to the limit, but we have time now (not in the middle of a block processing).
1952-
bool fCacheLarge = mode == FLUSH_STATE_PERIODIC && cacheSize * (10.0/9) > nCoinCacheUsage;
1951+
int64_t nMempoolSizeMax = GetArg("-maxmempool", DEFAULT_MAX_MEMPOOL_SIZE) * 1000000;
1952+
int64_t cacheSize = pcoinsTip->DynamicMemoryUsage();
1953+
int64_t nTotalSpace = nCoinCacheUsage + std::max<int64_t>(nMempoolSizeMax - nMempoolUsage, 0);
1954+
// 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).
1955+
bool fCacheLarge = mode == FLUSH_STATE_PERIODIC && cacheSize > std::max((9 * nTotalSpace) / 10, nTotalSpace - 100 * 1024 * 1024);
19531956
// The cache is over the limit, we have to write now.
1954-
bool fCacheCritical = mode == FLUSH_STATE_IF_NEEDED && cacheSize > nCoinCacheUsage;
1957+
bool fCacheCritical = mode == FLUSH_STATE_IF_NEEDED && cacheSize > nTotalSpace;
19551958
// 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.
19561959
bool fPeriodicWrite = mode == FLUSH_STATE_PERIODIC && nNow > nLastWrite + (int64_t)DATABASE_WRITE_INTERVAL * 1000000;
19571960
// It's been very long since we flushed the cache. Do this infrequently, to optimize cache usage.

0 commit comments

Comments
 (0)