[Cocoa] Slightly improve performance of Font::getCFStringAttributes()
authormmaxfield@apple.com <mmaxfield@apple.com@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Fri, 7 Feb 2020 18:59:10 +0000 (18:59 +0000)
committermmaxfield@apple.com <mmaxfield@apple.com@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Fri, 7 Feb 2020 18:59:10 +0000 (18:59 +0000)
https://bugs.webkit.org/show_bug.cgi?id=207374

Reviewed by Darin Adler.

Switch from CFDictionaryCreateMutable() to CFDictionaryCreate(). Also, don't construct a CTParagraphStyle each time the function is called.

No new tests because there is no behavior change.

* platform/graphics/FontCascade.cpp:
(WebCore::FontCascade::widthForSimpleText const):
* platform/graphics/mac/SimpleFontDataCoreText.cpp:
(WebCore::Font::getCFStringAttributes const):

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

Source/WebCore/ChangeLog
Source/WebCore/platform/graphics/FontCascade.cpp
Source/WebCore/platform/graphics/mac/SimpleFontDataCoreText.cpp

index 9652626..d280624 100644 (file)
@@ -1,3 +1,19 @@
+2020-02-07  Myles C. Maxfield  <mmaxfield@apple.com>
+
+        [Cocoa] Slightly improve performance of Font::getCFStringAttributes()
+        https://bugs.webkit.org/show_bug.cgi?id=207374
+
+        Reviewed by Darin Adler.
+
+        Switch from CFDictionaryCreateMutable() to CFDictionaryCreate(). Also, don't construct a CTParagraphStyle each time the function is called.
+
+        No new tests because there is no behavior change.
+
+        * platform/graphics/FontCascade.cpp:
+        (WebCore::FontCascade::widthForSimpleText const):
+        * platform/graphics/mac/SimpleFontDataCoreText.cpp:
+        (WebCore::Font::getCFStringAttributes const):
+
 2020-02-07  youenn fablet  <youenn@apple.com>
 
         Align getDisplayMedia() with spec
index fdc596e..1ec5a38 100644 (file)
@@ -428,8 +428,6 @@ float FontCascade::widthForSimpleText(StringView text) const
         return *cacheEntry;
 
     GlyphBuffer glyphBuffer;
-    Vector<GlyphBufferGlyph, 16> glyphs;
-    Vector<GlyphBufferAdvance, 16> advances;
     bool hasKerningOrLigatures = enableKerning() || requiresShaping();
     float runWidth = 0;
     auto& font = primaryFont();
index 97650d1..d9d4151 100644 (file)
@@ -34,29 +34,50 @@ namespace WebCore {
 
 RetainPtr<CFDictionaryRef> Font::getCFStringAttributes(bool enableKerning, FontOrientation orientation, const AtomString& locale) const
 {
-    auto attributesDictionary = adoptCF(CFDictionaryCreateMutable(kCFAllocatorDefault, 4, &kCFCopyStringDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks));
+    CFTypeRef keys[5];
+    CFTypeRef values[5];
+
+    keys[0] = kCTFontAttributeName;
+    values[0] = platformData().ctFont();
+    size_t count = 1;
 
-    CFDictionarySetValue(attributesDictionary.get(), kCTFontAttributeName, platformData().ctFont());
 #if USE(CTFONTTRANSFORMGLYPHSWITHLANGUAGE)
-    if (!locale.isEmpty())
-        CFDictionarySetValue(attributesDictionary.get(), kCTLanguageAttributeName, locale.string().createCFString().get());
+    RetainPtr<CFStringRef> localeString;
+    if (!locale.isEmpty()) {
+        localeString = locale.string().createCFString();
+        keys[count] = kCTLanguageAttributeName;
+        values[count] = localeString.get();
+        ++count;
+    }
 #else
     UNUSED_PARAM(locale);
 #endif
-    auto paragraphStyle = adoptCF(CTParagraphStyleCreate(nullptr, 0));
-    CTParagraphStyleSetCompositionLanguage(paragraphStyle.get(), kCTCompositionLanguageNone);
-    CFDictionarySetValue(attributesDictionary.get(), kCTParagraphStyleAttributeName, paragraphStyle.get());
+    static CTParagraphStyleRef paragraphStyle = [] {
+        auto paragraphStyle = CTParagraphStyleCreate(nullptr, 0);
+        CTParagraphStyleSetCompositionLanguage(paragraphStyle, kCTCompositionLanguageNone);
+        return paragraphStyle;
+    }();
+    keys[count] = kCTParagraphStyleAttributeName;
+    values[count] = paragraphStyle;
+    ++count;
 
     if (!enableKerning) {
         const float zero = 0;
         static CFNumberRef zeroKerningValue = CFNumberCreate(kCFAllocatorDefault, kCFNumberFloatType, &zero);
-        CFDictionarySetValue(attributesDictionary.get(), kCTKernAttributeName, zeroKerningValue);
+        keys[count] = kCTKernAttributeName;
+        values[count] = zeroKerningValue;
+        ++count;
+    }
+
+    if (orientation == FontOrientation::Vertical) {
+        keys[count] = kCTVerticalFormsAttributeName;
+        values[count] = kCFBooleanTrue;
+        ++count;
     }
 
-    if (orientation == FontOrientation::Vertical)
-        CFDictionarySetValue(attributesDictionary.get(), kCTVerticalFormsAttributeName, kCFBooleanTrue);
+    ASSERT(count <= WTF_ARRAY_LENGTH(keys));
 
-    return attributesDictionary;
+    return adoptCF(CFDictionaryCreate(kCFAllocatorDefault, keys, values, count, &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks));
 }
 
 #if HAVE(DISALLOWABLE_USER_INSTALLED_FONTS)