From 50eb01115d7690724873f7ec039e4999792c0dea Mon Sep 17 00:00:00 2001 From: Gary Talent Date: Sat, 5 Feb 2022 01:33:08 -0600 Subject: [PATCH] [ox/std] Silently suppress small Vector in constexpr --- deps/ox/src/ox/std/vector.hpp | 37 ++++++++++++++++++----------------- 1 file changed, 19 insertions(+), 18 deletions(-) diff --git a/deps/ox/src/ox/std/vector.hpp b/deps/ox/src/ox/std/vector.hpp index db7396c1..5f5a581f 100644 --- a/deps/ox/src/ox/std/vector.hpp +++ b/deps/ox/src/ox/std/vector.hpp @@ -22,25 +22,25 @@ namespace ox { namespace detail { template -struct SmallVector { +struct VectorAllocator { private: std::allocator m_allocator; AllocAlias m_data[Size] = {}; - public: - SmallVector() noexcept = default; - SmallVector(SmallVector&) noexcept = default; - SmallVector(SmallVector&&) noexcept = default; + constexpr VectorAllocator() noexcept = default; + constexpr VectorAllocator(VectorAllocator&) noexcept = default; + constexpr VectorAllocator(VectorAllocator&&) noexcept = default; protected: constexpr void allocate(T **items, std::size_t cap) noexcept { - if (cap <= Size) { - *items = reinterpret_cast(m_data); - } else { + // small vector optimization cannot be done it constexpr, but it doesn't really matter in constexpr + if (std::is_constant_evaluated() || cap > Size) { *items = m_allocator.allocate(cap); + } else { + *items = reinterpret_cast(m_data); } } - constexpr void moveConstructItemsFrom(T **items, SmallVector &src, const std::size_t count, const std::size_t cap) noexcept { + constexpr void moveConstructItemsFrom(T **items, VectorAllocator &src, const std::size_t count, const std::size_t cap) noexcept { if (cap <= Size) { const auto dstItems = reinterpret_cast(m_data); const auto srcItems = reinterpret_cast(src.m_data); @@ -51,7 +51,7 @@ struct SmallVector { } } - constexpr void moveItemsFrom(T **items, SmallVector &src, const std::size_t count, const std::size_t cap) noexcept { + constexpr void moveItemsFrom(T **items, VectorAllocator &src, const std::size_t count, const std::size_t cap) noexcept { // this totally idiotic redundant check (&& count <= Size) is required to address a bug in devkitARM, // try removing it later if (cap <= Size && count <= Size) { @@ -65,7 +65,8 @@ struct SmallVector { } constexpr void deallocate(T *items, std::size_t cap) noexcept { - if (items && static_cast(items) != static_cast(m_data)) { + // small vector optimization cannot be done it constexpr, but it doesn't really matter in constexpr + if (std::is_constant_evaluated() || (items && static_cast(items) != static_cast(m_data))) { m_allocator.deallocate(items, cap); } } @@ -73,24 +74,24 @@ struct SmallVector { }; template -struct SmallVector { +struct VectorAllocator { private: std::allocator m_allocator; public: - SmallVector() noexcept = default; - SmallVector(SmallVector&) noexcept = default; - SmallVector(SmallVector&&) noexcept = default; + constexpr VectorAllocator() noexcept = default; + constexpr VectorAllocator(VectorAllocator&) noexcept = default; + constexpr VectorAllocator(VectorAllocator&&) noexcept = default; protected: constexpr void allocate(T **items, std::size_t cap) noexcept { *items = m_allocator.allocate(cap); } [[maybe_unused]] - constexpr void moveConstructItemsFrom(T**, SmallVector&, const std::size_t, const std::size_t) noexcept { + constexpr void moveConstructItemsFrom(T**, VectorAllocator&, const std::size_t, const std::size_t) noexcept { } [[maybe_unused]] - constexpr void moveItemsFrom(T**, SmallVector&, const std::size_t, const std::size_t) noexcept { + constexpr void moveItemsFrom(T**, VectorAllocator&, const std::size_t, const std::size_t) noexcept { } constexpr void deallocate(T *items, std::size_t cap) noexcept { @@ -104,7 +105,7 @@ struct SmallVector { } template -class Vector: detail::SmallVector { +class Vector: detail::VectorAllocator { public: using value_type = T;