| 1 | // Copyright (C) 2013-2025 Free Software Foundation, Inc. |
| 2 | // |
| 3 | // This file is part of the GNU ISO C++ Library. This library is free |
| 4 | // software; you can redistribute it and/or modify it under the |
| 5 | // terms of the GNU General Public License as published by the |
| 6 | // Free Software Foundation; either version 3, or (at your option) |
| 7 | // any later version. |
| 8 | |
| 9 | // This library is distributed in the hope that it will be useful, |
| 10 | // but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | // GNU General Public License for more details. |
| 13 | |
| 14 | // You should have received a copy of the GNU General Public License along |
| 15 | // with this library; see the file COPYING3. If not see |
| 16 | // <http://www.gnu.org/licenses/>. |
| 17 | |
| 18 | // { dg-do run { target c++11 } } |
| 19 | |
| 20 | #include <testsuite_performance.h> |
| 21 | |
| 22 | #include <sstream> |
| 23 | #include <string> |
| 24 | #include <vector> |
| 25 | #include <unordered_set> |
| 26 | |
| 27 | namespace |
| 28 | { |
| 29 | const int sz = 2000000; |
| 30 | const std::string pattern = "long enough test string #"; |
| 31 | |
| 32 | // Perfect std::string hasher knowing how string instances have been |
| 33 | // generated. It is not tag as slow so that hash code is not cached. |
| 34 | // It is easier to show impact of hint in this context. |
| 35 | struct hasher |
| 36 | { |
| 37 | std::size_t |
| 38 | operator()(const std::string& str) const noexcept |
| 39 | { |
| 40 | std::hash<std::string> std_hasher; |
| 41 | auto hash = std_hasher(pattern); |
| 42 | std::istringstream isstr(str.substr(pattern.length())); |
| 43 | int idx; |
| 44 | isstr >> idx; |
| 45 | return (std::size_t)(hash / sz) * sz + idx; |
| 46 | } |
| 47 | }; |
| 48 | |
| 49 | // Like previous hasher but tagged as slow. |
| 50 | struct slow_hasher |
| 51 | { |
| 52 | std::size_t |
| 53 | operator()(const std::string& str) const noexcept |
| 54 | { return hasher{}(str); } |
| 55 | }; |
| 56 | |
| 57 | template<typename _Hash> |
| 58 | using ums_t = std::unordered_multiset<std::string, _Hash>; |
| 59 | |
| 60 | template<typename _Hash> |
| 61 | void |
| 62 | insert_with_perfect_hint(const std::vector<std::string>& strs, |
| 63 | ums_t<_Hash>& ms) |
| 64 | { |
| 65 | auto hint = ms.end(); |
| 66 | for (auto str : strs) |
| 67 | { |
| 68 | auto insert_pos = ms.insert(hint, str); |
| 69 | if (std::next(insert_pos) == ms.end()) |
| 70 | hint = insert_pos; |
| 71 | } |
| 72 | } |
| 73 | |
| 74 | template<typename _Hash> |
| 75 | void |
| 76 | insert_with_bad_hint(const std::vector<std::string>& strs, |
| 77 | ums_t<_Hash>& ms) |
| 78 | { |
| 79 | auto hint = ms.begin(); |
| 80 | for (auto str : strs) |
| 81 | { |
| 82 | auto insert_pos = ms.insert(hint, str); |
| 83 | if (std::next(insert_pos) == hint) |
| 84 | hint = ms.begin(); |
| 85 | } |
| 86 | } |
| 87 | |
| 88 | template<typename _Hash> |
| 89 | void |
| 90 | insert_without_hint(const std::vector<std::string>& strs, |
| 91 | ums_t<_Hash>& ms) |
| 92 | { |
| 93 | for (auto str : strs) |
| 94 | ms.insert(str); |
| 95 | } |
| 96 | |
| 97 | template<typename _Hash> |
| 98 | void |
| 99 | insert_range(const std::vector<std::string>& strs, |
| 100 | ums_t<_Hash>& ms) |
| 101 | { ms.insert(strs.begin(), strs.end()); } |
| 102 | } |
| 103 | |
| 104 | template<typename _Hash> |
| 105 | void bench(const char* ctx) |
| 106 | { |
| 107 | using namespace __gnu_test; |
| 108 | |
| 109 | const int nb_iter = 10; |
| 110 | |
| 111 | std::vector<std::string> strs; |
| 112 | strs.reserve(sz); |
| 113 | |
| 114 | for (int i = 0; i != sz; ++i) |
| 115 | { |
| 116 | std::ostringstream osstr; |
| 117 | osstr << pattern << i; |
| 118 | strs.push_back(osstr.str()); |
| 119 | } |
| 120 | |
| 121 | ums_t<_Hash> ms; |
| 122 | ms.reserve(sz); |
| 123 | |
| 124 | // Warm up. |
| 125 | { |
| 126 | for (auto str : strs) |
| 127 | ms.insert(str); |
| 128 | } |
| 129 | |
| 130 | time_counter time_no_hint, time_bad_hint, time_perfect_hint, |
| 131 | time_range; |
| 132 | resource_counter resource_no_hint, resource_bad_hint, resource_perfect_hint, |
| 133 | resource_range; |
| 134 | |
| 135 | for (int i = 0; i != nb_iter; ++i) |
| 136 | { |
| 137 | // Bad hint |
| 138 | { |
| 139 | ms.clear(); |
| 140 | start_counters(time_bad_hint, resource_bad_hint); |
| 141 | insert_with_bad_hint(strs, ms); |
| 142 | stop_counters(time_bad_hint, resource_bad_hint); |
| 143 | } |
| 144 | |
| 145 | // No hint |
| 146 | { |
| 147 | ms.clear(); |
| 148 | start_counters(time_no_hint, resource_no_hint); |
| 149 | insert_without_hint(strs, ms); |
| 150 | stop_counters(time_no_hint, resource_no_hint); |
| 151 | } |
| 152 | |
| 153 | // Perfect hint |
| 154 | { |
| 155 | ms.clear(); |
| 156 | start_counters(time_perfect_hint, resource_perfect_hint); |
| 157 | insert_with_perfect_hint(strs, ms); |
| 158 | stop_counters(time_perfect_hint, resource_perfect_hint); |
| 159 | } |
| 160 | |
| 161 | // Range insert |
| 162 | { |
| 163 | ms.clear(); |
| 164 | start_counters(time_range, resource_range); |
| 165 | insert_range(strs, ms); |
| 166 | stop_counters(time_range, resource_range); |
| 167 | } |
| 168 | } |
| 169 | |
| 170 | std::ostringstream ostr; |
| 171 | ostr << ctx << ' ' << sz << " insertions w/o hint"; |
| 172 | report_performance(__FILE__, ostr.str().c_str(), |
| 173 | time_no_hint, resource_no_hint); |
| 174 | |
| 175 | ostr.str(""); |
| 176 | ostr << ctx << ' ' << sz << " insertions with perfect hint"; |
| 177 | report_performance(__FILE__, ostr.str().c_str(), |
| 178 | time_perfect_hint, resource_perfect_hint); |
| 179 | |
| 180 | ostr.str(""); |
| 181 | ostr << ctx << ' ' << sz << " insertions with bad hint"; |
| 182 | report_performance(__FILE__, ostr.str().c_str(), |
| 183 | time_bad_hint, resource_bad_hint); |
| 184 | |
| 185 | ostr.str(""); |
| 186 | ostr << ctx << ' ' << sz << " range insertions"; |
| 187 | report_performance(__FILE__, ostr.str().c_str(), |
| 188 | time_range, resource_range); |
| 189 | } |
| 190 | |
| 191 | namespace std |
| 192 | { |
| 193 | template<> |
| 194 | struct __is_fast_hash<slow_hasher> : std::false_type |
| 195 | { }; |
| 196 | } |
| 197 | |
| 198 | int main() |
| 199 | { |
| 200 | bench<hasher>("hash code NOT cached"); |
| 201 | bench<slow_hasher>("hash code cached"); |
| 202 | return 0; |
| 203 | } |