1 /* Copyright (c) 2012, 2021, 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 also distributed 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 included with MySQL.
13 
14    This program is distributed in the hope that it will be useful,
15    but WITHOUT ANY WARRANTY; without even the implied warranty of
16    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17    GNU General Public License, version 2.0, for more details.
18 
19    You should have received a copy of the GNU General Public License
20    along with this program; if not, write to the Free Software
21    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA */
22 
23 // First include (the generated) my_config.h, to get correct platform defines.
24 #include "my_config.h"
25 #include <gtest/gtest.h>
26 
27 #include "my_global.h"
28 
29 #include <algorithm>
30 #include <vector>
31 
32 namespace alignment_unittest {
33 
34 /*
35   Testing performance penalty of accessing un-aligned data.
36   Seems to about 2% on my desktop machine.
37  */
38 class AlignmentTest : public ::testing::Test
39 {
40 protected:
41   // Increase num_iterations for actual benchmarking!
42   static const int num_iterations= 1;
43   static const int num_records= 100 * 1000;
44 
45   static int* aligned_data;
46   static uchar* unaligned_data;
47 
SetUpTestCase()48   static void SetUpTestCase()
49   {
50     aligned_data= new int[num_records];
51     unaligned_data= new uchar[(num_records + 1) * sizeof(int)];
52     for (int ix= 0; ix < num_records; ++ix)
53     {
54       aligned_data[ix]= ix / 10;
55     }
56     std::random_shuffle(aligned_data, aligned_data + num_records);
57     memcpy(unaligned_data + 1, aligned_data, num_records * sizeof(int));
58   }
59 
TearDownTestCase()60   static void TearDownTestCase()
61   {
62     delete[] aligned_data;
63     delete[] unaligned_data;
64   }
65 
SetUp()66   virtual void SetUp()
67   {
68     aligned_keys= new uchar* [num_records];
69     unaligned_keys= new uchar* [num_records];
70     for (int ix= 0; ix < num_records; ++ix)
71     {
72       aligned_keys[ix]=
73         static_cast<uchar*>(static_cast<void*>(&aligned_data[ix]));
74       unaligned_keys[ix]=
75         &unaligned_data[1 + (ix * sizeof(int))];
76     }
77   }
78 
TearDown()79   virtual void TearDown()
80   {
81     delete[] aligned_keys;
82     delete[] unaligned_keys;
83   }
84 
85   uchar **aligned_keys;
86   uchar **unaligned_keys;
87 };
88 
89 int* AlignmentTest::aligned_data;
90 uchar* AlignmentTest::unaligned_data;
91 
92 // A copy of the generic, byte-by-byte getter.
93 #define sint4korrgeneric(A) (int32) (((int32) ((uchar) (A)[0])) +\
94                                      (((int32) ((uchar) (A)[1]) << 8)) + \
95                                      (((int32) ((uchar) (A)[2]) << 16)) + \
96                                      (((int32) ((int16) (A)[3]) << 24)))
97 class Mem_compare_uchar_int :
98   public std::binary_function<const uchar*, const uchar*, bool>
99 {
100 public:
operator ()(const uchar * s1,const uchar * s2)101   bool operator() (const uchar *s1, const uchar *s2)
102   {
103     return *(int*) s1 < *(int*) s2;
104   }
105 };
106 
107 class Mem_compare_sint4 :
108   public std::binary_function<const uchar*, const uchar*, bool>
109 {
110 public:
operator ()(const uchar * s1,const uchar * s2)111   bool operator() (const uchar *s1, const uchar *s2)
112   {
113     return sint4korr(s1) < sint4korr(s2);
114   }
115 };
116 
117 class Mem_compare_sint4_generic :
118   public std::binary_function<const uchar*, const uchar*, bool>
119 {
120 public:
operator ()(const uchar * s1,const uchar * s2)121   bool operator() (const uchar *s1, const uchar *s2)
122   {
123     return sint4korrgeneric(s1) < sint4korrgeneric(s2);
124   }
125 };
126 
127 #if defined(__i386__) || defined(__x86_64__) || defined(_WIN32)
128 
129 
TEST_F(AlignmentTest,AlignedSort)130 TEST_F(AlignmentTest, AlignedSort)
131 {
132   for (int ix= 0; ix < num_iterations; ++ix)
133   {
134     std::vector<uchar*> keys(aligned_keys, aligned_keys + num_records);
135     std::sort(keys.begin(), keys.end(), Mem_compare_uchar_int());
136   }
137 }
138 
TEST_F(AlignmentTest,UnAlignedSort)139 TEST_F(AlignmentTest, UnAlignedSort)
140 {
141   for (int ix= 0; ix < num_iterations; ++ix)
142   {
143     std::vector<uchar*> keys(unaligned_keys, unaligned_keys + num_records);
144     std::sort(keys.begin(), keys.end(), Mem_compare_uchar_int());
145   }
146 }
147 
TEST_F(AlignmentTest,Sint4Sort)148 TEST_F(AlignmentTest, Sint4Sort)
149 {
150   for (int ix= 0; ix < num_iterations; ++ix)
151   {
152     std::vector<uchar*> keys(unaligned_keys, unaligned_keys + num_records);
153     std::sort(keys.begin(), keys.end(), Mem_compare_sint4());
154   }
155 }
156 
TEST_F(AlignmentTest,Sint4SortGeneric)157 TEST_F(AlignmentTest, Sint4SortGeneric)
158 {
159   for (int ix= 0; ix < num_iterations; ++ix)
160   {
161     std::vector<uchar*> keys(unaligned_keys, unaligned_keys + num_records);
162     std::sort(keys.begin(), keys.end(), Mem_compare_sint4_generic());
163   }
164 }
165 
166 #endif
167 
168 }
169