diff --git a/src/node/caches.cpp b/src/node/caches.cpp index 94d0ced88f6..c98b8ce6040 100644 --- a/src/node/caches.cpp +++ b/src/node/caches.cpp @@ -58,16 +58,27 @@ CacheSizes CalculateCacheSizes(const ArgsManager& args, size_t n_indexes) { size_t total_cache{CalculateDbCacheBytes(args)}; + // Allocate proportional to usage pattern benefit: + // - txindex (10%): serves getrawtransaction RPCs with mostly unique, + // non-repetitive lookups across the entire blockchain. + // - blockfilterindex (5%): serves BIP 157 light clients that repeatedly + // query recent blocks, benefiting from LevelDB cache, but the + // working set for a typical 2-week offline gap is ~200kiB, well within 5% + // of the total cache. + // - txospenderindex (5%): serves gettxspendingprevout RPCs with very + // specific, rarely repeated outpoint queries. + // - coinstatsindex: intentionally not included here, since usage pattern + // does not seem to suggest it would be necessary to cache. IndexCacheSizes index_sizes; - index_sizes.tx_index = std::min(total_cache / 8, args.GetBoolArg("-txindex", DEFAULT_TXINDEX) ? MAX_TX_INDEX_CACHE : 0); - total_cache -= index_sizes.tx_index; - index_sizes.txospender_index = std::min(total_cache / 8, args.GetBoolArg("-txospenderindex", DEFAULT_TXOSPENDERINDEX) ? MAX_TXOSPENDER_INDEX_CACHE : 0); - total_cache -= index_sizes.txospender_index; + index_sizes.tx_index = std::min(total_cache * 10 / 100, args.GetBoolArg("-txindex", DEFAULT_TXINDEX) ? MAX_TX_INDEX_CACHE : 0); + index_sizes.txospender_index = std::min(total_cache * 5 / 100, args.GetBoolArg("-txospenderindex", DEFAULT_TXOSPENDERINDEX) ? MAX_TXOSPENDER_INDEX_CACHE : 0); if (n_indexes > 0) { - size_t max_cache = std::min(total_cache / 8, MAX_FILTER_INDEX_CACHE); + size_t max_cache = std::min(total_cache * 5 / 100, MAX_FILTER_INDEX_CACHE); index_sizes.filter_index = max_cache / n_indexes; total_cache -= index_sizes.filter_index * n_indexes; } + total_cache -= index_sizes.tx_index; + total_cache -= index_sizes.txospender_index; return {index_sizes, kernel::CacheSizes{total_cache}}; }