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;

//! min. -dbcache (MiB)
static constexpr int64_t MIN_DB_CACHE{4};
//! -dbcache default (MiB)
static constexpr int64_t DEFAULT_DB_CACHE{DEFAULT_KERNEL_CACHE};

namespace node {
struct IndexCacheSizes {
    size_t tx_index;
    size_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: Me and the compilers would prefer a contrived name over `std::tuple<...>`. It still allows for the structured bindings in *init.cpp*.


```suggestion
struct IndexAndKernelCacheSizes {
    IndexCacheSizes index;
    kernel::CacheSizes kernel;
};
IndexAndKernelCacheSizes CalculateCacheSizes(const ArgsManager& args, size_t n_indexes = 0);
```

<details>
<summary>Full diff</summary>

```diff
diff --git a/src/node/caches.cpp b/src/node/caches.cpp
index bd0c93d76f..c7197b6ef1 100644
--- a/src/node/caches.cpp
+++ b/src/node/caches.cpp
@@ -18,7 +18,7 @@ static constexpr int64_t MAX_TX_INDEX_CACHE{1024};
 static constexpr int64_t MAX_FILTER_INDEX_CACHE{1024};
 
 namespace node {
-std::tuple<IndexCacheSizes, kernel::CacheSizes> CalculateCacheSizes(const ArgsManager& args, size_t n_indexes)
+IndexAndKernelCacheSizes CalculateCacheSizes(const ArgsManager& args, size_t n_indexes)
 {
     int64_t nTotalCache = (args.GetIntArg("-dbcache", DEFAULT_DB_CACHE) << 20);
     nTotalCache = std::max(nTotalCache, MIN_DB_CACHE << 20);
diff --git a/src/node/caches.h b/src/node/caches.h
index 02b91cee87..afa0946d8b 100644
--- a/src/node/caches.h
+++ b/src/node/caches.h
@@ -9,7 +9,6 @@
 
 #include <cstddef>
 #include <cstdint>
-#include <tuple>
 
 class ArgsManager;
 
@@ -23,7 +22,11 @@ struct IndexCacheSizes {
     size_t tx_index;
     size_t filter_index;
 };
-std::tuple<IndexCacheSizes, kernel::CacheSizes> CalculateCacheSizes(const ArgsManager& args, size_t n_indexes = 0);
+struct IndexAndKernelCacheSizes {
+    IndexCacheSizes index;
+    kernel::CacheSizes kernel;
+};
+IndexAndKernelCacheSizes 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 ca9e2111a4..33ad258457 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 {
-    kernel::CacheSizes m_kernel_cache_sizes{std::get<1>(node::CalculateCacheSizes(m_args))};
+    kernel::CacheSizes m_kernel_cache_sizes{node::CalculateCacheSizes(m_args).kernel};
     bool m_coins_db_in_memory{true};
     bool m_block_tree_db_in_memory{true};
     std::function<void()> m_make_chainman{};
```
</details>

The compilation speed difference is probably not measurable, more a case of death by a thousand eventual cuts.

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