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