summaryrefslogtreecommitdiffhomepage
path: root/benchmarks/cmap_benchmark2.cpp
blob: 39dd61b89688a3bad078e6a7b8154fe1de887240 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
#include <stc/crand.h>
#include <stc/cstr.h>
#include <stc/cmap.h>

#include <string>
#include <unordered_map>
#include "others/bytell_hash_map.hpp"
#include "others/robin_hood.hpp"
#include "others/hopscotch_map.h"
#include "others/sparsepp/spp.h"

#define PICOBENCH_IMPLEMENT_WITH_MAIN
#include "picobench.hpp"

PICOBENCH_SUITE("Map");
enum {N1 = 5000000, S1 = 1, MaxLoadFactor100 = 80};
uint64_t seed = time(NULL);

static inline uint32_t hash32(const void* data, size_t len) {
    return (uint32_t) ((*(const uint32_t *) data) * 2654435769u);
}
static inline uint32_t hash64(const void* data, size_t len) {
    return (uint32_t) (((*(const uint64_t *) data) * 11400714819323198485ull) >> 24);
}
template <class K, class V> using umap = std::unordered_map<K, V>;
template <class K, class V> using bmap = ska::bytell_hash_map<K, V>;
template <class K, class V> using fmap = ska::flat_hash_map<K, V>;
template <class K, class V> using hmap = tsl::hopscotch_map<K, V>;
template <class K, class V> using smap = spp::sparse_hash_map<K, V>;
template <class K, class V> using rmap = robin_hood::unordered_flat_map<K, V, robin_hood::hash<K>,
                                                                        std::equal_to<K>, MaxLoadFactor100>;
#define DEFMAP(map, ...) \
    using u##map = umap __VA_ARGS__; \
    using b##map = bmap __VA_ARGS__; \
    using f##map = fmap __VA_ARGS__; \
    using h##map = hmap __VA_ARGS__; \
    using s##map = smap __VA_ARGS__; \
    using r##map = rmap __VA_ARGS__

DEFMAP(map_i, <int, int>);
DEFMAP(map_x, <uint64_t, uint64_t>);
DEFMAP(map_s, <std::string, std::string>);

using_cmap(i, int, int, c_default_del, c_default_clone, c_default_equals, hash32);
using_cmap(x, uint64_t, uint64_t, c_default_del, c_default_clone, c_default_equals, hash64);
using_cmap_strkey(s, cstr, cstr_del, cstr_clone);

template <class MapInt>
static void ctor_and_ins_one_i(picobench::state& s)
{
    size_t result = 0;

    picobench::scope scope(s);
    c_forrange (n, s.iterations()) {
        MapInt map;
        map[n];
        result += map.size();
    }
    s.set_result(result);
}

static void ctor_and_ins_one_cmap_i(picobench::state& s)
{
    size_t result = 0;

    picobench::scope scope(s);
    c_forrange (n, s.iterations()) {
        cmap_i map = cmap_inits;
        cmap_i_emplace(&map, n, 0);
        result += cmap_i_size(map);
        cmap_i_del(&map);
    }
    s.set_result(result);
}

#define P samples(S1).iterations({N1})
PICOBENCH(ctor_and_ins_one_i<umap_i>).P.baseline();
PICOBENCH(ctor_and_ins_one_i<bmap_i>).P;
PICOBENCH(ctor_and_ins_one_i<fmap_i>).P;
PICOBENCH(ctor_and_ins_one_i<hmap_i>).P;
PICOBENCH(ctor_and_ins_one_i<smap_i>).P;
PICOBENCH(ctor_and_ins_one_i<rmap_i>).P;
PICOBENCH(ctor_and_ins_one_cmap_i).P;
#undef P


template <class MapInt>
static void ins_and_erase_i(picobench::state& s)
{
    size_t result = 0;
    MapInt map;
    map.max_load_factor(MaxLoadFactor100 / 100.0);
    stc64_srandom(seed);

    picobench::scope scope(s);
    c_forrange (s.iterations())
        map[stc64_random()];
    map.clear();
    stc64_srandom(seed);
    c_forrange (s.iterations())
        map[stc64_random()];
    stc64_srandom(seed);
    c_forrange (s.iterations())
        map.erase(stc64_random());
    s.set_result(map.size());
}

static void ins_and_erase_cmap_i(picobench::state& s)
{
    cmap_i map = cmap_inits;
    cmap_i_set_load_factors(&map, 0.0, MaxLoadFactor100 / 100.0);
    stc64_srandom(seed);

    picobench::scope scope(s);
    c_forrange (s.iterations())
        cmap_i_emplace(&map, stc64_random(), 0);
    cmap_i_clear(&map);
    stc64_srandom(seed);
    c_forrange (s.iterations())
        cmap_i_emplace(&map, stc64_random(), 0);
    stc64_srandom(seed);
    c_forrange (s.iterations())
        cmap_i_erase(&map, stc64_random());
    cmap_i_del(&map);
    s.set_result(cmap_i_size(map));
}

#define P samples(S1).iterations({N1/4})
PICOBENCH(ins_and_erase_i<umap_i>).P.baseline();
PICOBENCH(ins_and_erase_i<bmap_i>).P;
PICOBENCH(ins_and_erase_i<fmap_i>).P;
PICOBENCH(ins_and_erase_i<hmap_i>).P;
PICOBENCH(ins_and_erase_i<smap_i>).P;
PICOBENCH(ins_and_erase_i<rmap_i>).P;
PICOBENCH(ins_and_erase_cmap_i).P;
#undef P


template <class MapInt>
static void ins_and_access_i(picobench::state& s)
{
    uint64_t mask = (1ull << s.user_data()) - 1;
    size_t result = 0;
    MapInt map;
    map.max_load_factor(MaxLoadFactor100 / 100.0);
    stc64_srandom(seed);

    picobench::scope scope(s);
    c_forrange (N1)
        result += ++map[stc64_random() & mask];
    s.set_result(result);
}

static void ins_and_access_cmap_i(picobench::state& s)
{
    uint64_t mask = (1ull << s.user_data()) - 1;
    size_t result = 0;
    cmap_i map = cmap_inits;
    cmap_i_set_load_factors(&map, 0.0, MaxLoadFactor100 / 100.0);
    stc64_srandom(seed);

    picobench::scope scope(s);
    c_forrange (N1)
        result += ++cmap_i_emplace(&map, stc64_random() & mask, 0).first->second;
    s.set_result(result);
    cmap_i_del(&map);
}

#define P samples(S1).iterations({N1, N1, N1, N1}).user_data({18, 23, 25, 31})
PICOBENCH(ins_and_access_i<umap_i>).P.baseline();
PICOBENCH(ins_and_access_i<bmap_i>).P;
PICOBENCH(ins_and_access_i<fmap_i>).P;
PICOBENCH(ins_and_access_i<hmap_i>).P;
PICOBENCH(ins_and_access_i<smap_i>).P;
PICOBENCH(ins_and_access_i<rmap_i>).P;
PICOBENCH(ins_and_access_cmap_i).P;
#undef P


static void randomize(char* str, size_t len) {
    union {uint64_t i; char c[8];} r = {.i = stc64_random()};
    for (int i = len - 7, j = 0; i < len; ++j, ++i)
        str[i] = (r.c[j] & 63) + 48;
}

template <class MapStr>
static void ins_and_access_s(picobench::state& s)
{
    std::string str(s.user_data(), 'x');
    size_t result = 0;
    MapStr map;
    map.max_load_factor(MaxLoadFactor100 / 100.0);
    stc64_srandom(seed);

    picobench::scope scope(s);
    c_forrange (s.iterations()) {
        randomize(&str[0], str.size());
        map[str] = str;
        randomize(&str[0], str.size());
        auto it = map.find(str);
        if (it != map.end()) {
            ++result;
            map.erase(it);
        }
    }
    s.set_result(result);
}

static void ins_and_access_cmap_s(picobench::state& s)
{
    cstr str = cstr_with_size(s.user_data(), 'x');
    size_t result = 0;
    cmap_s map = cmap_inits;
    cmap_s_set_load_factors(&map, 0.0, MaxLoadFactor100 / 100.0);
    stc64_srandom(seed);

    picobench::scope scope(s);
    c_forrange (s.iterations()) {
        randomize(str.str, cstr_size(str));
        cmap_s_put(&map, str.str, cstr_clone(str));
        randomize(str.str, cstr_size(str));
        cmap_s_value_t* val = cmap_s_find(&map, str.str);
        if (val) {
            ++result;
            cmap_s_erase_entry(&map, val);
        }
    }
    s.set_result(result);
    cstr_del(&str);
    cmap_s_del(&map);
}

#define P samples(S1).iterations({N1/5, N1/5, N1/5, N1/10, N1/40}).user_data({13, 7, 8, 100, 1000})
PICOBENCH(ins_and_access_s<umap_s>).P.baseline();
PICOBENCH(ins_and_access_s<bmap_s>).P;
PICOBENCH(ins_and_access_s<fmap_s>).P;
PICOBENCH(ins_and_access_s<hmap_s>).P;
PICOBENCH(ins_and_access_s<smap_s>).P;
PICOBENCH(ins_and_access_s<rmap_s>).P;
PICOBENCH(ins_and_access_cmap_s).P;
#undef P