From 11b4216e65ea1be4704c66e9baf32f3357163a31 Mon Sep 17 00:00:00 2001 From: Andreas Kling Date: Sat, 24 Feb 2024 08:09:42 +0100 Subject: [PATCH] LibWeb: Run IntersectionObserver steps only when needed Instead of updating IOs in every iteration of the HTML event loop, we now only do it after a relayout, or after the viewport changes. --- Userland/Libraries/LibWeb/DOM/Document.cpp | 3 +++ Userland/Libraries/LibWeb/HTML/EventLoop/EventLoop.cpp | 4 +--- Userland/Libraries/LibWeb/HTML/Navigable.cpp | 4 ++++ 3 files changed, 8 insertions(+), 3 deletions(-) diff --git a/Userland/Libraries/LibWeb/DOM/Document.cpp b/Userland/Libraries/LibWeb/DOM/Document.cpp index 1f90a9c92b..268ea76956 100644 --- a/Userland/Libraries/LibWeb/DOM/Document.cpp +++ b/Userland/Libraries/LibWeb/DOM/Document.cpp @@ -1106,6 +1106,9 @@ void Document::update_layout() m_needs_layout = false; m_layout_update_timer->stop(); + + // OPTIMIZATION: We do this here instead of in HTML::EventLoop::process() to avoid redundant work. + run_the_update_intersection_observations_steps(HighResolutionTime::unsafe_shared_current_time()); } [[nodiscard]] static Element::RequiredInvalidationAfterStyleChange update_style_recursively(Node& node) diff --git a/Userland/Libraries/LibWeb/HTML/EventLoop/EventLoop.cpp b/Userland/Libraries/LibWeb/HTML/EventLoop/EventLoop.cpp index f60ea51c64..807d09dbf7 100644 --- a/Userland/Libraries/LibWeb/HTML/EventLoop/EventLoop.cpp +++ b/Userland/Libraries/LibWeb/HTML/EventLoop/EventLoop.cpp @@ -248,9 +248,7 @@ void EventLoop::process() }); // 14. For each fully active Document in docs, run the update intersection observations steps for that Document, passing in now as the timestamp. [INTERSECTIONOBSERVER] - for_each_fully_active_document_in_docs([&](DOM::Document& document) { - document.run_the_update_intersection_observations_steps(now); - }); + // OPTIMIZATION: We do this automatically after layout or viewport changes, so we don't need to do it here. // FIXME: 15. Invoke the mark paint timing algorithm for each Document object in docs. diff --git a/Userland/Libraries/LibWeb/HTML/Navigable.cpp b/Userland/Libraries/LibWeb/HTML/Navigable.cpp index d4e7abff7a..8add6d0d12 100644 --- a/Userland/Libraries/LibWeb/HTML/Navigable.cpp +++ b/Userland/Libraries/LibWeb/HTML/Navigable.cpp @@ -29,6 +29,7 @@ #include #include #include +#include #include #include #include @@ -1944,6 +1945,9 @@ void Navigable::scroll_offset_did_change() // 3. Append doc to doc’s pending scroll event targets. doc->pending_scroll_event_targets().append(*doc); + + // OPTIMIZATION: We do this here instead of in HTML::EventLoop::process() to avoid redundant work. + doc->run_the_update_intersection_observations_steps(HighResolutionTime::unsafe_shared_current_time()); } CSSPixelRect Navigable::to_top_level_rect(CSSPixelRect const& a_rect)