Title: [174456] trunk/Source/WebCore
- Revision
- 174456
- Author
- [email protected]
- Date
- 2014-10-08 09:20:37 -0700 (Wed, 08 Oct 2014)
Log Message
[Mac] We are spending a lot of time loading fonts when loading weather.com
https://bugs.webkit.org/show_bug.cgi?id=137454
Reviewed by Darin Adler.
We are spending a lot of time loading fonts when loading weather.com:
~4.2% of WebProcess's cpu time in FontCache::getCachedFrontData().
In particular, we are spending a lot of time doing font auto-activation
because we don't have the Open Sans fonts installed and weather.com is
trying to load those.
Before this patch, we were doing font auto-activation ~250 times when
loading weather.com, even though the site is loading ~10 distinct font
families.
This patch adds a cache of font families we already tried to
auto-activate so that we don't try again. This results in ~10 font
auto-activations when loading weather.com instead of 250. It reduces
the amount of time spent in getCachedFrontData() to 62.6ms from 276ms
(4.4x less) when loading weather.com.
No new tests, no behavior change.
* platform/graphics/mac/FontCacheMac.mm:
(WebCore::shouldAutoActivateFontIfNeeded):
(WebCore::FontCache::createFontPlatformData):
* platform/mac/WebFontCache.h:
* platform/mac/WebFontCache.mm:
(+[WebFontCache fontWithFamily:traits:weight:size:shouldAutoActivateIfNeeded:]):
(+[WebFontCache fontWithFamily:traits:weight:size:]):
(+[WebFontCache fontWithFamily:traits:size:]):
Modified Paths
Diff
Modified: trunk/Source/WebCore/ChangeLog (174455 => 174456)
--- trunk/Source/WebCore/ChangeLog 2014-10-08 16:18:03 UTC (rev 174455)
+++ trunk/Source/WebCore/ChangeLog 2014-10-08 16:20:37 UTC (rev 174456)
@@ -1,3 +1,37 @@
+2014-10-08 Christophe Dumez <[email protected]>
+
+ [Mac] We are spending a lot of time loading fonts when loading weather.com
+ https://bugs.webkit.org/show_bug.cgi?id=137454
+
+ Reviewed by Darin Adler.
+
+ We are spending a lot of time loading fonts when loading weather.com:
+ ~4.2% of WebProcess's cpu time in FontCache::getCachedFrontData().
+ In particular, we are spending a lot of time doing font auto-activation
+ because we don't have the Open Sans fonts installed and weather.com is
+ trying to load those.
+
+ Before this patch, we were doing font auto-activation ~250 times when
+ loading weather.com, even though the site is loading ~10 distinct font
+ families.
+
+ This patch adds a cache of font families we already tried to
+ auto-activate so that we don't try again. This results in ~10 font
+ auto-activations when loading weather.com instead of 250. It reduces
+ the amount of time spent in getCachedFrontData() to 62.6ms from 276ms
+ (4.4x less) when loading weather.com.
+
+ No new tests, no behavior change.
+
+ * platform/graphics/mac/FontCacheMac.mm:
+ (WebCore::shouldAutoActivateFontIfNeeded):
+ (WebCore::FontCache::createFontPlatformData):
+ * platform/mac/WebFontCache.h:
+ * platform/mac/WebFontCache.mm:
+ (+[WebFontCache fontWithFamily:traits:weight:size:shouldAutoActivateIfNeeded:]):
+ (+[WebFontCache fontWithFamily:traits:weight:size:]):
+ (+[WebFontCache fontWithFamily:traits:size:]):
+
2014-10-08 Darin Adler <[email protected]>
ASSERTION FAILED: underlyingStringIsValid()
Modified: trunk/Source/WebCore/platform/graphics/mac/FontCacheMac.mm (174455 => 174456)
--- trunk/Source/WebCore/platform/graphics/mac/FontCacheMac.mm 2014-10-08 16:18:03 UTC (rev 174455)
+++ trunk/Source/WebCore/platform/graphics/mac/FontCacheMac.mm 2014-10-08 16:20:37 UTC (rev 174456)
@@ -39,9 +39,11 @@
#import "WebFontCache.h"
#import <AppKit/AppKit.h>
#import <wtf/MainThread.h>
+#import <wtf/NeverDestroyed.h>
#import <wtf/StdLibExtras.h>
+#import <wtf/Threading.h>
+#import <wtf/text/AtomicStringHash.h>
-
namespace WebCore {
// The "void*" parameter makes the function match the prototype for callbacks from callOnMainThread.
@@ -88,6 +90,25 @@
return appKitFontWeight >= 7;
}
+static bool shouldAutoActivateFontIfNeeded(const AtomicString& family)
+{
+#ifndef NDEBUG
+ // This cache is not thread safe so the following assertion is there to
+ // make sure this function is always called from the same thread.
+ static ThreadIdentifier initThreadId = currentThread();
+ ASSERT(currentThread() == initThreadId);
+#endif
+
+ static NeverDestroyed<HashSet<AtomicString>> knownFamilies;
+ static const unsigned maxCacheSize = 128;
+ ASSERT(knownFamilies.get().size() <= maxCacheSize);
+ if (knownFamilies.get().size() == maxCacheSize)
+ knownFamilies.get().remove(knownFamilies.get().begin());
+
+ // Only attempt to auto-activate fonts once for performance reasons.
+ return knownFamilies.get().add(family).isNewEntry;
+}
+
PassRefPtr<SimpleFontData> FontCache::systemFallbackForCharacters(const FontDescription& description, const SimpleFontData* originalFontData, bool isPlatformFont, const UChar* characters, int length)
{
UChar32 character;
@@ -203,7 +224,7 @@
NSInteger weight = toAppKitFontWeight(fontDescription.weight());
float size = fontDescription.computedPixelSize();
- NSFont *nsFont = [WebFontCache fontWithFamily:family traits:traits weight:weight size:size];
+ NSFont *nsFont = [WebFontCache fontWithFamily:family traits:traits weight:weight size:size shouldAutoActivateIfNeeded:shouldAutoActivateFontIfNeeded(family)];
if (!nsFont)
return nullptr;
Modified: trunk/Source/WebCore/platform/mac/WebFontCache.h (174455 => 174456)
--- trunk/Source/WebCore/platform/mac/WebFontCache.h 2014-10-08 16:18:03 UTC (rev 174455)
+++ trunk/Source/WebCore/platform/mac/WebFontCache.h 2014-10-08 16:20:37 UTC (rev 174456)
@@ -29,6 +29,7 @@
// This interface exists so that third party products (like Silk) can patch in to an Obj-C method to manipulate WebKit's font caching/substitution.
WEBCORE_EXPORT @interface WebFontCache : NSObject
++ (NSFont *)fontWithFamily:(NSString *)desiredFamily traits:(NSFontTraitMask)desiredTraits weight:(int)desiredWeight size:(float)size shouldAutoActivateIfNeeded:(BOOL)shouldAutoActivateIfNeeded;
+ (NSFont *)fontWithFamily:(NSString *)desiredFamily traits:(NSFontTraitMask)desiredTraits weight:(int)desiredWeight size:(float)size;
+ (void)getTraits:(Vector<unsigned>&)traitsMasks inFamily:(NSString *)desiredFamily;
Modified: trunk/Source/WebCore/platform/mac/WebFontCache.mm (174455 => 174456)
--- trunk/Source/WebCore/platform/mac/WebFontCache.mm 2014-10-08 16:18:03 UTC (rev 174455)
+++ trunk/Source/WebCore/platform/mac/WebFontCache.mm 2014-10-08 16:20:37 UTC (rev 174456)
@@ -276,10 +276,10 @@
return font;
}
-+ (NSFont *)fontWithFamily:(NSString *)desiredFamily traits:(NSFontTraitMask)desiredTraits weight:(int)desiredWeight size:(float)size
++ (NSFont *)fontWithFamily:(NSString *)desiredFamily traits:(NSFontTraitMask)desiredTraits weight:(int)desiredWeight size:(float)size shouldAutoActivateIfNeeded:(BOOL)shouldAutoActivateIfNeeded
{
NSFont *font = [self internalFontWithFamily:desiredFamily traits:desiredTraits weight:desiredWeight size:size];
- if (font)
+ if (font || !shouldAutoActivateIfNeeded)
return font;
// Auto activate the font before looking for it a second time.
@@ -289,10 +289,15 @@
return [self internalFontWithFamily:desiredFamily traits:desiredTraits weight:desiredWeight size:size];
}
++ (NSFont *)fontWithFamily:(NSString *)desiredFamily traits:(NSFontTraitMask)desiredTraits weight:(int)desiredWeight size:(float)size
+{
+ return [self fontWithFamily:desiredFamily traits:desiredTraits weight:desiredWeight size:size shouldAutoActivateIfNeeded:YES];
+}
+
+ (NSFont *)fontWithFamily:(NSString *)desiredFamily traits:(NSFontTraitMask)desiredTraits size:(float)size
{
int desiredWeight = (desiredTraits & NSBoldFontMask) ? 9 : 5;
- return [self fontWithFamily:desiredFamily traits:desiredTraits weight:desiredWeight size:size];
+ return [self fontWithFamily:desiredFamily traits:desiredTraits weight:desiredWeight size:size shouldAutoActivateIfNeeded:YES];
}
@end
_______________________________________________
webkit-changes mailing list
[email protected]
https://lists.webkit.org/mailman/listinfo/webkit-changes