fix(util/cancellable): fix quadratic runtime with many children

This commit is contained in:
Gabriel Ebner 2017-03-11 12:48:37 +01:00
parent d26e870aa5
commit 7036bec999
2 changed files with 8 additions and 3 deletions

View file

@ -27,9 +27,13 @@ void cancellation_token_cell::cancel(std::shared_ptr<cancellable> const &) {
void cancellation_token_cell::gc() {
unique_lock<mutex> lock(m_mutex);
m_children.erase(std::remove_if(m_children.begin(), m_children.end(),
[] (std::weak_ptr<cancellable> 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<cancellable> const &c) { return c.expired(); }),
m_children.end());
m_expired_children = 0;
}
}
void cancellation_token_cell::add_child(std::weak_ptr<cancellable> const & c) {

View file

@ -25,6 +25,7 @@ class cancellation_token_cell : public cancellable {
mutex m_mutex;
atomic_bool m_cancelled;
std::vector<std::weak_ptr<cancellable>> m_children;
unsigned m_expired_children = 0;
cancellation_token m_parent;
friend cancellation_token mk_cancellation_token(cancellation_token const &);