aboutsummaryrefslogtreecommitdiffstats
path: root/yql/essentials/minikql/perf/presort/presort.cpp
blob: a7547e34e52ac057805e0d084f19b33ec2716d19 (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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
#include <yql/essentials/minikql/invoke_builtins/mkql_builtins.h>
#include <yql/essentials/minikql/defs.h>
#include <yql/essentials/minikql/computation/presort.h>
#include <yql/essentials/minikql/mkql_alloc.h>
#include <yql/essentials/minikql/mkql_string_util.h>
#include <yql/essentials/public/udf/udf_types.h>

#include <library/cpp/presort/presort.h>

#include <util/random/random.h>
#include <util/datetime/cputimer.h>
#include <util/string/builder.h>

using namespace NKikimr;
using namespace NKikimr::NMiniKQL;

namespace {

struct TSettings {
    ui32 Index;
    bool IsOptional;
    NKikimr::NUdf::EDataSlot Slot;
};

template <bool Desc>
struct TPresortOps : public NPresort::TResultOps {
    const TVector<TSettings>& Settings;
    NUdf::TUnboxedValue* Items;

    size_t Current = 0;

    TPresortOps(
        const TVector<TSettings>& settings,
        NUdf::TUnboxedValue* items)
        : Settings(settings)
        , Items(items)
    {}

    void Encode(IOutputStream& out) {
        for (const auto& setting : Settings) {
            auto& value = Items[setting.Index];

            switch (setting.Slot) {
            case NUdf::EDataSlot::Bool:
                NPresort::EncodeUnsignedInt(out, value.template Get<bool>(), Desc);
                break;
            case NUdf::EDataSlot::Uint8:
                NPresort::EncodeUnsignedInt(out, value.template Get<ui8>(), Desc);
                break;
            case NUdf::EDataSlot::Uint16:
            case NUdf::EDataSlot::Date:
                NPresort::EncodeUnsignedInt(out, value.template Get<ui16>(), Desc);
                break;
            case NUdf::EDataSlot::Uint32:
            case NUdf::EDataSlot::Datetime:
                NPresort::EncodeUnsignedInt(out, value.template Get<ui32>(), Desc);
                break;
            case NUdf::EDataSlot::Uint64:
            case NUdf::EDataSlot::Timestamp:
                NPresort::EncodeUnsignedInt(out, value.template Get<ui64>(), Desc);
                break;
            case NUdf::EDataSlot::Int8:
                NPresort::EncodeSignedInt(out, value.template Get<i8>(), Desc);
                break;
            case NUdf::EDataSlot::Int16:
                NPresort::EncodeSignedInt(out, value.template Get<i16>(), Desc);
                break;
            case NUdf::EDataSlot::Int32:
                NPresort::EncodeSignedInt(out, value.template Get<i32>(), Desc);
                break;
            case NUdf::EDataSlot::Int64:
            case NUdf::EDataSlot::Interval:
                NPresort::EncodeSignedInt(out, value.template Get<i64>(), Desc);
                break;
            case NUdf::EDataSlot::Float:
                NPresort::EncodeFloating(out, value.template Get<float>(), Desc);
                break;
            case NUdf::EDataSlot::Double:
                NPresort::EncodeFloating(out, value.template Get<double>(), Desc);
                break;
            case NUdf::EDataSlot::String:
            case NUdf::EDataSlot::Utf8: {
                auto strRef = value.AsStringRef();
                NPresort::EncodeString(out, TStringBuf(strRef.Data(), strRef.Size()), Desc);
                break;
            }
            default:
                MKQL_ENSURE(false, TStringBuilder() << "Unknown slot: " << setting.Slot);
            }
        }
    }

    void SetError(const TString& err) {
        MKQL_ENSURE(false, TStringBuilder() << "Presort decoding error: " << err);
    }

    void SetUnsignedInt(ui64 value) {
        const auto& setting = Settings[Current++];
        switch (setting.Slot) {
        case NUdf::EDataSlot::Bool:
            Items[setting.Index] = NUdf::TUnboxedValuePod(value != 0);
            break;
        case NUdf::EDataSlot::Uint8:
            Items[setting.Index] = NUdf::TUnboxedValuePod(static_cast<ui8>(value));
            break;
        case NUdf::EDataSlot::Uint16:
        case NUdf::EDataSlot::Date:
            Items[setting.Index] = NUdf::TUnboxedValuePod(static_cast<ui16>(value));
            break;
        case NUdf::EDataSlot::Uint32:
        case NUdf::EDataSlot::Datetime:
            Items[setting.Index] = NUdf::TUnboxedValuePod(static_cast<ui32>(value));
            break;
        case NUdf::EDataSlot::Uint64:
        case NUdf::EDataSlot::Timestamp:
            Items[setting.Index] = NUdf::TUnboxedValuePod(value);
            break;
        default:
            MKQL_ENSURE(false, TStringBuilder() << "Unknown slot: " << setting.Slot);
        }
    }

    void SetSignedInt(i64 value) {
        const auto& setting = Settings[Current++];
        switch (setting.Slot) {
        case NUdf::EDataSlot::Int8:
            Items[setting.Index] = NUdf::TUnboxedValuePod(static_cast<i8>(value));
            break;
        case NUdf::EDataSlot::Int16:
            Items[setting.Index] = NUdf::TUnboxedValuePod(static_cast<i16>(value));
            break;
        case NUdf::EDataSlot::Int32:
            Items[setting.Index] = NUdf::TUnboxedValuePod(static_cast<i32>(value));
            break;
        case NUdf::EDataSlot::Int64:
        case NUdf::EDataSlot::Interval:
            Items[setting.Index] = NUdf::TUnboxedValuePod(value);
            break;
        default:
            MKQL_ENSURE(false, "Unknown slot: " << setting.Slot);
        }
    }

    void SetFloat(float value) {
        Items[Settings[Current++].Index] = NUdf::TUnboxedValuePod(value);
    }

    void SetDouble(double value) {
        Items[Settings[Current++].Index] = NUdf::TUnboxedValuePod(value);
    }

    void SetString(const TString& value) {
        Items[Settings[Current++].Index] = MakeString(NUdf::TStringRef(value.data(), value.size()));
    }

    void SetOptional(bool) {}
};

template <typename T>
NUdf::TUnboxedValue RandomValue() {
    return NUdf::TUnboxedValuePod(RandomNumber<T>());
}

template <>
NUdf::TUnboxedValue RandomValue<char*>() {
    auto length = RandomNumber<ui64>(64);
    return MakeStringNotFilled(length);
}

template <typename T, NUdf::EDataSlot Slot, bool Desc>
std::pair<ui64, ui64> MeasureOld() {
    constexpr size_t count = 1000;
    constexpr size_t rowCount = 100000;

    TScopedAlloc alloc(__LOCATION__);
    TMemoryUsageInfo memInfo("Memory");

    TVector<NUdf::TUnboxedValue> values;
    TVector<TSettings> settings;

    for (ui32 i = 0; i < count; ++i) {
        values.push_back(RandomValue<T>());
        settings.push_back({i, false, NUdf::TDataType<T>::Slot});
    }

    TSimpleTimer timer;

    TStringStream stream;
    for (size_t n = 0; n < rowCount; ++n) {
        stream.clear();
        TPresortOps<Desc> ops{settings, values.begin()};
        ops.Encode(stream);
    }
    auto encodeTime = timer.Get().MicroSeconds();

    auto rowSize = stream.Str().size();
    Cerr << "row size " << rowSize << ", row count " << rowCount << Endl;
    Cerr << "encoding " << rowSize * rowCount * 1000000 / encodeTime << " bytes per sec ("
        << encodeTime << " us)" << Endl;

    timer.Reset();
    for (size_t n = 0; n < rowCount; ++n) {
        TPresortOps<Desc> ops{settings, values.begin()};
        auto str = stream.Str();
        NPresort::Decode(ops, TStringBuf(str.data(), str.size()));
    }
    auto decodeTime = timer.Get().MicroSeconds();

    Cerr << "decoding " << rowSize * rowCount * 1000000 / decodeTime << " bytes per sec ("
        << decodeTime << " us)" << Endl;
    Cerr << Endl;

    return std::make_pair(encodeTime, decodeTime);
}

template <typename T, NUdf::EDataSlot Slot, bool Desc>
std::pair<ui64, ui64> MeasureNew() {
    constexpr size_t count = 1000;
    constexpr size_t rowCount = 100000;

    TScopedAlloc alloc(__LOCATION__);
    TMemoryUsageInfo memInfo("Memory");

    TVector<NUdf::TUnboxedValuePod> values;
    TPresortEncoder encoder;
    TPresortDecoder decoder;

    for (size_t i = 0; i < count; ++i) {
        values.push_back(RandomValue<T>());
        encoder.AddType(Slot, false, Desc);
        decoder.AddType(Slot, false, Desc);
    }

    TSimpleTimer timer;
    TStringBuf buffer;

    for (size_t n = 0; n < rowCount; ++n) {
        encoder.Start();
        for (size_t i = 0; i < count; ++i) {
            encoder.Encode(values[i]);
        }
        buffer = encoder.Finish();
    }
    auto encodeTime = timer.Get().MicroSeconds();

    auto rowSize = buffer.size();

    Cerr << "row size " << rowSize << ", row count " << rowCount << Endl;
    Cerr << "encoding " << rowSize * rowCount * 1000000 / encodeTime << " bytes per sec ("
        << encodeTime << " us)" << Endl;

    timer.Reset();
    for (size_t n = 0; n < rowCount; ++n) {
        decoder.Start(buffer);
        for (size_t i = 0; i < count; ++i) {
            decoder.Decode();
        }
        encoder.Finish();
    }
    auto decodeTime = timer.Get().MicroSeconds();

    Cerr << "decoding " << rowSize * rowCount * 1000000 / decodeTime << " bytes per sec ("
        << decodeTime << " us)" << Endl;
    Cerr << Endl;

    return std::make_pair(encodeTime, decodeTime);
}

template <typename T, NUdf::EDataSlot Slot, bool Desc>
void Compare() {
    auto newTimes = MeasureNew<T, Slot, Desc>();
    auto oldTimes = MeasureOld<T, Slot, Desc>();

    Cerr << "encoding speedup " << (double)oldTimes.first / (double)newTimes.first << Endl;
    Cerr << "decoding speedup " << (double)oldTimes.second / (double)newTimes.second << Endl;
    Cerr << "--------" << Endl << Endl;
}

template <typename T, NUdf::EDataSlot Slot>
void CompareType(const char* type) {
    Cerr << type << Endl;
    Compare<T, Slot, false>();

    Cerr << type << " desc" << Endl;
    Compare<T, Slot, true>();
}

}

int main(int, char**) {
    CompareType<bool, NUdf::EDataSlot::Bool>("bool");
    CompareType<ui8, NUdf::EDataSlot::Uint8>("ui8");
    CompareType<ui16, NUdf::EDataSlot::Uint16>("ui16");
    CompareType<ui32, NUdf::EDataSlot::Uint32>("ui32");
    CompareType<ui64, NUdf::EDataSlot::Uint64>("ui64");
    CompareType<float, NUdf::EDataSlot::Float>("float");
    CompareType<double, NUdf::EDataSlot::Double>("double");
    CompareType<char*, NUdf::EDataSlot::String>("string");

    return 0;
}