From 5ee4b8cac896f794f036225775ba9dcbf1024ced 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 --- CMakeLists.txt | 3 +++ src/small_unique_ptr.hpp | 15 +++++++-------- test/CMakeLists.txt | 2 -- test/small_unique_ptr.cpp | 9 --------- 4 files changed, 10 insertions(+), 19 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 2703840..52b0a9f 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -24,4 +24,7 @@ target_include_directories(small_unique_ptr SYSTEM INTERFACE "${CMAKE_CURRENT_SO target_compile_features(small_unique_ptr INTERFACE "cxx_std_20") target_compile_options(small_unique_ptr INTERFACE "$<$:-Zc:preprocessor>" "$<$:-Zc:__cplusplus>") +include(CTest) + +enable_testing() add_subdirectory("${CMAKE_CURRENT_SOURCE_DIR}/test") 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/CMakeLists.txt b/test/CMakeLists.txt index 82819cc..4a8faba 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -1,8 +1,6 @@ find_package(Catch2 3 REQUIRED) include(Catch) -include(CTest) -enable_testing() add_executable(small_unique_ptr_test "${CMAKE_CURRENT_SOURCE_DIR}/small_unique_ptr.cpp") target_link_libraries(small_unique_ptr_test PRIVATE Catch2::Catch2WithMain small_unique_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())); -}