4
4
// Distributed under the Boost Software License, Version 1.0. (See accompanying
5
5
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
6
6
//
7
-
8
7
#if USE_MEMPROF
9
8
#include " memprof/memprof_stat.h"
10
9
#endif
11
10
12
- #include " td/utils/check .h"
11
+ #include " td/utils/common .h"
13
12
#include " td/utils/FlatHashMap.h"
14
- #include " td/utils/format .h"
13
+ #include " td/utils/logging .h"
15
14
#include " td/utils/misc.h"
16
15
#include " td/utils/port/Stat.h"
17
16
#include " td/utils/Slice.h"
17
+ #include " td/utils/StringBuilder.h"
18
18
19
19
#include < absl/container/flat_hash_map.h>
20
+ #include < array>
20
21
#include < folly/container/F14Map.h>
21
22
#include < map>
22
23
#include < unordered_map>
23
24
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 () {
28
29
#if USE_MEMPROF
29
- && is_memprof_on ()
30
+ return mem_stat_i < 0 && is_memprof_on ();
31
+ #else
32
+ return mem_stat_i < 0 ;
30
33
#endif
31
- ;
32
34
}
33
- auto get_memory () {
35
+
36
+ static auto get_memory () {
34
37
#if USE_MEMPROF
35
38
if (use_memprof ()) {
36
39
return get_used_memory_size ();
37
40
}
38
- #else
41
+ #endif
39
42
CHECK (!use_memprof ());
40
43
return td::mem_stat ().ok ().resident_size_ ;
41
- #endif
42
- };
44
+ }
43
45
44
46
template <class T >
45
47
class Generator {
@@ -67,68 +69,64 @@ class IntGenerator {
67
69
};
68
70
69
71
template <>
70
- class Generator <uint32_t > : public IntGenerator<uint32_t > {
71
- public:
72
- };
72
+ class Generator <td::uint32> final : public IntGenerator<td::uint32> {};
73
73
template <>
74
- class Generator <uint64_t > : public IntGenerator<uint64_t > {
75
- public:
76
- };
74
+ class Generator <td::uint64> final : public IntGenerator<td::uint64> {};
77
75
78
76
template <class T >
79
77
class Generator <td::unique_ptr<T>> {
80
78
public:
81
79
td::unique_ptr<T> next () {
82
80
return td::make_unique<T>();
83
81
}
84
- static size_t dyn_size () {
82
+ static std:: size_t dyn_size () {
85
83
return sizeof (T);
86
84
}
87
85
};
88
86
89
87
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) {
91
89
mem_stat_cur++;
92
90
if (mem_stat_i >= 0 && mem_stat_cur != mem_stat_i) {
93
91
return ;
94
92
}
95
93
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 ();
97
95
98
96
sb << " \t empty:" << sizeof (T);
99
97
struct Stat {
100
98
int pi ;
101
99
double min_ratio;
102
100
double max_ratio;
103
101
};
104
- std ::vector<Stat> stat;
102
+ td ::vector<Stat> stat;
105
103
stat.reserve (1024 );
106
- for (size_t size : {10000000u }) {
104
+ for (std:: size_t size : {1000000u }) {
107
105
Generator<KeyT> key_generator;
108
106
Generator<ValueT> value_generator;
109
107
auto start_mem = get_memory ();
110
108
T ht;
111
- auto ratio = [&]() {
109
+ auto ratio = [&] {
112
110
auto end_mem = get_memory ();
113
111
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 () ));
115
113
};
116
114
double min_ratio;
117
115
double max_ratio;
118
- auto reset = [&]() {
116
+ auto reset = [&] {
119
117
min_ratio = 1e100 ;
120
118
max_ratio = 0 ;
121
119
};
122
- auto update = [&]() {
120
+ auto update = [&] {
123
121
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);
126
124
};
127
125
reset ();
128
126
129
127
int p = 10 ;
130
128
int pi = 1 ;
131
- for (size_t i = 0 ; i < size; i++) {
129
+ for (std:: size_t i = 0 ; i < size; i++) {
132
130
ht.emplace (key_generator.next (), value_generator.next ());
133
131
update ();
134
132
if ((i + 1 ) % p == 0 ) {
@@ -140,23 +138,23 @@ void measure(td::StringBuilder &sb, td::Slice name, td::Slice key_name, td::Slic
140
138
}
141
139
}
142
140
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 ;
144
142
}
145
- sb << " \n " ;
143
+ sb << ' \n ' ;
146
144
}
147
145
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>;
150
148
151
149
template <template <typename ... Args> class T >
152
150
void print_memory_stats (td::Slice name) {
153
- std ::string big_buff (1 << 16 , ' \0 ' );
151
+ td ::string big_buff (1 << 16 , ' \0 ' );
154
152
td::StringBuilder sb (big_buff, false );
155
153
#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 >>);
158
156
if (!sb.as_cslice ().empty ()) {
159
- LOG (PLAIN) << " \n " << sb.as_cslice () << " \n " ;
157
+ LOG (PLAIN) << ' \n ' << sb.as_cslice () << ' \n ' ;
160
158
}
161
159
}
162
160
@@ -166,19 +164,18 @@ void print_memory_stats(td::Slice name) {
166
164
F(absl::flat_hash_map) \
167
165
F(std::unordered_map) \
168
166
F(std::map)
169
- #define BENCH_MEMORY (T ) print_memory_stats<T>(#T);
167
+ #define BENCHMARK_MEMORY (T ) print_memory_stats<T>(#T);
170
168
171
169
int main (int argc, const char *argv[]) {
172
170
// Usage:
173
171
// % benchmark/memory-hashset-os 0
174
- // max_i = 10
172
+ // Number of benchmarks = 10
175
173
// % for i in {1..10}; do ./benchmark/memory-hashset-os $i; done
176
174
if (argc > 1 ) {
177
175
mem_stat_i = td::to_integer<td::int32>(td::Slice (argv[1 ]));
178
176
}
179
- FOR_EACH_TABLE (BENCH_MEMORY );
177
+ FOR_EACH_TABLE (BENCHMARK_MEMORY );
180
178
if (mem_stat_i <= 0 ) {
181
- LOG (PLAIN) << " max_i = " << mem_stat_cur << " \n " ;
179
+ LOG (PLAIN) << " Number of benchmarks = " << mem_stat_cur << " \n " ;
182
180
}
183
- return 0 ;
184
- }
181
+ }
0 commit comments