File: filesort_compare-t.cc

package info (click to toggle)
mysql-8.0 8.0.43-3
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 1,273,924 kB
  • sloc: cpp: 4,684,605; ansic: 412,450; pascal: 108,398; java: 83,641; perl: 30,221; cs: 27,067; sql: 26,594; sh: 24,181; python: 21,816; yacc: 17,169; php: 11,522; xml: 7,388; javascript: 7,076; makefile: 2,194; lex: 1,075; awk: 670; asm: 520; objc: 183; ruby: 97; lisp: 86
file content (421 lines) | stat: -rw-r--r-- 12,902 bytes parent folder | download
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
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
/* Copyright (c) 2012, 2025, Oracle and/or its affiliates.

   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License, version 2.0,
   as published by the Free Software Foundation.

   This program is designed to work with certain software (including
   but not limited to OpenSSL) that is licensed under separate terms,
   as designated in a particular file or component or in included license
   documentation.  The authors of MySQL hereby grant you an additional
   permission to link the program and your derivative works with the
   separately licensed software that they have either included with
   the program or referenced in the documentation.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License, version 2.0, for more details.

   You should have received a copy of the GNU General Public License
   along with this program; if not, write to the Free Software
   Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301  USA */

#include <gtest/gtest.h>
#include <algorithm>
#include <memory>
#include <vector>

#include "my_byteorder.h"
#include "my_inttypes.h"
#include "sql/filesort_utils.h"
#include "unittest/gunit/benchmark.h"
#include "unittest/gunit/gunit_test_main.h"

namespace filesort_compare_unittest {

/*
  Below are some performance microbenchmarks in order to compare our sorting
  options:
  std::sort -        requires no extra memory,
                     typically implemented with introsort/insertion sort
  std::stable_sort - requires extra memory: array of n pointers,
                     typically implemented with mergesort

  The record format for filesort is constructed in such a way that we can
  compare records byte-by-byte, without knowing the data types.
  Nullable fields (maybe_null()) are pre-pended with an extra byte.
  If we are sorting in descending mode, all the bytes are simply flipped.

  This means that any variant of memcmp() can be used for comparing record.
  Below we test different variants, including memcmp() itself.
*/

inline int bytes_to_int(const uchar *s) {
  int val = longget(s);
  return val ^ 0x80000000;
}

inline void int_to_bytes(uchar *s, int val) {
  val = val ^ 0x80000000;
  longstore(s, val);
}

TEST(BufferAlignmentTest, IntsToBytesToInt) {
  uchar buf[10];
  memset(buf, 0, sizeof(buf));
  for (int ix = 0; ix < 6; ++ix) {
    int test_data[] = {INT_MIN32, -42, -1, 0, 1, 42, INT_MAX32};
    for (size_t iy = 0; iy < array_elements(test_data); ++iy) {
      int val = test_data[iy];
      int_to_bytes(buf + ix, val);
      EXPECT_EQ(val, bytes_to_int(buf + ix));
    }
  }
}

class FileSortBMHelper {
 public:
  // Number of records.
  static const int num_records = 100 * 100;
  // Number of keys in each record.
  static const int keys_per_record = 4;
  // Size of each record.
  static const int record_size = keys_per_record * sizeof(int);

  // Buffer containing data to be sorted.
  // (actually: we only sort the sort_keys below, data is stable).
  std::vector<int> test_data;

  FileSortBMHelper() {
    test_data.reserve(num_records * keys_per_record);
    union {
      int val;
      uchar buf[sizeof(int)];
    } sort_str;

    for (int ix = 0; ix < num_records * keys_per_record; ++ix) {
      int val = ix / (10 * keys_per_record);
      if (ix % 10 == 0) val = -val;
      int_to_bytes(sort_str.buf, val);
      test_data.push_back(sort_str.val);
    }
    // Comment away shuffling for testing partially pre-sorted data.
    // std::random_shuffle(test_data.begin(), test_data.end());

    sort_keys.reset(new uchar *[num_records]);
    for (int ix = 0; ix < num_records; ++ix)
      sort_keys[ix] = static_cast<uchar *>(
          static_cast<void *>(&test_data[keys_per_record * ix]));
  }

  std::vector<uchar *> GetKeys() const {
    return {sort_keys.get(), sort_keys.get() + num_records};
  }

  std::unique_ptr<uchar *[]> sort_keys;
};

/*
  Some different mem_compare functions.
  The second one seems to win on all platforms, except sparc,
  where the builtin memcmp() wins.

  TODO(sgunders): Consider re-tuning this and switch to one of
  the other ones; ideally, just memcmp().
 */
inline bool mem_compare_0(const uchar *s1, const uchar *s2, size_t len) {
  do {
    if (*s1++ != *s2++) return *--s1 < *--s2;
  } while (--len != 0);
  return s1 > s2;  // Return false for duplicate keys.
}

// This variant is safe against zero-length inputs.
inline bool mem_compare_0_zerosafe(const uchar *s1, const uchar *s2,
                                   size_t len) {
  for (size_t i = 0; i < len; ++i) {
    if (s1[i] != s2[i]) return s1[i] < s2[i];
  }
  return s1 > s2;  // Return false for duplicate keys.
}

inline bool mem_compare_1(const uchar *s1, const uchar *s2, size_t len) {
  do {
    if (*s1++ != *s2++) return *--s1 < *--s2;
  } while (--len != 0);
  return false;
}

inline bool mem_compare_2(const uchar *s1, const uchar *s2, size_t len) {
  int v = 0;
  while (len-- > 0 && v == 0) {
    v = *(s1++) - *(s2++);
  }
  return v < 0;
}

inline bool mem_compare_3(const uchar *s1, const uchar *s2, size_t len) {
  while (--len && (s1[0] == s2[0])) {
    ++s1;
    ++s2;
  }
  return s1[0] < s2[0];
}

#if defined(_WIN32)
#pragma intrinsic(memcmp)
#endif
// For gcc, __builtin_memcmp is actually *slower* than the library call:
// http://gcc.gnu.org/bugzilla/show_bug.cgi?id=43052

class Mem_compare_memcmp {
 public:
  explicit Mem_compare_memcmp(size_t n) : m_size(n) {}
  bool operator()(const uchar *s1, const uchar *s2) {
    return memcmp(s1, s2, m_size) < 0;
  }
  size_t m_size;
};

class Mem_compare_0 {
 public:
  explicit Mem_compare_0(size_t n) : m_size(n) {}
  bool operator()(const uchar *s1, const uchar *s2) {
    return mem_compare_0(s1, s2, m_size);
  }
  size_t m_size;
};

class Mem_compare_0_zerosafe {
 public:
  explicit Mem_compare_0_zerosafe(size_t n) : m_size(n) {}
  bool operator()(const uchar *s1, const uchar *s2) {
    return mem_compare_0_zerosafe(s1, s2, m_size);
  }
  size_t m_size;
};

class Mem_compare_1 {
 public:
  explicit Mem_compare_1(size_t n) : m_size(n) {}
  bool operator()(const uchar *s1, const uchar *s2) {
    return mem_compare_1(s1, s2, m_size);
  }
  size_t m_size;
};

class Mem_compare_2 {
 public:
  explicit Mem_compare_2(size_t n) : m_size(n) {}
  bool operator()(const uchar *s1, const uchar *s2) {
    return mem_compare_2(s1, s2, m_size);
  }
  size_t m_size;
};

class Mem_compare_3 {
 public:
  explicit Mem_compare_3(size_t n) : m_size(n) {}
  bool operator()(const uchar *s1, const uchar *s2) {
    return mem_compare_3(s1, s2, m_size);
  }
  size_t m_size;
};

#define COMPARE(N) \
  if (s1[N] != s2[N]) return s1[N] < s2[N]

class Mem_compare_4 {
 public:
  explicit Mem_compare_4(size_t n) : m_size(n) {}
  bool operator()(const uchar *s1, const uchar *s2) {
    size_t len = m_size;
    while (len > 0) {
      COMPARE(0);
      COMPARE(1);
      COMPARE(2);
      COMPARE(3);
      len -= 4;
      s1 += 4;
      s2 += 4;
    }
    return false;
  }
  size_t m_size;
};

class Mem_compare_5 {
 public:
  explicit Mem_compare_5(size_t n) : m_size(n) {}
  bool operator()(const uchar *s1, const uchar *s2) {
    COMPARE(0);
    COMPARE(1);
    COMPARE(2);
    COMPARE(3);
    return memcmp(s1 + 4, s2 + 4, m_size - 4) < 0;
  }
  size_t m_size;
};

// This one works for any number of keys.
// We treat the first key as int, the rest byte-by-byte.
class Mem_compare_int {
 public:
  explicit Mem_compare_int(size_t n) : m_size(n), rest(n - sizeof(int)) {}
  bool operator()(const uchar *s1, const uchar *s2) {
    int int1 = bytes_to_int(s1);
    int int2 = bytes_to_int(s2);
    if (int1 == int2) return mem_compare_1(s1 + rest, s2 + rest, rest);
    return int1 < int2;
  }

 private:
  size_t m_size;
  const size_t rest;
};

class Mem_compare_int_4 {
 public:
  explicit Mem_compare_int_4(size_t) : keyno(1) {}
  bool operator()(const uchar *s1, const uchar *s2) {
    int inta1 = bytes_to_int(s1);
    int intb1 = bytes_to_int(s2);
    if (keyno < 4 && inta1 == intb1) {
      ++keyno;
      return operator()(s1 + sizeof(int), s2 + sizeof(int));
    }
    return inta1 < intb1;
  }
  int keyno;
};

template <class Compare>
static inline void RunSortBenchmark(size_t num_iterations, bool stable_sort) {
  StopBenchmarkTiming();
  FileSortBMHelper helper;
  for (size_t ix = 0; ix < num_iterations; ++ix) {
    std::vector<uchar *> keys = helper.GetKeys();
    StartBenchmarkTiming();
    if (stable_sort) {
      std::stable_sort(keys.begin(), keys.end(), Compare(helper.record_size));
    } else {
      std::sort(keys.begin(), keys.end(), Compare(helper.record_size));
    }
    StopBenchmarkTiming();
  }
}

/*
  Several sorting tests below, each one runs num_iterations.
  For each iteration we take a copy of the key pointers, and sort the copy.
  Most of the tests below are run with std::sort and std::stable_sort.
  Stable sort seems to be faster for all test cases, on all platforms.
 */
static void BM_StdSortmemcmp(size_t num_iterations) {
  RunSortBenchmark<Mem_compare_memcmp>(num_iterations, /*stable_sort=*/false);
}
BENCHMARK(BM_StdSortmemcmp)

static void BM_StdStableSortmemcmp(size_t num_iterations) {
  RunSortBenchmark<Mem_compare_memcmp>(num_iterations, /*stable_sort=*/true);
}
BENCHMARK(BM_StdStableSortmemcmp)

static void BM_StdSortCompare0(size_t num_iterations) {
  RunSortBenchmark<Mem_compare_0>(num_iterations, /*stable_sort=*/false);
}
BENCHMARK(BM_StdSortCompare0)

static void BM_StdStableSortCompare0(size_t num_iterations) {
  RunSortBenchmark<Mem_compare_0>(num_iterations, /*stable_sort=*/true);
}
BENCHMARK(BM_StdStableSortCompare0)

static void BM_StdSortCompare0ZeroSafe(size_t num_iterations) {
  RunSortBenchmark<Mem_compare_0_zerosafe>(num_iterations,
                                           /*stable_sort=*/false);
}
BENCHMARK(BM_StdSortCompare0ZeroSafe)

static void BM_StdStableSortCompare0ZeroSafe(size_t num_iterations) {
  RunSortBenchmark<Mem_compare_0_zerosafe>(num_iterations,
                                           /*stable_sort=*/true);
}
BENCHMARK(BM_StdStableSortCompare0ZeroSafe)

static void BM_StdSortCompare1(size_t num_iterations) {
  RunSortBenchmark<Mem_compare_1>(num_iterations, /*stable_sort=*/false);
}
BENCHMARK(BM_StdSortCompare1)

static void BM_StdStableSortCompare1(size_t num_iterations) {
  RunSortBenchmark<Mem_compare_1>(num_iterations, /*stable_sort=*/true);
}
BENCHMARK(BM_StdStableSortCompare1)

static void BM_StdSortCompare2(size_t num_iterations) {
  RunSortBenchmark<Mem_compare_2>(num_iterations, /*stable_sort=*/false);
}
BENCHMARK(BM_StdSortCompare2)

static void BM_StdStableSortCompare2(size_t num_iterations) {
  RunSortBenchmark<Mem_compare_2>(num_iterations, /*stable_sort=*/true);
}
BENCHMARK(BM_StdStableSortCompare2)

static void BM_StdSortCompare3(size_t num_iterations) {
  RunSortBenchmark<Mem_compare_3>(num_iterations, /*stable_sort=*/false);
}
BENCHMARK(BM_StdSortCompare3)

static void BM_StdStableSortCompare3(size_t num_iterations) {
  RunSortBenchmark<Mem_compare_3>(num_iterations, /*stable_sort=*/true);
}
BENCHMARK(BM_StdStableSortCompare3)

static void BM_StdSortCompare4(size_t num_iterations) {
  RunSortBenchmark<Mem_compare_4>(num_iterations, /*stable_sort=*/false);
}
BENCHMARK(BM_StdSortCompare4)

static void BM_StdStableSortCompare4(size_t num_iterations) {
  RunSortBenchmark<Mem_compare_4>(num_iterations, /*stable_sort=*/true);
}
BENCHMARK(BM_StdStableSortCompare4)

static void BM_StdSortCompare5(size_t num_iterations) {
  RunSortBenchmark<Mem_compare_5>(num_iterations, /*stable_sort=*/false);
}
BENCHMARK(BM_StdSortCompare5)

static void BM_StdStableSortCompare5(size_t num_iterations) {
  RunSortBenchmark<Mem_compare_5>(num_iterations, /*stable_sort=*/true);
}
BENCHMARK(BM_StdStableSortCompare5)

// Disabled: experimental.
[[maybe_unused]] static void BM_StdSortIntCompare(size_t num_iterations) {
  RunSortBenchmark<Mem_compare_int>(num_iterations, /*stable_sort=*/false);
}
// BENCHMARK(BM_StdSortIntCompare)

[[maybe_unused]] static void BM_StdStableSortIntCompare(size_t num_iterations) {
  RunSortBenchmark<Mem_compare_int>(num_iterations, /*stable_sort=*/true);
}
// BENCHMARK(BM_StdStableSortIntCompare)

// Disabled: experimental.
[[maybe_unused]] static void BM_StdSortIntIntIntInt(size_t num_iterations) {
  RunSortBenchmark<Mem_compare_int_4>(num_iterations, /*stable_sort=*/false);
}
// BENCHMARK(BM_StdSortIntIntIntInt)

// Disabled: experimental.
[[maybe_unused]] static void BM_StdStableSortIntIntIntInt(
    size_t num_iterations) {
  RunSortBenchmark<Mem_compare_int_4>(num_iterations, /*stable_sort=*/true);
}
// BENCHMARK(BM_StdStableSortIntIntIntInt)

}  // namespace filesort_compare_unittest