Skip to content

Commit b8401c3

Browse files
martinussipa
andcommitted
Add pool based memory resource & allocator
A memory resource similar to std::pmr::unsynchronized_pool_resource, but optimized for node-based containers. Co-Authored-By: Pieter Wuille <[email protected]>
1 parent 2305643 commit b8401c3

File tree

8 files changed

+688
-0
lines changed

8 files changed

+688
-0
lines changed

src/Makefile.am

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -261,6 +261,7 @@ BITCOIN_CORE_H = \
261261
shutdown.h \
262262
signet.h \
263263
streams.h \
264+
support/allocators/pool.h \
264265
support/allocators/secure.h \
265266
support/allocators/zeroafterfree.h \
266267
support/cleanse.h \

src/Makefile.bench.include

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ bench_bench_bitcoin_SOURCES = \
4242
bench/nanobench.h \
4343
bench/peer_eviction.cpp \
4444
bench/poly1305.cpp \
45+
bench/pool.cpp \
4546
bench/prevector.cpp \
4647
bench/rollingbloom.cpp \
4748
bench/rpc_blockchain.cpp \

src/Makefile.test.include

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,7 @@ BITCOIN_TESTS =\
116116
test/pmt_tests.cpp \
117117
test/policy_fee_tests.cpp \
118118
test/policyestimator_tests.cpp \
119+
test/pool_tests.cpp \
119120
test/pow_tests.cpp \
120121
test/prevector_tests.cpp \
121122
test/raii_event_tests.cpp \

src/Makefile.test_util.include

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ TEST_UTIL_H = \
1515
test/util/logging.h \
1616
test/util/mining.h \
1717
test/util/net.h \
18+
test/util/poolresourcetester.h \
1819
test/util/random.h \
1920
test/util/script.h \
2021
test/util/setup_common.h \

src/bench/pool.cpp

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
// Copyright (c) 2022 The Bitcoin Core developers
2+
// Distributed under the MIT software license, see the accompanying
3+
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
4+
5+
#include <bench/bench.h>
6+
#include <support/allocators/pool.h>
7+
8+
#include <unordered_map>
9+
10+
template <typename Map>
11+
void BenchFillClearMap(benchmark::Bench& bench, Map& map)
12+
{
13+
size_t batch_size = 5000;
14+
15+
// make sure each iteration of the benchmark contains exactly 5000 inserts and one clear.
16+
// do this at least 10 times so we get reasonable accurate results
17+
18+
bench.batch(batch_size).minEpochIterations(10).run([&] {
19+
auto rng = ankerl::nanobench::Rng(1234);
20+
for (size_t i = 0; i < batch_size; ++i) {
21+
map[rng()];
22+
}
23+
map.clear();
24+
});
25+
}
26+
27+
static void PoolAllocator_StdUnorderedMap(benchmark::Bench& bench)
28+
{
29+
auto map = std::unordered_map<uint64_t, uint64_t>();
30+
BenchFillClearMap(bench, map);
31+
}
32+
33+
static void PoolAllocator_StdUnorderedMapWithPoolResource(benchmark::Bench& bench)
34+
{
35+
using Map = std::unordered_map<uint64_t,
36+
uint64_t,
37+
std::hash<uint64_t>,
38+
std::equal_to<uint64_t>,
39+
PoolAllocator<std::pair<const uint64_t, uint64_t>,
40+
sizeof(std::pair<const uint64_t, uint64_t>) + 4 * sizeof(void*),
41+
alignof(void*)>>;
42+
43+
// make sure the resource supports large enough pools to hold the node. We do this by adding the size of a few pointers to it.
44+
auto pool_resource = Map::allocator_type::ResourceType();
45+
auto map = Map{0, std::hash<uint64_t>{}, std::equal_to<uint64_t>{}, &pool_resource};
46+
BenchFillClearMap(bench, map);
47+
}
48+
49+
BENCHMARK(PoolAllocator_StdUnorderedMap, benchmark::PriorityLevel::HIGH);
50+
BENCHMARK(PoolAllocator_StdUnorderedMapWithPoolResource, benchmark::PriorityLevel::HIGH);

0 commit comments

Comments
 (0)