Skip to content

Commit

Permalink
base/task/sequence_manager: Reduce canceled task sweep allocation ove…
Browse files Browse the repository at this point in the history
…rhead

To reduce the per-task allocation overhead, use a stack container for
holding swept cancelled tasks instead of always falling back to the
heap.

Bug: 1190643,1212886
Change-Id: I5871a25e52805316a9deea76d52c381175c49b1a
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2917000
Commit-Queue: Sami Kyöstilä <skyostil@chromium.org>
Commit-Queue: Alexander Timin <altimin@chromium.org>
Auto-Submit: Sami Kyöstilä <skyostil@chromium.org>
Reviewed-by: Alexander Timin <altimin@chromium.org>
Cr-Commit-Position: refs/heads/master@{#886445}
  • Loading branch information
skyostil authored and Chromium LUCI CQ committed May 25, 2021
1 parent ce8c8d3 commit 2963bae
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 8 deletions.
10 changes: 6 additions & 4 deletions base/task/sequence_manager/task_queue_impl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include <memory>
#include <utility>

#include "base/containers/stack_container.h"
#include "base/logging.h"
#include "base/ranges/algorithm.h"
#include "base/strings/stringprintf.h"
Expand Down Expand Up @@ -1391,15 +1392,16 @@ size_t TaskQueueImpl::DelayedIncomingQueue::PQueue::SweepCancelledTasks(
// move all the cancelled tasks into a temporary container before deleting
// them. This is to avoid |c| from changing while c.erase() is running.
auto delete_start = std::stable_partition(c.begin(), c.end(), keep_task);
std::vector<Task> tasks_to_delete;
std::move(delete_start, c.end(), std::back_inserter(tasks_to_delete));
StackVector<Task, 8> tasks_to_delete;
std::move(delete_start, c.end(),
std::back_inserter(tasks_to_delete.container()));
c.erase(delete_start, c.end());

// stable_partition ensures order was not changed if there was nothing to
// delete.
if (!tasks_to_delete.empty()) {
if (!tasks_to_delete->empty()) {
ranges::make_heap(c, comp);
tasks_to_delete.clear();
tasks_to_delete->clear();
}
return num_high_res_tasks_swept;
}
Expand Down
9 changes: 5 additions & 4 deletions base/task/sequence_manager/work_queue.cc
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

#include "base/task/sequence_manager/work_queue.h"

#include "base/containers/stack_container.h"
#include "base/debug/alias.h"
#include "base/task/sequence_manager/sequence_manager_impl.h"
#include "base/task/sequence_manager/work_queue_sets.h"
Expand Down Expand Up @@ -205,7 +206,7 @@ bool WorkQueue::RemoveAllCanceledTasksFromFront() {
// Since task destructors could have a side-effect of deleting this task queue
// we move cancelled tasks into a temporary container which can be emptied
// without accessing |this|.
std::vector<Task> tasks_to_delete;
StackVector<Task, 8> tasks_to_delete;

while (!tasks_.empty()) {
const auto& pending_task = tasks_.front();
Expand All @@ -232,10 +233,10 @@ bool WorkQueue::RemoveAllCanceledTasksFromFront() {
#endif // !defined(OS_NACL)
if (pending_task.task && !pending_task.task.IsCancelled())
break;
tasks_to_delete.push_back(std::move(tasks_.front()));
tasks_to_delete->push_back(std::move(tasks_.front()));
tasks_.pop_front();
}
if (!tasks_to_delete.empty()) {
if (!tasks_to_delete->empty()) {
if (tasks_.empty()) {
// NB delayed tasks are inserted via Push, no don't need to reload those.
if (queue_type_ == QueueType::kImmediate) {
Expand All @@ -253,7 +254,7 @@ bool WorkQueue::RemoveAllCanceledTasksFromFront() {
work_queue_sets_->OnQueuesFrontTaskChanged(this);
task_queue_->TraceQueueSize();
}
return !tasks_to_delete.empty();
return !tasks_to_delete->empty();
}

void WorkQueue::AssignToWorkQueueSets(WorkQueueSets* work_queue_sets) {
Expand Down

0 comments on commit 2963bae

Please sign in to comment.