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 e7d6081
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 17 deletions.
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
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 e7d6081

Please sign in to comment.