From 5b4a056d6a46e4299fb95b77b11e20fe58fe0c0a Mon Sep 17 00:00:00 2001 From: Dennis Hezel Date: Sun, 6 Oct 2024 18:10:52 +0200 Subject: [PATCH] fix: Make memory_consumption() exclude lookup table --- src/cntgs/vector.hpp | 2 +- test/test-vector-alignment.cpp | 4 ++-- test/test-vector-erase.cpp | 4 ++-- test/test-vector-special-member.cpp | 4 +++- test/test-vector.cpp | 4 ++-- test/utils/check.hpp | 17 +++-------------- 6 files changed, 13 insertions(+), 22 deletions(-) diff --git a/src/cntgs/vector.hpp b/src/cntgs/vector.hpp index 360bfc8..d67f8a3 100644 --- a/src/cntgs/vector.hpp +++ b/src/cntgs/vector.hpp @@ -271,7 +271,7 @@ class BasicContiguousVector, Parameter...> [[nodiscard]] constexpr size_type memory_consumption() const noexcept { - return memory_.size() * alignof(StorageElementType) + locator_->memory_size(); + return memory_.size() * alignof(StorageElementType); } [[nodiscard]] constexpr iterator begin() noexcept { return iterator{*this}; } diff --git a/test/test-vector-alignment.cpp b/test/test-vector-alignment.cpp index 32027aa..0af6032 100644 --- a/test/test-vector-alignment.cpp +++ b/test/test-vector-alignment.cpp @@ -117,7 +117,7 @@ TEST_CASE("ContiguousVector: TwoVaryingAligned emplace_back() and subscript oper check_alignment(b, 8); check_alignment(c, 16); } - CHECK_EQ(352, test::contiguous_memory_consumption(vector)); + CHECK_EQ(352, vector.memory_consumption()); } TEST_CASE("ContiguousVector: OneFixedAligned emplace_back() and subscript operator") @@ -233,7 +233,7 @@ TEST_CASE("ContiguousVector: Larger alignment after VaryingSize") check_alignment(a, 8); check_alignment(&c, 16); } - CHECK_EQ(272, test::contiguous_memory_consumption(vector)); + CHECK_EQ(272, vector.memory_consumption()); } TEST_CASE("ContiguousVector: Larger alignment after VaryingSize with matching trailing alignment") diff --git a/test/test-vector-erase.cpp b/test/test-vector-erase.cpp index 964eef7..accbab5 100644 --- a/test/test-vector-erase.cpp +++ b/test/test-vector-erase.cpp @@ -62,14 +62,14 @@ TEST_CASE("ContiguousVector: TwoVarying erase(Iterator)") vector.emplace_back(20u, FLOATS2_ALT, FLOATS1); vector.emplace_back(30u, FLOATS1, FLOATS2_ALT); vector.emplace_back(40u, FLOATS2, FLOATS2_ALT); - CHECK_EQ(224, test::contiguous_memory_consumption(vector)); + CHECK_EQ(224, vector.memory_consumption()); auto it = vector.erase(++vector.begin()); CHECK_EQ(++vector.begin(), it); CheckedSoft vector2{3, 16 * sizeof(float)}; vector2.emplace_back(10u, FLOATS1, FLOATS2); vector2.emplace_back(30u, FLOATS1, FLOATS2_ALT); vector2.emplace_back(40u, FLOATS2, FLOATS2_ALT); - CHECK_EQ(168, test::contiguous_memory_consumption(vector2)); + CHECK_EQ(168, vector2.memory_consumption()); CHECK_EQ(vector2, vector); } diff --git a/test/test-vector-special-member.cpp b/test/test-vector-special-member.cpp index 121357e..b8d0932 100644 --- a/test/test-vector-special-member.cpp +++ b/test/test-vector-special-member.cpp @@ -47,13 +47,15 @@ TEST_CASE("ContiguousVector: OneVaryingUniquePtr with polymorphic_allocator move check_expected(vector2); } TestPmrMemoryResource resource2; +#ifdef NDEBUG SUBCASE("move into smaller vector") { decltype(vector) vector2{0, 0, resource2.get_allocator()}; vector2 = std::move(vector); - resource2.check_was_used(vector2.get_allocator()); + resource2.check_was_not_used(vector2.get_allocator()); check_expected(vector2); } +#endif SUBCASE("move into larger vector") { decltype(vector) vector2{3, 10, resource2.get_allocator()}; diff --git a/test/test-vector.cpp b/test/test-vector.cpp index a7d8ae6..7ea28d1 100644 --- a/test/test-vector.cpp +++ b/test/test-vector.cpp @@ -42,10 +42,10 @@ TEST_CASE("ContiguousVector: one fixed one varying size: correct memory_consumpt vector.emplace_back(std::initializer_list{1, 2, 3}, 10, std::array{0.f, 0.1f, 0.2f}); vector.emplace_back(std::initializer_list{4, 5, 6}, 11, std::array{0.3f, 0.4f, 0.5f}); const auto size = 3 * sizeof(uint16_t) + sizeof(uint32_t) + 6 + sizeof(std::size_t); - const auto expected = element_count * (size + 4) + varying_byte_count + element_count * sizeof(std::size_t); + const auto expected = element_count * (size + 4) + varying_byte_count; CHECK_EQ(expected, vector.memory_consumption()); #ifdef NDEBUG - CHECK_EQ(expected, resource.bytes_allocated); + CHECK_EQ(expected + element_count * sizeof(std::size_t), resource.bytes_allocated); #endif } diff --git a/test/utils/check.hpp b/test/utils/check.hpp index ff752e3..97364ae 100644 --- a/test/utils/check.hpp +++ b/test/utils/check.hpp @@ -225,17 +225,6 @@ void check_greater_equal(Vector& vector, LhsTransformer lhs_transformer, RhsTran CHECK_GE(lhs_transformer(std::as_const(vector)[3]), rhs_transformer(std::as_const(vector)[2])); } -template -std::size_t contiguous_memory_consumption(const cntgs::BasicContiguousVector& vector) -{ - auto mem = vector.memory_consumption(); - if constexpr ((test::IS_VARYING_SIZE || ...)) - { - mem -= vector.size() * sizeof(std::size_t); - } - return mem; -} - template std::size_t used_memory_size(const cntgs::BasicContiguousVector& vector, std::size_t alignment = 1) @@ -246,7 +235,7 @@ std::size_t used_memory_size(const cntgs::BasicContiguousVector void check_all_memory_is_used(const cntgs::BasicContiguousVector& vector, std::size_t alignment) { - CHECK_EQ(test::used_memory_size(vector, alignment), test::contiguous_memory_consumption(vector)); + CHECK_EQ(test::used_memory_size(vector, alignment), vector.memory_consumption()); } template @@ -258,11 +247,11 @@ struct BasicChecked : Vector { if constexpr (IsSoft) { - CHECK_LE(test::used_memory_size(*this, Alignment), test::contiguous_memory_consumption(*this)); + CHECK_LE(test::used_memory_size(*this, Alignment), this->memory_consumption()); } else { - CHECK_EQ(test::used_memory_size(*this, Alignment), test::contiguous_memory_consumption(*this)); + CHECK_EQ(test::used_memory_size(*this, Alignment), this->memory_consumption()); } } };