Repository: bitcoin/bitcoin
File: src/node/caches.h (C/C++)

Code before:
// Copyright (c) 2021 The Bitcoin Core developers
// Distributed under the MIT software license, see the accompanying
// file COPYING or http://www.opensource.org/licenses/mit-license.php.

#ifndef BITCOIN_NODE_CACHES_H
#define BITCOIN_NODE_CACHES_H

#include <kernel/caches.h>

#include <cstddef>
#include <cstdint>
#include <tuple>

class ArgsManager;

namespace node {
struct IndexCacheSizes {
    int64_t tx_index;
    int64_t filter_index;
};
std::tuple<IndexCacheSizes, kernel::CacheSizes> CalculateCacheSizes(const ArgsManager& args, size_t n_indexes = 0);
} // namespace node

#endif // BITCOIN_NODE_CACHES_H


Review comment (performance):
nit: will we have separate structs for different node cache sizes? I think just keeping it `CacheSizes` and adding the `node::` namespace in ambiguous places makes more sense for now?

<details>
<summary>git diff on 2e6c43f9c3</summary>

```diff
diff --git a/src/node/caches.cpp b/src/node/caches.cpp
index 50e41b8d1e..41bd8c95d0 100644
--- a/src/node/caches.cpp
+++ b/src/node/caches.cpp
@@ -13,11 +13,11 @@
 #include <string>
 
 namespace node {
-std::tuple<IndexCacheSizes, kernel::CacheSizes> CalculateCacheSizes(const ArgsManager& args, size_t n_indexes)
+std::tuple<node::CacheSizes, kernel::CacheSizes> CalculateCacheSizes(const ArgsManager& args, size_t n_indexes)
 {
     int64_t nTotalCache = (args.GetIntArg("-dbcache", nDefaultDbCache) << 20);
     nTotalCache = std::max(nTotalCache, nMinDbCache << 20); // total cache cannot be less than nMinDbCache
-    IndexCacheSizes sizes;
+    CacheSizes sizes;
     sizes.tx_index = std::min(nTotalCache / 8, args.GetBoolArg("-txindex", DEFAULT_TXINDEX) ? nMaxTxIndexCache << 20 : 0);
     nTotalCache -= sizes.tx_index;
     sizes.filter_index = 0;
diff --git a/src/node/caches.h b/src/node/caches.h
index f4cc14bb23..c47134539b 100644
--- a/src/node/caches.h
+++ b/src/node/caches.h
@@ -14,11 +14,11 @@
 class ArgsManager;
 
 namespace node {
-struct IndexCacheSizes {
+struct CacheSizes {
     int64_t tx_index;
     int64_t filter_index;
 };
-std::tuple<IndexCacheSizes, kernel::CacheSizes> CalculateCacheSizes(const ArgsManager& args, size_t n_indexes = 0);
+std::tuple<node::CacheSizes, kernel::CacheSizes> CalculateCacheSizes(const ArgsManager& args, size_t n_indexes = 0);
 } // namespace node
 
 #endif // BITCOIN_NODE_CACHES_H
diff --git a/src/test/util/setup_common.h b/src/test/util/setup_common.h
index 46035772f5..fbea90a20b 100644
--- a/src/test/util/setup_common.h
+++ b/src/test/util/setup_common.h
@@ -104,7 +104,7 @@ struct BasicTestingSetup {
  * initialization behaviour.
  */
 struct ChainTestingSetup : public BasicTestingSetup {
-    node::IndexCacheSizes m_index_cache_sizes{std::get<0>(node::CalculateCacheSizes(m_args))};
+    node::CacheSizes m_index_cache_sizes{std::get<0>(node::CalculateCacheSizes(m_args))};
     kernel::CacheSizes m_kernel_cache_sizes{std::get<1>(node::CalculateCacheSizes(m_args))};
     bool m_coins_db_in_memory{true};
     bool m_block_tree_db_in_memory{true};

```
</details>


Code after:
// Copyright (c) 2021 The Bitcoin Core developers
// Distributed under the MIT software license, see the accompanying
// file COPYING or http://www.opensource.org/licenses/mit-license.php.

#ifndef BITCOIN_NODE_CACHES_H
#define BITCOIN_NODE_CACHES_H

#include <kernel/caches.h>
#include <util/byte_units.h>

#include <cstddef>

class ArgsManager;

//! min. -dbcache (bytes)
static constexpr size_t MIN_DB_CACHE{4_MiB};
//! -dbcache default (bytes)
static constexpr size_t DEFAULT_DB_CACHE{DEFAULT_KERNEL_CACHE};

namespace node {
struct IndexCacheSizes {
    size_t tx_index{0};
    size_t filter_index{0};
};
struct CacheSizes {
    IndexCacheSizes index;
    kernel::CacheSizes kernel;
};
CacheSizes CalculateCacheSizes(const ArgsManager& args, size_t n_indexes = 0);
} // namespace node

#endif // BITCOIN_NODE_CACHES_H