Skip to content

Commit 9f947fc

Browse files
committed
Use PoolAllocator for CCoinsMap
In my benchmarks, using this pool allocator for CCoinsMap gives about 20% faster `-reindex-chainstate` with -dbcache=5000 with practically the same memory usage. The change in max RSS changed was 0.3%. The `validation_flush_tests` tests need to be updated because memory allocation is now done in large pools instead of one node at a time, so the limits need to be updated accordingly.
1 parent 5e4ac5a commit 9f947fc

File tree

5 files changed

+79
-17
lines changed

5 files changed

+79
-17
lines changed

src/coins.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ size_t CCoinsViewBacked::EstimateSize() const { return base->EstimateSize(); }
3434

3535
CCoinsViewCache::CCoinsViewCache(CCoinsView* baseIn, bool deterministic) :
3636
CCoinsViewBacked(baseIn), m_deterministic(deterministic),
37-
cacheCoins(0, SaltedOutpointHasher(/*deterministic=*/deterministic))
37+
cacheCoins(0, SaltedOutpointHasher(/*deterministic=*/deterministic), CCoinsMap::key_equal{}, &m_cache_coins_memory_resource)
3838
{}
3939

4040
size_t CCoinsViewCache::DynamicMemoryUsage() const {
@@ -317,7 +317,9 @@ void CCoinsViewCache::ReallocateCache()
317317
// Cache should be empty when we're calling this.
318318
assert(cacheCoins.size() == 0);
319319
cacheCoins.~CCoinsMap();
320-
::new (&cacheCoins) CCoinsMap(0, SaltedOutpointHasher(/*deterministic=*/m_deterministic));
320+
m_cache_coins_memory_resource.~CCoinsMapMemoryResource();
321+
::new (&m_cache_coins_memory_resource) CCoinsMapMemoryResource{};
322+
::new (&cacheCoins) CCoinsMap{0, SaltedOutpointHasher{/*deterministic=*/m_deterministic}, CCoinsMap::key_equal{}, &m_cache_coins_memory_resource};
321323
}
322324

323325
void CCoinsViewCache::SanityCheck() const

src/coins.h

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
#include <memusage.h>
1212
#include <primitives/transaction.h>
1313
#include <serialize.h>
14+
#include <support/allocators/pool.h>
1415
#include <uint256.h>
1516
#include <util/hasher.h>
1617

@@ -131,7 +132,23 @@ struct CCoinsCacheEntry
131132
CCoinsCacheEntry(Coin&& coin_, unsigned char flag) : coin(std::move(coin_)), flags(flag) {}
132133
};
133134

134-
typedef std::unordered_map<COutPoint, CCoinsCacheEntry, SaltedOutpointHasher> CCoinsMap;
135+
/**
136+
* PoolAllocator's MAX_BLOCK_SIZE_BYTES parameter here uses sizeof the data, and adds the size
137+
* of 4 pointers. We do not know the exact node size used in the std::unordered_node implementation
138+
* because it is implementation defined. Most implementations have an overhead of 1 or 2 pointers,
139+
* so nodes can be connected in a linked list, and in some cases the hash value is stored as well.
140+
* Using an additional sizeof(void*)*4 for MAX_BLOCK_SIZE_BYTES should thus be sufficient so that
141+
* all implementations can allocate the nodes from the PoolAllocator.
142+
*/
143+
using CCoinsMap = std::unordered_map<COutPoint,
144+
CCoinsCacheEntry,
145+
SaltedOutpointHasher,
146+
std::equal_to<COutPoint>,
147+
PoolAllocator<std::pair<const COutPoint, CCoinsCacheEntry>,
148+
sizeof(std::pair<const COutPoint, CCoinsCacheEntry>) + sizeof(void*) * 4,
149+
alignof(void*)>>;
150+
151+
using CCoinsMapMemoryResource = CCoinsMap::allocator_type::ResourceType;
135152

136153
/** Cursor for iterating over CoinsView state */
137154
class CCoinsViewCursor
@@ -220,6 +237,7 @@ class CCoinsViewCache : public CCoinsViewBacked
220237
* declared as "const".
221238
*/
222239
mutable uint256 hashBlock;
240+
mutable CCoinsMapMemoryResource m_cache_coins_memory_resource{};
223241
mutable CCoinsMap cacheCoins;
224242

225243
/* Cached dynamic memory usage for the inner Coin objects. */

src/test/coins_tests.cpp

Lines changed: 36 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
#include <coins.h>
77
#include <script/standard.h>
88
#include <streams.h>
9+
#include <test/util/poolresourcetester.h>
910
#include <test/util/random.h>
1011
#include <test/util/setup_common.h>
1112
#include <txdb.h>
@@ -612,7 +613,8 @@ void GetCoinsMapEntry(const CCoinsMap& map, CAmount& value, char& flags, const C
612613

613614
void WriteCoinsViewEntry(CCoinsView& view, CAmount value, char flags)
614615
{
615-
CCoinsMap map;
616+
CCoinsMapMemoryResource resource;
617+
CCoinsMap map{0, CCoinsMap::hasher{}, CCoinsMap::key_equal{}, &resource};
616618
InsertCoinsMapEntry(map, value, flags);
617619
BOOST_CHECK(view.BatchWrite(map, {}));
618620
}
@@ -911,6 +913,7 @@ void TestFlushBehavior(
911913
CAmount value;
912914
char flags;
913915
size_t cache_usage;
916+
size_t cache_size;
914917

915918
auto flush_all = [&all_caches](bool erase) {
916919
// Flush in reverse order to ensure that flushes happen from children up.
@@ -935,6 +938,8 @@ void TestFlushBehavior(
935938
view->AddCoin(outp, Coin(coin), false);
936939

937940
cache_usage = view->DynamicMemoryUsage();
941+
cache_size = view->map().size();
942+
938943
// `base` shouldn't have coin (no flush yet) but `view` should have cached it.
939944
BOOST_CHECK(!base.HaveCoin(outp));
940945
BOOST_CHECK(view->HaveCoin(outp));
@@ -949,6 +954,7 @@ void TestFlushBehavior(
949954

950955
// CoinsMap usage should be unchanged since we didn't erase anything.
951956
BOOST_CHECK_EQUAL(cache_usage, view->DynamicMemoryUsage());
957+
BOOST_CHECK_EQUAL(cache_size, view->map().size());
952958

953959
// --- 3. Ensuring the entry still exists in the cache and has been written to parent
954960
//
@@ -965,8 +971,10 @@ void TestFlushBehavior(
965971
//
966972
flush_all(/*erase=*/ true);
967973

968-
// Memory usage should have gone down.
969-
BOOST_CHECK(view->DynamicMemoryUsage() < cache_usage);
974+
// Memory does not necessarily go down due to the map using a memory pool
975+
BOOST_TEST(view->DynamicMemoryUsage() <= cache_usage);
976+
// Size of the cache must go down though
977+
BOOST_TEST(view->map().size() < cache_size);
970978

971979
// --- 5. Ensuring the entry is no longer in the cache
972980
//
@@ -1076,4 +1084,29 @@ BOOST_AUTO_TEST_CASE(ccoins_flush_behavior)
10761084
}
10771085
}
10781086

1087+
BOOST_AUTO_TEST_CASE(coins_resource_is_used)
1088+
{
1089+
CCoinsMapMemoryResource resource;
1090+
PoolResourceTester::CheckAllDataAccountedFor(resource);
1091+
1092+
{
1093+
CCoinsMap map{0, CCoinsMap::hasher{}, CCoinsMap::key_equal{}, &resource};
1094+
BOOST_TEST(memusage::DynamicUsage(map) >= resource.ChunkSizeBytes());
1095+
1096+
map.reserve(1000);
1097+
1098+
// The resource has preallocated a chunk, so we should have space for at several nodes without the need to allocate anything else.
1099+
const auto usage_before = memusage::DynamicUsage(map);
1100+
1101+
COutPoint out_point{};
1102+
for (size_t i = 0; i < 1000; ++i) {
1103+
out_point.n = i;
1104+
map[out_point];
1105+
}
1106+
BOOST_TEST(usage_before == memusage::DynamicUsage(map));
1107+
}
1108+
1109+
PoolResourceTester::CheckAllDataAccountedFor(resource);
1110+
}
1111+
10791112
BOOST_AUTO_TEST_SUITE_END()

src/test/fuzz/coins_view.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,8 @@ FUZZ_TARGET_INIT(coins_view, initialize_coins_view)
115115
random_mutable_transaction = *opt_mutable_transaction;
116116
},
117117
[&] {
118-
CCoinsMap coins_map;
118+
CCoinsMapMemoryResource resource;
119+
CCoinsMap coins_map{0, SaltedOutpointHasher{/*deterministic=*/true}, CCoinsMap::key_equal{}, &resource};
119120
LIMITED_WHILE(fuzzed_data_provider.ConsumeBool(), 10000) {
120121
CCoinsCacheEntry coins_cache_entry;
121122
coins_cache_entry.flags = fuzzed_data_provider.ConsumeIntegral<unsigned char>();

src/test/validation_flush_tests.cpp

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -36,12 +36,12 @@ BOOST_AUTO_TEST_CASE(getcoinscachesizestate)
3636
BOOST_TEST_MESSAGE("CCoinsViewCache memory usage: " << view.DynamicMemoryUsage());
3737
};
3838

39-
constexpr size_t MAX_COINS_CACHE_BYTES = 1024;
39+
// PoolResource defaults to 256 KiB that will be allocated, so we'll take that and make it a bit larger.
40+
constexpr size_t MAX_COINS_CACHE_BYTES = 262144 + 512;
4041

4142
// Without any coins in the cache, we shouldn't need to flush.
42-
BOOST_CHECK_EQUAL(
43-
chainstate.GetCoinsCacheSizeState(MAX_COINS_CACHE_BYTES, /*max_mempool_size_bytes=*/0),
44-
CoinsCacheSizeState::OK);
43+
BOOST_TEST(
44+
chainstate.GetCoinsCacheSizeState(MAX_COINS_CACHE_BYTES, /*max_mempool_size_bytes=*/ 0) != CoinsCacheSizeState::CRITICAL);
4545

4646
// If the initial memory allocations of cacheCoins don't match these common
4747
// cases, we can't really continue to make assertions about memory usage.
@@ -71,13 +71,21 @@ BOOST_AUTO_TEST_CASE(getcoinscachesizestate)
7171
// cacheCoins (unordered_map) preallocates.
7272
constexpr int COINS_UNTIL_CRITICAL{3};
7373

74+
// no coin added, so we have plenty of space left.
75+
BOOST_CHECK_EQUAL(
76+
chainstate.GetCoinsCacheSizeState(MAX_COINS_CACHE_BYTES, /*max_mempool_size_bytes*/ 0),
77+
CoinsCacheSizeState::OK);
78+
7479
for (int i{0}; i < COINS_UNTIL_CRITICAL; ++i) {
7580
const COutPoint res = AddTestCoin(view);
7681
print_view_mem_usage(view);
7782
BOOST_CHECK_EQUAL(view.AccessCoin(res).DynamicMemoryUsage(), COIN_SIZE);
83+
84+
// adding first coin causes the MemoryResource to allocate one 256 KiB chunk of memory,
85+
// pushing us immediately over to LARGE
7886
BOOST_CHECK_EQUAL(
79-
chainstate.GetCoinsCacheSizeState(MAX_COINS_CACHE_BYTES, /*max_mempool_size_bytes=*/0),
80-
CoinsCacheSizeState::OK);
87+
chainstate.GetCoinsCacheSizeState(MAX_COINS_CACHE_BYTES, /*max_mempool_size_bytes=*/ 0),
88+
CoinsCacheSizeState::LARGE);
8189
}
8290

8391
// Adding some additional coins will push us over the edge to CRITICAL.
@@ -94,16 +102,16 @@ BOOST_AUTO_TEST_CASE(getcoinscachesizestate)
94102
chainstate.GetCoinsCacheSizeState(MAX_COINS_CACHE_BYTES, /*max_mempool_size_bytes=*/0),
95103
CoinsCacheSizeState::CRITICAL);
96104

97-
// Passing non-zero max mempool usage should allow us more headroom.
105+
// Passing non-zero max mempool usage (512 KiB) should allow us more headroom.
98106
BOOST_CHECK_EQUAL(
99-
chainstate.GetCoinsCacheSizeState(MAX_COINS_CACHE_BYTES, /*max_mempool_size_bytes=*/1 << 10),
107+
chainstate.GetCoinsCacheSizeState(MAX_COINS_CACHE_BYTES, /*max_mempool_size_bytes=*/ 1 << 19),
100108
CoinsCacheSizeState::OK);
101109

102110
for (int i{0}; i < 3; ++i) {
103111
AddTestCoin(view);
104112
print_view_mem_usage(view);
105113
BOOST_CHECK_EQUAL(
106-
chainstate.GetCoinsCacheSizeState(MAX_COINS_CACHE_BYTES, /*max_mempool_size_bytes=*/1 << 10),
114+
chainstate.GetCoinsCacheSizeState(MAX_COINS_CACHE_BYTES, /*max_mempool_size_bytes=*/ 1 << 19),
107115
CoinsCacheSizeState::OK);
108116
}
109117

@@ -119,7 +127,7 @@ BOOST_AUTO_TEST_CASE(getcoinscachesizestate)
119127
BOOST_CHECK(usage_percentage >= 0.9);
120128
BOOST_CHECK(usage_percentage < 1);
121129
BOOST_CHECK_EQUAL(
122-
chainstate.GetCoinsCacheSizeState(MAX_COINS_CACHE_BYTES, 1 << 10),
130+
chainstate.GetCoinsCacheSizeState(MAX_COINS_CACHE_BYTES, /*max_mempool_size_bytes*/ 1 << 10), // 1024
123131
CoinsCacheSizeState::LARGE);
124132
}
125133

0 commit comments

Comments
 (0)