From 95e5e41e8e0eca0f19d372b211d7d74f5c395ae3 Mon Sep 17 00:00:00 2001 From: Michael Nutt Date: Fri, 19 Sep 2025 08:35:12 -0400 Subject: [PATCH] Cache font family lookups This call to `QFontDatabase::hasFamily()` is fairly expensive. It does quite a bit of name normalization, making it difficult for us to cache `QFontDatabase::families()` directly. --- Source/WebCore/platform/graphics/FontCache.h | 4 ++++ Source/WebCore/platform/graphics/qt/FontCacheQt.cpp | 6 +++++- 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/Source/WebCore/platform/graphics/FontCache.h b/Source/WebCore/platform/graphics/FontCache.h index 9f1b68b8f1e2..09bd1bc692f2 100644 --- a/Source/WebCore/platform/graphics/FontCache.h +++ b/Source/WebCore/platform/graphics/FontCache.h @@ -257,6 +257,10 @@ class FontCache : public CanMakeCheckedPtr { UncheckedKeyHashSet m_knownFamilies; #endif +#if PLATFORM(QT) + UncheckedKeyHashMap m_fontFamilyCache; +#endif + #if PLATFORM(COCOA) FontDatabase m_databaseAllowingUserInstalledFonts { AllowUserInstalledFonts::Yes }; FontDatabase m_databaseDisallowingUserInstalledFonts { AllowUserInstalledFonts::No }; diff --git a/Source/WebCore/platform/graphics/qt/FontCacheQt.cpp b/Source/WebCore/platform/graphics/qt/FontCacheQt.cpp index 7856f8e64525..3149e4ac0b86 100644 --- a/Source/WebCore/platform/graphics/qt/FontCacheQt.cpp +++ b/Source/WebCore/platform/graphics/qt/FontCacheQt.cpp @@ -96,7 +96,11 @@ Ref FontCache::lastResortFallbackFont(const FontDescription& fontDescripti std::unique_ptr FontCache::createFontPlatformData(const FontDescription& fontDescription, const AtomString& family, const FontCreationContext&, OptionSet) { - if (!QFontDatabase::hasFamily(family.string())) + auto addResult = m_fontFamilyCache.ensure(family, [&family] { + return QFontDatabase::hasFamily(family.string()); + }); + + if (!addResult.iterator->value) return nullptr; return std::make_unique(fontDescription, family); }