coins: introduce dynamic batch size calculator based on dbcache value

The size scales linearly with `-dbcache` above the default 450 MiB, clamped between a minimum of `DEFAULT_DB_CACHE_BATCH` (16 MiB) and a maximum of 256 MiB.
The minimum coincides with the default to prevent performance degradation for small caches and avoid discontinuities from integer division.
This allows larger in-memory caches to use proportionally larger batch sizes, improving flush efficiency without impacting default configurations.

Includes unit test coverage for key transition points.
This commit is contained in:
Lőrinc 2025-04-08 17:49:19 +02:00
parent d91a746815
commit a32489a175
3 changed files with 39 additions and 1 deletions

View file

@ -16,6 +16,9 @@ static constexpr size_t MAX_BLOCK_DB_CACHE{2_MiB};
//! Max memory allocated to coin DB specific cache (bytes)
static constexpr size_t MAX_COINS_DB_CACHE{8_MiB};
//! The batch size of DEFAULT_KERNEL_CACHE
static constexpr size_t DEFAULT_DB_CACHE_BATCH{16_MiB};
namespace kernel {
struct CacheSizes {
size_t block_tree_db;

View file

@ -5,10 +5,22 @@
#ifndef BITCOIN_NODE_COINS_VIEW_ARGS_H
#define BITCOIN_NODE_COINS_VIEW_ARGS_H
#include <kernel/caches.h>
class ArgsManager;
struct CoinsViewOptions;
namespace node {
namespace node
{
static constexpr size_t GetDbBatchSize(const size_t dbcache_bytes)
{
return std::clamp(
(dbcache_bytes / DEFAULT_KERNEL_CACHE) * DEFAULT_DB_CACHE_BATCH,
/*lo=*/DEFAULT_DB_CACHE_BATCH,
/*hi=*/256_MiB
);
}
void ReadCoinsViewArgs(const ArgsManager& args, CoinsViewOptions& options);
} // namespace node

View file

@ -20,6 +20,7 @@
#include <vector>
#include <boost/test/unit_test.hpp>
#include <node/coins_view_args.h>
using namespace util::hex_literals;
@ -1074,4 +1075,26 @@ BOOST_AUTO_TEST_CASE(coins_resource_is_used)
PoolResourceTester::CheckAllDataAccountedFor(resource);
}
BOOST_AUTO_TEST_CASE(db_batch_sizes)
{
BOOST_REQUIRE_EQUAL(node::GetDbBatchSize(DEFAULT_KERNEL_CACHE), DEFAULT_DB_CACHE_BATCH);
BOOST_REQUIRE_EQUAL(node::GetDbBatchSize(0_MiB), DEFAULT_DB_CACHE_BATCH);
BOOST_CHECK_EQUAL(node::GetDbBatchSize(4_MiB), 16'777'216);
BOOST_CHECK_EQUAL(node::GetDbBatchSize(10_MiB), 16'777'216);
BOOST_CHECK_EQUAL(node::GetDbBatchSize(45_MiB), 16'777'216);
BOOST_CHECK_EQUAL(node::GetDbBatchSize(100_MiB), 16'777'216);
BOOST_CHECK_EQUAL(node::GetDbBatchSize(450_MiB), 16'777'216);
BOOST_CHECK_EQUAL(node::GetDbBatchSize(1000_MiB), 33'554'432);
BOOST_CHECK_EQUAL(node::GetDbBatchSize(2000_MiB), 67'108'864);
BOOST_CHECK_EQUAL(node::GetDbBatchSize(3000_MiB), 100'663'296);
#if SIZE_MAX > UINT32_MAX
BOOST_CHECK_EQUAL(node::GetDbBatchSize(4500_MiB), 167'772'160);
BOOST_CHECK_EQUAL(node::GetDbBatchSize(7000_MiB), 251'658'240);
BOOST_CHECK_EQUAL(node::GetDbBatchSize(10000_MiB), 268'435'456);
BOOST_CHECK_EQUAL(node::GetDbBatchSize(45000_MiB), 268'435'456);
#endif
}
BOOST_AUTO_TEST_SUITE_END()