From 0d2751c5e1a99b1275717e26c16eb672150da77b Mon Sep 17 00:00:00 2001 From: Braden Ganetsky Date: Tue, 19 Dec 2023 10:08:08 -0600 Subject: [PATCH] Extract `at()`'s throw_exception calls into noinline function (#223) * Create function `detail::throw_out_of_range()` to make `at()` more inlineable * Replace `boost::throw_exception()` with `detail::throw_out_of_range()` --- .../unordered/detail/throw_exception.hpp | 30 +++++++++++++++++++ .../boost/unordered/unordered_flat_map.hpp | 18 +++++------ include/boost/unordered/unordered_map.hpp | 17 ++++++----- .../boost/unordered/unordered_node_map.hpp | 18 +++++------ 4 files changed, 57 insertions(+), 26 deletions(-) create mode 100644 include/boost/unordered/detail/throw_exception.hpp diff --git a/include/boost/unordered/detail/throw_exception.hpp b/include/boost/unordered/detail/throw_exception.hpp new file mode 100644 index 00000000..702f4be0 --- /dev/null +++ b/include/boost/unordered/detail/throw_exception.hpp @@ -0,0 +1,30 @@ +// Copyright (C) 2023 Braden Ganetsky +// Distributed under the Boost Software License, Version 1.0. (See accompanying +// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) + +#ifndef BOOST_UNORDERED_DETAIL_THROW_EXCEPTION_HPP +#define BOOST_UNORDERED_DETAIL_THROW_EXCEPTION_HPP + +#include +#if defined(BOOST_HAS_PRAGMA_ONCE) +#pragma once +#endif + +#include +#include + +namespace boost { + namespace unordered { + namespace detail { + + BOOST_NOINLINE BOOST_NORETURN inline void throw_out_of_range( + char const* message) + { + boost::throw_exception(std::out_of_range(message)); + } + + } // namespace detail + } // namespace unordered +} // namespace boost + +#endif // BOOST_UNORDERED_DETAIL_THROW_EXCEPTION_HPP diff --git a/include/boost/unordered/unordered_flat_map.hpp b/include/boost/unordered/unordered_flat_map.hpp index 5d565a31..41ce7081 100644 --- a/include/boost/unordered/unordered_flat_map.hpp +++ b/include/boost/unordered/unordered_flat_map.hpp @@ -14,12 +14,12 @@ #include #include #include +#include #include #include #include #include -#include #include #include @@ -459,8 +459,8 @@ namespace boost { // TODO: someday refactor this to conditionally serialize the key and // include it in the error message // - boost::throw_exception( - std::out_of_range("key was not found in unordered_flat_map")); + boost::unordered::detail::throw_out_of_range( + "key was not found in unordered_flat_map"); } mapped_type const& at(key_type const& key) const @@ -469,8 +469,8 @@ namespace boost { if (pos != table_.end()) { return pos->second; } - boost::throw_exception( - std::out_of_range("key was not found in unordered_flat_map")); + boost::unordered::detail::throw_out_of_range( + "key was not found in unordered_flat_map"); } template @@ -483,8 +483,8 @@ namespace boost { if (pos != table_.end()) { return pos->second; } - boost::throw_exception( - std::out_of_range("key was not found in unordered_flat_map")); + boost::unordered::detail::throw_out_of_range( + "key was not found in unordered_flat_map"); } template @@ -497,8 +497,8 @@ namespace boost { if (pos != table_.end()) { return pos->second; } - boost::throw_exception( - std::out_of_range("key was not found in unordered_flat_map")); + boost::unordered::detail::throw_out_of_range( + "key was not found in unordered_flat_map"); } BOOST_FORCEINLINE mapped_type& operator[](key_type const& key) diff --git a/include/boost/unordered/unordered_map.hpp b/include/boost/unordered/unordered_map.hpp index ba10a970..34f8ae53 100644 --- a/include/boost/unordered/unordered_map.hpp +++ b/include/boost/unordered/unordered_map.hpp @@ -16,6 +16,7 @@ #include #include +#include #include #include @@ -1586,8 +1587,8 @@ namespace boost { return p->value().second; } - boost::throw_exception( - std::out_of_range("Unable to find key in unordered_map.")); + boost::unordered::detail::throw_out_of_range( + "Unable to find key in unordered_map."); } template @@ -1602,8 +1603,8 @@ namespace boost { return p->value().second; } - boost::throw_exception( - std::out_of_range("Unable to find key in unordered_map.")); + boost::unordered::detail::throw_out_of_range( + "Unable to find key in unordered_map."); } template @@ -1620,8 +1621,8 @@ namespace boost { return p->value().second; } - boost::throw_exception( - std::out_of_range("Unable to find key in unordered_map.")); + boost::unordered::detail::throw_out_of_range( + "Unable to find key in unordered_map."); } template @@ -1638,8 +1639,8 @@ namespace boost { return p->value().second; } - boost::throw_exception( - std::out_of_range("Unable to find key in unordered_map.")); + boost::unordered::detail::throw_out_of_range( + "Unable to find key in unordered_map."); } template diff --git a/include/boost/unordered/unordered_node_map.hpp b/include/boost/unordered/unordered_node_map.hpp index 10c72cb4..b7a6a409 100644 --- a/include/boost/unordered/unordered_node_map.hpp +++ b/include/boost/unordered/unordered_node_map.hpp @@ -15,12 +15,12 @@ #include #include #include +#include #include #include #include #include -#include #include #include @@ -554,8 +554,8 @@ namespace boost { // TODO: someday refactor this to conditionally serialize the key and // include it in the error message // - boost::throw_exception( - std::out_of_range("key was not found in unordered_node_map")); + boost::unordered::detail::throw_out_of_range( + "key was not found in unordered_node_map"); } mapped_type const& at(key_type const& key) const @@ -564,8 +564,8 @@ namespace boost { if (pos != table_.end()) { return pos->second; } - boost::throw_exception( - std::out_of_range("key was not found in unordered_node_map")); + boost::unordered::detail::throw_out_of_range( + "key was not found in unordered_node_map"); } template @@ -578,8 +578,8 @@ namespace boost { if (pos != table_.end()) { return pos->second; } - boost::throw_exception( - std::out_of_range("key was not found in unordered_node_map")); + boost::unordered::detail::throw_out_of_range( + "key was not found in unordered_node_map"); } template @@ -592,8 +592,8 @@ namespace boost { if (pos != table_.end()) { return pos->second; } - boost::throw_exception( - std::out_of_range("key was not found in unordered_node_map")); + boost::unordered::detail::throw_out_of_range( + "key was not found in unordered_node_map"); } BOOST_FORCEINLINE mapped_type& operator[](key_type const& key)