Skip to content

Commit

Permalink
optimizations for non-polymorphic types
Browse files Browse the repository at this point in the history
  • Loading branch information
KRM7 committed Jan 30, 2024
1 parent 9cf29cc commit 5ee4b8c
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 19 deletions.
3 changes: 3 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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 "$<$<CXX_COMPILER_ID:MSVC>:-Zc:preprocessor>" "$<$<CXX_COMPILER_ID:MSVC>:-Zc:__cplusplus>")

include(CTest)

enable_testing()
add_subdirectory("${CMAKE_CURRENT_SOURCE_DIR}/test")
15 changes: 7 additions & 8 deletions src/small_unique_ptr.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ namespace detail
constexpr bool is_stack_allocated() const noexcept { return static_cast<bool>(this->move_); }

alignas(buffer_alignment_v<T>)
mutable buffer_t buffer_;
mutable buffer_t buffer_ = {};
T* data_ = nullptr;
move_fn move_ = nullptr;
};
Expand Down Expand Up @@ -153,7 +153,7 @@ namespace detail
constexpr bool is_stack_allocated() const noexcept { return !std::is_constant_evaluated() && (data_ == buffer()); }

alignas(buffer_alignment_v<T>)
mutable buffer_t buffer_;
mutable buffer_t buffer_ = {};
T* data_ = nullptr;
};

Expand All @@ -168,6 +168,7 @@ namespace detail

} // namespace detail


template<typename T>
class small_unique_ptr : private detail::small_unique_ptr_base<T>
{
Expand Down Expand Up @@ -299,7 +300,7 @@ class small_unique_ptr : private detail::small_unique_ptr_base<T>
[[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]]
Expand Down Expand Up @@ -357,13 +358,13 @@ class small_unique_ptr : private detail::small_unique_ptr_base<T>
}

private:
template<typename B = T>
template<typename Base = T>
constexpr std::ptrdiff_t offsetof_base() const noexcept
{
if (!is_stack_allocated()) return 0;

const auto derived_ptr = reinterpret_cast<const volatile unsigned char*>(this->buffer());
const auto base_ptr = reinterpret_cast<const volatile unsigned char*>(static_cast<const volatile B*>(this->data_));
const auto base_ptr = reinterpret_cast<const volatile unsigned char*>(static_cast<const volatile Base*>(this->data_));

return base_ptr - derived_ptr;
}
Expand All @@ -372,8 +373,7 @@ class small_unique_ptr : private detail::small_unique_ptr_base<T>
friend class small_unique_ptr;

template<typename U, typename... Args>
friend constexpr small_unique_ptr<U> make_unique_small(Args&&...)
noexcept(!detail::always_heap_allocated_v<U> && std::is_nothrow_constructible_v<U, Args...>);
friend constexpr small_unique_ptr<U> make_unique_small(Args&&...);
};

template<typename T>
Expand All @@ -397,7 +397,6 @@ namespace std

template<typename T, typename... Args>
[[nodiscard]] constexpr small_unique_ptr<T> make_unique_small(Args&&... args)
noexcept(!detail::always_heap_allocated_v<T> && std::is_nothrow_constructible_v<T, Args...>)
{
small_unique_ptr<T> ptr;

Expand Down
2 changes: 0 additions & 2 deletions test/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -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)
Expand Down
9 changes: 0 additions & 9 deletions test/small_unique_ptr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<SmallPOD>()));
STATIC_REQUIRE(!noexcept(make_unique_small<LargePOD>()));

STATIC_REQUIRE(noexcept(make_unique_small<SmallDerived>()));
STATIC_REQUIRE(!noexcept(make_unique_small<LargeDerived>()));
}

0 comments on commit 5ee4b8c

Please sign in to comment.