From c002215f3a1ee8a95a13342621213fc38d7a30a4 Mon Sep 17 00:00:00 2001 From: Erik Rigtorp Date: Sat, 14 Nov 2026 12:34:43 -0500 Subject: [PATCH] Use noinline throw helpers for empty key and out-of-range errors --- README.md | 2 +- include/rigtorp/HashMap.h | 33 ++++++++++++++++++++++++++++----- src/HashMapTest.cpp | 25 +++++++++++++++++++++++++ 3 files changed, 54 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index eee86b3..f032f5f 100644 --- a/README.md +++ b/README.md @@ -26,7 +26,7 @@ main differences are: - A key value to represent the empty key is required. - `Key` and `T` needs to be default constructible. - Iterators are invalidated on all modifying operations. -- It's invalid to perform any operations with the empty key. +- Performing operations with the empty key throws `std::invalid_argument`. - Destructors are not called on `erase`. - Extensions for lookups using related key types. diff --git a/include/rigtorp/HashMap.h b/include/rigtorp/HashMap.h index 48b3a3e..3ed6396 100644 --- a/include/rigtorp/HashMap.h +++ b/include/rigtorp/HashMap.h @@ -25,13 +25,32 @@ probing. #pragma once #include -#include #include #include #include namespace rigtorp { +namespace detail { +#if defined(_MSC_VER) +#define RIGTORP_HASHMAP_NOINLINE __declspec(noinline) +#elif defined(__GNUC__) || defined(__clang__) +#define RIGTORP_HASHMAP_NOINLINE __attribute__((noinline)) +#else +#define RIGTORP_HASHMAP_NOINLINE +#endif + +[[noreturn]] RIGTORP_HASHMAP_NOINLINE inline void throw_empty_key() { + throw std::invalid_argument("HashMap: empty key cannot be used"); +} + +[[noreturn]] RIGTORP_HASHMAP_NOINLINE inline void throw_out_of_range() { + throw std::out_of_range("HashMap::at"); +} + +#undef RIGTORP_HASHMAP_NOINLINE +} // namespace detail + template , typename KeyEqual = std::equal_to, typename Allocator = std::allocator>> @@ -231,7 +250,9 @@ class HashMap { private: template std::pair emplace_impl(const K &key, Args &&...args) { - assert(!key_equal()(empty_key_, key) && "empty key shouldn't be used"); + if (key_equal()(empty_key_, key)) [[unlikely]] { + detail::throw_empty_key(); + } reserve(size_ + 1); for (size_t idx = key_to_idx(key);; idx = probe_next(idx)) { if (key_equal()(buckets_[idx].first, empty_key_)) { @@ -273,10 +294,10 @@ class HashMap { template mapped_type &at_impl(const K &key) { iterator it = find_impl(key); - if (it != end()) { + if (it != end()) [[likely]] { return it->second; } - throw std::out_of_range("HashMap::at"); + detail::throw_out_of_range(); } template const mapped_type &at_impl(const K &key) const { @@ -288,7 +309,9 @@ class HashMap { } template iterator find_impl(const K &key) { - assert(!key_equal()(empty_key_, key) && "empty key shouldn't be used"); + if (key_equal()(empty_key_, key)) [[unlikely]] { + detail::throw_empty_key(); + } for (size_t idx = key_to_idx(key);; idx = probe_next(idx)) { if (key_equal()(buckets_[idx].first, key)) { return iterator(this, idx); diff --git a/src/HashMapTest.cpp b/src/HashMapTest.cpp index daa8427..2c24db2 100644 --- a/src/HashMapTest.cpp +++ b/src/HashMapTest.cpp @@ -546,6 +546,31 @@ int main(int argc, char *argv[]) { EXPECT(chm.bucket_count() == 32); } + // Empty key throws std::invalid_argument + { + HashMap hm(16, 0); + const auto &chm = hm; + auto throws_invalid_arg = [](auto &&fn) { + try { + fn(); + } catch (const std::invalid_argument &) { + return true; + } catch (...) { + return false; + } + return false; + }; + EXPECT(throws_invalid_arg([&]() { hm.insert({0, 1}); })); + EXPECT(throws_invalid_arg([&]() { hm.emplace(0, 1); })); + EXPECT(throws_invalid_arg([&]() { hm[0] = 1; })); + EXPECT(throws_invalid_arg([&]() { hm.find(0); })); + EXPECT(throws_invalid_arg([&]() { chm.find(0); })); + EXPECT(throws_invalid_arg([&]() { hm.count(0); })); + EXPECT(throws_invalid_arg([&]() { hm.at(0); })); + EXPECT(throws_invalid_arg([&]() { chm.at(0); })); + EXPECT(throws_invalid_arg([&]() { hm.erase(0); })); + } + if (!ok) { fprintf(stderr, "FAILED!\n"); }