diff --git a/src/util/cancellable.cpp b/src/util/cancellable.cpp index f3294e5797..84ab3481e3 100644 --- a/src/util/cancellable.cpp +++ b/src/util/cancellable.cpp @@ -27,9 +27,13 @@ void cancellation_token_cell::cancel(std::shared_ptr const &) { void cancellation_token_cell::gc() { unique_lock lock(m_mutex); - m_children.erase(std::remove_if(m_children.begin(), m_children.end(), - [] (std::weak_ptr const & c) { return c.expired(); }), - m_children.end()); + m_expired_children++; + if (m_expired_children > m_children.size()/2) { + m_children.erase(std::remove_if(m_children.begin(), m_children.end(), + [](std::weak_ptr const &c) { return c.expired(); }), + m_children.end()); + m_expired_children = 0; + } } void cancellation_token_cell::add_child(std::weak_ptr const & c) { diff --git a/src/util/cancellable.h b/src/util/cancellable.h index 2003640d78..d8ca6ab757 100644 --- a/src/util/cancellable.h +++ b/src/util/cancellable.h @@ -25,6 +25,7 @@ class cancellation_token_cell : public cancellable { mutex m_mutex; atomic_bool m_cancelled; std::vector> m_children; + unsigned m_expired_children = 0; cancellation_token m_parent; friend cancellation_token mk_cancellation_token(cancellation_token const &);