node: allocate index caches proportional to usage patterns
What changed, and why it matters
This commit changes how Bitcoin Core divides its database cache among optional indexes. It adjusts the percentages allocated to the transaction index, transaction-spender index, and block filter index, and adds explanatory comments. There is no security fix here; it is a performance-tuning change for cache sizing.
No security action required. Treat as a routine performance/configuration change during review or upgrade planning.
Security signals we found
No strong security signals were identified.
Evidence from the diff
The patch modifies CalculateCacheSizes() in src/node/caches.cpp. Previously each enabled index could reserve up to 1/8 (12.5%) of the total database cache in sequence. The new code reserves fixed proportions (10% for txindex, 5% for txospenderindex, 5% shared across blockfilterindex instances) and subtracts them from the remaining cache after all index allocations are computed. It also documents why coinstatsindex is intentionally excluded from index cache allocation. This is a resource-allocation refactor, not a correctness or security change.
Changed components
src/node/caches.cppInspect captured patch +14 / −5
diff --git a/src/node/caches.cpp b/src/node/caches.cpp
index cb8afbc9..d1a54b95 100644
--- a/src/node/caches.cpp
+++ b/src/node/caches.cpp
@@ -58,16 +58,25 @@ 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 most from LevelDB 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}};
}
Why this scored 15/100
Community notes
Notes can correct, qualify, or add evidence to the AI analysis. Every note shown here has been validated by a human moderator.
The AI analysis stands alone for now. Submit a note if you can add evidence or important context.