Skip to content

Truly independent web browser

LibWeb: Start an idle period right away for a new idle callback

Andreas Kling27c7e8bacc13
diff --git a/Libraries/LibWeb/HTML/EventLoop/EventLoop.cpp b/Libraries/LibWeb/HTML/EventLoop/EventLoop.cpp
index 792c162b66c..ea85c3742da 100644
--- a/Libraries/LibWeb/HTML/EventLoop/EventLoop.cpp
+++ b/Libraries/LibWeb/HTML/EventLoop/EventLoop.cpp
@@ -277,7 +277,11 @@ void EventLoop::process()
}
// 5. If this is a window event loop that has no runnable task in this event loop's task queues, then:
- if (m_type == Type::Window && !m_task_queue->has_runnable_tasks() && (!m_idle_period_timer || !m_idle_period_timer->is_active())) {
+ // NB: A callback that asks for the next one through a task still waits, as a page would otherwise spin while idle.
+ static constexpr double minimum_time_between_idle_periods_in_ms = 1;
+ bool idle_period_may_start = !m_idle_period_timer || !m_idle_period_timer->is_active()
+ || (m_idle_callback_requested_outside_idle_period && HighResolutionTime::unsafe_shared_current_time() - m_last_idle_period_start_time >= minimum_time_between_idle_periods_in_ms);
+ if (m_type == Type::Window && !m_task_queue->has_runnable_tasks() && idle_period_may_start) {
auto windows = same_loop_windows();
bool has_idle_callbacks = false;
for (auto& window : windows) {
@@ -289,7 +293,9 @@ void EventLoop::process()
if (has_idle_callbacks) {
// NB: Delay the next idle period until this one's 50 ms budget has elapsed. Callbacks registered
// during an idle period belong to the next one; immediately starting it lets self-scheduling
- // callbacks spin continuously even when they have no work to do.
+ // callbacks spin continuously even when they have no work to do. A callback requested from
+ // anything but an idle callback starts an idle period right away (see did_request_idle_callback()).
+ m_idle_callback_requested_outside_idle_period = false;
if (!m_idle_period_timer) {
m_idle_period_timer = Platform::Timer::create_single_shot(GC::Heap::the(), AK::Duration::from_milliseconds(50), GC::create_function(GC::Heap::the(), [this] {
schedule();
@@ -1937,6 +1943,15 @@ Vector<GC::Root<HTML::Window>> EventLoop::same_loop_windows() const
return windows;
}
+// NB: A page that asks for an idle callback from a task or a rendering update does not keep the event loop busy by
+// doing so, so its callback need not wait for the last idle period's 50 ms to run out. One that an idle callback
+// or a reaction to it asks for does wait, or the two would run back to back.
+void EventLoop::did_request_idle_callback()
+{
+ if (m_task_generation != m_task_generation_of_last_idle_callback)
+ m_idle_callback_requested_outside_idle_period = true;
+}
+
// https://html.spec.whatwg.org/multipage/webappapis.html#event-loop-processing-model:last-idle-period-start-time
double EventLoop::compute_deadline() const
{
diff --git a/Libraries/LibWeb/HTML/EventLoop/EventLoop.h b/Libraries/LibWeb/HTML/EventLoop/EventLoop.h
index 58b9c776308..220ae00d53c 100644
--- a/Libraries/LibWeb/HTML/EventLoop/EventLoop.h
+++ b/Libraries/LibWeb/HTML/EventLoop/EventLoop.h
@@ -174,6 +174,9 @@ public:
double compute_deadline() const;
+ void did_run_idle_callback() { m_task_generation_of_last_idle_callback = m_task_generation; }
+ void did_request_idle_callback();
+
enum class UpdateTheRendering {
No,
Yes,
@@ -275,6 +278,8 @@ private:
GC::Ptr<Platform::Timer> m_system_event_loop_timer;
GC::Ptr<Platform::Timer> m_idle_period_timer;
+ u64 m_task_generation_of_last_idle_callback { 0 };
+ bool m_idle_callback_requested_outside_idle_period { 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/Window.cpp b/Libraries/LibWeb/HTML/Window.cpp
index f0c5ed65100..dd9b1057c6e 100644
--- a/Libraries/LibWeb/HTML/Window.cpp
+++ b/Libraries/LibWeb/HTML/Window.cpp
@@ -983,6 +983,7 @@ void Window::invoke_idle_callbacks()
// 2. Let deadlineArg be a new IdleDeadline whose [get deadline time algorithm] is getDeadline.
auto deadline_arg = RequestIdleCallback::IdleDeadline::create();
// 3. Call callback with deadlineArg as its argument. If an uncaught runtime script error occurs, then report the exception.
+ event_loop.did_run_idle_callback();
auto result = callback->invoke(deadline_arg);
if (result.is_error())
report_exception(result, principal_realm());
@@ -1012,6 +1013,7 @@ void Window::invoke_idle_callback_timeout(u32 handle)
auto callback = taken.release_value();
callback->clear_timeout_timer_id();
+ main_thread_event_loop().did_run_idle_callback();
// 2.1. Remove callback from both window's list of idle request callbacks and the list of runnable idle callbacks.
// NB: Taking the callback from whichever list contained it above implements this step, since it can only be in one.
@@ -2215,6 +2217,7 @@ u32 Window::request_idle_callback(IdleCallbackHandler callback, IdleRequestOptio
// 4. Push callback to the end of window's list of idle request callbacks, associated with handle.
auto idle_callback = GC::Heap::the().allocate<IdleCallback>(GC::create_function(GC::Heap::the(), move(callback)), handle);
m_idle_request_callbacks.set(handle, idle_callback);
+ main_thread_event_loop().did_request_idle_callback();
// 5. Return handle and then continue running this algorithm asynchronously.