<!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>[285392] 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/285392">285392</a></dd>
<dt>Author</dt> <dd>antti@apple.com</dd>
<dt>Date</dt> <dd>2021-11-07 10:58:10 -0800 (Sun, 07 Nov 2021)</dd>
</dl>

<h3>Log Message</h3>
<pre>Use narrower types for style scope ordinal and cascade layer priority
https://bugs.webkit.org/show_bug.cgi?id=232797

Reviewed by Alan Bujtas.

This shrinks critical ElementRuleCollector data structures like MatchedRule.

64k layers ought to be enough for anybody (and 256 scopes).

* style/ElementRuleCollector.cpp:
(WebCore::Style::ElementRuleCollector::matchSlottedPseudoElementRules):
(WebCore::Style::ElementRuleCollector::matchPartPseudoElementRulesForScope):

Check the scope depth limits.

* style/ElementRuleCollector.h:
* style/RuleSet.h:
(WebCore::Style::RuleSet::cascadeLayerPriorityForIdentifier const):
(WebCore::Style::RuleSet::cascadeLayerPriorityFor const):

Add a type alias and make it uint16_t.

* style/RuleSetBuilder.cpp:
(WebCore::Style::RuleSetBuilder::updateCascadeLayerPriorities):
* style/StyleScopeOrdinal.h:
(WebCore::Style::operator++):
(WebCore::Style::operator--):

Use int8_t. No realistic case requires more than a handful.</pre>

<h3>Modified Paths</h3>
<ul>
<li><a href="#trunkSourceWebCoreChangeLog">trunk/Source/WebCore/ChangeLog</a></li>
<li><a href="#trunkSourceWebCorestyleElementRuleCollectorcpp">trunk/Source/WebCore/style/ElementRuleCollector.cpp</a></li>
<li><a href="#trunkSourceWebCorestyleElementRuleCollectorh">trunk/Source/WebCore/style/ElementRuleCollector.h</a></li>
<li><a href="#trunkSourceWebCorestyleRuleSeth">trunk/Source/WebCore/style/RuleSet.h</a></li>
<li><a href="#trunkSourceWebCorestyleRuleSetBuildercpp">trunk/Source/WebCore/style/RuleSetBuilder.cpp</a></li>
<li><a href="#trunkSourceWebCorestyleStyleScopeOrdinalh">trunk/Source/WebCore/style/StyleScopeOrdinal.h</a></li>
</ul>

</div>
<div id="patch">
<h3>Diff</h3>
<a id="trunkSourceWebCoreChangeLog"></a>
<div class="modfile"><h4>Modified: trunk/Source/WebCore/ChangeLog (285391 => 285392)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WebCore/ChangeLog   2021-11-07 18:39:07 UTC (rev 285391)
+++ trunk/Source/WebCore/ChangeLog      2021-11-07 18:58:10 UTC (rev 285392)
</span><span class="lines">@@ -1,3 +1,35 @@
</span><ins>+2021-11-07  Antti Koivisto  <antti@apple.com>
+
+        Use narrower types for style scope ordinal and cascade layer priority
+        https://bugs.webkit.org/show_bug.cgi?id=232797
+
+        Reviewed by Alan Bujtas.
+
+        This shrinks critical ElementRuleCollector data structures like MatchedRule.
+
+        64k layers ought to be enough for anybody (and 256 scopes).
+
+        * style/ElementRuleCollector.cpp:
+        (WebCore::Style::ElementRuleCollector::matchSlottedPseudoElementRules):
+        (WebCore::Style::ElementRuleCollector::matchPartPseudoElementRulesForScope):
+
+        Check the scope depth limits.
+
+        * style/ElementRuleCollector.h:
+        * style/RuleSet.h:
+        (WebCore::Style::RuleSet::cascadeLayerPriorityForIdentifier const):
+        (WebCore::Style::RuleSet::cascadeLayerPriorityFor const):
+
+        Add a type alias and make it uint16_t.
+
+        * style/RuleSetBuilder.cpp:
+        (WebCore::Style::RuleSetBuilder::updateCascadeLayerPriorities):
+        * style/StyleScopeOrdinal.h:
+        (WebCore::Style::operator++):
+        (WebCore::Style::operator--):
+
+        Use int8_t. No realistic case requires more than a handful.
+
</ins><span class="cx"> 2021-11-07  Alan Bujtas  <zalan@apple.com>
</span><span class="cx"> 
</span><span class="cx">         [LFC][IFC] bidi boundary does not necessarily mean soft wrapping opportunity
</span></span></pre></div>
<a id="trunkSourceWebCorestyleElementRuleCollectorcpp"></a>
<div class="modfile"><h4>Modified: trunk/Source/WebCore/style/ElementRuleCollector.cpp (285391 => 285392)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WebCore/style/ElementRuleCollector.cpp      2021-11-07 18:39:07 UTC (rev 285391)
+++ trunk/Source/WebCore/style/ElementRuleCollector.cpp 2021-11-07 18:58:10 UTC (rev 285392)
</span><span class="lines">@@ -286,6 +286,9 @@
</span><span class="cx"> 
</span><span class="cx">         MatchRequest scopeMatchRequest(&scopeAuthorRules, styleScopeOrdinal);
</span><span class="cx">         collectMatchingRulesForList(&scopeAuthorRules.slottedPseudoElementRules(), scopeMatchRequest);
</span><ins>+
+        if (styleScopeOrdinal == ScopeOrdinal::SlotLimit)
+            break;
</ins><span class="cx">     }
</span><span class="cx"> }
</span><span class="cx"> 
</span><span class="lines">@@ -320,6 +323,9 @@
</span><span class="cx">         // Element may only be exposed to styling from enclosing scopes via exportparts attributes.
</span><span class="cx">         if (element != &partMatchingElement && element->shadowRoot()->partMappings().isEmpty())
</span><span class="cx">             break;
</span><ins>+
+        if (styleScopeOrdinal == ScopeOrdinal::ContainingHostLimit)
+            break;
</ins><span class="cx">     }
</span><span class="cx"> }
</span><span class="cx"> 
</span></span></pre></div>
<a id="trunkSourceWebCorestyleElementRuleCollectorh"></a>
<div class="modfile"><h4>Modified: trunk/Source/WebCore/style/ElementRuleCollector.h (285391 => 285392)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WebCore/style/ElementRuleCollector.h        2021-11-07 18:39:07 UTC (rev 285391)
+++ trunk/Source/WebCore/style/ElementRuleCollector.h   2021-11-07 18:58:10 UTC (rev 285392)
</span><span class="lines">@@ -63,7 +63,7 @@
</span><span class="cx">     const RuleData* ruleData;
</span><span class="cx">     unsigned specificity;
</span><span class="cx">     ScopeOrdinal styleScopeOrdinal;
</span><del>-    unsigned cascadeLayerPriority;
</del><ins>+    CascadeLayerPriority cascadeLayerPriority;
</ins><span class="cx"> };
</span><span class="cx"> 
</span><span class="cx"> struct MatchedProperties {
</span></span></pre></div>
<a id="trunkSourceWebCorestyleRuleSeth"></a>
<div class="modfile"><h4>Modified: trunk/Source/WebCore/style/RuleSet.h (285391 => 285392)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WebCore/style/RuleSet.h     2021-11-07 18:39:07 UTC (rev 285391)
+++ trunk/Source/WebCore/style/RuleSet.h        2021-11-07 18:58:10 UTC (rev 285392)
</span><span class="lines">@@ -43,6 +43,8 @@
</span><span class="cx"> class Resolver;
</span><span class="cx"> class RuleSet;
</span><span class="cx"> 
</span><ins>+using CascadeLayerPriority = uint16_t;
+
</ins><span class="cx"> using InvalidationRuleSetVector = Vector<RefPtr<const RuleSet>, 1>;
</span><span class="cx"> 
</span><span class="cx"> struct DynamicMediaQueryEvaluationChanges {
</span><span class="lines">@@ -103,8 +105,8 @@
</span><span class="cx">     bool hasShadowPseudoElementRules() const { return !m_shadowPseudoElementRules.isEmpty(); }
</span><span class="cx">     bool hasHostPseudoClassRulesMatchingInShadowTree() const { return m_hasHostPseudoClassRulesMatchingInShadowTree; }
</span><span class="cx"> 
</span><del>-    static constexpr auto cascadeLayerPriorityForUnlayered = std::numeric_limits<unsigned>::max();
-    unsigned cascadeLayerPriorityFor(const RuleData&) const;
</del><ins>+    static constexpr auto cascadeLayerPriorityForUnlayered = std::numeric_limits<CascadeLayerPriority>::max();
+    CascadeLayerPriority cascadeLayerPriorityFor(const RuleData&) const;
</ins><span class="cx"> 
</span><span class="cx"> private:
</span><span class="cx">     friend class RuleSetBuilder;
</span><span class="lines">@@ -132,11 +134,11 @@
</span><span class="cx">     struct CascadeLayer {
</span><span class="cx">         CascadeLayerName resolvedName;
</span><span class="cx">         CascadeLayerIdentifier parentIdentifier;
</span><del>-        unsigned priority { 0 };
</del><ins>+        CascadeLayerPriority priority { 0 };
</ins><span class="cx">     };
</span><span class="cx">     CascadeLayer& cascadeLayerForIdentifier(CascadeLayerIdentifier identifier) { return m_cascadeLayers[identifier - 1]; }
</span><span class="cx">     const CascadeLayer& cascadeLayerForIdentifier(CascadeLayerIdentifier identifier) const { return m_cascadeLayers[identifier - 1]; }
</span><del>-    unsigned cascadeLayerPriorityForIdentifier(CascadeLayerIdentifier) const;
</del><ins>+    CascadeLayerPriority cascadeLayerPriorityForIdentifier(CascadeLayerIdentifier) const;
</ins><span class="cx"> 
</span><span class="cx">     struct DynamicMediaQueryRules {
</span><span class="cx">         Vector<Ref<const MediaQuerySet>> mediaQuerySets;
</span><span class="lines">@@ -193,7 +195,7 @@
</span><span class="cx">     return tagRules->get(key);
</span><span class="cx"> }
</span><span class="cx"> 
</span><del>-inline unsigned RuleSet::cascadeLayerPriorityForIdentifier(CascadeLayerIdentifier identifier) const
</del><ins>+inline CascadeLayerPriority RuleSet::cascadeLayerPriorityForIdentifier(CascadeLayerIdentifier identifier) const
</ins><span class="cx"> {
</span><span class="cx">     if (!identifier)
</span><span class="cx">         return cascadeLayerPriorityForUnlayered;
</span><span class="lines">@@ -200,7 +202,7 @@
</span><span class="cx">     return cascadeLayerForIdentifier(identifier).priority;
</span><span class="cx"> }
</span><span class="cx"> 
</span><del>-inline unsigned RuleSet::cascadeLayerPriorityFor(const RuleData& ruleData) const
</del><ins>+inline CascadeLayerPriority RuleSet::cascadeLayerPriorityFor(const RuleData& ruleData) const
</ins><span class="cx"> {
</span><span class="cx">     if (m_cascadeLayerIdentifierForRulePosition.size() <= ruleData.position())
</span><span class="cx">         return cascadeLayerPriorityForUnlayered;
</span></span></pre></div>
<a id="trunkSourceWebCorestyleRuleSetBuildercpp"></a>
<div class="modfile"><h4>Modified: trunk/Source/WebCore/style/RuleSetBuilder.cpp (285391 => 285392)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WebCore/style/RuleSetBuilder.cpp    2021-11-07 18:39:07 UTC (rev 285391)
+++ trunk/Source/WebCore/style/RuleSetBuilder.cpp       2021-11-07 18:58:10 UTC (rev 285392)
</span><span class="lines">@@ -270,8 +270,10 @@
</span><span class="cx"> 
</span><span class="cx">     std::sort(layersInPriorityOrder.begin(), layersInPriorityOrder.end(), compare);
</span><span class="cx"> 
</span><del>-    for (unsigned priority = 0; priority < layerCount; ++priority)
-        m_ruleSet->cascadeLayerForIdentifier(layersInPriorityOrder[priority]).priority = priority;
</del><ins>+    for (unsigned i = 0; i < layerCount; ++i) {
+        auto priority = std::min<unsigned>(i, RuleSet::cascadeLayerPriorityForUnlayered - 1);
+        m_ruleSet->cascadeLayerForIdentifier(layersInPriorityOrder[i]).priority = priority;
+    }
</ins><span class="cx"> }
</span><span class="cx"> 
</span><span class="cx"> void RuleSetBuilder::addMutatingRulesToResolver()
</span></span></pre></div>
<a id="trunkSourceWebCorestyleStyleScopeOrdinalh"></a>
<div class="modfile"><h4>Modified: trunk/Source/WebCore/style/StyleScopeOrdinal.h (285391 => 285392)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WebCore/style/StyleScopeOrdinal.h   2021-11-07 18:39:07 UTC (rev 285391)
+++ trunk/Source/WebCore/style/StyleScopeOrdinal.h      2021-11-07 18:58:10 UTC (rev 285392)
</span><span class="lines">@@ -29,23 +29,25 @@
</span><span class="cx"> 
</span><span class="cx"> // This is used to identify style scopes that can affect an element.
</span><span class="cx"> // Scopes are in tree-of-trees order. Styles from earlier scopes win over later ones (modulo !important).
</span><del>-enum class ScopeOrdinal : int {
</del><ins>+enum class ScopeOrdinal : int8_t {
+    ContainingHostLimit = std::numeric_limits<int8_t>::min(),
</ins><span class="cx">     ContainingHost = -1, // ::part rules and author-exposed UA pseudo classes from the host tree scope. Values less than ContainingHost indicate enclosing scopes.
</span><span class="cx">     Element = 0, // Normal rules in the same tree where the element is.
</span><span class="cx">     FirstSlot = 1, // ::slotted rules in the parent's shadow tree. Values greater than FirstSlot indicate subsequent slots in the chain.
</span><del>-    Shadow = std::numeric_limits<int>::max(), // :host rules in element's own shadow tree.
</del><ins>+    SlotLimit = std::numeric_limits<int8_t>::max() - 1,
+    Shadow = std::numeric_limits<int8_t>::max(), // :host rules in element's own shadow tree.
</ins><span class="cx"> };
</span><span class="cx"> 
</span><span class="cx"> inline ScopeOrdinal& operator++(ScopeOrdinal& ordinal)
</span><span class="cx"> {
</span><del>-    ASSERT(ordinal < ScopeOrdinal::Shadow);
-    return ordinal = static_cast<ScopeOrdinal>(static_cast<int>(ordinal) + 1);
</del><ins>+    ASSERT(ordinal < ScopeOrdinal::SlotLimit);
+    return ordinal = static_cast<ScopeOrdinal>(static_cast<int8_t>(ordinal) + 1);
</ins><span class="cx"> }
</span><span class="cx"> 
</span><span class="cx"> inline ScopeOrdinal& operator--(ScopeOrdinal& ordinal)
</span><span class="cx"> {
</span><del>-    ASSERT(ordinal < ScopeOrdinal::Shadow);
-    return ordinal = static_cast<ScopeOrdinal>(static_cast<int>(ordinal) - 1);
</del><ins>+    ASSERT(ordinal > ScopeOrdinal::ContainingHostLimit);
+    return ordinal = static_cast<ScopeOrdinal>(static_cast<int8_t>(ordinal) - 1);
</ins><span class="cx"> }
</span><span class="cx"> 
</span><span class="cx"> }
</span></span></pre>
</div>
</div>

</body>
</html>