Skip to content

Commit e19943f

Browse files
committed
Calculate memory usage correctly for unordered_maps that use PoolAllocator
Extracts the resource from a PoolAllocator and uses it for calculation of the node's memory usage.
1 parent b8401c3 commit e19943f

File tree

2 files changed

+54
-0
lines changed

2 files changed

+54
-0
lines changed

src/memusage.h

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
#include <indirectmap.h>
99
#include <prevector.h>
10+
#include <support/allocators/pool.h>
1011

1112
#include <cassert>
1213
#include <cstdlib>
@@ -166,6 +167,25 @@ static inline size_t DynamicUsage(const std::unordered_map<X, Y, Z>& m)
166167
return MallocUsage(sizeof(unordered_node<std::pair<const X, Y> >)) * m.size() + MallocUsage(sizeof(void*) * m.bucket_count());
167168
}
168169

170+
template <class Key, class T, class Hash, class Pred, std::size_t MAX_BLOCK_SIZE_BYTES, std::size_t ALIGN_BYTES>
171+
static inline size_t DynamicUsage(const std::unordered_map<Key,
172+
T,
173+
Hash,
174+
Pred,
175+
PoolAllocator<std::pair<const Key, T>,
176+
MAX_BLOCK_SIZE_BYTES,
177+
ALIGN_BYTES>>& m)
178+
{
179+
auto* pool_resource = m.get_allocator().resource();
180+
181+
// The allocated chunks are stored in a std::list. Size per node should
182+
// therefore be 3 pointers: next, previous, and a pointer to the chunk.
183+
size_t estimated_list_node_size = MallocUsage(sizeof(void*) * 3);
184+
size_t usage_resource = estimated_list_node_size * pool_resource->NumAllocatedChunks();
185+
size_t usage_chunks = MallocUsage(pool_resource->ChunkSizeBytes()) * pool_resource->NumAllocatedChunks();
186+
return usage_resource + usage_chunks + MallocUsage(sizeof(void*) * m.bucket_count());
169187
}
170188

189+
} // namespace memusage
190+
171191
#endif // BITCOIN_MEMUSAGE_H

src/test/pool_tests.cpp

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// Distributed under the MIT software license, see the accompanying
33
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
44

5+
#include <memusage.h>
56
#include <support/allocators/pool.h>
67
#include <test/util/poolresourcetester.h>
78
#include <test/util/random.h>
@@ -153,4 +154,37 @@ BOOST_AUTO_TEST_CASE(random_allocations)
153154
PoolResourceTester::CheckAllDataAccountedFor(resource);
154155
}
155156

157+
BOOST_AUTO_TEST_CASE(memusage_test)
158+
{
159+
auto std_map = std::unordered_map<int, int>{};
160+
161+
using Map = std::unordered_map<int,
162+
int,
163+
std::hash<int>,
164+
std::equal_to<int>,
165+
PoolAllocator<std::pair<const int, int>,
166+
sizeof(std::pair<const int, int>) + sizeof(void*) * 4,
167+
alignof(void*)>>;
168+
auto resource = Map::allocator_type::ResourceType(1024);
169+
170+
PoolResourceTester::CheckAllDataAccountedFor(resource);
171+
172+
{
173+
auto resource_map = Map{0, std::hash<int>{}, std::equal_to<int>{}, &resource};
174+
175+
// can't have the same resource usage
176+
BOOST_TEST(memusage::DynamicUsage(std_map) != memusage::DynamicUsage(resource_map));
177+
178+
for (size_t i = 0; i < 10000; ++i) {
179+
std_map[i];
180+
resource_map[i];
181+
}
182+
183+
// Eventually the resource_map should have a much lower memory usage because it has less malloc overhead
184+
BOOST_TEST(memusage::DynamicUsage(resource_map) <= memusage::DynamicUsage(std_map) * 90 / 100);
185+
}
186+
187+
PoolResourceTester::CheckAllDataAccountedFor(resource);
188+
}
189+
156190
BOOST_AUTO_TEST_SUITE_END()

0 commit comments

Comments
 (0)