From b8ab910b81475b27545b8469234a7cfea517ed27 Mon Sep 17 00:00:00 2001 From: levlam Date: Thu, 10 Feb 2022 23:01:28 +0300 Subject: [PATCH] Various improvements. --- benchmark/CMakeLists.txt | 3 +- benchmark/bench_empty.cpp | 1 - benchmark/bench_http_server_fast.cpp | 1 - benchmark/hashset_memory.cpp | 87 ++++++++++++++-------------- memprof/memprof.cpp | 3 + memprof/memprof_stat.cpp | 15 +++-- memprof/memprof_stat.h | 7 +++ td/telegram/td_emscripten.cpp | 1 - tdactor/example/example.cpp | 1 - tdutils/td/utils/FlatHashMap.h | 8 +-- test/main.cpp | 1 - 11 files changed, 65 insertions(+), 63 deletions(-) diff --git a/benchmark/CMakeLists.txt b/benchmark/CMakeLists.txt index e8608df9b..03198b662 100644 --- a/benchmark/CMakeLists.txt +++ b/benchmark/CMakeLists.txt @@ -74,11 +74,10 @@ if (NOT WIN32 AND NOT CYGWIN) endif() find_package(ABSL QUIET) -find_package(benchmark QUIET) find_package(gflags QUIET) find_package(folly QUIET) -if (ABSL_FOUND AND benchmark_FOUND AND gflags_FOUND AND folly_FOUND) +if (ABSL_FOUND AND gflags_FOUND AND folly_FOUND) add_executable(memory-hashset-memprof EXCLUDE_FROM_ALL ${CMAKE_CURRENT_SOURCE_DIR}/hashset_memory.cpp) target_compile_definitions(memory-hashset-memprof PRIVATE USE_MEMPROF=1) target_link_libraries(memory-hashset-memprof PRIVATE tdutils memprof_stat) diff --git a/benchmark/bench_empty.cpp b/benchmark/bench_empty.cpp index f27f64e29..ae2068c05 100644 --- a/benchmark/bench_empty.cpp +++ b/benchmark/bench_empty.cpp @@ -5,5 +5,4 @@ // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) // int main() { - return 0; } diff --git a/benchmark/bench_http_server_fast.cpp b/benchmark/bench_http_server_fast.cpp index 93e3061b4..d46273cab 100644 --- a/benchmark/bench_http_server_fast.cpp +++ b/benchmark/bench_http_server_fast.cpp @@ -114,5 +114,4 @@ int main() { // empty } scheduler->finish(); - return 0; } diff --git a/benchmark/hashset_memory.cpp b/benchmark/hashset_memory.cpp index a9ee5f9be..67e871d67 100644 --- a/benchmark/hashset_memory.cpp +++ b/benchmark/hashset_memory.cpp @@ -4,42 +4,44 @@ // Distributed under the Boost Software License, Version 1.0. (See accompanying // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) // - #if USE_MEMPROF #include "memprof/memprof_stat.h" #endif -#include "td/utils/check.h" +#include "td/utils/common.h" #include "td/utils/FlatHashMap.h" -#include "td/utils/format.h" +#include "td/utils/logging.h" #include "td/utils/misc.h" #include "td/utils/port/Stat.h" #include "td/utils/Slice.h" +#include "td/utils/StringBuilder.h" #include +#include #include #include #include -int mem_stat_i = -1; -int mem_stat_cur = 0; -bool use_memprof() { - return mem_stat_i < 0 +static int mem_stat_i = -1; +static int mem_stat_cur = 0; + +static bool use_memprof() { #if USE_MEMPROF - && is_memprof_on() + return mem_stat_i < 0 && is_memprof_on(); +#else + return mem_stat_i < 0; #endif - ; } -auto get_memory() { + +static auto get_memory() { #if USE_MEMPROF if (use_memprof()) { return get_used_memory_size(); } -#else +#endif CHECK(!use_memprof()); return td::mem_stat().ok().resident_size_; -#endif -}; +} template class Generator { @@ -67,13 +69,9 @@ class IntGenerator { }; template <> -class Generator : public IntGenerator { - public: -}; +class Generator final : public IntGenerator {}; template <> -class Generator : public IntGenerator { - public: -}; +class Generator final : public IntGenerator {}; template class Generator> { @@ -81,19 +79,19 @@ class Generator> { td::unique_ptr next() { return td::make_unique(); } - static size_t dyn_size() { + static std::size_t dyn_size() { return sizeof(T); } }; template -void measure(td::StringBuilder &sb, td::Slice name, td::Slice key_name, td::Slice value_name) { +static void measure(td::StringBuilder &sb, td::Slice name, td::Slice key_name, td::Slice value_name) { mem_stat_cur++; if (mem_stat_i >= 0 && mem_stat_cur != mem_stat_i) { return; } sb << name << "<" << key_name << "," << value_name << "> " << (use_memprof() ? "memprof" : "os") << "\n"; - size_t ideal_size = sizeof(KeyT) + sizeof(ValueT) + Generator::dyn_size(); + std::size_t ideal_size = sizeof(KeyT) + sizeof(ValueT) + Generator::dyn_size(); sb << "\tempty:" << sizeof(T); struct Stat { @@ -101,34 +99,34 @@ void measure(td::StringBuilder &sb, td::Slice name, td::Slice key_name, td::Slic double min_ratio; double max_ratio; }; - std::vector stat; + td::vector stat; stat.reserve(1024); - for (size_t size : {10000000u}) { + for (std::size_t size : {1000000u}) { Generator key_generator; Generator value_generator; auto start_mem = get_memory(); T ht; - auto ratio = [&]() { + auto ratio = [&] { auto end_mem = get_memory(); auto used_mem = end_mem - start_mem; - return double(used_mem) / double(ideal_size * ht.size()); + return static_cast(used_mem) / (static_cast(ideal_size) * static_cast(ht.size())); }; double min_ratio; double max_ratio; - auto reset = [&]() { + auto reset = [&] { min_ratio = 1e100; max_ratio = 0; }; - auto update = [&]() { + auto update = [&] { auto x = ratio(); - min_ratio = std::min(min_ratio, x); - max_ratio = std::max(max_ratio, x); + min_ratio = td::min(min_ratio, x); + max_ratio = td::max(max_ratio, x); }; reset(); int p = 10; int pi = 1; - for (size_t i = 0; i < size; i++) { + for (std::size_t i = 0; i < size; i++) { ht.emplace(key_generator.next(), value_generator.next()); update(); if ((i + 1) % p == 0) { @@ -140,23 +138,23 @@ void measure(td::StringBuilder &sb, td::Slice name, td::Slice key_name, td::Slic } } for (auto &s : stat) { - sb << " " << 10 << "^" << s.pi << ":" << s.min_ratio << "->" << s.max_ratio; + sb << " 10^" << s.pi << ":" << s.min_ratio << "->" << s.max_ratio; } - sb << "\n"; + sb << '\n'; } -template -using Bytes = std::array; +template +using Bytes = std::array; template