| 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 "my_config.h" |
| 25 | |
| 26 | #include <gtest/gtest.h> |
| 27 | #include <algorithm> |
| 28 | #include <random> |
| 29 | #include <vector> |
| 30 | |
| 31 | #include "my_byteorder.h" |
| 32 | #include "my_compiler.h" |
| 33 | #include "my_inttypes.h" |
| 34 | |
| 35 | namespace alignment_unittest { |
| 36 | |
| 37 | /* |
| 38 | Testing performance penalty of accessing un-aligned data. |
| 39 | Seems to about 2% on my desktop machine. |
| 40 | */ |
| 41 | class AlignmentTest : public ::testing::Test { |
| 42 | protected: |
| 43 | // Increase num_iterations for actual benchmarking! |
| 44 | static const int num_iterations = 1; |
| 45 | static const int num_records = 100 * 1000; |
| 46 | |
| 47 | static int *aligned_data; |
| 48 | static uchar *unaligned_data; |
| 49 | |
| 50 | static void SetUpTestCase() { |
| 51 | aligned_data = new int[num_records]; |
| 52 | unaligned_data = new uchar[(num_records + 1) * sizeof(int)]; |
| 53 | for (int ix = 0; ix < num_records; ++ix) { |
| 54 | aligned_data[ix] = ix / 10; |
| 55 | } |
| 56 | std::random_device rng; |
| 57 | std::mt19937 urng(rng()); |
| 58 | std::shuffle(aligned_data, aligned_data + num_records, urng); |
| 59 | memcpy(unaligned_data + 1, aligned_data, num_records * sizeof(int)); |
| 60 | } |
| 61 | |
| 62 | static void TearDownTestCase() { |
| 63 | delete[] aligned_data; |
| 64 | delete[] unaligned_data; |
| 65 | } |
| 66 | |
| 67 | void SetUp() override { |
| 68 | aligned_keys = new uchar *[num_records]; |
| 69 | unaligned_keys = new uchar *[num_records]; |
| 70 | for (int ix = 0; ix < num_records; ++ix) { |
| 71 | aligned_keys[ix] = |
| 72 | static_cast<uchar *>(static_cast<void *>(&aligned_data[ix])); |
| 73 | unaligned_keys[ix] = &unaligned_data[1 + (ix * sizeof(int))]; |
| 74 | } |
| 75 | } |
| 76 | |
| 77 | void TearDown() override { |
| 78 | delete[] aligned_keys; |
| 79 | delete[] unaligned_keys; |
| 80 | } |
| 81 | |
| 82 | uchar **aligned_keys; |
| 83 | uchar **unaligned_keys; |
| 84 | }; |
| 85 | |
| 86 | int *AlignmentTest::aligned_data; |
| 87 | uchar *AlignmentTest::unaligned_data; |
| 88 | |
| 89 | // A copy of the generic, byte-by-byte getter. |
| 90 | #define sint4korrgeneric(A) \ |
| 91 | (int32)(((int32)((uchar)(A)[0])) + (((int32)((uchar)(A)[1]) << 8)) + \ |
| 92 | (((int32)((uchar)(A)[2]) << 16)) + (((int32)((int16)(A)[3]) << 24))) |
| 93 | class Mem_compare_uchar_int { |
| 94 | public: |
| 95 | // SUPPRESS_UBSAN: only executed on intel, misaligned read works OK. |
| 96 | bool operator()(const uchar *s1, const uchar *s2) SUPPRESS_UBSAN { |
| 97 | return *pointer_cast<const int *>(s1) < *pointer_cast<const int *>(s2); |
| 98 | } |
| 99 | }; |
| 100 | |
| 101 | class Mem_compare_sint4 { |
| 102 | public: |
| 103 | bool operator()(const uchar *s1, const uchar *s2) { |
| 104 | return sint4korr(s1) < sint4korr(s2); |
| 105 | } |
| 106 | }; |
| 107 | |
| 108 | class Mem_compare_sint4_generic { |
| 109 | public: |
| 110 | bool operator()(const uchar *s1, const uchar *s2) { |
| 111 | return sint4korrgeneric(s1) < sint4korrgeneric(s2); |
| 112 | } |
| 113 | }; |
| 114 | |
| 115 | #if defined(__i386__) || defined(__x86_64__) || defined(_WIN32) |
| 116 | |
| 117 | TEST_F(AlignmentTest, AlignedSort) { |
| 118 | for (int ix = 0; ix < num_iterations; ++ix) { |
| 119 | std::vector<uchar *> keys(aligned_keys, aligned_keys + num_records); |
| 120 | std::sort(keys.begin(), keys.end(), Mem_compare_uchar_int()); |
| 121 | } |
| 122 | } |
| 123 | |
| 124 | TEST_F(AlignmentTest, UnAlignedSort) { |
| 125 | for (int ix = 0; ix < num_iterations; ++ix) { |
| 126 | std::vector<uchar *> keys(unaligned_keys, unaligned_keys + num_records); |
| 127 | std::sort(keys.begin(), keys.end(), Mem_compare_uchar_int()); |
| 128 | } |
| 129 | } |
| 130 | |
| 131 | TEST_F(AlignmentTest, Sint4Sort) { |
| 132 | for (int ix = 0; ix < num_iterations; ++ix) { |
| 133 | std::vector<uchar *> keys(unaligned_keys, unaligned_keys + num_records); |
| 134 | std::sort(keys.begin(), keys.end(), Mem_compare_sint4()); |
| 135 | } |
| 136 | } |
| 137 | |
| 138 | TEST_F(AlignmentTest, Sint4SortGeneric) { |
| 139 | for (int ix = 0; ix < num_iterations; ++ix) { |
| 140 | std::vector<uchar *> keys(unaligned_keys, unaligned_keys + num_records); |
| 141 | std::sort(keys.begin(), keys.end(), Mem_compare_sint4_generic()); |
| 142 | } |
| 143 | } |
| 144 | |
| 145 | #endif |
| 146 | |
| 147 | } // namespace alignment_unittest |