summaryrefslogtreecommitdiff
path: root/Userland/Libraries
diff options
context:
space:
mode:
authorAndreas Kling <kling@serenityos.org>2022-04-10 12:32:16 +0200
committerAndreas Kling <kling@serenityos.org>2022-04-10 12:40:07 +0200
commit751b605690fa28540243a347836c19b972362f20 (patch)
tree518a24be838db179fe83a5d3dee5801e0ac92152 /Userland/Libraries
parentae6b09f4dcea65e4ba0e9bb70d7c0322bab958a6 (diff)
downloadserenity-751b605690fa28540243a347836c19b972362f20.zip
LibWeb: Cache scaled web fonts instead of recreating them every time
Previously, we would create a new Gfx::ScaledFont whenever we needed one for an element's computed style. This worked fine on Acid3 since the use of web fonts was extremely limited. In the wild, web fonts obviously get used a lot more, so let's have a per-point-size font cache for them.
Diffstat (limited to 'Userland/Libraries')
-rw-r--r--Userland/Libraries/LibWeb/CSS/StyleComputer.cpp16
1 files changed, 15 insertions, 1 deletions
diff --git a/Userland/Libraries/LibWeb/CSS/StyleComputer.cpp b/Userland/Libraries/LibWeb/CSS/StyleComputer.cpp
index 9d7441c2b9..ac474a653f 100644
--- a/Userland/Libraries/LibWeb/CSS/StyleComputer.cpp
+++ b/Userland/Libraries/LibWeb/CSS/StyleComputer.cpp
@@ -68,7 +68,19 @@ public:
{
if (!m_vector_font)
return nullptr;
- return adopt_ref(*new Gfx::ScaledFont(*m_vector_font, point_size, point_size));
+
+ if (auto it = m_cached_fonts.find(point_size); it != m_cached_fonts.end())
+ return it->value;
+
+ // FIXME: It might be nicer to have a global cap on the number of fonts we cache
+ // instead of doing it at the per-font level like this.
+ constexpr size_t max_cached_font_size_count = 64;
+ if (m_cached_fonts.size() > max_cached_font_size_count)
+ m_cached_fonts.remove(m_cached_fonts.begin());
+
+ auto font = adopt_ref(*new Gfx::ScaledFont(*m_vector_font, point_size, point_size));
+ m_cached_fonts.set(point_size, font);
+ return font;
}
private:
@@ -92,6 +104,8 @@ private:
StyleComputer& m_style_computer;
FlyString m_family_name;
RefPtr<Gfx::VectorFont> m_vector_font;
+
+ HashMap<float, NonnullRefPtr<Gfx::ScaledFont>> mutable m_cached_fonts;
};
static StyleSheet& default_stylesheet()