| 1 | /* Copyright (c) 2013, 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 | /* |
| 25 | Bug#68477 Suboptimal code in skip_trailing_space() |
| 26 | Bug#16395778 SUBOPTIMAL CODE IN SKIP_TRAILING_SPACE() |
| 27 | |
| 28 | Below we test some alternative implementations for skip_trailing_space. |
| 29 | */ |
| 30 | |
| 31 | #include <gtest/gtest.h> |
| 32 | #include <string> |
| 33 | |
| 34 | #include "m_string.h" |
| 35 | #include "template_utils.h" |
| 36 | #include "unittest/gunit/benchmark.h" |
| 37 | #include "unittest/gunit/skip_trailing.h" |
| 38 | |
| 39 | namespace skip_trailing_space_unittest { |
| 40 | |
| 41 | static inline void benchmark_func(size_t iters, |
| 42 | const uchar *func(const uchar *, size_t), |
| 43 | size_t length) { |
| 44 | StopBenchmarkTiming(); |
| 45 | // Insert something else (or nothing) here, |
| 46 | // to see effects of alignment of data: |
| 47 | std::string str = "1"; |
| 48 | str.append(length, ' '); |
| 49 | StartBenchmarkTiming(); |
| 50 | |
| 51 | for (size_t i = 0; i < iters; ++i) { |
| 52 | func(pointer_cast<const uchar *>(str.data()), length); |
| 53 | } |
| 54 | } |
| 55 | |
| 56 | #define INSTANTIATE_TEST(name, func, length) \ |
| 57 | static void name(size_t iters) { benchmark_func(iters, func, length); } \ |
| 58 | BENCHMARK(name) |
| 59 | |
| 60 | INSTANTIATE_TEST(BM_SkipTrailingSpaceTest_Unaligned_0, skip_trailing_unalgn, 0) |
| 61 | INSTANTIATE_TEST(BM_SkipTrailingSpaceTest_Unaligned_24, skip_trailing_unalgn, |
| 62 | 24) |
| 63 | INSTANTIATE_TEST(BM_SkipTrailingSpaceTest_Unaligned_100, skip_trailing_unalgn, |
| 64 | 100) |
| 65 | INSTANTIATE_TEST(BM_SkipTrailingSpaceTest_Unaligned_150, skip_trailing_unalgn, |
| 66 | 150) |
| 67 | |
| 68 | INSTANTIATE_TEST(BM_SkipTrailingSpaceTest_Original_0, skip_trailing_orig, 0) |
| 69 | INSTANTIATE_TEST(BM_SkipTrailingSpaceTest_Original_24, skip_trailing_orig, 24) |
| 70 | INSTANTIATE_TEST(BM_SkipTrailingSpaceTest_Original_100, skip_trailing_orig, 100) |
| 71 | INSTANTIATE_TEST(BM_SkipTrailingSpaceTest_Original_150, skip_trailing_orig, 150) |
| 72 | |
| 73 | INSTANTIATE_TEST(BM_SkipTrailingSpaceTest_FourByte_0, skip_trailing_4byte, 0) |
| 74 | INSTANTIATE_TEST(BM_SkipTrailingSpaceTest_FourByte_24, skip_trailing_4byte, 24) |
| 75 | INSTANTIATE_TEST(BM_SkipTrailingSpaceTest_FourByte_100, skip_trailing_4byte, |
| 76 | 100) |
| 77 | INSTANTIATE_TEST(BM_SkipTrailingSpaceTest_FourByte_150, skip_trailing_4byte, |
| 78 | 150) |
| 79 | |
| 80 | INSTANTIATE_TEST(BM_SkipTrailingSpaceTest_EightByte_0, skip_trailing_8byte, 0) |
| 81 | INSTANTIATE_TEST(BM_SkipTrailingSpaceTest_EightByte_24, skip_trailing_8byte, 24) |
| 82 | INSTANTIATE_TEST(BM_SkipTrailingSpaceTest_EightByte_100, skip_trailing_8byte, |
| 83 | 100) |
| 84 | INSTANTIATE_TEST(BM_SkipTrailingSpaceTest_EightByte_150, skip_trailing_8byte, |
| 85 | 150) |
| 86 | |
| 87 | INSTANTIATE_TEST(BM_SkipTrailingSpaceTest_Current_0, skip_trailing_space, 0) |
| 88 | INSTANTIATE_TEST(BM_SkipTrailingSpaceTest_Current_24, skip_trailing_space, 24) |
| 89 | INSTANTIATE_TEST(BM_SkipTrailingSpaceTest_Current_100, skip_trailing_space, 100) |
| 90 | INSTANTIATE_TEST(BM_SkipTrailingSpaceTest_Current_150, skip_trailing_space, 150) |
| 91 | |
| 92 | } // namespace skip_trailing_space_unittest |