From e7d60814fc538a1bd0591ca915274cca0a6106ae Mon Sep 17 00:00:00 2001 From: KRM7 <70973547+KRM7@users.noreply.github.com> Date: Tue, 30 Jan 2024 23:14:09 +0100 Subject: [PATCH] optimizations for non-polymorphic types --- src/small_unique_ptr.hpp | 15 +++++++-------- test/small_unique_ptr.cpp | 9 --------- 2 files changed, 7 insertions(+), 17 deletions(-) diff --git a/src/small_unique_ptr.hpp b/src/small_unique_ptr.hpp index 28292eb..62ff889 100644 --- a/src/small_unique_ptr.hpp +++ b/src/small_unique_ptr.hpp @@ -119,7 +119,7 @@ namespace detail constexpr bool is_stack_allocated() const noexcept { return static_cast(this->move_); } alignas(buffer_alignment_v) - mutable buffer_t buffer_; + mutable buffer_t buffer_ = {}; T* data_ = nullptr; move_fn move_ = nullptr; }; @@ -153,7 +153,7 @@ namespace detail constexpr bool is_stack_allocated() const noexcept { return !std::is_constant_evaluated() && (data_ == buffer()); } alignas(buffer_alignment_v) - mutable buffer_t buffer_; + mutable buffer_t buffer_ = {}; T* data_ = nullptr; }; @@ -168,6 +168,7 @@ namespace detail } // namespace detail + template class small_unique_ptr : private detail::small_unique_ptr_base { @@ -299,7 +300,7 @@ class small_unique_ptr : private detail::small_unique_ptr_base [[nodiscard]] constexpr bool is_stack_allocated() const noexcept { - return small_unique_ptr_base::is_stack_allocated(); + return small_unique_ptr::small_unique_ptr_base::is_stack_allocated(); } [[nodiscard]] @@ -357,13 +358,13 @@ class small_unique_ptr : private detail::small_unique_ptr_base } private: - template + template constexpr std::ptrdiff_t offsetof_base() const noexcept { if (!is_stack_allocated()) return 0; const auto derived_ptr = reinterpret_cast(this->buffer()); - const auto base_ptr = reinterpret_cast(static_cast(this->data_)); + const auto base_ptr = reinterpret_cast(static_cast(this->data_)); return base_ptr - derived_ptr; } @@ -372,8 +373,7 @@ class small_unique_ptr : private detail::small_unique_ptr_base friend class small_unique_ptr; template - friend constexpr small_unique_ptr make_unique_small(Args&&...) - noexcept(!detail::always_heap_allocated_v && std::is_nothrow_constructible_v); + friend constexpr small_unique_ptr make_unique_small(Args&&...); }; template @@ -397,7 +397,6 @@ namespace std template [[nodiscard]] constexpr small_unique_ptr make_unique_small(Args&&... args) -noexcept(!detail::always_heap_allocated_v && std::is_nothrow_constructible_v) { small_unique_ptr ptr; diff --git a/test/small_unique_ptr.cpp b/test/small_unique_ptr.cpp index 100004d..ea74c58 100644 --- a/test/small_unique_ptr.cpp +++ b/test/small_unique_ptr.cpp @@ -457,12 +457,3 @@ TEST_CASE("const_unique_ptr", "[small_unique_ptr]") REQUIRE(*p == 2); } - -TEST_CASE("noexcept_construct", "[small_unique_ptr]") -{ - STATIC_REQUIRE(noexcept(make_unique_small())); - STATIC_REQUIRE(!noexcept(make_unique_small())); - - STATIC_REQUIRE(noexcept(make_unique_small())); - STATIC_REQUIRE(!noexcept(make_unique_small())); -}