From 478b36c37b887d6d33915738111e9063778ceab2 Mon Sep 17 00:00:00 2001 From: Andreas Kling Date: Sun, 3 Oct 2021 18:23:25 +0200 Subject: [PATCH] LibWeb: Only auto-reschedule HTML::EventLoop when there are runnables HTML::EventLoop tries to reschedule itself when there are more tasks in any of its queues, but let's not do it if none of them are runnable. --- Userland/Libraries/LibWeb/HTML/EventLoop/EventLoop.cpp | 2 +- Userland/Libraries/LibWeb/HTML/EventLoop/TaskQueue.cpp | 9 +++++++++ Userland/Libraries/LibWeb/HTML/EventLoop/TaskQueue.h | 2 ++ 3 files changed, 12 insertions(+), 1 deletion(-) diff --git a/Userland/Libraries/LibWeb/HTML/EventLoop/EventLoop.cpp b/Userland/Libraries/LibWeb/HTML/EventLoop/EventLoop.cpp index 41a52e5a53..129ff9174c 100644 --- a/Userland/Libraries/LibWeb/HTML/EventLoop/EventLoop.cpp +++ b/Userland/Libraries/LibWeb/HTML/EventLoop/EventLoop.cpp @@ -200,7 +200,7 @@ void EventLoop::process() // FIXME: 2. If there are no tasks in the event loop's task queues and the WorkerGlobalScope object's closing flag is true, then destroy the event loop, aborting these steps, resuming the run a worker steps described in the Web workers section below. // If there are tasks in the queue, schedule a new round of processing. :^) - if (!m_task_queue.is_empty() || !m_microtask_queue.is_empty()) + if (m_task_queue.has_runnable_tasks() || !m_microtask_queue.is_empty()) schedule(); } diff --git a/Userland/Libraries/LibWeb/HTML/EventLoop/TaskQueue.cpp b/Userland/Libraries/LibWeb/HTML/EventLoop/TaskQueue.cpp index 51fb07df29..8a773faa89 100644 --- a/Userland/Libraries/LibWeb/HTML/EventLoop/TaskQueue.cpp +++ b/Userland/Libraries/LibWeb/HTML/EventLoop/TaskQueue.cpp @@ -33,4 +33,13 @@ OwnPtr TaskQueue::take_first_runnable() return nullptr; } +bool TaskQueue::has_runnable_tasks() const +{ + for (auto& task : m_tasks) { + if (task->is_runnable()) + return true; + } + return false; +} + } diff --git a/Userland/Libraries/LibWeb/HTML/EventLoop/TaskQueue.h b/Userland/Libraries/LibWeb/HTML/EventLoop/TaskQueue.h index e7638b07bb..cfc2a15254 100644 --- a/Userland/Libraries/LibWeb/HTML/EventLoop/TaskQueue.h +++ b/Userland/Libraries/LibWeb/HTML/EventLoop/TaskQueue.h @@ -18,6 +18,8 @@ public: bool is_empty() const { return m_tasks.is_empty(); } + bool has_runnable_tasks() const; + void add(NonnullOwnPtr); OwnPtr take_first_runnable();