Title: [161210] trunk/Source/WebCore
Revision
161210
Author
akl...@apple.com
Date
2014-01-02 10:45:33 -0800 (Thu, 02 Jan 2014)

Log Message

Simplify the insides of DocumentSharedObjectPool and reduce memory usage.

Merging Blink r164152 by Elliott Sprehn.

Instead of storing an OwnPtr to an object that has a pointer to the
ShareableElementData as well as a pointer into the ShareableElementData
and the length we can just store a RefPtr to the SharableElementData.

This also reduces the memory usage of the pool by 2 pointers per entry.

* dom/DocumentSharedObjectPool.h:
* dom/DocumentSharedObjectPool.cpp:
(WebCore::attributeHash):
(WebCore::hasSameAttributes):
(WebCore::DocumentSharedObjectPool::cachedShareableElementDataWithAttributes):

Modified Paths

Diff

Modified: trunk/Source/WebCore/ChangeLog (161209 => 161210)


--- trunk/Source/WebCore/ChangeLog	2014-01-02 18:11:51 UTC (rev 161209)
+++ trunk/Source/WebCore/ChangeLog	2014-01-02 18:45:33 UTC (rev 161210)
@@ -1,3 +1,21 @@
+2014-01-02  Andreas Kling  <akl...@apple.com>
+
+        Simplify the insides of DocumentSharedObjectPool and reduce memory usage.
+
+        Merging Blink r164152 by Elliott Sprehn.
+
+        Instead of storing an OwnPtr to an object that has a pointer to the
+        ShareableElementData as well as a pointer into the ShareableElementData
+        and the length we can just store a RefPtr to the SharableElementData.
+
+        This also reduces the memory usage of the pool by 2 pointers per entry.
+
+        * dom/DocumentSharedObjectPool.h:
+        * dom/DocumentSharedObjectPool.cpp:
+        (WebCore::attributeHash):
+        (WebCore::hasSameAttributes):
+        (WebCore::DocumentSharedObjectPool::cachedShareableElementDataWithAttributes):
+
 2014-01-02  Dirk Schulze  <k...@webkit.org>
 
         Support <box> values computed style for 'clip-path' property

Modified: trunk/Source/WebCore/dom/DocumentSharedObjectPool.cpp (161209 => 161210)


--- trunk/Source/WebCore/dom/DocumentSharedObjectPool.cpp	2014-01-02 18:11:51 UTC (rev 161209)
+++ trunk/Source/WebCore/dom/DocumentSharedObjectPool.cpp	2014-01-02 18:45:33 UTC (rev 161210)
@@ -31,64 +31,32 @@
 
 namespace WebCore {
 
-class ShareableElementDataCacheKey {
-public:
-    ShareableElementDataCacheKey(const Attribute* attributes, unsigned attributeCount)
-        : m_attributes(attributes)
-        , m_attributeCount(attributeCount)
-    { }
+inline unsigned attributeHash(const Vector<Attribute>& attributes)
+{
+    return StringHasher::hashMemory(attributes.data(), attributes.size() * sizeof(Attribute));
+}
 
-    bool operator!=(const ShareableElementDataCacheKey& other) const
-    {
-        if (m_attributeCount != other.m_attributeCount)
-            return true;
-        return memcmp(m_attributes, other.m_attributes, sizeof(Attribute) * m_attributeCount);
-    }
+inline bool hasSameAttributes(const Vector<Attribute>& attributes, ShareableElementData& elementData)
+{
+    if (attributes.size() != elementData.length())
+        return false;
+    return !memcmp(attributes.data(), elementData.m_attributeArray, attributes.size() * sizeof(Attribute));
+}
 
-    unsigned hash() const
-    {
-        return StringHasher::hashMemory(m_attributes, m_attributeCount * sizeof(Attribute));
-    }
-
-private:
-    const Attribute* m_attributes;
-    unsigned m_attributeCount;
-};
-
-class ShareableElementDataCacheEntry {
-public:
-    ShareableElementDataCacheEntry(const ShareableElementDataCacheKey& k, ShareableElementData& v)
-        : key(k)
-        , value(v)
-    { }
-
-    ShareableElementDataCacheKey key;
-    Ref<ShareableElementData> value;
-};
-
 PassRef<ShareableElementData> DocumentSharedObjectPool::cachedShareableElementDataWithAttributes(const Vector<Attribute>& attributes)
 {
     ASSERT(!attributes.isEmpty());
 
-    ShareableElementDataCacheKey cacheKey(attributes.data(), attributes.size());
-    unsigned cacheHash = cacheKey.hash();
+    auto& cachedData = m_shareableElementDataCache.add(attributeHash(attributes), nullptr).iterator->value;
 
-    ShareableElementDataCache::iterator cacheIterator = m_shareableElementDataCache.add(cacheHash, nullptr).iterator;
-    if (cacheIterator->value && cacheIterator->value->key != cacheKey)
-        cacheHash = 0;
+    // FIXME: This prevents sharing when there's a hash collision.
+    if (cachedData && !hasSameAttributes(attributes, *cachedData))
+        return ShareableElementData::createWithAttributes(attributes);
 
-    RefPtr<ShareableElementData> elementData;
-    if (cacheHash && cacheIterator->value)
-        elementData = &cacheIterator->value->value.get();
-    else
-        elementData = ShareableElementData::createWithAttributes(attributes);
+    if (!cachedData)
+        cachedData = ShareableElementData::createWithAttributes(attributes);
 
-    if (!cacheHash || cacheIterator->value)
-        return elementData.releaseNonNull();
-
-    cacheIterator->value = adoptPtr(new ShareableElementDataCacheEntry(ShareableElementDataCacheKey(elementData->m_attributeArray, elementData->length()), *elementData));
-
-    return elementData.releaseNonNull();
+    return *cachedData;
 }
 
 DocumentSharedObjectPool::DocumentSharedObjectPool()

Modified: trunk/Source/WebCore/dom/DocumentSharedObjectPool.h (161209 => 161210)


--- trunk/Source/WebCore/dom/DocumentSharedObjectPool.h	2014-01-02 18:11:51 UTC (rev 161209)
+++ trunk/Source/WebCore/dom/DocumentSharedObjectPool.h	2014-01-02 18:45:33 UTC (rev 161210)
@@ -29,13 +29,13 @@
 
 #include <wtf/HashMap.h>
 #include <wtf/PassOwnPtr.h>
+#include <wtf/RefPtr.h>
 #include <wtf/text/StringHash.h>
 
 namespace WebCore {
 
 class Attribute;
 class ShareableElementData;
-class ShareableElementDataCacheEntry;
 
 class DocumentSharedObjectPool {
 public:
@@ -47,7 +47,7 @@
 private:
     DocumentSharedObjectPool();
 
-    typedef HashMap<unsigned, OwnPtr<ShareableElementDataCacheEntry>, AlreadyHashed> ShareableElementDataCache;
+    typedef HashMap<unsigned, RefPtr<ShareableElementData>, AlreadyHashed> ShareableElementDataCache;
     ShareableElementDataCache m_shareableElementDataCache;
 };
 
_______________________________________________
webkit-changes mailing list
webkit-changes@lists.webkit.org
https://lists.webkit.org/mailman/listinfo/webkit-changes

Reply via email to