Skip to content

Truly independent web browser

LibWeb: Disarm the event loop's timer when no tasks are left

Andreas Kling336052433d16
diff --git a/Libraries/LibWeb/HTML/EventLoop/EventLoop.cpp b/Libraries/LibWeb/HTML/EventLoop/EventLoop.cpp
index 4226d653d25..01b51f54e94 100644
--- a/Libraries/LibWeb/HTML/EventLoop/EventLoop.cpp
+++ b/Libraries/LibWeb/HTML/EventLoop/EventLoop.cpp
@@ -104,6 +104,12 @@ void EventLoop::visit_edges(Visitor& visitor)
}
void EventLoop::schedule()
+{
+ m_turn_requested_without_a_task = true;
+ schedule_for_queued_task();
+}
+
+void EventLoop::schedule_for_queued_task()
{
if (!m_system_event_loop_timer) {
m_system_event_loop_timer = Platform::Timer::create_single_shot(GC::Heap::the(), AK::Duration::from_milliseconds(0), GC::create_function(GC::Heap::the(), [this] {
@@ -169,6 +175,8 @@ void EventLoop::spin_until(GC::Ref<GC::Function<bool()>> goal_condition)
void EventLoop::process()
{
+ m_turn_requested_without_a_task = false;
+
if (execution_paused())
return;
@@ -189,8 +197,15 @@ void EventLoop::process()
}
// If there are eligible tasks in the queue, schedule a new round of processing. :^)
- if (has_runnable_tasks || (!m_microtask_queue.is_empty() && !m_performing_a_microtask_checkpoint))
+ if (has_runnable_tasks || (!m_microtask_queue.is_empty() && !m_performing_a_microtask_checkpoint)) {
schedule();
+ return;
+ }
+
+ // NB: A task that the turns above have run may have armed the timer for itself. What asked for a turn without
+ // queueing a task still gets one.
+ if (m_system_event_loop_timer && !m_turn_requested_without_a_task)
+ m_system_event_loop_timer->stop();
}
// https://html.spec.whatwg.org/multipage/webappapis.html#event-loop-processing-model
diff --git a/Libraries/LibWeb/HTML/EventLoop/EventLoop.h b/Libraries/LibWeb/HTML/EventLoop/EventLoop.h
index 5cbe774b43c..1ceb027c470 100644
--- a/Libraries/LibWeb/HTML/EventLoop/EventLoop.h
+++ b/Libraries/LibWeb/HTML/EventLoop/EventLoop.h
@@ -141,6 +141,7 @@ public:
u64 task_generation() const { return m_task_generation; }
void schedule();
+ void schedule_for_queued_task();
void perform_a_microtask_checkpoint();
@@ -285,6 +286,7 @@ private:
u64 m_task_generation_of_last_idle_callback { 0 };
bool m_idle_callback_requested_outside_idle_period { false };
bool m_may_have_idle_callbacks { false };
+ bool m_turn_requested_without_a_task { false };
// https://html.spec.whatwg.org/multipage/webappapis.html#performing-a-microtask-checkpoint
bool m_performing_a_microtask_checkpoint { false };
diff --git a/Libraries/LibWeb/HTML/EventLoop/TaskQueue.cpp b/Libraries/LibWeb/HTML/EventLoop/TaskQueue.cpp
index 79997f2f0e9..f984a149861 100644
--- a/Libraries/LibWeb/HTML/EventLoop/TaskQueue.cpp
+++ b/Libraries/LibWeb/HTML/EventLoop/TaskQueue.cpp
@@ -64,7 +64,7 @@ void TaskQueue::add(GC::Ref<Task> task)
m_idle_tasks.append(*task);
else
m_tasks.append(*task);
- m_event_loop->schedule();
+ m_event_loop->schedule_for_queued_task();
}
// A task that is runnable, and that no rendering update in flight holds back.