@@ -25,13 +25,24 @@ 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+ [[noreturn]]
36+ #if defined(_MSC_VER)
37+ __declspec (noinline)
38+ #elif defined(__GNUC__) || defined(__clang__)
39+ __attribute__ ((noinline))
40+ #endif
41+ inline void throw_empty_key () {
42+ throw std::invalid_argument (" HashMap: empty key cannot be used" );
43+ }
44+ } // namespace detail
45+
3546template <typename Key, typename T, typename Hash = std::hash<Key>,
3647 typename KeyEqual = std::equal_to<void >,
3748 typename Allocator = std::allocator<std::pair<Key, T>>>
@@ -231,7 +242,9 @@ class HashMap {
231242private:
232243 template <typename K, typename ... Args>
233244 std::pair<iterator, bool > emplace_impl (const K &key, Args &&...args) {
234- assert (!key_equal ()(empty_key_, key) && " empty key shouldn't be used" );
245+ if (key_equal ()(empty_key_, key)) [[unlikely]] {
246+ detail::throw_empty_key ();
247+ }
235248 reserve (size_ + 1 );
236249 for (size_t idx = key_to_idx (key);; idx = probe_next (idx)) {
237250 if (key_equal ()(buckets_[idx].first , empty_key_)) {
@@ -288,7 +301,9 @@ class HashMap {
288301 }
289302
290303 template <typename K> iterator find_impl (const K &key) {
291- assert (!key_equal ()(empty_key_, key) && " empty key shouldn't be used" );
304+ if (key_equal ()(empty_key_, key)) [[unlikely]] {
305+ detail::throw_empty_key ();
306+ }
292307 for (size_t idx = key_to_idx (key);; idx = probe_next (idx)) {
293308 if (key_equal ()(buckets_[idx].first , key)) {
294309 return iterator (this , idx);
0 commit comments