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.
This commit is contained in:
Aliaksandr Kalenik 2024-09-11 22:15:14 +02:00 committed by Andreas Kling
parent 112dd4af3b
commit 143c9581a2
Notes: github-actions[bot] 2024-09-12 05:38:09 +00:00

View file

@ -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<ScrollFrame const> 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<CSSPixelPoint> m_cached_cumulative_offset;
};
}