aboutsummaryrefslogtreecommitdiffstats
path: root/yql/essentials/minikql/benchmark/pack_num/pack_num_bench.cpp
blob: 7255b7bc4790ebb2d64a32038b817b23d7d5466f (plain) (blame)
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
#include "pack.h"

#include <yql/essentials/minikql/pack_num.h>

#include <library/cpp/testing/benchmark/bench.h>
#include <library/cpp/packedtypes/longs.h>

#include <util/generic/xrange.h>
#include <util/generic/singleton.h>
#include <util/random/random.h>

namespace {

template <ui32 UPPER, size_t MAX_BYTE_SIZE = (1 << 15) - (1 << 10)>
struct TSamples32 {
    constexpr static size_t COUNT = MAX_BYTE_SIZE / sizeof(ui32);
    TSamples32() {
        for (size_t i: xrange(COUNT)) {
            Data[i] = RandomNumber<ui32>(UPPER);
        }
    }

    ui32 Data[COUNT];
};

template <ui64 UPPER, size_t MAX_BYTE_SIZE = (1 << 15) - (1 << 10)>
struct TSamples64 {
    constexpr static size_t COUNT = MAX_BYTE_SIZE / sizeof(ui64);
    TSamples64() {
        for (size_t i: xrange(COUNT)) {
            Data[i] = RandomNumber<ui64>(UPPER);
        }
    }

    ui64 Data[COUNT];
};

template <ui32 UPPER, typename TCoder, size_t MAX_BYTE_SIZE = (1 << 15) - (1 << 10)>
struct TCodedData32 {
    constexpr static size_t BYTES_PER_NUM = sizeof(ui32) + 2;
    constexpr static size_t COUNT = MAX_BYTE_SIZE / BYTES_PER_NUM;
    TCodedData32() {
        for (size_t i: xrange(COUNT)) {
            Length[i] = TCoder()(RandomNumber<ui32>(UPPER), Data + i * BYTES_PER_NUM);
        }
    }

    char Data[MAX_BYTE_SIZE + BYTES_PER_NUM];
    size_t Length[COUNT];
};

template <ui64 UPPER, typename TCoder, size_t MAX_BYTE_SIZE = (1 << 15) - (1 << 10)>
struct TCodedData64 {
    constexpr static size_t BYTES_PER_NUM = sizeof(ui64) + 2;
    constexpr static size_t COUNT = MAX_BYTE_SIZE / BYTES_PER_NUM;
    TCodedData64() {
        for (size_t i: xrange(COUNT)) {
            Length[i] = TCoder()(RandomNumber<ui64>(UPPER), Data + i * BYTES_PER_NUM);
        }
    }

    char Data[MAX_BYTE_SIZE + BYTES_PER_NUM];
    size_t Length[COUNT];
};

struct TKikimrCoder32 {
    size_t operator() (ui32 num, char* buf) const {
        return NKikimr::Pack32(num, buf);
    }
};

struct TKikimrCoder64 {
    size_t operator() (ui64 num, char* buf) const {
        return NKikimr::Pack64(num, buf);
    }
};

struct TPackedTypesCoder32 {
    size_t operator() (ui32 num, char* buf) const {
        return Pack32(num, buf) - buf;
    }
};

struct TPackedTypesCoder64 {
    size_t operator() (ui64 num, char* buf) const {
        return Pack64(num, buf) - buf;
    }
};

struct TDictUtilsCoder32 {
    size_t operator() (ui32 num, char* buf) const {
        return PackU32(num, buf);
    }
};

struct TDictUtilsCoder64 {
    size_t operator() (ui64 num, char* buf) const {
        return PackU64(num, buf);
    }
};

} // unnamed

#define DEF_WRITE_BENCH(base, limit) \
    Y_CPU_BENCHMARK(Write##base##_Kikimr_##limit, iface) { \
        char buffer[sizeof(ui##base) + 1]; \
        auto& data = Default<TSamples##base<limit>>(); \
        for (size_t i = 0; i < iface.Iterations(); ++i) { \
            Y_DO_NOT_OPTIMIZE_AWAY(NKikimr::Pack##base(data.Data[i % data.COUNT], buffer)); \
            NBench::Clobber(); \
        } \
    } \
    Y_CPU_BENCHMARK(Write##base##_PackedTypes_##limit, iface) { \
        char buffer[sizeof(ui##base) + 1]; \
        auto& data = Default<TSamples##base<limit>>(); \
        for (size_t i = 0; i < iface.Iterations(); ++i) { \
            Y_DO_NOT_OPTIMIZE_AWAY(Pack##base(data.Data[i % data.COUNT], buffer)); \
            NBench::Clobber(); \
        } \
    } \
    Y_CPU_BENCHMARK(Write##base##_DictUtils_##limit, iface) { \
        char buffer[sizeof(ui##base) + 1]; \
        auto& data = Default<TSamples##base<limit>>(); \
        for (size_t i = 0; i < iface.Iterations(); ++i) { \
            Y_DO_NOT_OPTIMIZE_AWAY(PackU##base(data.Data[i % data.COUNT], buffer)); \
            NBench::Clobber(); \
        } \
    }


#define DEF_READ_BENCH(base, limit) \
    Y_CPU_BENCHMARK(Read##base##_KikimrLong_##limit, iface) { \
        ui##base num = 0; \
        NBench::Escape(&num); \
        const auto& data = *HugeSingleton<TCodedData##base<limit, TKikimrCoder##base>>(); \
        for (size_t i = 0; i < iface.Iterations(); ++i) { \
            const size_t pos = i % data.COUNT; \
            Y_DO_NOT_OPTIMIZE_AWAY(NKikimr::Unpack##base(data.Data + pos * data.BYTES_PER_NUM, data.BYTES_PER_NUM, num)); \
            NBench::Clobber(); \
        } \
    } \
    Y_CPU_BENCHMARK(Read##base##_KikimrShort_##limit, iface) { \
        ui##base num = 0; \
        NBench::Escape(&num); \
        const auto& data = *HugeSingleton<TCodedData##base<limit, TKikimrCoder##base>>(); \
        for (size_t i = 0; i < iface.Iterations(); ++i) { \
            const size_t pos = i % data.COUNT; \
            Y_DO_NOT_OPTIMIZE_AWAY(NKikimr::Unpack##base(data.Data + pos * data.BYTES_PER_NUM, data.Length[pos], num)); \
            NBench::Clobber(); \
        } \
    } \
    Y_CPU_BENCHMARK(Read##base##_PackedTypes_##limit, iface) { \
        ui##base num = 0; \
        NBench::Escape(&num); \
        const auto& data = *HugeSingleton<TCodedData##base<limit, TPackedTypesCoder##base>>(); \
        for (size_t i = 0; i < iface.Iterations(); ++i) { \
            const size_t pos = i % data.COUNT; \
            Y_DO_NOT_OPTIMIZE_AWAY(Unpack##base(num, data.Data + pos * data.BYTES_PER_NUM)); \
            NBench::Clobber(); \
        } \
    } \
    Y_CPU_BENCHMARK(Read##base##_DictUtils_##limit, iface) { \
        ui##base num = 0; \
        NBench::Escape(&num); \
        const auto& data = *HugeSingleton<TCodedData##base<limit, TDictUtilsCoder##base>>(); \
        for (size_t i = 0; i < iface.Iterations(); ++i) { \
            const size_t pos = i % data.COUNT; \
            Y_DO_NOT_OPTIMIZE_AWAY(UnpackU##base(&num, data.Data + pos * data.BYTES_PER_NUM)); \
            NBench::Clobber(); \
        } \
    }


DEF_WRITE_BENCH(32, 10)
DEF_WRITE_BENCH(32, 126)
DEF_WRITE_BENCH(32, 127)
DEF_WRITE_BENCH(32, 128)
DEF_WRITE_BENCH(32, 254)
DEF_WRITE_BENCH(32, 255)
DEF_WRITE_BENCH(32, 256)
DEF_WRITE_BENCH(32, 65534)
DEF_WRITE_BENCH(32, 65535)
DEF_WRITE_BENCH(32, 65536)
DEF_WRITE_BENCH(32, 4294967295)

DEF_WRITE_BENCH(64, 10)
DEF_WRITE_BENCH(64, 126)
DEF_WRITE_BENCH(64, 127)
DEF_WRITE_BENCH(64, 128)
DEF_WRITE_BENCH(64, 254)
DEF_WRITE_BENCH(64, 255)
DEF_WRITE_BENCH(64, 256)
DEF_WRITE_BENCH(64, 65534)
DEF_WRITE_BENCH(64, 65535)
DEF_WRITE_BENCH(64, 65536)
DEF_WRITE_BENCH(64, 4294967294ull)
DEF_WRITE_BENCH(64, 4294967295ull)
DEF_WRITE_BENCH(64, 4294967296ull)
DEF_WRITE_BENCH(64, 18446744073709551615ull)

DEF_READ_BENCH(32, 10)
DEF_READ_BENCH(32, 126)
DEF_READ_BENCH(32, 127)
DEF_READ_BENCH(32, 128)
DEF_READ_BENCH(32, 254)
DEF_READ_BENCH(32, 255)
DEF_READ_BENCH(32, 256)
DEF_READ_BENCH(32, 65534)
DEF_READ_BENCH(32, 65535)
DEF_READ_BENCH(32, 65536)
DEF_READ_BENCH(32, 4294967295)

DEF_READ_BENCH(64, 10)
DEF_READ_BENCH(64, 126)
DEF_READ_BENCH(64, 127)
DEF_READ_BENCH(64, 128)
DEF_READ_BENCH(64, 254)
DEF_READ_BENCH(64, 255)
DEF_READ_BENCH(64, 256)
DEF_READ_BENCH(64, 65534)
DEF_READ_BENCH(64, 65535)
DEF_READ_BENCH(64, 65536)
DEF_READ_BENCH(64, 4294967294ull)
DEF_READ_BENCH(64, 4294967295ull)
DEF_READ_BENCH(64, 4294967296ull)
DEF_READ_BENCH(64, 18446744073709551615ull)