Some improvements to RuleSet shrinking.
authorakling@apple.com <akling@apple.com@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Fri, 16 Jan 2015 17:50:14 +0000 (17:50 +0000)
committerakling@apple.com <akling@apple.com@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Fri, 16 Jan 2015 17:50:14 +0000 (17:50 +0000)
<https://webkit.org/b/140534>

Reviewed by Antti Koivisto.

Give an inline capacity (1) to the RuleData Vectors in RuleSet.
The vast majority of Vectors have only a single entry, and this
avoids having to allocate a separate Vector backing store for them.

Also make sure to shrink some Vectors that we weren't already,
like those in RuleFeatureSet.

* css/ElementRuleCollector.cpp:
(WebCore::ElementRuleCollector::collectMatchingRulesForList):
* css/ElementRuleCollector.h:
* css/RuleFeature.cpp:
(WebCore::RuleFeatureSet::shrinkToFit):
* css/RuleFeature.h:
* css/RuleSet.cpp:
(WebCore::RuleSet::addToRuleSet):
(WebCore::rulesCountForName):
(WebCore::shrinkMapVectorsToFit):
(WebCore::RuleSet::shrinkToFit):
* css/RuleSet.h:
(WebCore::RuleSet::idRules):
(WebCore::RuleSet::classRules):
(WebCore::RuleSet::tagRules):
(WebCore::RuleSet::shadowPseudoElementRules):
(WebCore::RuleSet::linkPseudoClassRules):
(WebCore::RuleSet::cuePseudoRules):
(WebCore::RuleSet::focusPseudoClassRules):
(WebCore::RuleSet::universalRules):

git-svn-id: https://svn.webkit.org/repository/webkit/trunk@178580 268f45cc-cd09-0410-ab3c-d52691b4dbfc

Source/WebCore/ChangeLog
Source/WebCore/css/ElementRuleCollector.cpp
Source/WebCore/css/ElementRuleCollector.h
Source/WebCore/css/RuleFeature.cpp
Source/WebCore/css/RuleFeature.h
Source/WebCore/css/RuleSet.cpp
Source/WebCore/css/RuleSet.h

index a43aa0c..755f70b 100644 (file)
@@ -1,3 +1,38 @@
+2015-01-16  Andreas Kling  <akling@apple.com>
+
+        Some improvements to RuleSet shrinking.
+        <https://webkit.org/b/140534>
+
+        Reviewed by Antti Koivisto.
+
+        Give an inline capacity (1) to the RuleData Vectors in RuleSet.
+        The vast majority of Vectors have only a single entry, and this
+        avoids having to allocate a separate Vector backing store for them.
+
+        Also make sure to shrink some Vectors that we weren't already,
+        like those in RuleFeatureSet.
+
+        * css/ElementRuleCollector.cpp:
+        (WebCore::ElementRuleCollector::collectMatchingRulesForList):
+        * css/ElementRuleCollector.h:
+        * css/RuleFeature.cpp:
+        (WebCore::RuleFeatureSet::shrinkToFit):
+        * css/RuleFeature.h:
+        * css/RuleSet.cpp:
+        (WebCore::RuleSet::addToRuleSet):
+        (WebCore::rulesCountForName):
+        (WebCore::shrinkMapVectorsToFit):
+        (WebCore::RuleSet::shrinkToFit):
+        * css/RuleSet.h:
+        (WebCore::RuleSet::idRules):
+        (WebCore::RuleSet::classRules):
+        (WebCore::RuleSet::tagRules):
+        (WebCore::RuleSet::shadowPseudoElementRules):
+        (WebCore::RuleSet::linkPseudoClassRules):
+        (WebCore::RuleSet::cuePseudoRules):
+        (WebCore::RuleSet::focusPseudoClassRules):
+        (WebCore::RuleSet::universalRules):
+
 2015-01-16  Antti Koivisto  <antti@apple.com>
 
         Correct naming for FontCascade implementation files
index 0081a27..bacf797 100644 (file)
@@ -351,7 +351,7 @@ inline bool ElementRuleCollector::ruleMatches(const RuleData& ruleData, unsigned
     return selectorChecker.match(ruleData.selector(), &m_element, context, specificity);
 }
 
-void ElementRuleCollector::collectMatchingRulesForList(const Vector<RuleData>* rules, const MatchRequest& matchRequest, StyleResolver::RuleRange& ruleRange)
+void ElementRuleCollector::collectMatchingRulesForList(const RuleSet::RuleDataVector* rules, const MatchRequest& matchRequest, StyleResolver::RuleRange& ruleRange)
 {
     if (!rules)
         return;
index 18a50cb..36b31ee 100644 (file)
@@ -85,7 +85,7 @@ private:
 
     void collectMatchingRules(const MatchRequest&, StyleResolver::RuleRange&);
     void collectMatchingRulesForRegion(const MatchRequest&, StyleResolver::RuleRange&);
-    void collectMatchingRulesForList(const Vector<RuleData>*, const MatchRequest&, StyleResolver::RuleRange&);
+    void collectMatchingRulesForList(const RuleSet::RuleDataVector*, const MatchRequest&, StyleResolver::RuleRange&);
     bool ruleMatches(const RuleData&, unsigned &specificity);
 
     void sortMatchedRules();
index a3fa98a..c24ec82 100644 (file)
@@ -103,4 +103,10 @@ void RuleFeatureSet::clear()
     usesFirstLetterRules = false;
 }
 
+void RuleFeatureSet::shrinkToFit()
+{
+    siblingRules.shrinkToFit();
+    uncommonAttributeRules.shrinkToFit();
+}
+
 } // namespace WebCore
index 0aad811..0a3a57e 100644 (file)
@@ -52,7 +52,7 @@ struct RuleFeatureSet {
 
     void add(const RuleFeatureSet&);
     void clear();
-
+    void shrinkToFit();
     void collectFeaturesFromSelector(const CSSSelector&, bool& hasSiblingSelector);
 
     HashSet<AtomicStringImpl*> idsInRules;
index 59a1a26..be73c21 100644 (file)
@@ -183,15 +183,15 @@ void RuleSet::addToRuleSet(AtomicStringImpl* key, AtomRuleMap& map, const RuleDa
 {
     if (!key)
         return;
-    std::unique_ptr<Vector<RuleData>>& rules = map.add(key, nullptr).iterator->value;
+    auto& rules = map.add(key, nullptr).iterator->value;
     if (!rules)
-        rules = std::make_unique<Vector<RuleData>>();
+        rules = std::make_unique<RuleDataVector>();
     rules->append(ruleData);
 }
 
 static unsigned rulesCountForName(const RuleSet::AtomRuleMap& map, AtomicStringImpl* name)
 {
-    if (const Vector<RuleData>* rules = map.get(name))
+    if (const auto* rules = map.get(name))
         return rules->size();
     return 0;
 }
@@ -377,9 +377,8 @@ void RuleSet::addStyleRule(StyleRule* rule, AddRuleFlags addRuleFlags)
 
 static inline void shrinkMapVectorsToFit(RuleSet::AtomRuleMap& map)
 {
-    RuleSet::AtomRuleMap::iterator end = map.end();
-    for (RuleSet::AtomRuleMap::iterator it = map.begin(); it != end; ++it)
-        it->value->shrinkToFit();
+    for (auto& vector : map.values())
+        vector->shrinkToFit();
 }
 
 void RuleSet::shrinkToFit()
@@ -395,6 +394,8 @@ void RuleSet::shrinkToFit()
     m_focusPseudoClassRules.shrinkToFit();
     m_universalRules.shrinkToFit();
     m_pageRules.shrinkToFit();
+    m_features.shrinkToFit();
+    m_regionSelectorsAndRuleSets.shrinkToFit();
 }
 
 } // namespace WebCore
index e7d8a0b..e6430cc 100644 (file)
@@ -155,7 +155,8 @@ public:
 
     RuleSet();
 
-    typedef HashMap<AtomicStringImpl*, std::unique_ptr<Vector<RuleData>>> AtomRuleMap;
+    typedef Vector<RuleData, 1> RuleDataVector;
+    typedef HashMap<AtomicStringImpl*, std::unique_ptr<RuleDataVector>> AtomRuleMap;
 
     void addRulesFromSheet(StyleSheetContents*, const MediaQueryEvaluator&, StyleResolver* = 0);
 
@@ -169,16 +170,17 @@ public:
 
     const RuleFeatureSet& features() const { return m_features; }
 
-    const Vector<RuleData>* idRules(AtomicStringImpl* key) const { return m_idRules.get(key); }
-    const Vector<RuleData>* classRules(AtomicStringImpl* key) const { return m_classRules.get(key); }
-    const Vector<RuleData>* tagRules(AtomicStringImpl* key) const { return m_tagRules.get(key); }
-    const Vector<RuleData>* shadowPseudoElementRules(AtomicStringImpl* key) const { return m_shadowPseudoElementRules.get(key); }
-    const Vector<RuleData>* linkPseudoClassRules() const { return &m_linkPseudoClassRules; }
+    const RuleDataVector* idRules(AtomicStringImpl* key) const { return m_idRules.get(key); }
+    const RuleDataVector* classRules(AtomicStringImpl* key) const { return m_classRules.get(key); }
+    const RuleDataVector* tagRules(AtomicStringImpl* key) const { return m_tagRules.get(key); }
+    const RuleDataVector* shadowPseudoElementRules(AtomicStringImpl* key) const { return m_shadowPseudoElementRules.get(key); }
+    const RuleDataVector* linkPseudoClassRules() const { return &m_linkPseudoClassRules; }
 #if ENABLE(VIDEO_TRACK)
-    const Vector<RuleData>* cuePseudoRules() const { return &m_cuePseudoRules; }
+    const RuleDataVector* cuePseudoRules() const { return &m_cuePseudoRules; }
 #endif
-    const Vector<RuleData>* focusPseudoClassRules() const { return &m_focusPseudoClassRules; }
-    const Vector<RuleData>* universalRules() const { return &m_universalRules; }
+    const RuleDataVector* focusPseudoClassRules() const { return &m_focusPseudoClassRules; }
+    const RuleDataVector* universalRules() const { return &m_universalRules; }
+
     const Vector<StyleRulePage*>& pageRules() const { return m_pageRules; }
     const Vector<RuleSetSelectorPair>& regionSelectorsAndRuleSets() const { return m_regionSelectorsAndRuleSets; }
 
@@ -193,12 +195,12 @@ private:
     AtomRuleMap m_classRules;
     AtomRuleMap m_tagRules;
     AtomRuleMap m_shadowPseudoElementRules;
-    Vector<RuleData> m_linkPseudoClassRules;
+    RuleDataVector m_linkPseudoClassRules;
 #if ENABLE(VIDEO_TRACK)
-    Vector<RuleData> m_cuePseudoRules;
+    RuleDataVector m_cuePseudoRules;
 #endif
-    Vector<RuleData> m_focusPseudoClassRules;
-    Vector<RuleData> m_universalRules;
+    RuleDataVector m_focusPseudoClassRules;
+    RuleDataVector m_universalRules;
     Vector<StyleRulePage*> m_pageRules;
     unsigned m_ruleCount;
     bool m_autoShrinkToFitEnabled;