From 6882a04fb36642862b11efe514251d32070c3d65 Mon Sep 17 00:00:00 2001 From: Konstantin Tokarev Date: Thu, 25 Aug 2016 19:20:41 +0300 Subject: Imported QtWebKit TP3 (git b57bc6801f1876c3220d5a4bfea33d620d477443) Change-Id: I3b1d8a2808782c9f34d50240000e20cb38d3680f Reviewed-by: Konstantin Tokarev --- Source/WebCore/dom/DocumentSharedObjectPool.cpp | 80 +++++++------------------ 1 file changed, 20 insertions(+), 60 deletions(-) (limited to 'Source/WebCore/dom/DocumentSharedObjectPool.cpp') 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 v) - : key(k) - , value(v) - { } +inline unsigned attributeHash(const Vector& attributes) +{ + return StringHasher::hashMemory(attributes.data(), attributes.size() * sizeof(Attribute)); +} - ShareableElementDataCacheKey key; - RefPtr value; -}; +inline bool hasSameAttributes(const Vector& attributes, ShareableElementData& elementData) +{ + if (attributes.size() != elementData.length()) + return false; + return !memcmp(attributes.data(), elementData.m_attributeArray, attributes.size() * sizeof(Attribute)); +} -PassRefPtr DocumentSharedObjectPool::cachedShareableElementDataWithAttributes(const Vector& attributes) +Ref DocumentSharedObjectPool::cachedShareableElementDataWithAttributes(const Vector& 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 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; } } -- cgit v1.2.1