From 6bb365567b0033cbc50ff705c6d4a3840be07c82 Mon Sep 17 00:00:00 2001 From: Arseny Smirnov Date: Thu, 10 Feb 2022 16:30:23 +0100 Subject: [PATCH] FlatHashMap: improve memory benchmark --- tdutils/test/hashset_memory.cpp | 39 +++++++++++++++++++++++++-------- 1 file changed, 30 insertions(+), 9 deletions(-) diff --git a/tdutils/test/hashset_memory.cpp b/tdutils/test/hashset_memory.cpp index 817e11a04..55f82f626 100644 --- a/tdutils/test/hashset_memory.cpp +++ b/tdutils/test/hashset_memory.cpp @@ -23,6 +23,9 @@ class Generator { T next() { UNREACHABLE(); } + static size_t dyn_size() { + UNREACHABLE(); + } }; template @@ -31,6 +34,9 @@ class IntGenerator { T next() { return ++value; } + static size_t dyn_size() { + return 0; + } private: T value{}; @@ -45,10 +51,21 @@ class Generator : public IntGenerator { public: }; +template +class Generator> { + public: + td::unique_ptr next() { + return td::make_unique(); + } + static size_t dyn_size() { + return sizeof(T); + } +}; + template void measure(td::StringBuilder &sb, td::Slice name, td::Slice key_name, td::Slice value_name) { sb << name << "<" << key_name << "," << value_name << ">:\n"; - size_t ideal_size = sizeof(KeyT) + sizeof(ValueT); + size_t ideal_size = sizeof(KeyT) + sizeof(ValueT) + Generator::dyn_size(); sb << "\tempty:" << sizeof(T); struct Stat { @@ -60,11 +77,13 @@ void measure(td::StringBuilder &sb, td::Slice name, td::Slice key_name, td::Slic stat.reserve(1024); for (size_t size : {10000000u}) { Generator key_generator; + Generator value_generator; auto start_mem = get_used_memory_size(); T ht; auto ratio = [&]() { auto end_mem = get_used_memory_size(); auto used_mem = end_mem - start_mem; + //return double(used_mem); return double(used_mem) / double(ideal_size * ht.size()); }; double min_ratio; @@ -77,16 +96,13 @@ void measure(td::StringBuilder &sb, td::Slice name, td::Slice key_name, td::Slic auto x = ratio(); min_ratio = std::min(min_ratio, x); max_ratio = std::max(max_ratio, x); - if (x > 14) { - LOG(ERROR) << "WTF"; - } }; reset(); int p = 10; int pi = 1; for (size_t i = 0; i < size; i++) { - ht.emplace(key_generator.next(), ValueT{}); + ht.emplace(key_generator.next(), value_generator.next()); update(); if ((i + 1) % p == 0) { stat.emplace_back(Stat{pi, min_ratio, max_ratio}); @@ -102,13 +118,18 @@ void measure(td::StringBuilder &sb, td::Slice name, td::Slice key_name, td::Slic sb << "\n"; } -template