Skip to content

Commit b8ab910

Browse files
committed
Various improvements.
1 parent 3d8e5e0 commit b8ab910

File tree

11 files changed

+65
-63
lines changed

11 files changed

+65
-63
lines changed

‎benchmark/CMakeLists.txt

+1-2
Original file line numberDiff line numberDiff line change
@@ -74,11 +74,10 @@ if (NOT WIN32 AND NOT CYGWIN)
7474
endif()
7575

7676
find_package(ABSL QUIET)
77-
find_package(benchmark QUIET)
7877
find_package(gflags QUIET)
7978
find_package(folly QUIET)
8079

81-
if (ABSL_FOUND AND benchmark_FOUND AND gflags_FOUND AND folly_FOUND)
80+
if (ABSL_FOUND AND gflags_FOUND AND folly_FOUND)
8281
add_executable(memory-hashset-memprof EXCLUDE_FROM_ALL ${CMAKE_CURRENT_SOURCE_DIR}/hashset_memory.cpp)
8382
target_compile_definitions(memory-hashset-memprof PRIVATE USE_MEMPROF=1)
8483
target_link_libraries(memory-hashset-memprof PRIVATE tdutils memprof_stat)

‎benchmark/bench_empty.cpp

-1
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,4 @@
55
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
66
//
77
int main() {
8-
return 0;
98
}

‎benchmark/bench_http_server_fast.cpp

-1
Original file line numberDiff line numberDiff line change
@@ -114,5 +114,4 @@ int main() {
114114
// empty
115115
}
116116
scheduler->finish();
117-
return 0;
118117
}

‎benchmark/hashset_memory.cpp

+42-45
Original file line numberDiff line numberDiff line change
@@ -4,42 +4,44 @@
44
// Distributed under the Boost Software License, Version 1.0. (See accompanying
55
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
66
//
7-
87
#if USE_MEMPROF
98
#include "memprof/memprof_stat.h"
109
#endif
1110

12-
#include "td/utils/check.h"
11+
#include "td/utils/common.h"
1312
#include "td/utils/FlatHashMap.h"
14-
#include "td/utils/format.h"
13+
#include "td/utils/logging.h"
1514
#include "td/utils/misc.h"
1615
#include "td/utils/port/Stat.h"
1716
#include "td/utils/Slice.h"
17+
#include "td/utils/StringBuilder.h"
1818

1919
#include <absl/container/flat_hash_map.h>
20+
#include <array>
2021
#include <folly/container/F14Map.h>
2122
#include <map>
2223
#include <unordered_map>
2324

24-
int mem_stat_i = -1;
25-
int mem_stat_cur = 0;
26-
bool use_memprof() {
27-
return mem_stat_i < 0
25+
static int mem_stat_i = -1;
26+
static int mem_stat_cur = 0;
27+
28+
static bool use_memprof() {
2829
#if USE_MEMPROF
29-
&& is_memprof_on()
30+
return mem_stat_i < 0 && is_memprof_on();
31+
#else
32+
return mem_stat_i < 0;
3033
#endif
31-
;
3234
}
33-
auto get_memory() {
35+
36+
static auto get_memory() {
3437
#if USE_MEMPROF
3538
if (use_memprof()) {
3639
return get_used_memory_size();
3740
}
38-
#else
41+
#endif
3942
CHECK(!use_memprof());
4043
return td::mem_stat().ok().resident_size_;
41-
#endif
42-
};
44+
}
4345

4446
template <class T>
4547
class Generator {
@@ -67,68 +69,64 @@ class IntGenerator {
6769
};
6870

6971
template <>
70-
class Generator<uint32_t> : public IntGenerator<uint32_t> {
71-
public:
72-
};
72+
class Generator<td::uint32> final : public IntGenerator<td::uint32> {};
7373
template <>
74-
class Generator<uint64_t> : public IntGenerator<uint64_t> {
75-
public:
76-
};
74+
class Generator<td::uint64> final : public IntGenerator<td::uint64> {};
7775

7876
template <class T>
7977
class Generator<td::unique_ptr<T>> {
8078
public:
8179
td::unique_ptr<T> next() {
8280
return td::make_unique<T>();
8381
}
84-
static size_t dyn_size() {
82+
static std::size_t dyn_size() {
8583
return sizeof(T);
8684
}
8785
};
8886

8987
template <class T, class KeyT, class ValueT>
90-
void measure(td::StringBuilder &sb, td::Slice name, td::Slice key_name, td::Slice value_name) {
88+
static void measure(td::StringBuilder &sb, td::Slice name, td::Slice key_name, td::Slice value_name) {
9189
mem_stat_cur++;
9290
if (mem_stat_i >= 0 && mem_stat_cur != mem_stat_i) {
9391
return;
9492
}
9593
sb << name << "<" << key_name << "," << value_name << "> " << (use_memprof() ? "memprof" : "os") << "\n";
96-
size_t ideal_size = sizeof(KeyT) + sizeof(ValueT) + Generator<ValueT>::dyn_size();
94+
std::size_t ideal_size = sizeof(KeyT) + sizeof(ValueT) + Generator<ValueT>::dyn_size();
9795

9896
sb << "\tempty:" << sizeof(T);
9997
struct Stat {
10098
int pi;
10199
double min_ratio;
102100
double max_ratio;
103101
};
104-
std::vector<Stat> stat;
102+
td::vector<Stat> stat;
105103
stat.reserve(1024);
106-
for (size_t size : {10000000u}) {
104+
for (std::size_t size : {1000000u}) {
107105
Generator<KeyT> key_generator;
108106
Generator<ValueT> value_generator;
109107
auto start_mem = get_memory();
110108
T ht;
111-
auto ratio = [&]() {
109+
auto ratio = [&] {
112110
auto end_mem = get_memory();
113111
auto used_mem = end_mem - start_mem;
114-
return double(used_mem) / double(ideal_size * ht.size());
112+
return static_cast<double>(used_mem) / (static_cast<double>(ideal_size) * static_cast<double>(ht.size()));
115113
};
116114
double min_ratio;
117115
double max_ratio;
118-
auto reset = [&]() {
116+
auto reset = [&] {
119117
min_ratio = 1e100;
120118
max_ratio = 0;
121119
};
122-
auto update = [&]() {
120+
auto update = [&] {
123121
auto x = ratio();
124-
min_ratio = std::min(min_ratio, x);
125-
max_ratio = std::max(max_ratio, x);
122+
min_ratio = td::min(min_ratio, x);
123+
max_ratio = td::max(max_ratio, x);
126124
};
127125
reset();
128126

129127
int p = 10;
130128
int pi = 1;
131-
for (size_t i = 0; i < size; i++) {
129+
for (std::size_t i = 0; i < size; i++) {
132130
ht.emplace(key_generator.next(), value_generator.next());
133131
update();
134132
if ((i + 1) % p == 0) {
@@ -140,23 +138,23 @@ void measure(td::StringBuilder &sb, td::Slice name, td::Slice key_name, td::Slic
140138
}
141139
}
142140
for (auto &s : stat) {
143-
sb << " " << 10 << "^" << s.pi << ":" << s.min_ratio << "->" << s.max_ratio;
141+
sb << " 10^" << s.pi << ":" << s.min_ratio << "->" << s.max_ratio;
144142
}
145-
sb << "\n";
143+
sb << '\n';
146144
}
147145

148-
template <size_t size>
149-
using Bytes = std::array<uint8_t, size>;
146+
template <std::size_t size>
147+
using Bytes = std::array<char, size>;
150148

151149
template <template <typename... Args> class T>
152150
void print_memory_stats(td::Slice name) {
153-
std::string big_buff(1 << 16, '\0');
151+
td::string big_buff(1 << 16, '\0');
154152
td::StringBuilder sb(big_buff, false);
155153
#define MEASURE(KeyT, ValueT) measure<T<KeyT, ValueT>, KeyT, ValueT>(sb, name, #KeyT, #ValueT);
156-
MEASURE(uint32_t, uint32_t);
157-
MEASURE(uint64_t, td::unique_ptr<Bytes<360>>);
154+
MEASURE(td::uint32, td::uint32);
155+
MEASURE(td::uint64, td::unique_ptr<Bytes<360>>);
158156
if (!sb.as_cslice().empty()) {
159-
LOG(PLAIN) << "\n" << sb.as_cslice() << "\n";
157+
LOG(PLAIN) << '\n' << sb.as_cslice() << '\n';
160158
}
161159
}
162160

@@ -166,19 +164,18 @@ void print_memory_stats(td::Slice name) {
166164
F(absl::flat_hash_map) \
167165
F(std::unordered_map) \
168166
F(std::map)
169-
#define BENCH_MEMORY(T) print_memory_stats<T>(#T);
167+
#define BENCHMARK_MEMORY(T) print_memory_stats<T>(#T);
170168

171169
int main(int argc, const char *argv[]) {
172170
// Usage:
173171
// % benchmark/memory-hashset-os 0
174-
// max_i = 10
172+
// Number of benchmarks = 10
175173
// % for i in {1..10}; do ./benchmark/memory-hashset-os $i; done
176174
if (argc > 1) {
177175
mem_stat_i = td::to_integer<td::int32>(td::Slice(argv[1]));
178176
}
179-
FOR_EACH_TABLE(BENCH_MEMORY);
177+
FOR_EACH_TABLE(BENCHMARK_MEMORY);
180178
if (mem_stat_i <= 0) {
181-
LOG(PLAIN) << "max_i = " << mem_stat_cur << "\n";
179+
LOG(PLAIN) << "Number of benchmarks = " << mem_stat_cur << "\n";
182180
}
183-
return 0;
184-
}
181+
}

‎memprof/memprof.cpp

+3
Original file line numberDiff line numberDiff line change
@@ -265,12 +265,14 @@ void free(void *data_void) {
265265
#endif
266266
return free_old(info);
267267
}
268+
268269
void *calloc(std::size_t size_a, std::size_t size_b) {
269270
auto size = size_a * size_b;
270271
void *res = malloc_with_frame(size, get_backtrace());
271272
std::memset(res, 0, size);
272273
return res;
273274
}
275+
274276
void *realloc(void *ptr, std::size_t size) {
275277
if (ptr == nullptr) {
276278
return malloc_with_frame(size, get_backtrace());
@@ -282,6 +284,7 @@ void *realloc(void *ptr, std::size_t size) {
282284
free(ptr);
283285
return new_ptr;
284286
}
287+
285288
void *memalign(std::size_t aligment, std::size_t size) {
286289
my_assert(false && "Memalign is unsupported");
287290
return nullptr;

‎memprof/memprof_stat.cpp

+9-6
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
// Distributed under the Boost Software License, Version 1.0. (See accompanying
55
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
66
//
7-
#include "memprof_stat.h"
7+
#include "memprof/memprof_stat.h"
88

99
#include "td/utils/port/platform.h"
1010

@@ -15,7 +15,6 @@
1515
#include <cstdint>
1616
#include <cstdlib>
1717
#include <cstring>
18-
#include <functional>
1918
#include <new>
2019
#include <utility>
2120
#include <vector>
@@ -37,7 +36,8 @@ struct malloc_info {
3736
std::int32_t size;
3837
};
3938

40-
std::atomic<uint64_t> total_memory_used;
39+
static std::atomic<std::size_t> total_memory_used;
40+
4141
void register_xalloc(malloc_info *info, std::int32_t diff) {
4242
my_assert(info->size >= 0);
4343
// TODO: this is very slow in case of several threads.
@@ -106,12 +106,14 @@ void free(void *data_void) {
106106
#endif
107107
return free_old(info);
108108
}
109+
109110
void *calloc(std::size_t size_a, std::size_t size_b) {
110111
auto size = size_a * size_b;
111112
void *res = do_malloc(size);
112113
std::memset(res, 0, size);
113114
return res;
114115
}
116+
115117
void *realloc(void *ptr, std::size_t size) {
116118
if (ptr == nullptr) {
117119
return do_malloc(size);
@@ -123,15 +125,16 @@ void *realloc(void *ptr, std::size_t size) {
123125
free(ptr);
124126
return new_ptr;
125127
}
128+
126129
void *memalign(std::size_t alignment, std::size_t size) {
127130
auto res = malloc(size);
128-
my_assert(reinterpret_cast<uint64_t>(res) % alignment == 0);
131+
my_assert(reinterpret_cast<std::uintptr_t>(res) % alignment == 0);
129132
return res;
130133
}
131134

132135
int posix_memalign(void **memptr, size_t alignment, size_t size) {
133136
auto res = malloc(size);
134-
my_assert(reinterpret_cast<uint64_t>(res) % alignment == 0);
137+
my_assert(reinterpret_cast<std::uintptr_t>(res) % alignment == 0);
135138
*memptr = res;
136139
return 0;
137140
}
@@ -158,6 +161,6 @@ bool is_memprof_on() {
158161
return false;
159162
}
160163
std::size_t get_used_memory_size() {
161-
return false;
164+
return 0;
162165
}
163166
#endif

‎memprof/memprof_stat.h

+7
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,13 @@
1+
//
2+
// Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2022
3+
//
4+
// Distributed under the Boost Software License, Version 1.0. (See accompanying
5+
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
6+
//
17
#pragma once
28

39
#include <cstddef>
410

511
bool is_memprof_on();
12+
613
std::size_t get_used_memory_size();

‎td/telegram/td_emscripten.cpp

-1
Original file line numberDiff line numberDiff line change
@@ -36,5 +36,4 @@ EMSCRIPTEN_KEEPALIVE double td_emscripten_get_timeout() {
3636

3737
int main() {
3838
emscripten_exit_with_live_runtime();
39-
return 0;
4039
}

‎tdactor/example/example.cpp

-1
Original file line numberDiff line numberDiff line change
@@ -47,5 +47,4 @@ int main() {
4747
scheduler.run_main(td::Timestamp::in(10));
4848
}
4949
scheduler.finish();
50-
return 0;
5150
}

‎tdutils/td/utils/FlatHashMap.h

+3-5
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88

99
#include "td/utils/bits.h"
1010
#include "td/utils/common.h"
11-
#include "td/utils/logging.h"
1211

1312
#include <cstddef>
1413
#include <functional>
@@ -419,12 +418,11 @@ class FlatHashMapImpl {
419418
}
420419
}
421420
static bool should_shrink(size_t used_count, size_t bucket_count) {
422-
return used_count * 5 < bucket_count;
421+
return used_count * 10 < bucket_count;
423422
}
424423

425424
static size_t normalize(size_t size) {
426-
size |= (size != 0) * 7;
427-
return static_cast<size_t>(1) << (64 - count_leading_zeroes64(size));
425+
return static_cast<size_t>(1) << (64 - count_leading_zeroes64(size | 7));
428426
}
429427

430428
void shrink() {
@@ -433,7 +431,7 @@ class FlatHashMapImpl {
433431
}
434432

435433
void grow() {
436-
size_t want_size = normalize((used_nodes_ + 1) * 5 / 3 + 1);
434+
size_t want_size = normalize(2 * nodes_.size() - 1);
437435
resize(want_size);
438436
}
439437

‎test/main.cpp

-1
Original file line numberDiff line numberDiff line change
@@ -61,5 +61,4 @@ int main(int argc, char **argv) {
6161
#else
6262
runner.run_all();
6363
#endif
64-
return 0;
6564
}

0 commit comments

Comments
 (0)