Skip to content

Truly independent web browser

LibWeb: Let tasks of a loading document go ahead of rendering

Andreas Kling46b6ba945475
diff --git a/Libraries/LibWeb/HTML/EventLoop/EventLoop.cpp b/Libraries/LibWeb/HTML/EventLoop/EventLoop.cpp
index 01b51f54e94..58a355f0a55 100644
--- a/Libraries/LibWeb/HTML/EventLoop/EventLoop.cpp
+++ b/Libraries/LibWeb/HTML/EventLoop/EventLoop.cpp
@@ -249,7 +249,7 @@ bool EventLoop::run_one_turn_of_the_processing_model()
// AD-HOC: Beside an idle event loop, whose rendering updates run a document's animations, the render clock's lanes
// only hover what is under the pointer.
- if (!oldest_task)
+ if (!oldest_task && !task_queue->has_runnable_tasks())
note_clock_lanes(ClockAnimations::Hold);
if (oldest_task) {
@@ -438,6 +438,7 @@ bool EventLoop::rendering_opportunity(HighResolutionTime::DOMHighResTimeStamp fr
// opportunity instead of one global task per page local root.
VERIFY(!m_rendering_task_queued);
m_rendering_task_queued = true;
+ m_rendering_task_queued_time = MonotonicTime::now();
queue_a_task(Task::Source::Rendering, this, nullptr, *m_rendering_task_function);
++m_rendering_scheduler_counters.opportunities_that_queued_a_task;
return true;
@@ -1586,6 +1587,26 @@ bool EventLoop::lays_out_rendering_update_in_flight() const
return m_rendering_update_in_flight && m_rendering_update_in_flight->layout_flew;
}
+// AD-HOC: A rendering update of a document that is still loading lays out content that is about to change, and a first
+// layout can take long. The event loop may choose its task queues freely, so the tasks that take the document
+// to DOMContentLoaded go first, for a short while. Without this, the first layout of a large document runs
+// between the last of its bytes and the steps after them, as the rendering task is queued first.
+bool EventLoop::rendering_task_yields_to_document_loading() const
+{
+ static constexpr auto maximum_time_to_yield = AK::Duration::from_milliseconds(4);
+
+ if (m_spin_depth > 0 || MonotonicTime::now() - m_rendering_task_queued_time >= maximum_time_to_yield)
+ return false;
+ return any_of(m_documents, [](auto const& document) {
+ return document
+ && document->readiness() != DocumentReadyState::Complete
+ && document->load_timing_info().dom_content_loaded_event_start_time == 0
+ && !document->has_layout_box()
+ && !document->is_initial_about_blank()
+ && document->is_fully_active();
+ });
+}
+
bool EventLoop::holds_rendering_opportunity() const
{
return m_rendering_update_in_flight && !m_rendering_update_in_flight->ended();
diff --git a/Libraries/LibWeb/HTML/EventLoop/EventLoop.h b/Libraries/LibWeb/HTML/EventLoop/EventLoop.h
index 1ceb027c470..3c92ba14bb5 100644
--- a/Libraries/LibWeb/HTML/EventLoop/EventLoop.h
+++ b/Libraries/LibWeb/HTML/EventLoop/EventLoop.h
@@ -12,6 +12,7 @@
#include <AK/Noncopyable.h>
#include <AK/OwnPtr.h>
#include <AK/Queue.h>
+#include <AK/Time.h>
#include <LibCore/Forward.h>
#include <LibGC/Ptr.h>
#include <LibGC/Weak.h>
@@ -215,6 +216,8 @@ public:
// The serial number of the first rendering update that may gather a resize observation of `document` made now. A
// task beside a rendering update whose style and layout of the document have yet to run comes after the whole of it.
u64 first_rendering_update_to_gather_resize_observations_of(DOM::Document const&) const;
+ // Whether the rendering task lets the tasks behind it go first, for a document to finish loading.
+ bool rendering_task_yields_to_document_loading() const;
// A rendering task that would find a rendering update not yet finished keeps its place in the queue until the update
// has finished, rather than wait for it. One that finds a frame still in flight runs beside it.
bool holds_rendering_opportunity() const;
@@ -314,6 +317,7 @@ private:
bool m_rendering_task_queued { false };
bool m_input_events_task_queued { false };
bool m_rendering_update_requested { false };
+ MonotonicTime m_rendering_task_queued_time { MonotonicTime::now_coarse() };
RenderingSchedulerCounters m_rendering_scheduler_counters;
RenderingSchedulerCounters m_rendering_scheduler_counters_at_last_update;
diff --git a/Libraries/LibWeb/HTML/EventLoop/TaskQueue.cpp b/Libraries/LibWeb/HTML/EventLoop/TaskQueue.cpp
index f984a149861..511144df72f 100644
--- a/Libraries/LibWeb/HTML/EventLoop/TaskQueue.cpp
+++ b/Libraries/LibWeb/HTML/EventLoop/TaskQueue.cpp
@@ -5,6 +5,7 @@
*/
#include <AK/ScopeGuard.h>
+#include <LibCore/ThreadEventQueue.h>
#include <LibGC/RootVector.h>
#include <LibWeb/DOM/Document.h>
#include <LibWeb/HTML/EventLoop/EventLoop.h>
@@ -80,6 +81,7 @@ GC::Ptr<Task> TaskQueue::take_first_runnable()
ScopeGuard run_discard_steps_guard { [&] { run_discard_steps(); } };
+ GC::Ptr<Task> rendering_task_that_yielded;
for (auto it = m_tasks.begin(); it != m_tasks.end();) {
auto& task = *it;
@@ -93,6 +95,12 @@ GC::Ptr<Task> TaskQueue::take_first_runnable()
if (task.source() == Task::Source::Rendering && m_event_loop->holds_rendering_opportunity())
return nullptr;
+ if (task.source() == Task::Source::Rendering && is_runnable_now(task) && m_event_loop->rendering_task_yields_to_document_loading()) {
+ rendering_task_that_yielded = &task;
+ ++it;
+ continue;
+ }
+
if (is_runnable_now(task)) {
if (m_last_added_task.ptr() == &task)
m_last_added_task = {};
@@ -109,6 +117,16 @@ GC::Ptr<Task> TaskQueue::take_first_runnable()
++it;
}
+ // NB: What has been posted to the platform event loop may be what queues the task that the rendering task yields to.
+ if (rendering_task_that_yielded) {
+ if (Core::ThreadEventQueue::current().has_pending_events())
+ return nullptr;
+ if (m_last_added_task.ptr() == rendering_task_that_yielded.ptr())
+ m_last_added_task = {};
+ m_tasks.remove(*rendering_task_that_yielded);
+ return rendering_task_that_yielded;
+ }
+
for (auto it = m_idle_tasks.begin(); it != m_idle_tasks.end();) {
auto& task = *it;