diff --git a/CHANGELOG.md b/CHANGELOG.md index 1f898e2e..c53edc8f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,6 +10,7 @@ Version 200 * Add const and mutable buffer sequence traits * Add buffers_iterator_type trait * Use new buffer traits, remove old unused traits +* Optimize for size on buffers_cat preconditions API Changes: diff --git a/include/boost/beast/core/impl/buffers_cat.hpp b/include/boost/beast/core/impl/buffers_cat.hpp index 0dd04858..1e729a8f 100644 --- a/include/boost/beast/core/impl/buffers_cat.hpp +++ b/include/boost/beast/core/impl/buffers_cat.hpp @@ -14,7 +14,6 @@ #include #include #include -#include #include #include #include @@ -24,6 +23,45 @@ namespace boost { namespace beast { +#if defined(_MSC_VER) && ! defined(__clang__) +# define BOOST_BEAST_UNREACHABLE() __assume(false) +# define BOOST_BEAST_UNREACHABLE_RETURN(v) __assume(false) +#else +# define BOOST_BEAST_UNREACHABLE() __builtin_unreachable() +# define BOOST_BEAST_UNREACHABLE_RETURN(v) \ + do { __builtin_unreachable(); return v; } while(false) +#endif + +#ifdef BOOST_BEAST_TESTS + +#define BOOST_BEAST_LOGIC_ERROR(s) \ + do { \ + BOOST_THROW_EXCEPTION(std::logic_error((s))); \ + BOOST_BEAST_UNREACHABLE(); \ + } while(false) + +#define BOOST_BEAST_LOGIC_ERROR_RETURN(v, s) \ + do { \ + BOOST_THROW_EXCEPTION(std::logic_error(s)); \ + BOOST_BEAST_UNREACHABLE_RETURN(v); \ + } while(false) + +#else + +#define BOOST_BEAST_LOGIC_ERROR(s) \ + do { \ + BOOST_ASSERT_MSG(false, s); \ + BOOST_BEAST_UNREACHABLE(); \ + } while(false) + +#define BOOST_BEAST_LOGIC_ERROR_RETURN(v, s) \ + do { \ + BOOST_ASSERT_MSG(false, (s)); \ + BOOST_BEAST_UNREACHABLE_RETURN(v); \ + } while(false) + +#endif + namespace detail { struct buffers_cat_view_iterator_base @@ -35,9 +73,8 @@ struct buffers_cat_view_iterator_base net::mutable_buffer operator*() const { - // Dereferencing a one-past-the-end iterator - BOOST_THROW_EXCEPTION(std::logic_error{ - "invalid iterator"}); + BOOST_BEAST_LOGIC_ERROR_RETURN({}, + "Dereferencing a one-past-the-end iterator"); } operator bool() const noexcept @@ -115,13 +152,11 @@ private: { const_iterator const& self; - [[noreturn]] reference operator()(mp11::mp_size_t<0>) { - // Dereferencing a default-constructed iterator - BOOST_THROW_EXCEPTION(std::logic_error{ - "invalid iterator"}); + BOOST_BEAST_LOGIC_ERROR_RETURN({}, + "Dereferencing a default-constructed iterator"); } template @@ -135,13 +170,11 @@ private: { const_iterator& self; - [[noreturn]] void operator()(mp11::mp_size_t<0>) { - // Incrementing a default-constructed iterator - BOOST_THROW_EXCEPTION(std::logic_error{ - "invalid iterator"}); + BOOST_BEAST_LOGIC_ERROR( + "Incrementing a default-constructed iterator"); } template @@ -198,13 +231,11 @@ private: self.it_.template emplace(); } - [[noreturn]] void operator()(mp11::mp_size_t) { - // Incrementing a one-past-the-end iterator - BOOST_THROW_EXCEPTION(std::logic_error{ - "invalid iterator"}); + BOOST_BEAST_LOGIC_ERROR( + "Incrementing a one-past-the-end iterator"); } }; @@ -212,13 +243,11 @@ private: { const_iterator& self; - [[noreturn]] void operator()(mp11::mp_size_t<0>) { - // Decrementing a default-constructed iterator - BOOST_THROW_EXCEPTION(std::logic_error{ - "invalid iterator"}); + BOOST_BEAST_LOGIC_ERROR( + "Decrementing a default-constructed iterator"); } void @@ -232,9 +261,8 @@ private: if(it == net::buffer_sequence_begin( detail::get(*self.bn_))) { - // Decrementing an iterator to the beginning - BOOST_THROW_EXCEPTION(std::logic_error{ - "invalid iterator"}); + BOOST_BEAST_LOGIC_ERROR( + "Decrementing an iterator to the beginning"); } --it; if(net::const_buffer(*it).size() > 0) diff --git a/test/Jamfile b/test/Jamfile index bb7a9c2d..ebe05f33 100644 --- a/test/Jamfile +++ b/test/Jamfile @@ -22,6 +22,7 @@ project /boost/beast/test ] ./extern BOOST_BEAST_ALLOW_DEPRECATED + BOOST_BEAST_TESTS BOOST_ASIO_SEPARATE_COMPILATION ; diff --git a/test/beast/CMakeLists.txt b/test/beast/CMakeLists.txt index bcc6d851..00a181cc 100644 --- a/test/beast/CMakeLists.txt +++ b/test/beast/CMakeLists.txt @@ -8,6 +8,8 @@ # add_definitions (-DBOOST_BEAST_ALLOW_DEPRECATED) +add_definitions (-DBOOST_BEAST_TESTS) + add_subdirectory (core) add_subdirectory (experimental) diff --git a/test/beast/Jamfile b/test/beast/Jamfile index 839426ae..b557f011 100644 --- a/test/beast/Jamfile +++ b/test/beast/Jamfile @@ -14,18 +14,18 @@ alias run-tests : [ compile websocket.cpp ] [ compile zlib.cpp ] core//run-tests - experimental//run-tests http//run-tests websocket//run-tests zlib//run-tests + experimental//run-tests ; alias fat-tests : core//fat-tests - experimental//fat-tests http//fat-tests websocket//fat-tests zlib//fat-tests + experimental//fat-tests ; explicit fat-tests ; diff --git a/test/beast/core/buffers_cat.cpp b/test/beast/core/buffers_cat.cpp index 73a4c05a..266e60a9 100644 --- a/test/beast/core/buffers_cat.cpp +++ b/test/beast/core/buffers_cat.cpp @@ -119,26 +119,27 @@ public: net::const_buffer b3{" world!", 7}; auto const b = beast::buffers_cat(b1, b2, b3); + using type = decltype(b); // Dereferencing a default-constructed iterator checkException( [] { - *(decltype(b)::const_iterator{}); + *(type::const_iterator{}); }); // Incrementing a default-constructed iterator checkException( [] { - ++(decltype(b)::const_iterator{}); + ++(type::const_iterator{}); }); // Decrementing a default-constructed iterator checkException( [] { - --(decltype(b)::const_iterator{}); + --(type::const_iterator{}); }); // Decrementing an iterator to the beginning @@ -270,11 +271,10 @@ public: net::const_buffer{"lo", 2}, e1, net::const_buffer{", ", 2} }}; - auto b3 = std::array{ + auto b3 = std::array{{ net::const_buffer{"w", 1}, net::const_buffer{"orld!", 5}, - e1 - }; + e1 }}; { auto const b = beast::buffers_cat( e2, b1, e2, b2, e2, b3, e2);