summaryrefslogtreecommitdiff
path: root/Source/WebCore/dom/DocumentSharedObjectPool.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'Source/WebCore/dom/DocumentSharedObjectPool.cpp')
-rw-r--r--Source/WebCore/dom/DocumentSharedObjectPool.cpp80
1 files changed, 20 insertions, 60 deletions
diff --git a/Source/WebCore/dom/DocumentSharedObjectPool.cpp b/Source/WebCore/dom/DocumentSharedObjectPool.cpp
index 997e8668d..b5f721cae 100644
--- a/Source/WebCore/dom/DocumentSharedObjectPool.cpp
+++ b/Source/WebCore/dom/DocumentSharedObjectPool.cpp
@@ -10,10 +10,10 @@
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
- * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY
+ * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
- * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE COMPUTER, INC. OR
+ * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
@@ -31,72 +31,32 @@
namespace WebCore {
-class ShareableElementDataCacheKey {
-public:
- ShareableElementDataCacheKey(const Attribute* attributes, unsigned attributeCount)
- : m_attributes(attributes)
- , m_attributeCount(attributeCount)
- { }
-
- 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);
- }
-
- 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, PassRefPtr<ShareableElementData> v)
- : key(k)
- , value(v)
- { }
+inline unsigned attributeHash(const Vector<Attribute>& attributes)
+{
+ return StringHasher::hashMemory(attributes.data(), attributes.size() * sizeof(Attribute));
+}
- ShareableElementDataCacheKey key;
- RefPtr<ShareableElementData> value;
-};
+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));
+}
-PassRefPtr<ShareableElementData> DocumentSharedObjectPool::cachedShareableElementDataWithAttributes(const Vector<Attribute>& attributes)
+Ref<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;
- else
- elementData = ShareableElementData::createWithAttributes(attributes);
+ if (!cachedData)
+ cachedData = ShareableElementData::createWithAttributes(attributes);
- if (!cacheHash || cacheIterator->value)
- return elementData.release();
-
- cacheIterator->value = adoptPtr(new ShareableElementDataCacheEntry(ShareableElementDataCacheKey(elementData->m_attributeArray, elementData->length()), elementData));
-
- return elementData.release();
-}
-
-DocumentSharedObjectPool::DocumentSharedObjectPool()
-{
-}
-
-DocumentSharedObjectPool::~DocumentSharedObjectPool()
-{
+ return *cachedData;
}
}