aboutsummaryrefslogtreecommitdiff
path: root/src/test/pool_tests.cpp
diff options
context:
space:
mode:
authorMartin Leitner-Ankerl <martin.ankerl@gmail.com>2022-06-11 09:28:13 +0200
committerMartin Leitner-Ankerl <martin.ankerl@gmail.com>2023-03-23 19:38:38 +0100
commite19943f049ed8aa4f32a1d8440a9fbf160367f0f (patch)
treead5c684a7b0188649c620754635db9a630a25c41 /src/test/pool_tests.cpp
parentb8401c3281978beed6198b2f9782b6a8dd35cbd7 (diff)
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.
Diffstat (limited to 'src/test/pool_tests.cpp')
-rw-r--r--src/test/pool_tests.cpp34
1 files changed, 34 insertions, 0 deletions
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 <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()