Use finer grained locking in FontDatabase
authorantti@apple.com <antti@apple.com@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Thu, 11 Oct 2018 18:39:05 +0000 (18:39 +0000)
committerantti@apple.com <antti@apple.com@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Thu, 11 Oct 2018 18:39:05 +0000 (18:39 +0000)
https://bugs.webkit.org/show_bug.cgi?id=190467

Reviewed by Alex Christensen.

* platform/graphics/FontCache.h:

Also use ListHashSet for prewarming info so we can prewarm in the same order the fonts were
seen last time.

* platform/graphics/cocoa/FontCacheCoreText.cpp:
(WebCore::FontDatabase::collectionForFamily):

Only hold the lock when accessing the hashmap. There is no need to hold it during font construction
which can take a long time.

(WebCore::FontDatabase::fontForPostScriptName):

This is currently not prewarmed from a thread so no need for locking.

(WebCore::FontDatabase::clear):

git-svn-id: https://svn.webkit.org/repository/webkit/trunk@237040 268f45cc-cd09-0410-ab3c-d52691b4dbfc

Source/WebCore/ChangeLog
Source/WebCore/platform/graphics/FontCache.h
Source/WebCore/platform/graphics/cocoa/FontCacheCoreText.cpp

index dcb3c1b..638cc46 100644 (file)
@@ -1,3 +1,27 @@
+2018-10-11  Antti Koivisto  <antti@apple.com>
+
+        Use finer grained locking in FontDatabase
+        https://bugs.webkit.org/show_bug.cgi?id=190467
+
+        Reviewed by Alex Christensen.
+
+        * platform/graphics/FontCache.h:
+
+        Also use ListHashSet for prewarming info so we can prewarm in the same order the fonts were
+        seen last time.
+
+        * platform/graphics/cocoa/FontCacheCoreText.cpp:
+        (WebCore::FontDatabase::collectionForFamily):
+
+        Only hold the lock when accessing the hashmap. There is no need to hold it during font construction
+        which can take a long time.
+
+        (WebCore::FontDatabase::fontForPostScriptName):
+
+        This is currently not prewarmed from a thread so no need for locking.
+
+        (WebCore::FontDatabase::clear):
+
 2018-10-11  Thibault Saunier  <tsaunier@igalia.com>
 
         [GStreamer] Fix race condition in GStreamerVideoDecoder
index aa56f1b..62cd6db 100644 (file)
@@ -36,6 +36,7 @@
 #include <array>
 #include <limits.h>
 #include <wtf/Forward.h>
+#include <wtf/ListHashSet.h>
 #include <wtf/RefPtr.h>
 #include <wtf/Vector.h>
 #include <wtf/WorkQueue.h>
@@ -264,7 +265,7 @@ private:
     bool m_shouldMockBoldSystemFontForAccessibility { false };
 
 #if PLATFORM(COCOA)
-    HashSet<String> m_seenFamiliesForPrewarming;
+    ListHashSet<String> m_seenFamiliesForPrewarming;
     RefPtr<WorkQueue> m_prewarmQueue;
 
     friend class ComplexTextController;
index 82243af..8727bce 100644 (file)
@@ -887,10 +887,15 @@ public:
 
     const InstalledFontFamily& collectionForFamily(const String& familyName)
     {
-        std::lock_guard<Lock> locker(m_descriptorMapLock);
-
         auto folded = familyName.foldCase();
-        return m_familyNameToFontDescriptors.ensure(folded, [&] {
+        {
+            std::lock_guard<Lock> locker(m_familyNameToFontDescriptorsLock);
+            auto it = m_familyNameToFontDescriptors.find(folded);
+            if (it != m_familyNameToFontDescriptors.end())
+                return it->value;
+        }
+
+        auto installedFontFamily = [&] {
             auto familyNameString = folded.createCFString();
             auto attributes = adoptCF(CFDictionaryCreateMutable(kCFAllocatorDefault, 0, &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks));
             CFDictionaryAddValue(attributes.get(), kCTFontFamilyNameAttribute, familyNameString.get());
@@ -908,13 +913,14 @@ public:
                 return InstalledFontFamily(WTFMove(result));
             }
             return InstalledFontFamily();
-        }).iterator->value;
+        }();
+
+        std::lock_guard<Lock> locker(m_familyNameToFontDescriptorsLock);
+        return m_familyNameToFontDescriptors.add(folded.isolatedCopy(), installedFontFamily).iterator->value;
     }
 
     const InstalledFont& fontForPostScriptName(const AtomicString& postScriptName)
     {
-        std::lock_guard<Lock> locker(m_descriptorMapLock);
-
         const auto& folded = FontCascadeDescription::foldedFamilyName(postScriptName);
         return m_postScriptNameToFontDescriptors.ensure(folded, [&] {
             auto postScriptNameString = folded.createCFString();
@@ -936,9 +942,10 @@ public:
 
     void clear()
     {
-        std::lock_guard<Lock> locker(m_descriptorMapLock);
-
-        m_familyNameToFontDescriptors.clear();
+        {
+            std::lock_guard<Lock> locker(m_familyNameToFontDescriptorsLock);
+            m_familyNameToFontDescriptors.clear();
+        }
         m_postScriptNameToFontDescriptors.clear();
     }
 
@@ -950,7 +957,7 @@ private:
     {
     }
 
-    Lock m_descriptorMapLock;
+    Lock m_familyNameToFontDescriptorsLock;
     HashMap<String, InstalledFontFamily> m_familyNameToFontDescriptors;
     HashMap<String, InstalledFont> m_postScriptNameToFontDescriptors;
     AllowUserInstalledFonts m_allowUserInstalledFonts;