| 1 | /* Copyright (c) 2012, 2026, Oracle and/or its affiliates. |
| 2 | |
| 3 | This program is free software; you can redistribute it and/or modify |
| 4 | it under the terms of the GNU General Public License, version 2.0, |
| 5 | as published by the Free Software Foundation. |
| 6 | |
| 7 | This program is designed to work with certain software (including |
| 8 | but not limited to OpenSSL) that is licensed under separate terms, |
| 9 | as designated in a particular file or component or in included license |
| 10 | documentation. The authors of MySQL hereby grant you an additional |
| 11 | permission to link the program and your derivative works with the |
| 12 | separately licensed software that they have either included with |
| 13 | the program or referenced in the documentation. |
| 14 | |
| 15 | This program is distributed in the hope that it will be useful, |
| 16 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 17 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 18 | GNU General Public License, version 2.0, for more details. |
| 19 | |
| 20 | You should have received a copy of the GNU General Public License |
| 21 | along with this program; if not, write to the Free Software |
| 22 | Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */ |
| 23 | |
| 24 | #include <gtest/gtest.h> |
| 25 | #include <algorithm> |
| 26 | #include <memory> |
| 27 | #include <vector> |
| 28 | |
| 29 | #include "my_byteorder.h" |
| 30 | #include "my_inttypes.h" |
| 31 | #include "sql/filesort_utils.h" |
| 32 | #include "unittest/gunit/benchmark.h" |
| 33 | #include "unittest/gunit/gunit_test_main.h" |
| 34 | |
| 35 | namespace filesort_compare_unittest { |
| 36 | |
| 37 | /* |
| 38 | Below are some performance microbenchmarks in order to compare our sorting |
| 39 | options: |
| 40 | std::sort - requires no extra memory, |
| 41 | typically implemented with introsort/insertion sort |
| 42 | std::stable_sort - requires extra memory: array of n pointers, |
| 43 | typically implemented with mergesort |
| 44 | |
| 45 | The record format for filesort is constructed in such a way that we can |
| 46 | compare records byte-by-byte, without knowing the data types. |
| 47 | Nullable fields (maybe_null()) are pre-pended with an extra byte. |
| 48 | If we are sorting in descending mode, all the bytes are simply flipped. |
| 49 | |
| 50 | This means that any variant of memcmp() can be used for comparing record. |
| 51 | Below we test different variants, including memcmp() itself. |
| 52 | */ |
| 53 | |
| 54 | inline int bytes_to_int(const uchar *s) { |
| 55 | int val = longget(s); |
| 56 | return val ^ 0x80000000; |
| 57 | } |
| 58 | |
| 59 | inline void int_to_bytes(uchar *s, int val) { |
| 60 | val = val ^ 0x80000000; |
| 61 | longstore(s, val); |
| 62 | } |
| 63 | |
| 64 | TEST(BufferAlignmentTest, IntsToBytesToInt) { |
| 65 | uchar buf[10]; |
| 66 | memset(buf, 0, sizeof(buf)); |
| 67 | for (int ix = 0; ix < 6; ++ix) { |
| 68 | int test_data[] = {INT_MIN32, -42, -1, 0, 1, 42, INT_MAX32}; |
| 69 | for (size_t iy = 0; iy < array_elements(test_data); ++iy) { |
| 70 | int val = test_data[iy]; |
| 71 | int_to_bytes(buf + ix, val); |
| 72 | EXPECT_EQ(val, bytes_to_int(buf + ix)); |
| 73 | } |
| 74 | } |
| 75 | } |
| 76 | |
| 77 | class FileSortBMHelper { |
| 78 | public: |
| 79 | // Number of records. |
| 80 | static const int num_records = 100 * 100; |
| 81 | // Number of keys in each record. |
| 82 | static const int keys_per_record = 4; |
| 83 | // Size of each record. |
| 84 | static const int record_size = keys_per_record * sizeof(int); |
| 85 | |
| 86 | // Buffer containing data to be sorted. |
| 87 | // (actually: we only sort the sort_keys below, data is stable). |
| 88 | std::vector<int> test_data; |
| 89 | |
| 90 | FileSortBMHelper() { |
| 91 | test_data.reserve(num_records * keys_per_record); |
| 92 | union { |
| 93 | int val; |
| 94 | uchar buf[sizeof(int)]; |
| 95 | } sort_str; |
| 96 | |
| 97 | for (int ix = 0; ix < num_records * keys_per_record; ++ix) { |
| 98 | int val = ix / (10 * keys_per_record); |
| 99 | if (ix % 10 == 0) val = -val; |
| 100 | int_to_bytes(sort_str.buf, val); |
| 101 | test_data.push_back(sort_str.val); |
| 102 | } |
| 103 | // Comment away shuffling for testing partially pre-sorted data. |
| 104 | // std::random_shuffle(test_data.begin(), test_data.end()); |
| 105 | |
| 106 | sort_keys.reset(new uchar *[num_records]); |
| 107 | for (int ix = 0; ix < num_records; ++ix) |
| 108 | sort_keys[ix] = static_cast<uchar *>( |
| 109 | static_cast<void *>(&test_data[keys_per_record * ix])); |
| 110 | } |
| 111 | |
| 112 | std::vector<uchar *> GetKeys() const { |
| 113 | return {sort_keys.get(), sort_keys.get() + num_records}; |
| 114 | } |
| 115 | |
| 116 | std::unique_ptr<uchar *[]> sort_keys; |
| 117 | }; |
| 118 | |
| 119 | /* |
| 120 | Some different mem_compare functions. |
| 121 | The second one seems to win on all platforms, except sparc, |
| 122 | where the builtin memcmp() wins. |
| 123 | |
| 124 | TODO(sgunders): Consider re-tuning this and switch to one of |
| 125 | the other ones; ideally, just memcmp(). |
| 126 | */ |
| 127 | inline bool mem_compare_0(const uchar *s1, const uchar *s2, size_t len) { |
| 128 | do { |
| 129 | if (*s1++ != *s2++) return *--s1 < *--s2; |
| 130 | } while (--len != 0); |
| 131 | return s1 > s2; // Return false for duplicate keys. |
| 132 | } |
| 133 | |
| 134 | // This variant is safe against zero-length inputs. |
| 135 | inline bool mem_compare_0_zerosafe(const uchar *s1, const uchar *s2, |
| 136 | size_t len) { |
| 137 | for (size_t i = 0; i < len; ++i) { |
| 138 | if (s1[i] != s2[i]) return s1[i] < s2[i]; |
| 139 | } |
| 140 | return s1 > s2; // Return false for duplicate keys. |
| 141 | } |
| 142 | |
| 143 | inline bool mem_compare_1(const uchar *s1, const uchar *s2, size_t len) { |
| 144 | do { |
| 145 | if (*s1++ != *s2++) return *--s1 < *--s2; |
| 146 | } while (--len != 0); |
| 147 | return false; |
| 148 | } |
| 149 | |
| 150 | inline bool mem_compare_2(const uchar *s1, const uchar *s2, size_t len) { |
| 151 | int v = 0; |
| 152 | while (len-- > 0 && v == 0) { |
| 153 | v = *(s1++) - *(s2++); |
| 154 | } |
| 155 | return v < 0; |
| 156 | } |
| 157 | |
| 158 | inline bool mem_compare_3(const uchar *s1, const uchar *s2, size_t len) { |
| 159 | while (--len && (s1[0] == s2[0])) { |
| 160 | ++s1; |
| 161 | ++s2; |
| 162 | } |
| 163 | return s1[0] < s2[0]; |
| 164 | } |
| 165 | |
| 166 | #if defined(_WIN32) |
| 167 | #pragma intrinsic(memcmp) |
| 168 | #endif |
| 169 | // For gcc, __builtin_memcmp is actually *slower* than the library call: |
| 170 | // http://gcc.gnu.org/bugzilla/show_bug.cgi?id=43052 |
| 171 | |
| 172 | class Mem_compare_memcmp { |
| 173 | public: |
| 174 | explicit Mem_compare_memcmp(size_t n) : m_size(n) {} |
| 175 | bool operator()(const uchar *s1, const uchar *s2) { |
| 176 | return memcmp(s1, s2, m_size) < 0; |
| 177 | } |
| 178 | size_t m_size; |
| 179 | }; |
| 180 | |
| 181 | class Mem_compare_0 { |
| 182 | public: |
| 183 | explicit Mem_compare_0(size_t n) : m_size(n) {} |
| 184 | bool operator()(const uchar *s1, const uchar *s2) { |
| 185 | return mem_compare_0(s1, s2, m_size); |
| 186 | } |
| 187 | size_t m_size; |
| 188 | }; |
| 189 | |
| 190 | class Mem_compare_0_zerosafe { |
| 191 | public: |
| 192 | explicit Mem_compare_0_zerosafe(size_t n) : m_size(n) {} |
| 193 | bool operator()(const uchar *s1, const uchar *s2) { |
| 194 | return mem_compare_0_zerosafe(s1, s2, m_size); |
| 195 | } |
| 196 | size_t m_size; |
| 197 | }; |
| 198 | |
| 199 | class Mem_compare_1 { |
| 200 | public: |
| 201 | explicit Mem_compare_1(size_t n) : m_size(n) {} |
| 202 | bool operator()(const uchar *s1, const uchar *s2) { |
| 203 | return mem_compare_1(s1, s2, m_size); |
| 204 | } |
| 205 | size_t m_size; |
| 206 | }; |
| 207 | |
| 208 | class Mem_compare_2 { |
| 209 | public: |
| 210 | explicit Mem_compare_2(size_t n) : m_size(n) {} |
| 211 | bool operator()(const uchar *s1, const uchar *s2) { |
| 212 | return mem_compare_2(s1, s2, m_size); |
| 213 | } |
| 214 | size_t m_size; |
| 215 | }; |
| 216 | |
| 217 | class Mem_compare_3 { |
| 218 | public: |
| 219 | explicit Mem_compare_3(size_t n) : m_size(n) {} |
| 220 | bool operator()(const uchar *s1, const uchar *s2) { |
| 221 | return mem_compare_3(s1, s2, m_size); |
| 222 | } |
| 223 | size_t m_size; |
| 224 | }; |
| 225 | |
| 226 | #define COMPARE(N) \ |
| 227 | if (s1[N] != s2[N]) return s1[N] < s2[N] |
| 228 | |
| 229 | class Mem_compare_4 { |
| 230 | public: |
| 231 | explicit Mem_compare_4(size_t n) : m_size(n) {} |
| 232 | bool operator()(const uchar *s1, const uchar *s2) { |
| 233 | size_t len = m_size; |
| 234 | while (len > 0) { |
| 235 | COMPARE(0); |
| 236 | COMPARE(1); |
| 237 | COMPARE(2); |
| 238 | COMPARE(3); |
| 239 | len -= 4; |
| 240 | s1 += 4; |
| 241 | s2 += 4; |
| 242 | } |
| 243 | return false; |
| 244 | } |
| 245 | size_t m_size; |
| 246 | }; |
| 247 | |
| 248 | class Mem_compare_5 { |
| 249 | public: |
| 250 | explicit Mem_compare_5(size_t n) : m_size(n) {} |
| 251 | bool operator()(const uchar *s1, const uchar *s2) { |
| 252 | COMPARE(0); |
| 253 | COMPARE(1); |
| 254 | COMPARE(2); |
| 255 | COMPARE(3); |
| 256 | return memcmp(s1 + 4, s2 + 4, m_size - 4) < 0; |
| 257 | } |
| 258 | size_t m_size; |
| 259 | }; |
| 260 | |
| 261 | // This one works for any number of keys. |
| 262 | // We treat the first key as int, the rest byte-by-byte. |
| 263 | class Mem_compare_int { |
| 264 | public: |
| 265 | explicit Mem_compare_int(size_t n) : m_size(n), rest(n - sizeof(int)) {} |
| 266 | bool operator()(const uchar *s1, const uchar *s2) { |
| 267 | int int1 = bytes_to_int(s1); |
| 268 | int int2 = bytes_to_int(s2); |
| 269 | if (int1 == int2) return mem_compare_1(s1 + rest, s2 + rest, rest); |
| 270 | return int1 < int2; |
| 271 | } |
| 272 | |
| 273 | private: |
| 274 | size_t m_size; |
| 275 | const size_t rest; |
| 276 | }; |
| 277 | |
| 278 | class Mem_compare_int_4 { |
| 279 | public: |
| 280 | explicit Mem_compare_int_4(size_t) : keyno(1) {} |
| 281 | bool operator()(const uchar *s1, const uchar *s2) { |
| 282 | int inta1 = bytes_to_int(s1); |
| 283 | int intb1 = bytes_to_int(s2); |
| 284 | if (keyno < 4 && inta1 == intb1) { |
| 285 | ++keyno; |
| 286 | return operator()(s1 + sizeof(int), s2 + sizeof(int)); |
| 287 | } |
| 288 | return inta1 < intb1; |
| 289 | } |
| 290 | int keyno; |
| 291 | }; |
| 292 | |
| 293 | template <class Compare> |
| 294 | static inline void RunSortBenchmark(size_t num_iterations, bool stable_sort) { |
| 295 | StopBenchmarkTiming(); |
| 296 | FileSortBMHelper helper; |
| 297 | for (size_t ix = 0; ix < num_iterations; ++ix) { |
| 298 | std::vector<uchar *> keys = helper.GetKeys(); |
| 299 | StartBenchmarkTiming(); |
| 300 | if (stable_sort) { |
| 301 | std::stable_sort(keys.begin(), keys.end(), Compare(helper.record_size)); |
| 302 | } else { |
| 303 | std::sort(keys.begin(), keys.end(), Compare(helper.record_size)); |
| 304 | } |
| 305 | StopBenchmarkTiming(); |
| 306 | } |
| 307 | } |
| 308 | |
| 309 | /* |
| 310 | Several sorting tests below, each one runs num_iterations. |
| 311 | For each iteration we take a copy of the key pointers, and sort the copy. |
| 312 | Most of the tests below are run with std::sort and std::stable_sort. |
| 313 | Stable sort seems to be faster for all test cases, on all platforms. |
| 314 | */ |
| 315 | static void BM_StdSortmemcmp(size_t num_iterations) { |
| 316 | RunSortBenchmark<Mem_compare_memcmp>(num_iterations, /*stable_sort=*/false); |
| 317 | } |
| 318 | BENCHMARK(BM_StdSortmemcmp) |
| 319 | |
| 320 | static void BM_StdStableSortmemcmp(size_t num_iterations) { |
| 321 | RunSortBenchmark<Mem_compare_memcmp>(num_iterations, /*stable_sort=*/true); |
| 322 | } |
| 323 | BENCHMARK(BM_StdStableSortmemcmp) |
| 324 | |
| 325 | static void BM_StdSortCompare0(size_t num_iterations) { |
| 326 | RunSortBenchmark<Mem_compare_0>(num_iterations, /*stable_sort=*/false); |
| 327 | } |
| 328 | BENCHMARK(BM_StdSortCompare0) |
| 329 | |
| 330 | static void BM_StdStableSortCompare0(size_t num_iterations) { |
| 331 | RunSortBenchmark<Mem_compare_0>(num_iterations, /*stable_sort=*/true); |
| 332 | } |
| 333 | BENCHMARK(BM_StdStableSortCompare0) |
| 334 | |
| 335 | static void BM_StdSortCompare0ZeroSafe(size_t num_iterations) { |
| 336 | RunSortBenchmark<Mem_compare_0_zerosafe>(num_iterations, |
| 337 | /*stable_sort=*/false); |
| 338 | } |
| 339 | BENCHMARK(BM_StdSortCompare0ZeroSafe) |
| 340 | |
| 341 | static void BM_StdStableSortCompare0ZeroSafe(size_t num_iterations) { |
| 342 | RunSortBenchmark<Mem_compare_0_zerosafe>(num_iterations, |
| 343 | /*stable_sort=*/true); |
| 344 | } |
| 345 | BENCHMARK(BM_StdStableSortCompare0ZeroSafe) |
| 346 | |
| 347 | static void BM_StdSortCompare1(size_t num_iterations) { |
| 348 | RunSortBenchmark<Mem_compare_1>(num_iterations, /*stable_sort=*/false); |
| 349 | } |
| 350 | BENCHMARK(BM_StdSortCompare1) |
| 351 | |
| 352 | static void BM_StdStableSortCompare1(size_t num_iterations) { |
| 353 | RunSortBenchmark<Mem_compare_1>(num_iterations, /*stable_sort=*/true); |
| 354 | } |
| 355 | BENCHMARK(BM_StdStableSortCompare1) |
| 356 | |
| 357 | static void BM_StdSortCompare2(size_t num_iterations) { |
| 358 | RunSortBenchmark<Mem_compare_2>(num_iterations, /*stable_sort=*/false); |
| 359 | } |
| 360 | BENCHMARK(BM_StdSortCompare2) |
| 361 | |
| 362 | static void BM_StdStableSortCompare2(size_t num_iterations) { |
| 363 | RunSortBenchmark<Mem_compare_2>(num_iterations, /*stable_sort=*/true); |
| 364 | } |
| 365 | BENCHMARK(BM_StdStableSortCompare2) |
| 366 | |
| 367 | static void BM_StdSortCompare3(size_t num_iterations) { |
| 368 | RunSortBenchmark<Mem_compare_3>(num_iterations, /*stable_sort=*/false); |
| 369 | } |
| 370 | BENCHMARK(BM_StdSortCompare3) |
| 371 | |
| 372 | static void BM_StdStableSortCompare3(size_t num_iterations) { |
| 373 | RunSortBenchmark<Mem_compare_3>(num_iterations, /*stable_sort=*/true); |
| 374 | } |
| 375 | BENCHMARK(BM_StdStableSortCompare3) |
| 376 | |
| 377 | static void BM_StdSortCompare4(size_t num_iterations) { |
| 378 | RunSortBenchmark<Mem_compare_4>(num_iterations, /*stable_sort=*/false); |
| 379 | } |
| 380 | BENCHMARK(BM_StdSortCompare4) |
| 381 | |
| 382 | static void BM_StdStableSortCompare4(size_t num_iterations) { |
| 383 | RunSortBenchmark<Mem_compare_4>(num_iterations, /*stable_sort=*/true); |
| 384 | } |
| 385 | BENCHMARK(BM_StdStableSortCompare4) |
| 386 | |
| 387 | static void BM_StdSortCompare5(size_t num_iterations) { |
| 388 | RunSortBenchmark<Mem_compare_5>(num_iterations, /*stable_sort=*/false); |
| 389 | } |
| 390 | BENCHMARK(BM_StdSortCompare5) |
| 391 | |
| 392 | static void BM_StdStableSortCompare5(size_t num_iterations) { |
| 393 | RunSortBenchmark<Mem_compare_5>(num_iterations, /*stable_sort=*/true); |
| 394 | } |
| 395 | BENCHMARK(BM_StdStableSortCompare5) |
| 396 | |
| 397 | // Disabled: experimental. |
| 398 | [[maybe_unused]] static void BM_StdSortIntCompare(size_t num_iterations) { |
| 399 | RunSortBenchmark<Mem_compare_int>(num_iterations, /*stable_sort=*/false); |
| 400 | } |
| 401 | // BENCHMARK(BM_StdSortIntCompare) |
| 402 | |
| 403 | [[maybe_unused]] static void BM_StdStableSortIntCompare(size_t num_iterations) { |
| 404 | RunSortBenchmark<Mem_compare_int>(num_iterations, /*stable_sort=*/true); |
| 405 | } |
| 406 | // BENCHMARK(BM_StdStableSortIntCompare) |
| 407 | |
| 408 | // Disabled: experimental. |
| 409 | [[maybe_unused]] static void BM_StdSortIntIntIntInt(size_t num_iterations) { |
| 410 | RunSortBenchmark<Mem_compare_int_4>(num_iterations, /*stable_sort=*/false); |
| 411 | } |
| 412 | // BENCHMARK(BM_StdSortIntIntIntInt) |
| 413 | |
| 414 | // Disabled: experimental. |
| 415 | [[maybe_unused]] static void BM_StdStableSortIntIntIntInt( |
| 416 | size_t num_iterations) { |
| 417 | RunSortBenchmark<Mem_compare_int_4>(num_iterations, /*stable_sort=*/true); |
| 418 | } |
| 419 | // BENCHMARK(BM_StdStableSortIntIntIntInt) |
| 420 | |
| 421 | } // namespace filesort_compare_unittest |