@@ -25,13 +25,32 @@ probing.
2525#pragma once
2626
2727#include < algorithm>
28- #include < cassert>
2928#include < cstddef>
3029#include < stdexcept>
3130#include < vector>
3231
3332namespace rigtorp {
3433
34+ namespace detail {
35+ #if defined(_MSC_VER)
36+ #define RIGTORP_HASHMAP_NOINLINE __declspec (noinline)
37+ #elif defined(__GNUC__) || defined(__clang__)
38+ #define RIGTORP_HASHMAP_NOINLINE __attribute__ ((noinline))
39+ #else
40+ #define RIGTORP_HASHMAP_NOINLINE
41+ #endif
42+
43+ [[noreturn]] RIGTORP_HASHMAP_NOINLINE inline void throw_empty_key () {
44+ throw std::invalid_argument (" HashMap: empty key cannot be used" );
45+ }
46+
47+ [[noreturn]] RIGTORP_HASHMAP_NOINLINE inline void throw_out_of_range () {
48+ throw std::out_of_range (" HashMap::at" );
49+ }
50+
51+ #undef RIGTORP_HASHMAP_NOINLINE
52+ } // namespace detail
53+
3554template <typename Key, typename T, typename Hash = std::hash<Key>,
3655 typename KeyEqual = std::equal_to<void >,
3756 typename Allocator = std::allocator<std::pair<Key, T>>>
@@ -231,7 +250,9 @@ class HashMap {
231250private:
232251 template <typename K, typename ... Args>
233252 std::pair<iterator, bool > emplace_impl (const K &key, Args &&...args) {
234- assert (!key_equal ()(empty_key_, key) && " empty key shouldn't be used" );
253+ if (key_equal ()(empty_key_, key)) [[unlikely]] {
254+ detail::throw_empty_key ();
255+ }
235256 reserve (size_ + 1 );
236257 for (size_t idx = key_to_idx (key);; idx = probe_next (idx)) {
237258 if (key_equal ()(buckets_[idx].first , empty_key_)) {
@@ -273,10 +294,10 @@ class HashMap {
273294
274295 template <typename K> mapped_type &at_impl (const K &key) {
275296 iterator it = find_impl (key);
276- if (it != end ()) {
297+ if (it != end ()) [[likely]] {
277298 return it->second ;
278299 }
279- throw std::out_of_range ( " HashMap::at " );
300+ detail::throw_out_of_range ( );
280301 }
281302
282303 template <typename K> const mapped_type &at_impl (const K &key) const {
@@ -288,7 +309,9 @@ class HashMap {
288309 }
289310
290311 template <typename K> iterator find_impl (const K &key) {
291- assert (!key_equal ()(empty_key_, key) && " empty key shouldn't be used" );
312+ if (key_equal ()(empty_key_, key)) [[unlikely]] {
313+ detail::throw_empty_key ();
314+ }
292315 for (size_t idx = key_to_idx (key);; idx = probe_next (idx)) {
293316 if (key_equal ()(buckets_[idx].first , key)) {
294317 return iterator (this , idx);
0 commit comments