Title: [151359] trunk/Source/WebCore
Revision
151359
Author
[email protected]
Date
2013-06-09 13:57:49 -0700 (Sun, 09 Jun 2013)

Log Message

Split the 3 paths of SelectorDataList::execute() into 3 separate functions
https://bugs.webkit.org/show_bug.cgi?id=117378

Patch by Benjamin Poulain <[email protected]> on 2013-06-09
Reviewed by Ryosuke Niwa.

Split those to have several small execute() fucntions instead of a giant one.

* dom/SelectorQuery.cpp:
(WebCore::selectorForIdLookup):
(WebCore::isTreeScopeRoot):
(WebCore::SelectorDataList::executeFastPathForIdSelector):
(WebCore::SelectorDataList::executeSingleSelectorData):
(WebCore::SelectorDataList::executeSingleMultiSelectorData):
(WebCore::SelectorDataList::execute):
* dom/SelectorQuery.h:

Modified Paths

Diff

Modified: trunk/Source/WebCore/ChangeLog (151358 => 151359)


--- trunk/Source/WebCore/ChangeLog	2013-06-09 20:56:29 UTC (rev 151358)
+++ trunk/Source/WebCore/ChangeLog	2013-06-09 20:57:49 UTC (rev 151359)
@@ -1,5 +1,23 @@
 2013-06-09  Benjamin Poulain  <[email protected]>
 
+        Split the 3 paths of SelectorDataList::execute() into 3 separate functions
+        https://bugs.webkit.org/show_bug.cgi?id=117378
+
+        Reviewed by Ryosuke Niwa.
+
+        Split those to have several small execute() fucntions instead of a giant one.
+
+        * dom/SelectorQuery.cpp:
+        (WebCore::selectorForIdLookup):
+        (WebCore::isTreeScopeRoot):
+        (WebCore::SelectorDataList::executeFastPathForIdSelector):
+        (WebCore::SelectorDataList::executeSingleSelectorData):
+        (WebCore::SelectorDataList::executeSingleMultiSelectorData):
+        (WebCore::SelectorDataList::execute):
+        * dom/SelectorQuery.h:
+
+2013-06-09  Benjamin Poulain  <[email protected]>
+
         Scrolling with platformWidget and delegateScrolling is incorrectly clamped
         https://bugs.webkit.org/show_bug.cgi?id=117369
         <rdar://problem/13985064>

Modified: trunk/Source/WebCore/dom/SelectorQuery.cpp (151358 => 151359)


--- trunk/Source/WebCore/dom/SelectorQuery.cpp	2013-06-09 20:56:29 UTC (rev 151358)
+++ trunk/Source/WebCore/dom/SelectorQuery.cpp	2013-06-09 20:57:49 UTC (rev 151359)
@@ -98,64 +98,43 @@
     return toElement(result.first().get());
 }
 
-const CSSSelector* SelectorDataList::selectorForIdLookup(Node* rootNode) const
+static const CSSSelector* selectorForIdLookup(const Node* rootNode, const CSSSelector* selector)
 {
-    // We need to return the matches in document order. To use id lookup while there is possiblity of multiple matches
-    // we would need to sort the results. For now, just traverse the document in that case.
-    if (m_selectors.size() != 1)
-        return 0;
     if (!rootNode->inDocument())
         return 0;
     if (rootNode->document()->inQuirksMode())
         return 0;
-    for (const CSSSelector* selector = m_selectors[0].selector; selector; selector = selector->tagHistory()) {
+
+    do {
         if (selector->m_match == CSSSelector::Id)
             return selector;
         if (selector->relation() != CSSSelector::SubSelector)
             break;
-    }
+        selector = selector->tagHistory();
+    } while (selector);
+
     return 0;
 }
 
-static inline bool isTreeScopeRoot(Node* node)
+static inline bool isTreeScopeRoot(const Node* node)
 {
     ASSERT(node);
     return node->isDocumentNode() || node->isShadowRoot();
 }
 
 template <bool firstMatchOnly>
-void SelectorDataList::execute(Node* rootNode, Vector<RefPtr<Node> >& matchedElements) const
+ALWAYS_INLINE void SelectorDataList::executeFastPathForIdSelector(const Node* rootNode, const SelectorData& selectorData, const CSSSelector* idSelector, Vector<RefPtr<Node> >& matchedElements) const
 {
-    if (const CSSSelector* idSelector = selectorForIdLookup(rootNode)) {
-        ASSERT(m_selectors.size() == 1);
-        const AtomicString& idToMatch = idSelector->value();
-        if (UNLIKELY(rootNode->treeScope()->containsMultipleElementsWithId(idToMatch))) {
-            const Vector<Element*>* elements = rootNode->treeScope()->getAllElementsById(idToMatch);
-            ASSERT(elements);
-            size_t count = elements->size();
-            for (size_t i = 0; i < count; ++i) {
-                Element* element = elements->at(i);
-                if (selectorMatches(m_selectors[0], element, rootNode)) {
-                    matchedElements.append(element);
-                    if (firstMatchOnly)
-                        return;
-                }
-            }
-            return;
-        }
-        Element* element = rootNode->treeScope()->getElementById(idToMatch);
-        if (!element || !(isTreeScopeRoot(rootNode) || element->isDescendantOf(rootNode)))
-            return;
-        if (selectorMatches(m_selectors[0], element, rootNode))
-            matchedElements.append(element);
-        return;
-    }
-
-    unsigned selectorCount = m_selectors.size();
-    if (selectorCount == 1) {
-        const SelectorData& selector = m_selectors[0];
-        for (Element* element = ElementTraversal::firstWithin(rootNode); element; element = ElementTraversal::next(element, rootNode)) {
-            if (selectorMatches(selector, element, rootNode)) {
+    ASSERT(m_selectors.size() == 1);
+    ASSERT(idSelector);
+    const AtomicString& idToMatch = idSelector->value();
+    if (UNLIKELY(rootNode->treeScope()->containsMultipleElementsWithId(idToMatch))) {
+        const Vector<Element*>* elements = rootNode->treeScope()->getAllElementsById(idToMatch);
+        ASSERT(elements);
+        size_t count = elements->size();
+        for (size_t i = 0; i < count; ++i) {
+            Element* element = elements->at(i);
+            if (selectorMatches(selectorData, element, rootNode)) {
                 matchedElements.append(element);
                 if (firstMatchOnly)
                     return;
@@ -163,7 +142,32 @@
         }
         return;
     }
+    Element* element = rootNode->treeScope()->getElementById(idToMatch);
+    if (!element || !(isTreeScopeRoot(rootNode) || element->isDescendantOf(rootNode)))
+        return;
+    if (selectorMatches(selectorData, element, rootNode))
+        matchedElements.append(element);
+}
+
+template <bool firstMatchOnly>
+ALWAYS_INLINE void SelectorDataList::executeSingleSelectorData(const Node* rootNode, const SelectorData& selectorData, Vector<RefPtr<Node> >& matchedElements) const
+{
+    ASSERT(m_selectors.size() == 1);
+
     for (Element* element = ElementTraversal::firstWithin(rootNode); element; element = ElementTraversal::next(element, rootNode)) {
+        if (selectorMatches(selectorData, element, rootNode)) {
+            matchedElements.append(element);
+            if (firstMatchOnly)
+                return;
+        }
+    }
+}
+
+template <bool firstMatchOnly>
+ALWAYS_INLINE void SelectorDataList::executeSingleMultiSelectorData(const Node* rootNode, Vector<RefPtr<Node> >& matchedElements) const
+{
+    unsigned selectorCount = m_selectors.size();
+    for (Element* element = ElementTraversal::firstWithin(rootNode); element; element = ElementTraversal::next(element, rootNode)) {
         for (unsigned i = 0; i < selectorCount; ++i) {
             if (selectorMatches(m_selectors[i], element, rootNode)) {
                 matchedElements.append(element);
@@ -175,6 +179,20 @@
     }
 }
 
+template <bool firstMatchOnly>
+ALWAYS_INLINE void SelectorDataList::execute(Node* rootNode, Vector<RefPtr<Node> >& matchedElements) const
+{
+    if (m_selectors.size() == 1) {
+        const SelectorData& selectorData = m_selectors[0];
+        if (const CSSSelector* idSelector = selectorForIdLookup(rootNode, selectorData.selector))
+            executeFastPathForIdSelector<firstMatchOnly>(rootNode, selectorData, idSelector, matchedElements);
+        else
+            executeSingleSelectorData<firstMatchOnly>(rootNode, selectorData, matchedElements);
+        return;
+    }
+    executeSingleMultiSelectorData<firstMatchOnly>(rootNode, matchedElements);
+}
+
 SelectorQuery::SelectorQuery(const CSSSelectorList& selectorList)
     : m_selectorList(selectorList)
 {

Modified: trunk/Source/WebCore/dom/SelectorQuery.h (151358 => 151359)


--- trunk/Source/WebCore/dom/SelectorQuery.h	2013-06-09 20:56:29 UTC (rev 151358)
+++ trunk/Source/WebCore/dom/SelectorQuery.h	2013-06-09 20:57:49 UTC (rev 151359)
@@ -58,10 +58,13 @@
     };
 
     bool selectorMatches(const SelectorData&, Element*, const Node*) const;
-    const CSSSelector* selectorForIdLookup(Node* rootNode) const;
     template <bool firstMatchOnly>
     void execute(Node* rootNode, Vector<RefPtr<Node> >&) const;
 
+    template <bool firstMatchOnly> void executeFastPathForIdSelector(const Node* rootNode, const SelectorData&, const CSSSelector* idSelector, Vector<RefPtr<Node> >&) const;
+    template <bool firstMatchOnly> void executeSingleSelectorData(const Node* rootNode, const SelectorData&, Vector<RefPtr<Node> >&) const;
+    template <bool firstMatchOnly> void executeSingleMultiSelectorData(const Node* rootNode, Vector<RefPtr<Node> >&) const;
+
     Vector<SelectorData> m_selectors;
 };
 
_______________________________________________
webkit-changes mailing list
[email protected]
https://lists.webkit.org/mailman/listinfo/webkit-changes

Reply via email to