Title: [102147] trunk/Source/WebCore
Revision
102147
Author
[email protected]
Date
2011-12-06 09:30:00 -0800 (Tue, 06 Dec 2011)

Log Message

Use HashMap<OwnPtr> in CollectionCache
https://bugs.webkit.org/show_bug.cgi?id=73784

Reviewed by Andreas Kling.

* html/CollectionCache.cpp:
(WebCore::CollectionCache::copyCacheMap): Use adoptPtr.
(WebCore::CollectionCache::reset): Removed now-unneeded calls to deleteAllValues.
(WebCore::append): Added. Helper function for appending elements to the maps from
the collection cache.

* html/CollectionCache.h: Changed mapped type in NodeCacheMap to OwnPtr.
Added append function.

* html/HTMLCollection.cpp:
(WebCore::nameShouldBeVisibleInDocumentAll): Added, to factor out common code in
two functions below.
(WebCore::HTMLCollection::checkForNameMatch): Changed to call nameShouldBeVisibleInDocumentAll.
(WebCore::HTMLCollection::updateNameCache): Ditto. Also updated cache code to use the append
function, so it will work with OwnPtr. Also eliminated an unneeded get call before
each hash table add; we do both at once in the new append function.
* html/HTMLFormCollection.cpp:
(WebCore::HTMLFormCollection::updateNameCache): More of the same.

Modified Paths

Diff

Modified: trunk/Source/WebCore/ChangeLog (102146 => 102147)


--- trunk/Source/WebCore/ChangeLog	2011-12-06 17:21:11 UTC (rev 102146)
+++ trunk/Source/WebCore/ChangeLog	2011-12-06 17:30:00 UTC (rev 102147)
@@ -1,3 +1,29 @@
+2011-12-06  Darin Adler  <[email protected]>
+
+        Use HashMap<OwnPtr> in CollectionCache
+        https://bugs.webkit.org/show_bug.cgi?id=73784
+
+        Reviewed by Andreas Kling.
+
+        * html/CollectionCache.cpp:
+        (WebCore::CollectionCache::copyCacheMap): Use adoptPtr.
+        (WebCore::CollectionCache::reset): Removed now-unneeded calls to deleteAllValues.
+        (WebCore::append): Added. Helper function for appending elements to the maps from
+        the collection cache.
+
+        * html/CollectionCache.h: Changed mapped type in NodeCacheMap to OwnPtr.
+        Added append function.
+
+        * html/HTMLCollection.cpp:
+        (WebCore::nameShouldBeVisibleInDocumentAll): Added, to factor out common code in
+        two functions below.
+        (WebCore::HTMLCollection::checkForNameMatch): Changed to call nameShouldBeVisibleInDocumentAll.
+        (WebCore::HTMLCollection::updateNameCache): Ditto. Also updated cache code to use the append
+        function, so it will work with OwnPtr. Also eliminated an unneeded get call before
+        each hash table add; we do both at once in the new append function.
+        * html/HTMLFormCollection.cpp:
+        (WebCore::HTMLFormCollection::updateNameCache): More of the same.
+
 2011-12-06  Yury Semikhatsky  <[email protected]>
 
         [Chromium] Web Inspector: getFunctionLocation should return scriptId as String not as int

Modified: trunk/Source/WebCore/html/CollectionCache.cpp (102146 => 102147)


--- trunk/Source/WebCore/html/CollectionCache.cpp	2011-12-06 17:21:11 UTC (rev 102146)
+++ trunk/Source/WebCore/html/CollectionCache.cpp	2011-12-06 17:30:00 UTC (rev 102147)
@@ -1,5 +1,5 @@
 /*
- * Copyright (C) 2004, 2005, 2006, 2007, 2008 Apple Inc. All rights reserved.
+ * Copyright (C) 2004, 2005, 2006, 2007, 2008, 2011 Apple Inc. All rights reserved.
  *
  * This library is free software; you can redistribute it and/or
  * modify it under the terms of the GNU Library General Public
@@ -21,6 +21,9 @@
 #include "config.h"
 #include "CollectionCache.h"
 
+#include <wtf/PassOwnPtr.h>
+#include <wtf/text/AtomicString.h>
+
 namespace WebCore {
 
 CollectionCache::CollectionCache()
@@ -34,7 +37,7 @@
     ASSERT(dest.isEmpty());
     NodeCacheMap::const_iterator end = src.end();
     for (NodeCacheMap::const_iterator it = src.begin(); it != end; ++it)
-        dest.add(it->first, new Vector<Element*>(*it->second));
+        dest.add(it->first, adoptPtr(new Vector<Element*>(*it->second)));
 }
 
 CollectionCache::CollectionCache(const CollectionCache& other)
@@ -65,12 +68,6 @@
     std::swap(hasNameCache, other.hasNameCache);
 }
 
-CollectionCache::~CollectionCache()
-{
-    deleteAllValues(idCache);
-    deleteAllValues(nameCache);
-}
-
 void CollectionCache::reset()
 {
     current = 0;
@@ -78,9 +75,7 @@
     length = 0;
     hasLength = false;
     elementsArrayPosition = 0;
-    deleteAllValues(idCache);
     idCache.clear();
-    deleteAllValues(nameCache);
     nameCache.clear();
     hasNameCache = false;
 }
@@ -93,4 +88,12 @@
 }
 #endif
 
+void append(CollectionCache::NodeCacheMap& map, const AtomicString& key, Element* element)
+{
+    OwnPtr<Vector<Element*> >& vector = map.add(key.impl(), nullptr).first->second;
+    if (!vector)
+        vector = adoptPtr(new Vector<Element*>);
+    vector->append(element);
+}
+
 } // namespace WebCore

Modified: trunk/Source/WebCore/html/CollectionCache.h (102146 => 102147)


--- trunk/Source/WebCore/html/CollectionCache.h	2011-12-06 17:21:11 UTC (rev 102146)
+++ trunk/Source/WebCore/html/CollectionCache.h	2011-12-06 17:30:00 UTC (rev 102147)
@@ -40,14 +40,13 @@
         swap(tmp);
         return *this;
     }
-    ~CollectionCache();
 
     void reset();
     void swap(CollectionCache&);
 
     void checkConsistency();
 
-    typedef HashMap<AtomicStringImpl*, Vector<Element*>*> NodeCacheMap;
+    typedef HashMap<AtomicStringImpl*, OwnPtr<Vector<Element*> > > NodeCacheMap;
 
     uint64_t version;
     Element* current;
@@ -63,8 +62,10 @@
     static void copyCacheMap(NodeCacheMap&, const NodeCacheMap&);
 };
 
+void append(CollectionCache::NodeCacheMap&, const AtomicString&, Element*);
+    
 #if ASSERT_DISABLED
-    inline void CollectionCache::checkConsistency() { }
+inline void CollectionCache::checkConsistency() { }
 #endif
 
 } // namespace

Modified: trunk/Source/WebCore/html/HTMLCollection.cpp (102146 => 102147)


--- trunk/Source/WebCore/html/HTMLCollection.cpp	2011-12-06 17:21:11 UTC (rev 102146)
+++ trunk/Source/WebCore/html/HTMLCollection.cpp	2011-12-06 17:30:00 UTC (rev 102147)
@@ -1,7 +1,7 @@
 /*
  * Copyright (C) 1999 Lars Knoll ([email protected])
  *           (C) 1999 Antti Koivisto ([email protected])
- * Copyright (C) 2003, 2004, 2005, 2006, 2007, 2008 Apple Inc. All rights reserved.
+ * Copyright (C) 2003, 2004, 2005, 2006, 2007, 2008, 2011 Apple Inc. All rights reserved.
  *
  * This library is free software; you can redistribute it and/or
  * modify it under the terms of the GNU Library General Public
@@ -266,6 +266,19 @@
      return retval;
 }
 
+static inline bool nameShouldBeVisibleInDocumentAll(HTMLElement* element)
+{
+    // The document.all collection returns only certain types of elements by name,
+    // although it returns any type of element by id.
+    return element->hasLocalName(appletTag)
+        || element->hasLocalName(embedTag)
+        || element->hasLocalName(formTag)
+        || element->hasLocalName(imgTag)
+        || element->hasLocalName(inputTag)
+        || element->hasLocalName(objectTag)
+        || element->hasLocalName(selectTag);
+}
+
 bool HTMLCollection::checkForNameMatch(Element* element, bool checkName, const AtomicString& name) const
 {
     if (!element->isHTMLElement())
@@ -275,13 +288,7 @@
     if (!checkName)
         return e->getIdAttribute() == name;
 
-    // document.all returns only images, forms, applets, objects and embeds
-    // by name (though everything by id)
-    if (m_type == DocAll && 
-        !(e->hasLocalName(imgTag) || e->hasLocalName(formTag) ||
-          e->hasLocalName(appletTag) || e->hasLocalName(objectTag) ||
-          e->hasLocalName(embedTag) || e->hasLocalName(inputTag) ||
-          e->hasLocalName(selectTag)))
+    if (m_type == DocAll && !nameShouldBeVisibleInDocumentAll(e))
         return false;
 
     return e->getAttribute(nameAttr) == name && e->getIdAttribute() != name;
@@ -325,29 +332,10 @@
         HTMLElement* e = toHTMLElement(element);
         const AtomicString& idAttrVal = e->getIdAttribute();
         const AtomicString& nameAttrVal = e->getAttribute(nameAttr);
-        if (!idAttrVal.isEmpty()) {
-            // add to id cache
-            Vector<Element*>* idVector = m_info->idCache.get(idAttrVal.impl());
-            if (!idVector) {
-                idVector = new Vector<Element*>;
-                m_info->idCache.add(idAttrVal.impl(), idVector);
-            }
-            idVector->append(e);
-        }
-        if (!nameAttrVal.isEmpty() && idAttrVal != nameAttrVal
-            && (m_type != DocAll || 
-                (e->hasLocalName(imgTag) || e->hasLocalName(formTag) ||
-                 e->hasLocalName(appletTag) || e->hasLocalName(objectTag) ||
-                 e->hasLocalName(embedTag) || e->hasLocalName(inputTag) ||
-                 e->hasLocalName(selectTag)))) {
-            // add to name cache
-            Vector<Element*>* nameVector = m_info->nameCache.get(nameAttrVal.impl());
-            if (!nameVector) {
-                nameVector = new Vector<Element*>;
-                m_info->nameCache.add(nameAttrVal.impl(), nameVector);
-            }
-            nameVector->append(e);
-        }
+        if (!idAttrVal.isEmpty())
+            append(m_info->idCache, idAttrVal, e);
+        if (!nameAttrVal.isEmpty() && idAttrVal != nameAttrVal && (m_type != DocAll || nameShouldBeVisibleInDocumentAll(e)))
+            append(m_info->nameCache, nameAttrVal, e);
     }
 
     m_info->hasNameCache = true;

Modified: trunk/Source/WebCore/html/HTMLFormCollection.cpp (102146 => 102147)


--- trunk/Source/WebCore/html/HTMLFormCollection.cpp	2011-12-06 17:21:11 UTC (rev 102146)
+++ trunk/Source/WebCore/html/HTMLFormCollection.cpp	2011-12-06 17:30:00 UTC (rev 102147)
@@ -170,23 +170,11 @@
             const AtomicString& idAttrVal = element->getIdAttribute();
             const AtomicString& nameAttrVal = element->getAttribute(nameAttr);
             if (!idAttrVal.isEmpty()) {
-                // add to id cache
-                Vector<Element*>* idVector = info()->idCache.get(idAttrVal.impl());
-                if (!idVector) {
-                    idVector = new Vector<Element*>;
-                    info()->idCache.add(idAttrVal.impl(), idVector);
-                }
-                idVector->append(element);
+                append(info()->idCache, idAttrVal, element);
                 foundInputElements.add(idAttrVal.impl());
             }
             if (!nameAttrVal.isEmpty() && idAttrVal != nameAttrVal) {
-                // add to name cache
-                Vector<Element*>* nameVector = info()->nameCache.get(nameAttrVal.impl());
-                if (!nameVector) {
-                    nameVector = new Vector<Element*>;
-                    info()->nameCache.add(nameAttrVal.impl(), nameVector);
-                }
-                nameVector->append(element);
+                append(info()->nameCache, nameAttrVal, element);
                 foundInputElements.add(nameAttrVal.impl());
             }
         }
@@ -196,24 +184,10 @@
         HTMLImageElement* element = f->m_imageElements[i];
         const AtomicString& idAttrVal = element->getIdAttribute();
         const AtomicString& nameAttrVal = element->getAttribute(nameAttr);
-        if (!idAttrVal.isEmpty() && !foundInputElements.contains(idAttrVal.impl())) {
-            // add to id cache
-            Vector<Element*>* idVector = info()->idCache.get(idAttrVal.impl());
-            if (!idVector) {
-                idVector = new Vector<Element*>;
-                info()->idCache.add(idAttrVal.impl(), idVector);
-            }
-            idVector->append(element);
-        }
-        if (!nameAttrVal.isEmpty() && idAttrVal != nameAttrVal && !foundInputElements.contains(nameAttrVal.impl())) {
-            // add to name cache
-            Vector<Element*>* nameVector = info()->nameCache.get(nameAttrVal.impl());
-            if (!nameVector) {
-                nameVector = new Vector<Element*>;
-                info()->nameCache.add(nameAttrVal.impl(), nameVector);
-            }
-            nameVector->append(element);
-        }
+        if (!idAttrVal.isEmpty() && !foundInputElements.contains(idAttrVal.impl()))
+            append(info()->idCache, idAttrVal, element);
+        if (!nameAttrVal.isEmpty() && idAttrVal != nameAttrVal && !foundInputElements.contains(nameAttrVal.impl()))
+            append(info()->nameCache, nameAttrVal, element);
     }
 
     info()->hasNameCache = true;
_______________________________________________
webkit-changes mailing list
[email protected]
http://lists.webkit.org/mailman/listinfo.cgi/webkit-changes

Reply via email to