Skip to content

Commit

Permalink
fix: Make memory_consumption() exclude lookup table
Browse files Browse the repository at this point in the history
  • Loading branch information
Tradias committed Oct 6, 2024
1 parent b45ad59 commit 5b4a056
Show file tree
Hide file tree
Showing 6 changed files with 13 additions and 22 deletions.
2 changes: 1 addition & 1 deletion src/cntgs/vector.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -271,7 +271,7 @@ class BasicContiguousVector<cntgs::Options<Option...>, 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}; }
Expand Down
4 changes: 2 additions & 2 deletions test/test-vector-alignment.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down Expand Up @@ -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")
Expand Down
4 changes: 2 additions & 2 deletions test/test-vector-erase.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<TwoVarying> 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);
}

Expand Down
4 changes: 3 additions & 1 deletion test/test-vector-special-member.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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()};
Expand Down
4 changes: 2 additions & 2 deletions test/test-vector.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,10 @@ TEST_CASE("ContiguousVector: one fixed one varying size: correct memory_consumpt
vector.emplace_back(std::initializer_list<uint16_t>{1, 2, 3}, 10, std::array{0.f, 0.1f, 0.2f});
vector.emplace_back(std::initializer_list<uint16_t>{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
}

Expand Down
17 changes: 3 additions & 14 deletions test/utils/check.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 <class Options, class... Parameter>
std::size_t contiguous_memory_consumption(const cntgs::BasicContiguousVector<Options, Parameter...>& vector)
{
auto mem = vector.memory_consumption();
if constexpr ((test::IS_VARYING_SIZE<Parameter> || ...))
{
mem -= vector.size() * sizeof(std::size_t);
}
return mem;
}

template <class Options, class... Parameter>
std::size_t used_memory_size(const cntgs::BasicContiguousVector<Options, Parameter...>& vector,
std::size_t alignment = 1)
Expand All @@ -246,7 +235,7 @@ std::size_t used_memory_size(const cntgs::BasicContiguousVector<Options, Paramet
template <class Options, class... Parameter>
void check_all_memory_is_used(const cntgs::BasicContiguousVector<Options, Parameter...>& 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 <class Vector, std::size_t Alignment, bool IsSoft>
Expand All @@ -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());
}
}
};
Expand Down

0 comments on commit 5b4a056

Please sign in to comment.