Consider implementing the same optimization as https://chromium.googlesource.com/chromium/blink/+/1cd83d3588973a02ab15d94b1b05a28620853624. There existed an optimization that did an element lookup for an id if the leftmost part of the selector was an id selector. Without this change, these queries would do an id lookup: querySelector("#id"), querySelector("#id.class") while these wouldn't: querySelector(".class#id"), querySelector("[name=x]#id") This change modifies the element lookup to include the latter two. More importantly, it also extends the optimization for id selectors to limit traversal of the dom-tree to the sub-tree rooted at the element with an id matching the rightmost id selector in the whole selector. For id selectors appearing left of adjacent combinators, the traversal needs to be rooted at the parent of the element with the given id. Examples: querySelector("#id span") - traversal from the id element. querySelector("#id + div span") - traversal from the parent of the id element. This fix should make this: querySelector("#id span") as fast as: getElementById("id").querySelector("span")
Created attachment 225028 [details] Patch
Comment on attachment 225028 [details] Patch View in context: https://bugs.webkit.org/attachment.cgi?id=225028&action=review r=me > Source/WebCore/ChangeLog:13 > + cache, and start matching the childre from there. Typo, children. > Source/WebCore/dom/SelectorQuery.cpp:246 > + if (LIKELY(!rootNode.treeScope().containsMultipleElementsWithId(idToMatch))) { > + if (ContainerNode* searchRoot = rootNode.treeScope().getElementById(idToMatch)) { It would be more efficient to do these two checks in opposite order. > Source/WebCore/dom/SelectorQuery.cpp:417 > + CompiledSingleWithRootFilterCase: Indentation is off here. > Source/WebCore/dom/SelectorQuery.h:49 > - void initialize(const CSSSelectorList&); > + SelectorDataList(const CSSSelectorList&); explicit
Created attachment 225557 [details] Patch
Created attachment 225558 [details] Patch
Committed r164924: <http://trac.webkit.org/changeset/164924>