From 6218702ec69bd00416c6246f6c4212b9ff96f420 Mon Sep 17 00:00:00 2001 From: Mathias Kraus Date: Mon, 23 Oct 2023 20:12:02 +0200 Subject: [PATCH] iox-#2044 Optimize 'size' method --- .../iox/detail/fixed_position_container.inl | 19 +++++-------------- .../include/iox/fixed_position_container.hpp | 1 + 2 files changed, 6 insertions(+), 14 deletions(-) diff --git a/iceoryx_dust/container/include/iox/detail/fixed_position_container.inl b/iceoryx_dust/container/include/iox/detail/fixed_position_container.inl index 1959638c75d..925ca96d775 100644 --- a/iceoryx_dust/container/include/iox/detail/fixed_position_container.inl +++ b/iceoryx_dust/container/include/iox/detail/fixed_position_container.inl @@ -69,6 +69,7 @@ inline void FixedPositionContainer::clear() noexcept } m_slots[Index::LAST].next = Index::INVALID; + m_size = 0; m_begin_free = Index::FIRST; m_begin_used = Index::INVALID; } @@ -146,6 +147,7 @@ FixedPositionContainer::emplace(Targs&&... args) noexcept new (&m_data[index]) T(std::forward(args)...); m_slots[index].status = SlotStatus::USED; + ++m_size; if (index < m_begin_used) { @@ -256,6 +258,7 @@ FixedPositionContainer::erase(const IndexType index) noexcept m_data[index].~T(); m_slots[index].status = SlotStatus::FREE; + --m_size; auto next_used = m_slots[index].next; bool is_removed_from_used_list{false}; @@ -347,7 +350,7 @@ FixedPositionContainer::erase(ConstIterator it) noexcept template inline bool FixedPositionContainer::empty() const noexcept { - return m_begin_used >= Index::INVALID; + return m_size == 0; } template @@ -359,19 +362,7 @@ inline bool FixedPositionContainer::full() const noexcept template inline uint64_t FixedPositionContainer::size() const noexcept { - uint64_t count{0}; - IndexType pos = m_begin_used; - // use a for loop to abort at CAPACITY iterations in case the container is corrupted - for (IndexType i = 0; i < CAPACITY; ++i) - { - if (pos > Index::LAST) - { - break; - } - ++count; - pos = m_slots[pos].next; - } - return count; + return m_size; } template diff --git a/iceoryx_dust/container/include/iox/fixed_position_container.hpp b/iceoryx_dust/container/include/iox/fixed_position_container.hpp index 7c755f4b17c..b8ff9dc55a3 100644 --- a/iceoryx_dust/container/include/iox/fixed_position_container.hpp +++ b/iceoryx_dust/container/include/iox/fixed_position_container.hpp @@ -327,6 +327,7 @@ class FixedPositionContainer final private: UninitializedArray m_data; UninitializedArray m_slots; + IndexType m_size{0}; IndexType m_begin_free{Index::FIRST}; IndexType m_begin_used{Index::INVALID}; };