Skip to content

Commit 5373108

Browse files
committed
List: Deallocate items in clear() after 200,000
1 parent 7c0912f commit 5373108

File tree

1 file changed

+9
-1
lines changed

1 file changed

+9
-1
lines changed

include/scratchcpp/list.h

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,15 @@ class LIBSCRATCHCPP_EXPORT List : public Entity
6464
inline bool contains(const Value &value) const { return contains(value.data()); }
6565

6666
/*! Clears the list. */
67-
inline void clear() { m_size = 0; }
67+
inline void clear()
68+
{
69+
// Keep at least 200,000 items allocated if the list has more
70+
constexpr size_t limit = 200000;
71+
m_size = 0;
72+
73+
if (m_dataPtr->size() > limit)
74+
reserve(limit);
75+
}
6876

6977
/*! Appends an empty item and returns the reference to it. Can be used for custom initialization. */
7078
inline ValueData &appendEmpty()

0 commit comments

Comments
 (0)