From 143c9581a296831141bcb62f9c4237d0b470f885 Mon Sep 17 00:00:00 2001 From: Aliaksandr Kalenik Date: Wed, 11 Sep 2024 22:15:14 +0200 Subject: [PATCH] LibWeb: Add a cache for ScrollFrame::cumulative_offset() Calculating cumulative scroll offset is visible in profiles on very large pages, so let's add a simple caching for it. --- .../Libraries/LibWeb/Painting/ScrollFrame.h | 20 +++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) diff --git a/Userland/Libraries/LibWeb/Painting/ScrollFrame.h b/Userland/Libraries/LibWeb/Painting/ScrollFrame.h index 75ac9065160..3a587481d5e 100644 --- a/Userland/Libraries/LibWeb/Painting/ScrollFrame.h +++ b/Userland/Libraries/LibWeb/Painting/ScrollFrame.h @@ -22,18 +22,30 @@ public: CSSPixelPoint cumulative_offset() const { - if (m_parent) - return m_parent->cumulative_offset() + m_own_offset; - return m_own_offset; + if (!m_cached_cumulative_offset.has_value()) { + m_cached_cumulative_offset = m_own_offset; + if (m_parent) { + m_cached_cumulative_offset.value() += m_parent->cumulative_offset(); + } + } + return m_cached_cumulative_offset.value(); } CSSPixelPoint own_offset() const { return m_own_offset; } - void set_own_offset(CSSPixelPoint offset) { m_own_offset = offset; } + void set_own_offset(CSSPixelPoint offset) + { + m_cached_cumulative_offset.clear(); + m_own_offset = offset; + } private: i32 m_id { -1 }; RefPtr m_parent; CSSPixelPoint m_own_offset; + + // Caching here relies on the fact that offsets of all scroll frames are invalidated when any of them changes, + // so we don't need to worry about invalidating the cache when the parent's offset changes. + mutable Optional m_cached_cumulative_offset; }; }