Title: [135429] trunk/Source/WebCore
Revision
135429
Author
[email protected]
Date
2012-11-21 13:38:24 -0800 (Wed, 21 Nov 2012)

Log Message

HTMLCollection should use the same storage as DynamicNodeList
https://bugs.webkit.org/show_bug.cgi?id=102886

Reviewed by Antti Koivisto.

This patch removes ElementRareData::m_collection. HTMLCollections are now held by
NodeListNodeData in NodeRareData like DynamicNodeLists. This simplifies several
Element and Node functions where we used to call into both ElementRareData and
NodeRareData to invalidate caches.

This is a refactoring and there should be no behavioral change.

* dom/DynamicNodeList.h:
* dom/Element.cpp:
(WebCore::ElementRareData::ensureCachedHTMLCollection):
* dom/ElementRareData.cpp:
(WebCore::ElementRareData::reportMemoryUsage):
* dom/ElementRareData.h:
(ElementRareData):
(WebCore::ElementRareData::cachedHTMLCollection):
(WebCore::ElementRareData::removeCachedHTMLCollection):
* dom/Node.cpp:
(WebCore::Node::invalidateNodeListCachesInAncestors):
* dom/NodeRareData.h:
(NodeListsNodeData):
(WebCore::NodeListsNodeData::addCacheWithAtomicName):
(WebCore::NodeListsNodeData::cacheWithAtomicName):
(WebCore::NodeListsNodeData::removeCacheWithAtomicName):
(WebCore::NodeListsNodeData::adoptTreeScope):
(WebCore::NodeListsNodeData::namedNodeListKey):
* dom/TreeScopeAdopter.cpp:
(WebCore::TreeScopeAdopter::moveTreeToNewScope):
* html/HTMLFormControlsCollection.cpp:
(WebCore::HTMLFormControlsCollection::create):
* html/HTMLFormControlsCollection.h:
(HTMLFormControlsCollection):
* html/HTMLOptionsCollection.cpp:
(WebCore::HTMLOptionsCollection::create):
* html/HTMLOptionsCollection.h:
(HTMLOptionsCollection::create):
* html/HTMLPropertiesCollection.h:
(HTMLPropertiesCollection::create):
* html/HTMLTableRowsCollection.cpp:
(WebCore::HTMLTableRowsCollection::create):
* html/HTMLTableRowsCollection.h:
(HTMLTableRowsCollection):

Modified Paths

Diff

Modified: trunk/Source/WebCore/ChangeLog (135428 => 135429)


--- trunk/Source/WebCore/ChangeLog	2012-11-21 21:29:40 UTC (rev 135428)
+++ trunk/Source/WebCore/ChangeLog	2012-11-21 21:38:24 UTC (rev 135429)
@@ -1,3 +1,52 @@
+2012-11-20  Ryosuke Niwa  <[email protected]>
+
+        HTMLCollection should use the same storage as DynamicNodeList
+        https://bugs.webkit.org/show_bug.cgi?id=102886
+
+        Reviewed by Antti Koivisto.
+
+        This patch removes ElementRareData::m_collection. HTMLCollections are now held by
+        NodeListNodeData in NodeRareData like DynamicNodeLists. This simplifies several
+        Element and Node functions where we used to call into both ElementRareData and
+        NodeRareData to invalidate caches.
+
+        This is a refactoring and there should be no behavioral change.
+
+        * dom/DynamicNodeList.h:
+        * dom/Element.cpp:
+        (WebCore::ElementRareData::ensureCachedHTMLCollection):
+        * dom/ElementRareData.cpp:
+        (WebCore::ElementRareData::reportMemoryUsage):
+        * dom/ElementRareData.h:
+        (ElementRareData):
+        (WebCore::ElementRareData::cachedHTMLCollection):
+        (WebCore::ElementRareData::removeCachedHTMLCollection):
+        * dom/Node.cpp:
+        (WebCore::Node::invalidateNodeListCachesInAncestors):
+        * dom/NodeRareData.h:
+        (NodeListsNodeData):
+        (WebCore::NodeListsNodeData::addCacheWithAtomicName):
+        (WebCore::NodeListsNodeData::cacheWithAtomicName):
+        (WebCore::NodeListsNodeData::removeCacheWithAtomicName):
+        (WebCore::NodeListsNodeData::adoptTreeScope):
+        (WebCore::NodeListsNodeData::namedNodeListKey):
+        * dom/TreeScopeAdopter.cpp:
+        (WebCore::TreeScopeAdopter::moveTreeToNewScope):
+        * html/HTMLFormControlsCollection.cpp:
+        (WebCore::HTMLFormControlsCollection::create):
+        * html/HTMLFormControlsCollection.h:
+        (HTMLFormControlsCollection):
+        * html/HTMLOptionsCollection.cpp:
+        (WebCore::HTMLOptionsCollection::create):
+        * html/HTMLOptionsCollection.h:
+        (HTMLOptionsCollection::create):
+        * html/HTMLPropertiesCollection.h:
+        (HTMLPropertiesCollection::create):
+        * html/HTMLTableRowsCollection.cpp:
+        (WebCore::HTMLTableRowsCollection::create):
+        * html/HTMLTableRowsCollection.h:
+        (HTMLTableRowsCollection):
+
 2012-11-21  Andreas Kling  <[email protected]>
 
         Update incorrect assertion in ImmutableElementAttributeData(MutableElementAttributeData).

Modified: trunk/Source/WebCore/dom/DynamicNodeList.h (135428 => 135429)


--- trunk/Source/WebCore/dom/DynamicNodeList.h	2012-11-21 21:29:40 UTC (rev 135428)
+++ trunk/Source/WebCore/dom/DynamicNodeList.h	2012-11-21 21:38:24 UTC (rev 135429)
@@ -199,6 +199,7 @@
         LabelsNodeListType,
         MicroDataItemListType,
         PropertyNodeListType,
+        PastLastNodeListType,
     };
     DynamicNodeList(PassRefPtr<Node> ownerNode, NodeListType type, NodeListRootType rootType, NodeListInvalidationType invalidationType)
         : DynamicNodeListCacheBase(ownerNode.get(), rootType, invalidationType, type == ChildNodeListType, NodeListCollectionType, DoesNotOverrideItemAfter)

Modified: trunk/Source/WebCore/dom/Element.cpp (135428 => 135429)


--- trunk/Source/WebCore/dom/Element.cpp	2012-11-21 21:29:40 UTC (rev 135428)
+++ trunk/Source/WebCore/dom/Element.cpp	2012-11-21 21:38:24 UTC (rev 135429)
@@ -2419,33 +2419,25 @@
 
 PassRefPtr<HTMLCollection> ElementRareData::ensureCachedHTMLCollection(Element* element, CollectionType type)
 {
-    if (!m_cachedCollections) {
-        m_cachedCollections = adoptPtr(new CachedHTMLCollectionArray);
-        for (unsigned i = 0; i < NumNodeCollectionTypes; i++)
-            (*m_cachedCollections)[i] = 0;
-    }
-
-    if (HTMLCollection* collection = (*m_cachedCollections)[type - FirstNodeCollectionType])
+    if (HTMLCollection* collection = cachedHTMLCollection(type))
         return collection;
 
     RefPtr<HTMLCollection> collection;
     if (type == TableRows) {
         ASSERT(element->hasTagName(tableTag));
-        collection = HTMLTableRowsCollection::create(element);
+        return ensureNodeLists()->addCacheWithAtomicName<HTMLTableRowsCollection>(element, type);
     } else if (type == SelectOptions) {
         ASSERT(element->hasTagName(selectTag));
-        collection = HTMLOptionsCollection::create(element);
+        return ensureNodeLists()->addCacheWithAtomicName<HTMLOptionsCollection>(element, type);
     } else if (type == FormControls) {
         ASSERT(element->hasTagName(formTag) || element->hasTagName(fieldsetTag));
-        collection = HTMLFormControlsCollection::create(element);
+        return ensureNodeLists()->addCacheWithAtomicName<HTMLFormControlsCollection>(element, type);
 #if ENABLE(MICRODATA)
-    } else if (type == ItemProperties) {
-        collection = HTMLPropertiesCollection::create(element);
+    } else if (nodeLists()->type == ItemProperties) {
+        return ensureNodeLists()->addCacheWithAtomicName<HTMLPropertiesCollection>(element, type);
 #endif
-    } else
-        collection = HTMLCollection::create(element, type);
-    (*m_cachedCollections)[type - FirstNodeCollectionType] = collection.get();
-    return collection.release();
+    }
+    return ensureNodeLists()->addCacheWithAtomicName<HTMLCollection>(element, type);
 }
 
 HTMLCollection* Element::cachedHTMLCollection(CollectionType type)

Modified: trunk/Source/WebCore/dom/ElementRareData.cpp (135428 => 135429)


--- trunk/Source/WebCore/dom/ElementRareData.cpp	2012-11-21 21:29:40 UTC (rev 135428)
+++ trunk/Source/WebCore/dom/ElementRareData.cpp	2012-11-21 21:38:24 UTC (rev 135429)
@@ -40,7 +40,6 @@
 {
     MemoryClassInfo info(memoryObjectInfo, this, WebCoreMemoryTypes::DOM);
     NodeRareData::reportMemoryUsage(memoryObjectInfo);
-    info.addMember(m_cachedCollections);
     info.addMember(m_computedStyle);
 
     info.addMember(m_datasetDOMStringMap);

Modified: trunk/Source/WebCore/dom/ElementRareData.h (135428 => 135429)


--- trunk/Source/WebCore/dom/ElementRareData.h	2012-11-21 21:29:40 UTC (rev 135428)
+++ trunk/Source/WebCore/dom/ElementRareData.h	2012-11-21 21:38:24 UTC (rev 135429)
@@ -58,60 +58,20 @@
     using NodeRareData::setIsInTopLayer;
 #endif
 
-    bool hasCachedHTMLCollections() const
-    {
-        return m_cachedCollections;
-    }
-
     PassRefPtr<HTMLCollection> ensureCachedHTMLCollection(Element*, CollectionType);
     HTMLCollection* cachedHTMLCollection(CollectionType type)
     {
-        if (!m_cachedCollections)
-            return 0;
-
-        return (*m_cachedCollections)[type - FirstNodeCollectionType];
+        return nodeLists() ? nodeLists()->cacheWithAtomicName<HTMLCollection>(type) : 0;
     }
 
     void removeCachedHTMLCollection(HTMLCollection* collection, CollectionType type)
     {
-        ASSERT(m_cachedCollections);
-        ASSERT_UNUSED(collection, (*m_cachedCollections)[type - FirstNodeCollectionType] == collection);
-        (*m_cachedCollections)[type - FirstNodeCollectionType] = 0;
+        ASSERT(nodeLists());
+        nodeLists()->removeCacheWithAtomicName(collection, type);
     }
 
-    void clearHTMLCollectionCaches(const QualifiedName* attrName)
-    {
-        if (!m_cachedCollections)
-            return;
-
-        for (unsigned i = 0; i < (*m_cachedCollections).size(); i++) {
-            if (HTMLCollection* collection = (*m_cachedCollections)[i])
-                collection->invalidateCache(attrName);
-        }
-    }
-
-    void adoptTreeScope(Document* oldDocument, Document* newDocument)
-    {
-        if (!m_cachedCollections)
-            return;
-
-        for (unsigned i = 0; i < (*m_cachedCollections).size(); i++) {
-            HTMLCollection* collection = (*m_cachedCollections)[i];
-            if (!collection)
-                continue;
-            collection->invalidateCache();
-            if (oldDocument != newDocument) {
-                oldDocument->unregisterNodeListCache(collection);
-                newDocument->registerNodeListCache(collection);
-            }
-        }
-    }
-
     virtual void reportMemoryUsage(MemoryObjectInfo*) const OVERRIDE;
 
-    typedef FixedArray<HTMLCollection*, NumNodeCollectionTypes> CachedHTMLCollectionArray;
-    OwnPtr<CachedHTMLCollectionArray> m_cachedCollections;
-
     LayoutSize m_minimumSizeForResizing;
     RefPtr<RenderStyle> m_computedStyle;
 

Modified: trunk/Source/WebCore/dom/Node.cpp (135428 => 135429)


--- trunk/Source/WebCore/dom/Node.cpp	2012-11-21 21:29:40 UTC (rev 135428)
+++ trunk/Source/WebCore/dom/Node.cpp	2012-11-21 21:38:24 UTC (rev 135429)
@@ -1022,8 +1022,6 @@
         NodeRareData* data = ""
         if (data->nodeLists())
             data->nodeLists()->invalidateCaches(attrName);
-        if (node->isElementNode())
-            static_cast<ElementRareData*>(data)->clearHTMLCollectionCaches(attrName);
     }
 }
 

Modified: trunk/Source/WebCore/dom/NodeRareData.h (135428 => 135429)


--- trunk/Source/WebCore/dom/NodeRareData.h	2012-11-21 21:29:40 UTC (rev 135428)
+++ trunk/Source/WebCore/dom/NodeRareData.h	2012-11-21 21:38:24 UTC (rev 135429)
@@ -59,7 +59,7 @@
         static const bool safeToCompareToEmptyOrDeleted = DefaultHash<StringType>::Hash::safeToCompareToEmptyOrDeleted;
     };
 
-    typedef HashMap<std::pair<unsigned char, AtomicString>, DynamicSubtreeNodeList*, NodeListCacheMapEntryHash<AtomicString> > NodeListAtomicNameCacheMap;
+    typedef HashMap<std::pair<unsigned char, AtomicString>, DynamicNodeListCacheBase*, NodeListCacheMapEntryHash<AtomicString> > NodeListAtomicNameCacheMap;
     typedef HashMap<std::pair<unsigned char, String>, DynamicSubtreeNodeList*, NodeListCacheMapEntryHash<String> > NodeListNameCacheMap;
     typedef HashMap<QualifiedName, TagNodeList*> TagNodeListCacheNS;
 
@@ -76,6 +76,26 @@
     }
 
     template<typename T>
+    PassRefPtr<T> addCacheWithAtomicName(Element* node, CollectionType collectionType)
+    {
+        unsigned char type = DynamicNodeList::PastLastNodeListType + collectionType - FirstNodeCollectionType;
+        NodeListAtomicNameCacheMap::AddResult result = m_atomicNameCaches.add(namedNodeListKey(type, starAtom), 0);
+        if (!result.isNewEntry)
+            return static_cast<T*>(result.iterator->value);
+
+        RefPtr<T> list = T::create(node, collectionType);
+        result.iterator->value = list.get();
+        return list.release();
+    }
+
+    template<typename T>
+    T* cacheWithAtomicName(CollectionType collectionType)
+    {
+        unsigned char type = DynamicNodeList::PastLastNodeListType + collectionType - FirstNodeCollectionType;
+        return static_cast<T*>(m_atomicNameCaches.get(namedNodeListKey(type, starAtom)));
+    }
+
+    template<typename T>
     PassRefPtr<T> addCacheWithName(Node* node, DynamicNodeList::NodeListType listType, const String& name)
     {
         NodeListNameCacheMap::AddResult result = m_nameCaches.add(namedNodeListKey(listType, name), 0);
@@ -99,12 +119,19 @@
         return list.release();
     }
 
-    void removeCacheWithAtomicName(DynamicSubtreeNodeList* list, DynamicNodeList::NodeListType listType, const AtomicString& name)
+    void removeCacheWithAtomicName(DynamicNodeListCacheBase* list, DynamicNodeList::NodeListType listType, const AtomicString& name)
     {
         ASSERT_UNUSED(list, list == m_atomicNameCaches.get(namedNodeListKey(listType, name)));
         m_atomicNameCaches.remove(namedNodeListKey(listType, name));
     }
 
+    void removeCacheWithAtomicName(DynamicNodeListCacheBase* list, CollectionType collectionType)
+    {
+        unsigned char type = DynamicNodeList::PastLastNodeListType + collectionType - FirstNodeCollectionType;
+        ASSERT_UNUSED(list, list == m_atomicNameCaches.get(namedNodeListKey(type, starAtom)));
+        m_atomicNameCaches.remove(namedNodeListKey(type, starAtom));
+    }
+
     void removeCacheWithName(DynamicSubtreeNodeList* list, DynamicNodeList::NodeListType listType, const String& name)
     {
         ASSERT_UNUSED(list, list == m_nameCaches.get(namedNodeListKey(listType, name)));
@@ -136,21 +163,21 @@
         if (oldDocument != newDocument) {
             NodeListAtomicNameCacheMap::const_iterator atomicNameCacheEnd = m_atomicNameCaches.end();
             for (NodeListAtomicNameCacheMap::const_iterator it = m_atomicNameCaches.begin(); it != atomicNameCacheEnd; ++it) {
-                DynamicSubtreeNodeList* list = it->value;
+                DynamicNodeListCacheBase* list = it->value;
                 oldDocument->unregisterNodeListCache(list);
                 newDocument->registerNodeListCache(list);
             }
 
             NodeListNameCacheMap::const_iterator nameCacheEnd = m_nameCaches.end();
             for (NodeListNameCacheMap::const_iterator it = m_nameCaches.begin(); it != nameCacheEnd; ++it) {
-                DynamicSubtreeNodeList* list = it->value;
+                DynamicNodeListCacheBase* list = it->value;
                 oldDocument->unregisterNodeListCache(list);
                 newDocument->registerNodeListCache(list);
             }
 
             TagNodeListCacheNS::const_iterator tagEnd = m_tagNodeListCacheNS.end();
             for (TagNodeListCacheNS::const_iterator it = m_tagNodeListCacheNS.begin(); it != tagEnd; ++it) {
-                DynamicSubtreeNodeList* list = it->value;
+                DynamicNodeListCacheBase* list = it->value;
                 ASSERT(!list->isRootedAtDocument());
                 oldDocument->unregisterNodeListCache(list);
                 newDocument->registerNodeListCache(list);
@@ -163,12 +190,12 @@
 private:
     NodeListsNodeData() { }
 
-    std::pair<unsigned char, AtomicString> namedNodeListKey(DynamicNodeList::NodeListType listType, const AtomicString& name)
+    std::pair<unsigned char, AtomicString> namedNodeListKey(unsigned char listType, const AtomicString& name)
     {
         return std::pair<unsigned char, AtomicString>(listType, name);
     }
 
-    std::pair<unsigned char, String> namedNodeListKey(DynamicNodeList::NodeListType listType, const String& name)
+    std::pair<unsigned char, String> namedNodeListKey(unsigned char listType, const String& name)
     {
         return std::pair<unsigned char, String>(listType, name);
     }

Modified: trunk/Source/WebCore/dom/TreeScopeAdopter.cpp (135428 => 135429)


--- trunk/Source/WebCore/dom/TreeScopeAdopter.cpp	2012-11-21 21:29:40 UTC (rev 135428)
+++ trunk/Source/WebCore/dom/TreeScopeAdopter.cpp	2012-11-21 21:38:24 UTC (rev 135429)
@@ -52,8 +52,6 @@
         if (NodeRareData* rareData = node->setTreeScope(newDocument == m_newScope ? 0 : m_newScope)) {
             if (rareData->nodeLists())
                 rareData->nodeLists()->adoptTreeScope(oldDocument, newDocument);
-            if (node->isElementNode())
-                static_cast<ElementRareData*>(rareData)->adoptTreeScope(oldDocument, newDocument);
         }
 
         if (willMoveToNewDocument)

Modified: trunk/Source/WebCore/html/HTMLFormControlsCollection.cpp (135428 => 135429)


--- trunk/Source/WebCore/html/HTMLFormControlsCollection.cpp	2012-11-21 21:29:40 UTC (rev 135428)
+++ trunk/Source/WebCore/html/HTMLFormControlsCollection.cpp	2012-11-21 21:38:24 UTC (rev 135429)
@@ -42,7 +42,7 @@
     ASSERT(base->hasTagName(formTag) || base->hasTagName(fieldsetTag));
 }
 
-PassRefPtr<HTMLFormControlsCollection> HTMLFormControlsCollection::create(Element* base)
+PassRefPtr<HTMLFormControlsCollection> HTMLFormControlsCollection::create(Element* base, CollectionType)
 {
     return adoptRef(new HTMLFormControlsCollection(base));
 }

Modified: trunk/Source/WebCore/html/HTMLFormControlsCollection.h (135428 => 135429)


--- trunk/Source/WebCore/html/HTMLFormControlsCollection.h	2012-11-21 21:29:40 UTC (rev 135428)
+++ trunk/Source/WebCore/html/HTMLFormControlsCollection.h	2012-11-21 21:38:24 UTC (rev 135429)
@@ -37,7 +37,7 @@
 
 class HTMLFormControlsCollection : public HTMLCollection {
 public:
-    static PassRefPtr<HTMLFormControlsCollection> create(Element*);
+    static PassRefPtr<HTMLFormControlsCollection> create(Element*, CollectionType);
 
     virtual ~HTMLFormControlsCollection();
 

Modified: trunk/Source/WebCore/html/HTMLOptionsCollection.cpp (135428 => 135429)


--- trunk/Source/WebCore/html/HTMLOptionsCollection.cpp	2012-11-21 21:29:40 UTC (rev 135428)
+++ trunk/Source/WebCore/html/HTMLOptionsCollection.cpp	2012-11-21 21:38:24 UTC (rev 135429)
@@ -33,7 +33,7 @@
     ASSERT(select->hasTagName(HTMLNames::selectTag));
 }
 
-PassRefPtr<HTMLOptionsCollection> HTMLOptionsCollection::create(Element* select)
+PassRefPtr<HTMLOptionsCollection> HTMLOptionsCollection::create(Element* select, CollectionType)
 {
     return adoptRef(new HTMLOptionsCollection(select));
 }

Modified: trunk/Source/WebCore/html/HTMLOptionsCollection.h (135428 => 135429)


--- trunk/Source/WebCore/html/HTMLOptionsCollection.h	2012-11-21 21:29:40 UTC (rev 135428)
+++ trunk/Source/WebCore/html/HTMLOptionsCollection.h	2012-11-21 21:38:24 UTC (rev 135429)
@@ -35,7 +35,7 @@
 
 class HTMLOptionsCollection : public HTMLCollection {
 public:
-    static PassRefPtr<HTMLOptionsCollection> create(Element*);
+    static PassRefPtr<HTMLOptionsCollection> create(Element*, CollectionType);
 
     void add(PassRefPtr<HTMLOptionElement>, ExceptionCode&);
     void add(PassRefPtr<HTMLOptionElement>, int index, ExceptionCode&);

Modified: trunk/Source/WebCore/html/HTMLPropertiesCollection.h (135428 => 135429)


--- trunk/Source/WebCore/html/HTMLPropertiesCollection.h	2012-11-21 21:29:40 UTC (rev 135428)
+++ trunk/Source/WebCore/html/HTMLPropertiesCollection.h	2012-11-21 21:38:24 UTC (rev 135429)
@@ -43,7 +43,7 @@
 
 class HTMLPropertiesCollection : public HTMLCollection {
 public:
-    static PassRefPtr<HTMLPropertiesCollection> create(Node*);
+    static PassRefPtr<HTMLPropertiesCollection> create(Node*, CollectionType);
     virtual ~HTMLPropertiesCollection();
 
     void updateRefElements() const;

Modified: trunk/Source/WebCore/html/HTMLTableRowsCollection.cpp (135428 => 135429)


--- trunk/Source/WebCore/html/HTMLTableRowsCollection.cpp	2012-11-21 21:29:40 UTC (rev 135428)
+++ trunk/Source/WebCore/html/HTMLTableRowsCollection.cpp	2012-11-21 21:38:24 UTC (rev 135429)
@@ -157,7 +157,7 @@
     ASSERT(table->hasTagName(tableTag));
 }
 
-PassRefPtr<HTMLTableRowsCollection> HTMLTableRowsCollection::create(Element* table)
+PassRefPtr<HTMLTableRowsCollection> HTMLTableRowsCollection::create(Element* table, CollectionType)
 {
     return adoptRef(new HTMLTableRowsCollection(table));
 }

Modified: trunk/Source/WebCore/html/HTMLTableRowsCollection.h (135428 => 135429)


--- trunk/Source/WebCore/html/HTMLTableRowsCollection.h	2012-11-21 21:29:40 UTC (rev 135428)
+++ trunk/Source/WebCore/html/HTMLTableRowsCollection.h	2012-11-21 21:38:24 UTC (rev 135429)
@@ -38,7 +38,7 @@
 
 class HTMLTableRowsCollection : public HTMLCollection {
 public:
-    static PassRefPtr<HTMLTableRowsCollection> create(Element*);
+    static PassRefPtr<HTMLTableRowsCollection> create(Element*, CollectionType);
 
     static HTMLTableRowElement* rowAfter(HTMLTableElement*, HTMLTableRowElement*);
     static HTMLTableRowElement* lastRow(HTMLTableElement*);
_______________________________________________
webkit-changes mailing list
[email protected]
http://lists.webkit.org/mailman/listinfo/webkit-changes

Reply via email to