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.
This commit is contained in:
Martin Leitner-Ankerl
2022-06-11 09:28:13 +02:00
parent b8401c3281
commit e19943f049
2 changed files with 54 additions and 0 deletions

View File

@@ -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 <memusage.h>
#include <support/allocators/pool.h>
#include <test/util/poolresourcetester.h>
#include <test/util/random.h>
@@ -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<int, int>{};
using Map = std::unordered_map<int,
int,
std::hash<int>,
std::equal_to<int>,
PoolAllocator<std::pair<const int, int>,
sizeof(std::pair<const int, int>) + sizeof(void*) * 4,
alignof(void*)>>;
auto resource = Map::allocator_type::ResourceType(1024);
PoolResourceTester::CheckAllDataAccountedFor(resource);
{
auto resource_map = Map{0, std::hash<int>{}, std::equal_to<int>{}, &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()