<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head><meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>[286494] trunk/Source/WebCore</title>
</head>
<body>
<style type="text/css"><!--
#msg dl.meta { border: 1px #006 solid; background: #369; padding: 6px; color: #fff; }
#msg dl.meta dt { float: left; width: 6em; font-weight: bold; }
#msg dt:after { content:':';}
#msg dl, #msg dt, #msg ul, #msg li, #header, #footer, #logmsg { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; }
#msg dl a { font-weight: bold}
#msg dl a:link { color:#fc3; }
#msg dl a:active { color:#ff0; }
#msg dl a:visited { color:#cc6; }
h3 { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; font-weight: bold; }
#msg pre { overflow: auto; background: #ffc; border: 1px #fa0 solid; padding: 6px; }
#logmsg { background: #ffc; border: 1px #fa0 solid; padding: 1em 1em 0 1em; }
#logmsg p, #logmsg pre, #logmsg blockquote { margin: 0 0 1em 0; }
#logmsg p, #logmsg li, #logmsg dt, #logmsg dd { line-height: 14pt; }
#logmsg h1, #logmsg h2, #logmsg h3, #logmsg h4, #logmsg h5, #logmsg h6 { margin: .5em 0; }
#logmsg h1:first-child, #logmsg h2:first-child, #logmsg h3:first-child, #logmsg h4:first-child, #logmsg h5:first-child, #logmsg h6:first-child { margin-top: 0; }
#logmsg ul, #logmsg ol { padding: 0; list-style-position: inside; margin: 0 0 0 1em; }
#logmsg ul { text-indent: -1em; padding-left: 1em; }#logmsg ol { text-indent: -1.5em; padding-left: 1.5em; }
#logmsg > ul, #logmsg > ol { margin: 0 0 1em 0; }
#logmsg pre { background: #eee; padding: 1em; }
#logmsg blockquote { border: 1px solid #fa0; border-left-width: 10px; padding: 1em 1em 0 1em; background: white;}
#logmsg dl { margin: 0; }
#logmsg dt { font-weight: bold; }
#logmsg dd { margin: 0; padding: 0 0 0.5em 0; }
#logmsg dd:before { content:'\00bb';}
#logmsg table { border-spacing: 0px; border-collapse: collapse; border-top: 4px solid #fa0; border-bottom: 1px solid #fa0; background: #fff; }
#logmsg table th { text-align: left; font-weight: normal; padding: 0.2em 0.5em; border-top: 1px dotted #fa0; }
#logmsg table td { text-align: right; border-top: 1px dotted #fa0; padding: 0.2em 0.5em; }
#logmsg table thead th { text-align: center; border-bottom: 1px solid #fa0; }
#logmsg table th.Corner { text-align: left; }
#logmsg hr { border: none 0; border-top: 2px dashed #fa0; height: 1px; }
#header, #footer { color: #fff; background: #636; border: 1px #300 solid; padding: 6px; }
#patch { width: 100%; }
#patch h4 {font-family: verdana,arial,helvetica,sans-serif;font-size:10pt;padding:8px;background:#369;color:#fff;margin:0;}
#patch .propset h4, #patch .binary h4 {margin:0;}
#patch pre {padding:0;line-height:1.2em;margin:0;}
#patch .diff {width:100%;background:#eee;padding: 0 0 10px 0;overflow:auto;}
#patch .propset .diff, #patch .binary .diff {padding:10px 0;}
#patch span {display:block;padding:0 10px;}
#patch .modfile, #patch .addfile, #patch .delfile, #patch .propset, #patch .binary, #patch .copfile {border:1px solid #ccc;margin:10px 0;}
#patch ins {background:#dfd;text-decoration:none;display:block;padding:0 10px;}
#patch del {background:#fdd;text-decoration:none;display:block;padding:0 10px;}
#patch .lines, .info {color:#888;background:#fff;}
--></style>
<div id="msg">
<dl class="meta">
<dt>Revision</dt> <dd><a href="http://trac.webkit.org/projects/webkit/changeset/286494">286494</a></dd>
<dt>Author</dt> <dd>antti@apple.com</dd>
<dt>Date</dt> <dd>2021-12-03 09:10:13 -0800 (Fri, 03 Dec 2021)</dd>
</dl>
<h3>Log Message</h3>
<pre>[:has() pseudo-class] Improve result caching
https://bugs.webkit.org/show_bug.cgi?id=233806
Reviewed by Simon Fraser.
Improve caching to to avoid O(n^2) cases during invalidation and style resolution.
* css/SelectorChecker.cpp:
(WebCore::SelectorChecker::matchHasPseudoClass const):
Cache matches as well as failures.
Cache failures that don't apply to subtrees.
Check cached matches and failures for all :has() match element types.
* style/SelectorMatchingState.h:</pre>
<h3>Modified Paths</h3>
<ul>
<li><a href="#trunkSourceWebCoreChangeLog">trunk/Source/WebCore/ChangeLog</a></li>
<li><a href="#trunkSourceWebCorecssSelectorCheckercpp">trunk/Source/WebCore/css/SelectorChecker.cpp</a></li>
<li><a href="#trunkSourceWebCorestyleSelectorMatchingStateh">trunk/Source/WebCore/style/SelectorMatchingState.h</a></li>
</ul>
</div>
<div id="patch">
<h3>Diff</h3>
<a id="trunkSourceWebCoreChangeLog"></a>
<div class="modfile"><h4>Modified: trunk/Source/WebCore/ChangeLog (286493 => 286494)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WebCore/ChangeLog 2021-12-03 17:08:36 UTC (rev 286493)
+++ trunk/Source/WebCore/ChangeLog 2021-12-03 17:10:13 UTC (rev 286494)
</span><span class="lines">@@ -1,3 +1,21 @@
</span><ins>+2021-12-03 Antti Koivisto <antti@apple.com>
+
+ [:has() pseudo-class] Improve result caching
+ https://bugs.webkit.org/show_bug.cgi?id=233806
+
+ Reviewed by Simon Fraser.
+
+ Improve caching to to avoid O(n^2) cases during invalidation and style resolution.
+
+ * css/SelectorChecker.cpp:
+ (WebCore::SelectorChecker::matchHasPseudoClass const):
+
+ Cache matches as well as failures.
+ Cache failures that don't apply to subtrees.
+ Check cached matches and failures for all :has() match element types.
+
+ * style/SelectorMatchingState.h:
+
</ins><span class="cx"> 2021-12-03 Alex Christensen <achristensen@webkit.org>
</span><span class="cx">
</span><span class="cx"> Add room for more bytecode in WKContentRuleList file format
</span></span></pre></div>
<a id="trunkSourceWebCorecssSelectorCheckercpp"></a>
<div class="modfile"><h4>Modified: trunk/Source/WebCore/css/SelectorChecker.cpp (286493 => 286494)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WebCore/css/SelectorChecker.cpp 2021-12-03 17:08:36 UTC (rev 286493)
+++ trunk/Source/WebCore/css/SelectorChecker.cpp 2021-12-03 17:10:13 UTC (rev 286494)
</span><span class="lines">@@ -1249,8 +1249,22 @@
</span><span class="cx">
</span><span class="cx"> bool SelectorChecker::matchHasPseudoClass(CheckingContext& checkingContext, const Element& element, const CSSSelector& hasSelector) const
</span><span class="cx"> {
</span><del>- // FIXME: This could be better in terms of performance.
</del><ins>+ auto* cache = checkingContext.selectorMatchingState ? &checkingContext.selectorMatchingState->hasPseudoClassMatchCache : nullptr;
</ins><span class="cx">
</span><ins>+ Style::HasPseudoClassMatch* cachedMatch = nullptr;
+ if (cache) {
+ cachedMatch = &cache->add(Style::makeHasPseudoClassCacheKey(hasSelector, element), Style::HasPseudoClassMatch::None).iterator->value;
+ switch (*cachedMatch) {
+ case Style::HasPseudoClassMatch::Matches:
+ return true;
+ case Style::HasPseudoClassMatch::Fails:
+ case Style::HasPseudoClassMatch::FailsSubtree:
+ return false;
+ case Style::HasPseudoClassMatch::None:
+ break;
+ }
+ }
+
</ins><span class="cx"> SelectorChecker hasChecker(element.document());
</span><span class="cx"> bool matchedInsideScope = false;
</span><span class="cx">
</span><span class="lines">@@ -1267,69 +1281,90 @@
</span><span class="cx"> };
</span><span class="cx">
</span><span class="cx"> auto checkDescendants = [&](const Element& descendantRoot) {
</span><del>- auto descendants = descendantsOfType<Element>(descendantRoot);
- if (!descendants.first())
- return false;
-
- if (checkingContext.selectorMatchingState) {
- // See if we already know this :has() selector doesn't match in this subtree.
- auto& failureCache = checkingContext.selectorMatchingState->hasPseudoClassDescendantFailureCache;
- if (!failureCache.isEmpty()) {
- for (auto* ancestor = descendantRoot.parentElement(); ancestor; ancestor = ancestor->parentElement()) {
- if (failureCache.contains(Style::makeHasPseudoClassCacheKey(hasSelector, *ancestor)))
- return false;
</del><ins>+ for (auto it = descendantsOfType<Element>(descendantRoot).begin(); it;) {
+ auto& descendant = *it;
+ if (cache) {
+ auto key = Style::makeHasPseudoClassCacheKey(hasSelector, descendant);
+ if (cache->get(key) == Style::HasPseudoClassMatch::FailsSubtree) {
+ it.traverseNextSkippingChildren();
+ continue;
</ins><span class="cx"> }
</span><span class="cx"> }
</span><del>- }
-
- matchedInsideScope = false;
-
- for (auto& descendant : descendants) {
</del><span class="cx"> if (checkRelative(descendant))
</span><span class="cx"> return true;
</span><del>- }
</del><span class="cx">
</span><del>- if (checkingContext.selectorMatchingState && !matchedInsideScope) {
- auto& failureCache = checkingContext.selectorMatchingState->hasPseudoClassDescendantFailureCache;
- failureCache.add(Style::makeHasPseudoClassCacheKey(hasSelector, descendantRoot));
</del><ins>+ it.traverseNext();
</ins><span class="cx"> }
</span><span class="cx">
</span><span class="cx"> return false;
</span><span class="cx"> };
</span><span class="cx">
</span><del>- auto matchElement = Style::computeHasPseudoClassMatchElement(hasSelector);
</del><ins>+ auto match = [&] {
+ auto matchElement = Style::computeHasPseudoClassMatchElement(hasSelector);
</ins><span class="cx">
</span><del>- switch (matchElement) {
- case Style::MatchElement::HasChild:
- for (auto& child : childrenOfType<Element>(element)) {
- if (checkRelative(child))
</del><ins>+ switch (matchElement) {
+ // :has(> .child)
+ case Style::MatchElement::HasChild:
+ for (auto& child : childrenOfType<Element>(element)) {
+ if (checkRelative(child))
+ return true;
+ }
+ break;
+ // :has(.descendant)
+ case Style::MatchElement::HasDescendant: {
+ if (!element.firstElementChild())
+ return false;
+ if (cache) {
+ // See if we already know this descendant selector doesn't match in this subtree.
+ for (auto* ancestor = element.parentElement(); ancestor; ancestor = ancestor->parentElement()) {
+ auto key = Style::makeHasPseudoClassCacheKey(hasSelector, *ancestor);
+ if (cache->get(key) == Style::HasPseudoClassMatch::FailsSubtree)
+ return false;
+ }
+ }
+ if (checkDescendants(element))
</ins><span class="cx"> return true;
</span><ins>+ break;
</ins><span class="cx"> }
</span><del>- break;
- case Style::MatchElement::HasDescendant: {
- if (checkDescendants(element))
- return true;
- break;
- }
- case Style::MatchElement::HasSibling:
- for (auto* sibling = element.nextElementSibling(); sibling; sibling = sibling->nextElementSibling()) {
- if (checkRelative(*sibling))
- return true;
</del><ins>+ // FIXME: Add a separate case for adjacent combinator.
+ // :has(+ .sibling)
+ // :has(~ .sibling)
+ case Style::MatchElement::HasSibling:
+ for (auto* sibling = element.nextElementSibling(); sibling; sibling = sibling->nextElementSibling()) {
+ if (checkRelative(*sibling))
+ return true;
+ }
+ break;
+ // FIXME: Add a separate case for adjacent combinator.
+ // :has(+ .sibling .descendant)
+ // :has(~ .sibling .descendant)
+ case Style::MatchElement::HasSiblingDescendant:
+ for (auto* sibling = element.nextElementSibling(); sibling; sibling = sibling->nextElementSibling()) {
+ if (checkDescendants(*sibling))
+ return true;
+ }
+ break;
+
+ default:
+ ASSERT_NOT_REACHED();
+ break;
</ins><span class="cx"> }
</span><del>- break;
- case Style::MatchElement::HasSiblingDescendant:
- for (auto* sibling = element.nextElementSibling(); sibling; sibling = sibling->nextElementSibling()) {
- if (checkDescendants(*sibling))
- return true;
- }
- break;
</del><ins>+ return false;
+ };
</ins><span class="cx">
</span><del>- default:
- ASSERT_NOT_REACHED();
- break;
</del><ins>+ auto result = match();
+
+ if (cachedMatch) {
+ *cachedMatch = [&] {
+ if (result)
+ return Style::HasPseudoClassMatch::Matches;
+ if (matchedInsideScope)
+ return Style::HasPseudoClassMatch::Fails;
+ return Style::HasPseudoClassMatch::FailsSubtree;
+ }();
</ins><span class="cx"> }
</span><span class="cx">
</span><del>- return false;
</del><ins>+ return result;
</ins><span class="cx"> }
</span><span class="cx">
</span><span class="cx"> bool SelectorChecker::checkScrollbarPseudoClass(const CheckingContext& checkingContext, const Element& element, const CSSSelector& selector) const
</span></span></pre></div>
<a id="trunkSourceWebCorestyleSelectorMatchingStateh"></a>
<div class="modfile"><h4>Modified: trunk/Source/WebCore/style/SelectorMatchingState.h (286493 => 286494)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WebCore/style/SelectorMatchingState.h 2021-12-03 17:08:36 UTC (rev 286493)
+++ trunk/Source/WebCore/style/SelectorMatchingState.h 2021-12-03 17:10:13 UTC (rev 286494)
</span><span class="lines">@@ -25,15 +25,17 @@
</span><span class="cx"> #pragma once
</span><span class="cx">
</span><span class="cx"> #include "SelectorFilter.h"
</span><del>-#include <wtf/HashSet.h>
</del><ins>+#include <wtf/HashMap.h>
</ins><span class="cx">
</span><span class="cx"> namespace WebCore::Style {
</span><span class="cx">
</span><span class="cx"> using HasPseudoClassCacheKey = std::pair<const CSSSelector*, const Element*>;
</span><span class="cx">
</span><ins>+enum class HasPseudoClassMatch : uint8_t { None, Matches, Fails, FailsSubtree };
+
</ins><span class="cx"> struct SelectorMatchingState {
</span><span class="cx"> SelectorFilter selectorFilter;
</span><del>- HashSet<HasPseudoClassCacheKey> hasPseudoClassDescendantFailureCache;
</del><ins>+ HashMap<HasPseudoClassCacheKey, HasPseudoClassMatch> hasPseudoClassMatchCache;
</ins><span class="cx"> };
</span><span class="cx">
</span><span class="cx"> inline HasPseudoClassCacheKey makeHasPseudoClassCacheKey(const CSSSelector& selector, const Element& element)
</span></span></pre>
</div>
</div>
</body>
</html>