Title: [195392] trunk/Source/WebCore
Revision
195392
Author
commit-qu...@webkit.org
Date
2016-01-20 16:41:46 -0800 (Wed, 20 Jan 2016)

Log Message

Use TinyLRUCache in caching the CGColorRef in WebCore::cachedCGColor()
https://bugs.webkit.org/show_bug.cgi?id=153279

Patch by Said Abou-Hallawa <sabouhall...@apple.com> on 2016-01-20
Reviewed by Dean Jackson.

Reuse the new template TinyLRUCache in caching the CGColor instead of
having the same code repeated twice.

* platform/graphics/cg/ColorCG.cpp:
(WebCore::leakCGColor):
(WebCore::RetainPtr<CGColorRef>>::createValueForKey):
(WebCore::cachedCGColor):

Modified Paths

Diff

Modified: trunk/Source/WebCore/ChangeLog (195391 => 195392)


--- trunk/Source/WebCore/ChangeLog	2016-01-21 00:20:33 UTC (rev 195391)
+++ trunk/Source/WebCore/ChangeLog	2016-01-21 00:41:46 UTC (rev 195392)
@@ -1,3 +1,18 @@
+2016-01-20  Said Abou-Hallawa  <sabouhall...@apple.com>
+
+        Use TinyLRUCache in caching the CGColorRef in WebCore::cachedCGColor()
+        https://bugs.webkit.org/show_bug.cgi?id=153279
+
+        Reviewed by Dean Jackson.
+
+        Reuse the new template TinyLRUCache in caching the CGColor instead of
+        having the same code repeated twice.
+
+        * platform/graphics/cg/ColorCG.cpp:
+        (WebCore::leakCGColor):
+        (WebCore::RetainPtr<CGColorRef>>::createValueForKey):
+        (WebCore::cachedCGColor):
+
 2016-01-20  Timothy Hatcher  <timo...@apple.com>
 
         Web Inspector: InspectorCSSAgent does not call disable in willDestroyFrontendAndBackend

Modified: trunk/Source/WebCore/platform/graphics/cg/ColorCG.cpp (195391 => 195392)


--- trunk/Source/WebCore/platform/graphics/cg/ColorCG.cpp	2016-01-21 00:20:33 UTC (rev 195391)
+++ trunk/Source/WebCore/platform/graphics/cg/ColorCG.cpp	2016-01-21 00:41:46 UTC (rev 195392)
@@ -31,6 +31,7 @@
 #include "GraphicsContextCG.h"
 #include <wtf/Assertions.h>
 #include <wtf/RetainPtr.h>
+#include <wtf/TinyLRUCache.h>
 #if !PLATFORM(IOS)
 #include <ApplicationServices/ApplicationServices.h>
 #else
@@ -107,6 +108,12 @@
     return CGColorCreate(sRGBColorSpaceRef(), components);
 }
 
+template<>
+RetainPtr<CGColorRef> TinyLRUCachePolicy<Color, RetainPtr<CGColorRef>>::createValueForKey(const Color& color)
+{
+    return adoptCF(leakCGColor(color));
+}
+
 CGColorRef cachedCGColor(const Color& color)
 {
     switch (color.rgb()) {
@@ -126,24 +133,8 @@
 
     ASSERT(color.rgb());
 
-    const size_t cacheSize = 32;
-    static RGBA32 cachedRGBAValues[cacheSize];
-    static RetainPtr<CGColorRef>* cachedCGColors = new RetainPtr<CGColorRef>[cacheSize];
-
-    for (size_t i = 0; i < cacheSize; ++i) {
-        if (cachedRGBAValues[i] == color.rgb())
-            return cachedCGColors[i].get();
-    }
-
-    CGColorRef newCGColor = leakCGColor(color);
-
-    static size_t cursor;
-    cachedRGBAValues[cursor] = color.rgb();
-    cachedCGColors[cursor] = adoptCF(newCGColor);
-    if (++cursor == cacheSize)
-        cursor = 0;
-
-    return newCGColor;
+    static NeverDestroyed<TinyLRUCache<Color, RetainPtr<CGColorRef>, 32>> cache;
+    return cache.get().get(color).get();
 }
 
 }
_______________________________________________
webkit-changes mailing list
webkit-changes@lists.webkit.org
https://lists.webkit.org/mailman/listinfo/webkit-changes

Reply via email to