From e19943f049ed8aa4f32a1d8440a9fbf160367f0f Mon Sep 17 00:00:00 2001 From: Martin Leitner-Ankerl Date: Sat, 11 Jun 2022 09:28:13 +0200 Subject: 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. --- src/test/pool_tests.cpp | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) (limited to 'src/test/pool_tests.cpp') diff --git a/src/test/pool_tests.cpp b/src/test/pool_tests.cpp index dfe857d05b..8a07e09a44 100644 --- a/src/test/pool_tests.cpp +++ b/src/test/pool_tests.cpp @@ -2,6 +2,7 @@ // Distributed under the MIT software license, see the accompanying // file COPYING or http://www.opensource.org/licenses/mit-license.php. +#include #include #include #include @@ -153,4 +154,37 @@ BOOST_AUTO_TEST_CASE(random_allocations) PoolResourceTester::CheckAllDataAccountedFor(resource); } +BOOST_AUTO_TEST_CASE(memusage_test) +{ + auto std_map = std::unordered_map{}; + + using Map = std::unordered_map, + std::equal_to, + PoolAllocator, + sizeof(std::pair) + sizeof(void*) * 4, + alignof(void*)>>; + auto resource = Map::allocator_type::ResourceType(1024); + + PoolResourceTester::CheckAllDataAccountedFor(resource); + + { + auto resource_map = Map{0, std::hash{}, std::equal_to{}, &resource}; + + // can't have the same resource usage + BOOST_TEST(memusage::DynamicUsage(std_map) != memusage::DynamicUsage(resource_map)); + + for (size_t i = 0; i < 10000; ++i) { + std_map[i]; + resource_map[i]; + } + + // Eventually the resource_map should have a much lower memory usage because it has less malloc overhead + BOOST_TEST(memusage::DynamicUsage(resource_map) <= memusage::DynamicUsage(std_map) * 90 / 100); + } + + PoolResourceTester::CheckAllDataAccountedFor(resource); +} + BOOST_AUTO_TEST_SUITE_END() -- cgit v1.2.3