Skip to content

Commit

Permalink
perf: use vector for dispatcher deferred deletion (#190)
Browse files Browse the repository at this point in the history
Using a linked list leads to repeated allocations for no reason.
A vector once large enough is stable and will not allocate anything.
  • Loading branch information
mattklein123 authored Nov 3, 2016
1 parent 4173452 commit 83c421c
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 5 deletions.
9 changes: 5 additions & 4 deletions source/common/event/dispatcher_impl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,14 @@ DispatcherImpl::DispatcherImpl()
DispatcherImpl::~DispatcherImpl() {}

void DispatcherImpl::clearDeferredDeleteList() {
while (!to_delete_.empty()) {
size_t index = 0;
while (index != to_delete_.size()) {
// The destructor of a deferred deletion item can yield more deferred deletion. Loop
// and destroy all of them until there is nothing left.
std::list<DeferredDeletablePtr> copy(std::move(to_delete_));
log_trace("clearing deferred deletion list (size={})", copy.size());
copy.clear();
log_trace("clearing deferred deletion list (index={})", index);
to_delete_[index++].reset();
}
to_delete_.clear();
}

Network::ClientConnectionPtr DispatcherImpl::createClientConnection(const std::string& url) {
Expand Down
2 changes: 1 addition & 1 deletion source/common/event/dispatcher_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ class DispatcherImpl : Logger::Loggable<Logger::Id::main>, public Dispatcher {

Libevent::BasePtr base_;
TimerPtr deferred_delete_timer_;
std::list<DeferredDeletablePtr> to_delete_;
std::vector<DeferredDeletablePtr> to_delete_;
std::mutex post_lock_;
std::list<std::function<void()>> post_callbacks_;
};
Expand Down

0 comments on commit 83c421c

Please sign in to comment.