CSSValuePool: Make numeric value caches fixed-size arrays.
authorkling@webkit.org <kling@webkit.org@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Wed, 18 Apr 2012 21:01:56 +0000 (21:01 +0000)
committerkling@webkit.org <kling@webkit.org@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Wed, 18 Apr 2012 21:01:56 +0000 (21:01 +0000)
<http://webkit.org/b/84268>

Reviewed by Antti Koivisto.

Change the numeric CSSPrimitiveValue caches in CSSValuePool from HashMaps to
fixed-size arrays of RefPtr<CSSPrimitiveValue>s.

This is more space efficient and doesn't incur the cost of a hash lookup every
time a numeric CSSPrimitiveValue is needed. We retain the limit of caching
only values between 0-255 for now.

* css/CSSValuePool.cpp:
(WebCore::CSSValuePool::CSSValuePool):
(WebCore::CSSValuePool::createValue):
* css/CSSValuePool.h:
(CSSValuePool):

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

Source/WebCore/ChangeLog
Source/WebCore/css/CSSValuePool.cpp
Source/WebCore/css/CSSValuePool.h

index c35212d..b1ba56d 100644 (file)
@@ -1,3 +1,23 @@
+2012-04-18  Andreas Kling  <kling@webkit.org>
+
+        CSSValuePool: Make numeric value caches fixed-size arrays.
+        <http://webkit.org/b/84268>
+
+        Reviewed by Antti Koivisto.
+
+        Change the numeric CSSPrimitiveValue caches in CSSValuePool from HashMaps to
+        fixed-size arrays of RefPtr<CSSPrimitiveValue>s.
+
+        This is more space efficient and doesn't incur the cost of a hash lookup every
+        time a numeric CSSPrimitiveValue is needed. We retain the limit of caching
+        only values between 0-255 for now.
+
+        * css/CSSValuePool.cpp:
+        (WebCore::CSSValuePool::CSSValuePool):
+        (WebCore::CSSValuePool::createValue):
+        * css/CSSValuePool.h:
+        (CSSValuePool):
+
 2012-04-18  Dana Jansens  <danakj@chromium.org>
 
         [chromium] Rename overdraw histograms so we can use field trials in histograms.xml
index 78391f8..ce757b9 100644 (file)
@@ -46,9 +46,6 @@ CSSValuePool::CSSValuePool()
     , m_colorTransparent(CSSPrimitiveValue::createColor(Color::transparent))
     , m_colorWhite(CSSPrimitiveValue::createColor(Color::white))
     , m_colorBlack(CSSPrimitiveValue::createColor(Color::black))
-    , m_pixelZero(CSSPrimitiveValue::create(0, CSSPrimitiveValue::CSS_PX))
-    , m_percentZero(CSSPrimitiveValue::create(0, CSSPrimitiveValue::CSS_PERCENTAGE))
-    , m_numberZero(CSSPrimitiveValue::create(0, CSSPrimitiveValue::CSS_NUMBER))
 {
 }
 
@@ -87,41 +84,31 @@ PassRefPtr<CSSPrimitiveValue> CSSValuePool::createColorValue(unsigned rgbValue)
 
 PassRefPtr<CSSPrimitiveValue> CSSValuePool::createValue(double value, CSSPrimitiveValue::UnitTypes type)
 {
-    // Small positive integers repeat often.
-    static const int maximumCacheableValue = 256;
-    if (value < 0 || value > maximumCacheableValue)
+    if (value < 0 || value > maximumCacheableIntegerValue)
         return CSSPrimitiveValue::create(value, type);
 
     int intValue = static_cast<int>(value);
     if (value != intValue)
         return CSSPrimitiveValue::create(value, type);
 
-    IntegerValueCache* cache;
+    RefPtr<CSSPrimitiveValue>* cache;
     switch (type) {
     case CSSPrimitiveValue::CSS_PX:
-        if (intValue == 0)
-            return m_pixelZero;
-        cache = &m_pixelValueCache;
+        cache = m_pixelValueCache;
         break;
     case CSSPrimitiveValue::CSS_PERCENTAGE:
-        if (intValue == 0)
-            return m_percentZero;
-        cache = &m_percentValueCache;
+        cache = m_percentValueCache;
         break;
     case CSSPrimitiveValue::CSS_NUMBER:
-        if (intValue == 0)
-            return m_numberZero;
-        cache = &m_numberValueCache;
+        cache = m_numberValueCache;
         break;
     default:
         return CSSPrimitiveValue::create(value, type);
     }
 
-    RefPtr<CSSPrimitiveValue> dummyValue;
-    IntegerValueCache::AddResult entry = cache->add(intValue, dummyValue);
-    if (entry.isNewEntry)
-        entry.iterator->second = CSSPrimitiveValue::create(value, type);
-    return entry.iterator->second;
+    if (!cache[intValue])
+        cache[intValue] = CSSPrimitiveValue::create(value, type);
+    return cache[intValue];
 }
 
 PassRefPtr<CSSPrimitiveValue> CSSValuePool::createFontFamilyValue(const String& familyName)
index 4a032c1..e2ac9d6 100644 (file)
@@ -66,13 +66,11 @@ private:
     RefPtr<CSSPrimitiveValue> m_colorWhite;
     RefPtr<CSSPrimitiveValue> m_colorBlack;
 
-    typedef HashMap<int, RefPtr<CSSPrimitiveValue> > IntegerValueCache;
-    RefPtr<CSSPrimitiveValue> m_pixelZero;
-    RefPtr<CSSPrimitiveValue> m_percentZero;
-    RefPtr<CSSPrimitiveValue> m_numberZero;
-    IntegerValueCache m_pixelValueCache;
-    IntegerValueCache m_percentValueCache;
-    IntegerValueCache m_numberValueCache;
+    static const int maximumCacheableIntegerValue = 255;
+
+    RefPtr<CSSPrimitiveValue> m_pixelValueCache[maximumCacheableIntegerValue + 1];
+    RefPtr<CSSPrimitiveValue> m_percentValueCache[maximumCacheableIntegerValue + 1];
+    RefPtr<CSSPrimitiveValue> m_numberValueCache[maximumCacheableIntegerValue + 1];
 
     typedef HashMap<AtomicString, RefPtr<CSSValueList> > FontFaceValueCache;
     FontFaceValueCache m_fontFaceValueCache;