From f0722671c368ffe10f4c5dc02bcd958cdb901134 Mon Sep 17 00:00:00 2001 From: Andreas Kling Date: Thu, 11 Jan 2024 14:04:18 +0100 Subject: [PATCH] LibWeb: Cache the viewport rect across all of style computation Fetching the viewport rect is currently somewhat expensive, since it requires finding the navigable the document is active in. We can avoid the cost of repeated calls by simply allowing StyleComputer to cache the viewport rect at the start of style computation. --- Userland/Libraries/LibWeb/CSS/StyleComputer.cpp | 7 ------- Userland/Libraries/LibWeb/CSS/StyleComputer.h | 7 ++++++- Userland/Libraries/LibWeb/DOM/Document.cpp | 3 +++ 3 files changed, 9 insertions(+), 8 deletions(-) diff --git a/Userland/Libraries/LibWeb/CSS/StyleComputer.cpp b/Userland/Libraries/LibWeb/CSS/StyleComputer.cpp index 0c0e2b67cc..1b77c3c624 100644 --- a/Userland/Libraries/LibWeb/CSS/StyleComputer.cpp +++ b/Userland/Libraries/LibWeb/CSS/StyleComputer.cpp @@ -2356,13 +2356,6 @@ void StyleComputer::invalidate_rule_cache() m_user_agent_rule_cache = nullptr; } -CSSPixelRect StyleComputer::viewport_rect() const -{ - if (auto const navigable = document().navigable()) - return navigable->viewport_rect(); - return {}; -} - void StyleComputer::did_load_font(FlyString const&) { document().invalidate_style(); diff --git a/Userland/Libraries/LibWeb/CSS/StyleComputer.h b/Userland/Libraries/LibWeb/CSS/StyleComputer.h index e33a6a7e3c..d9513278a3 100644 --- a/Userland/Libraries/LibWeb/CSS/StyleComputer.h +++ b/Userland/Libraries/LibWeb/CSS/StyleComputer.h @@ -109,6 +109,8 @@ public: Variant timing_function; }; + void set_viewport_rect(Badge, CSSPixelRect const& viewport_rect) { m_viewport_rect = viewport_rect; } + private: enum class ComputeStyleMode { Normal, @@ -136,7 +138,8 @@ private: template void for_each_stylesheet(CascadeOrigin, Callback) const; - CSSPixelRect viewport_rect() const; + [[nodiscard]] CSSPixelRect viewport_rect() const { return m_viewport_rect; } + [[nodiscard]] Length::FontMetrics calculate_root_element_font_metrics(StyleProperties const&) const; CSSPixels parent_or_root_element_line_height(DOM::Element const*, Optional) const; @@ -241,6 +244,8 @@ private: mutable HashMap> m_active_animations; mutable HashMap> m_finished_animations; // If fill-mode is forward/both, this is non-null and contains the final state. mutable RefPtr m_animation_driver_timer; + + CSSPixelRect m_viewport_rect; }; } diff --git a/Userland/Libraries/LibWeb/DOM/Document.cpp b/Userland/Libraries/LibWeb/DOM/Document.cpp index 189027e003..c8e8c72f3a 100644 --- a/Userland/Libraries/LibWeb/DOM/Document.cpp +++ b/Userland/Libraries/LibWeb/DOM/Document.cpp @@ -1129,6 +1129,9 @@ void Document::update_style() if (m_created_for_appropriate_template_contents) return; + // Fetch the viewport rect once, instead of repeatedly, during style computation. + style_computer().set_viewport_rect({}, viewport_rect()); + evaluate_media_rules(); auto invalidation = update_style_recursively(*this);